This repository has been archived by the owner on Sep 3, 2018. It is now read-only.
forked from hebertialmeida/ModelGen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.swift
68 lines (54 loc) · 2.14 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// main.swift
// ModelGen
//
// Created by Heberti Almeida on 2017-05-10.
// Copyright © 2017 ModelGen. All rights reserved.
//
import Commander
import PathKit
// MARK: - Options
let specOption = Option<Path>("spec", Path(), flag: "s", description: "The spec folder or file path e.g: ./specs or user.json", validator: pathExists)
let templateOption = Option<String>("template", "", flag: "t", description: "The path of the template to use for code generation.")
let languageOption = Option<String>("language", Language.swift.rawValue, flag: "l", description: "The output language e.g: swift")
let outputOption = Option<OutputDestination>("output", .console, flag: "o", description: "The path to the file to generate (Omit to generate to stdout)")
// MARK: - Version
let version = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "0.2.0"
// MARK: - Main
command(outputOption, templateOption, languageOption, specOption) { output, templatePath, lang, path in
guard path.string.isEmpty else {
do {
try parse(output: output, template: templatePath, lang: lang, path: path)
} catch {
printError(error.localizedDescription)
}
return
}
let yamlPath = Path(".modelgen.yml")
do {
let yamlContents = try yamlPath.read(.utf8)
let dict = try YamlParser.parse(yamlContents)
guard let config = Configuration(dictionary: dict) else {
printError("Error creating configuration")
exit(1)
}
guard let configSpec = config.spec else {
printError(YamlParserError.missingSpecPath.localizedDescription)
exit(1)
}
guard let configTemplate = config.template else {
printError(YamlParserError.missingTemplate.localizedDescription)
exit(1)
}
var configOutput: OutputDestination {
guard let output = config.output else {
return .console
}
return OutputDestination.file(Path(output))
}
try render(output: configOutput, template: configTemplate, lang: config.language ?? Language.swift.rawValue, path: Path(configSpec))
exit(0)
} catch {
printError(error.localizedDescription)
}
}.run("ModelGen v\(version)")