Skip to content

Commit c39ccdc

Browse files
author
Brandon Sneed
committed
scaffolding stuff
1 parent 652254d commit c39ccdc

7 files changed

Lines changed: 202 additions & 7 deletions

File tree

.swiftpm/xcode/xcshareddata/xcschemes/segmentcli.xcscheme

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434
ReferencedContainer = "container:">
3535
</BuildableReference>
3636
</BuildActionEntry>
37+
<BuildActionEntry
38+
buildForTesting = "YES"
39+
buildForRunning = "YES"
40+
buildForProfiling = "YES"
41+
buildForArchiving = "YES"
42+
buildForAnalyzing = "YES">
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "segmentcli_segmentcli"
46+
BuildableName = "segmentcli_segmentcli"
47+
BlueprintName = "segmentcli_segmentcli"
48+
ReferencedContainer = "container:">
49+
</BuildableReference>
50+
</BuildActionEntry>
3751
</BuildActionEntries>
3852
</BuildAction>
3953
<TestAction
@@ -79,6 +93,10 @@
7993
argument = "--help"
8094
isEnabled = "NO">
8195
</CommandLineArgument>
96+
<CommandLineArgument
97+
argument = "scaffold --plugin --swift"
98+
isEnabled = "YES">
99+
</CommandLineArgument>
82100
<CommandLineArgument
83101
argument = "auth test1234"
84102
isEnabled = "NO">
@@ -97,7 +115,7 @@
97115
</CommandLineArgument>
98116
<CommandLineArgument
99117
argument = "analytics flush Sf37SZu7TfysLklHCahTo5HlSP9m9O6h"
100-
isEnabled = "YES">
118+
isEnabled = "NO">
101119
</CommandLineArgument>
102120
<CommandLineArgument
103121
argument = "auth --help"

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,20 @@ let package = Package(
1313
.package(url: "https://github.com/dominicegginton/Spinner", from: "1.1.4"),
1414
.package(url: "https://github.com/mtynior/ColorizeSwift.git", from: "1.5.0"),
1515
.package(url: "https://github.com/segmentio/analytics-swift.git", branch: "bsneed/cli_additions"),
16-
.package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.6.1")
16+
.package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.6.1"),
17+
.package(url: "https://github.com/AlwaysRightInstitute/Mustache", from: "1.0.0")
1718
],
1819
targets: [
1920
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
2021
// Targets can depend on other targets in this package, and on products in packages this package depends on.
2122
.executableTarget(
2223
name: "segmentcli",
23-
dependencies: ["SwiftCLI", "Spinner", "ColorizeSwift", "SwiftCSV", .product(name: "Segment", package: "analytics-swift")]),
24+
dependencies: ["SwiftCLI",
25+
"Spinner",
26+
"ColorizeSwift",
27+
"SwiftCSV",
28+
.product(name: "mustache", package: "Mustache"),
29+
.product(name: "Segment", package: "analytics-swift")]),
2430
.testTarget(
2531
name: "segmentcliTests",
2632
dependencies: ["segmentcli"]),
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by Brandon Sneed on 12/7/21.
6+
//
7+
8+
import Foundation
9+
import SwiftCLI
10+
import mustache
11+
12+
class ScaffoldCommand: Command {
13+
let name = "scaffold"
14+
let shortDescription = "Create baseline implementation of a given code artifact"
15+
16+
@Flag("-p", "--plugin", description: "Generate an analytics plugin (objc/swift/java/kotlin/ts)")
17+
var plugin: Bool
18+
19+
@Flag("-e", "--edgefn", description: "Generate an edge function (js)")
20+
var edgeFn: Bool
21+
22+
@Flag("-i", "--importer", description: "Generate a CSV importer script (js)")
23+
var importer: Bool
24+
25+
@Flag("--objc", description: "Use Objective-C for generated plugin")
26+
var useObjc: Bool
27+
@Flag("--swift", description: "Use Swift for generated plugin")
28+
var useSwift: Bool
29+
@Flag("--java", description: "Use Java for generated plugin")
30+
var useJava: Bool
31+
@Flag("--kotlin", description: "Use Kotlin for generated plugin")
32+
var useKotlin: Bool
33+
@Flag("--ts", description: "Use Typescript for generated plugin")
34+
var useTypescript: Bool
35+
@Flag("--js", description: "Use Javascript for generated edgefn/importer")
36+
var useJavascript: Bool
37+
38+
@Key("-n", "--name", description: "Optionally specify a name for the generated scaffold")
39+
var nameParam: String?
40+
41+
var scaffoldName: String?
42+
let fileManager = FileManager.default
43+
44+
var optionGroups: [OptionGroup] {
45+
return [
46+
.atLeastOne($plugin, $edgeFn, $importer),
47+
.atMostOne($plugin, $edgeFn, $importer),
48+
.atLeastOne($useObjc, $useSwift, $useJava, $useKotlin, $useTypescript, $useJavascript)]
49+
}
50+
51+
func execute() throws {
52+
if nameParam == nil {
53+
if plugin {
54+
scaffoldName = "MyPlugin"
55+
} else if edgeFn {
56+
scaffoldName = "MyEdgeFunction"
57+
} else if importer {
58+
scaffoldName = "MyCSVImporter"
59+
}
60+
} else {
61+
scaffoldName = nameParam
62+
}
63+
64+
if plugin && useSwift {
65+
generateSwiftPlugin()
66+
}
67+
}
68+
69+
func generateSwiftPlugin() {
70+
guard let scaffoldName = scaffoldName else {
71+
exitWithError("Could not determine a plugin name to use.")
72+
return
73+
}
74+
75+
let filename = scaffoldName + ".swift"
76+
77+
print("Generating a Swift Plugin from template...")
78+
79+
for file in plugin_templates_swift {
80+
if fileManager.fileExists(atPath: filename) {
81+
let overwrite = Input.readBool(prompt: "\(filename) exists. Overwrite? [y/N]: ", defaultValue: false)
82+
if overwrite == false {
83+
exitWithError(code: .commandFailed)
84+
}
85+
}
86+
let generate = Mustache(file)
87+
let result = generate(name: scaffoldName, filename: filename)
88+
do {
89+
try result.write(toFile: filename, atomically: true, encoding: .utf8)
90+
print("Created \(filename).")
91+
} catch {
92+
exitWithError("Unable to write \(filename)")
93+
}
94+
}
95+
96+
print("\n")
97+
}
98+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
let plugin_templates_swift = [plugin_swift]
2+
3+
4+
let plugin_swift = """
5+
//
6+
// {{filename}}
7+
//
8+
//
9+
// Created by `segmentcli --plugin --swift -n {{name}}`.
10+
//
11+
// Add this code to your project. To apply this plugin to
12+
// the analytics timeline, it will also be necessary to add
13+
// the following code after you've created your Analytics
14+
// instance.
15+
//
16+
// ```
17+
// analytics.add(plugin: {{name}}())
18+
// ```
19+
//
20+
// This will add the {{name}} plugin such that events will
21+
// start flowing through it as they come in.
22+
//
23+
// See the link below for more information:
24+
// https://segment.com/docs/connections/sources/catalog/libraries/mobile/swift-ios/#adding-a-plugin
25+
//
26+
27+
import Foundation
28+
import Segment
29+
30+
class {{name}}: EventPlugin {
31+
var type: PluginType = .enrichment
32+
33+
var analytics: Analytics?
34+
35+
func track(event: TrackEvent) -> TrackEvent? {
36+
return event
37+
}
38+
39+
func identify(event: IdentifyEvent) -> IdentifyEvent? {
40+
return event
41+
}
42+
43+
func screen(event: ScreenEvent) -> ScreenEvent? {
44+
return event
45+
}
46+
47+
func group(event: GroupEvent) -> GroupEvent? {
48+
return event
49+
}
50+
51+
func alias(event: AliasEvent) -> AliasEvent? {
52+
return event
53+
}
54+
55+
func flush() {
56+
57+
}
58+
59+
func reset() {
60+
61+
}
62+
}
63+
"""

Sources/segmentcli/Utilities/Misc.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ func exitWithError(code: ErrorCode) {
3333
}
3434

3535
func exitWithError(code: ErrorCode, message: String) {
36-
fputs("\(message)\n\n", stderr)
36+
fputs("Error: \(message)\n\n", stderr)
3737
exit(Int32(code.rawValue))
3838
}
3939

4040
func exitWithError(_ error: Error) {
4141
if let str = error as? String {
42-
fputs("\(str)\n\n", stderr)
42+
fputs("Error: \(str)\n\n", stderr)
4343
} else {
44-
fputs("\(error.localizedDescription)\n\n", stderr)
44+
fputs("Error: \(error.localizedDescription)\n\n", stderr)
4545
}
4646
exit(Int32(ErrorCode.commandFailed.rawValue))
4747
}

Sources/segmentcli/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ func main() {
99
AuthCommand(),
1010
ProfileGroup(),
1111
ImportCommand(),
12-
AnalyticsGroup()
12+
AnalyticsGroup(),
13+
ScaffoldCommand()
1314
])
1415

1516
segment.globalOptions.append(specifiedProfileKey)

0 commit comments

Comments
 (0)