Last active
October 8, 2017 19:00
-
-
Save nathanborror/59ef7cab17f32ffc2486dea7a73ee2cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
//go:generate esc -o static.go -prefix "/" static | |
import ( | |
"flag" | |
"fmt" | |
"os" | |
"text/template" | |
) | |
var templates = []string{ | |
"Client/AppDelegate.swift", | |
"Client/HomeController.swift", | |
"Client/LaunchScreen.storyboard", | |
"Client/AppIcon-Contents.json", | |
"Client/Contents.json", | |
"Kit/Service.swift", | |
"Kit/Types.swift", | |
"Kit/Error.swift", | |
"Tests/Tests.swift", | |
"Tests/UITests.swift", | |
"gitignore", | |
"Info.plist", | |
"Makefile", | |
"project.yml", | |
} | |
var dirs = []string{ | |
"%s/Sources/%s", | |
"%s/Sources/%s/Assets.xcassets/AppIcon.appiconset", | |
"%s/Sources/%s/Base.lproj", | |
"%s/Sources/%sKit", | |
"%s/Tests/%sTests", | |
"%s/Tests/%sUITests", | |
"%s/Tests/%sKitTests", | |
} | |
var ( | |
name = flag.String("name", "App", "Name of the project") | |
hasKit = flag.Bool("kit", false, "Adds a Kit framework to project") | |
hasTests = flag.Bool("tests", false, "Adds Tests to project") | |
hasUITests = flag.Bool("uitests", false, "Adds UI Tests to project") | |
liveFlag = flag.Bool("liveassets", false, "Serve Static Assets from Disk relative to CWD") | |
) | |
func main() { | |
flag.Parse() | |
for _, dir := range dirs { | |
path := fmt.Sprintf(dir, *name, *name) | |
if _, err := os.Stat(path); os.IsNotExist(err) { | |
os.MkdirAll(path, os.ModePerm) | |
} | |
} | |
proj := projectContext{*name, "project.yml", *hasKit, *hasTests, *hasUITests} | |
infoBNDL := infoPlistContext{*name, "Info.plist", "BNDL", "$(CURRENT_PROJECT_VERSION)"} | |
infoFMWK := infoPlistContext{*name, "Info.plist", "FMWK", "$(CURRENT_PROJECT_VERSION)"} | |
infoAPPL := infoPlistContext{*name, "Info.plist", "APPL", "1"} | |
base := map[string]templateContext{ | |
fmt.Sprintf("%s/.gitignore", *name): fileContext{*name, "gitignore"}, | |
fmt.Sprintf("%s/Makefile", *name): fileContext{*name, "Makefile"}, | |
fmt.Sprintf("%s/project.yml", *name): proj, | |
} | |
writeFiles(base) | |
client := map[string]templateContext{ | |
fmt.Sprintf("%s/Sources/%s/AppDelegate.swift", *name, *name): fileContext{*name, "Client/AppDelegate.swift"}, | |
fmt.Sprintf("%s/Sources/%s/HomeController.swift", *name, *name): fileContext{*name, "Client/HomeController.swift"}, | |
fmt.Sprintf("%s/Sources/%s/Info.plist", *name, *name): infoAPPL, | |
fmt.Sprintf("%s/Sources/%s/Base.lproj/LaunchScreen.storyboard", *name, *name): fileContext{*name, "Client/LaunchScreen.storyboard"}, | |
fmt.Sprintf("%s/Sources/%s/Assets.xcassets/Contents.json", *name, *name): fileContext{*name, "Client/Contents.json"}, | |
fmt.Sprintf("%s/Sources/%s/Assets.xcassets/AppIcon.appiconset/Contents.json", *name, *name): fileContext{*name, "Client/AppIcon-Contents.json"}, | |
} | |
writeFiles(client) | |
if proj.HasKit { | |
kit := map[string]templateContext{ | |
fmt.Sprintf("%s/Sources/%sKit/%s.swift", *name, *name, *name): fileContext{*name, "Kit/Service.swift"}, | |
fmt.Sprintf("%s/Sources/%sKit/Types.swift", *name, *name): fileContext{*name, "Kit/Types.swift"}, | |
fmt.Sprintf("%s/Sources/%sKit/Error.swift", *name, *name): fileContext{*name, "Kit/Error.swift"}, | |
fmt.Sprintf("%s/Sources/%sKit/Info.plist", *name, *name): infoFMWK, | |
} | |
writeFiles(kit) | |
} | |
if proj.HasTests { | |
tests := map[string]templateContext{ | |
fmt.Sprintf("%s/Tests/%sTests/Info.plist", *name, *name): infoBNDL, | |
fmt.Sprintf("%s/Tests/%sTests/Tests.swift", *name, *name): fileContext{*name, "Tests/Tests.swift"}, | |
fmt.Sprintf("%s/Tests/%sKitTests/Info.plist", *name, *name): infoBNDL, | |
fmt.Sprintf("%s/Tests/%sKitTests/Tests.swift", *name, *name): fileContext{*name, "Tests/Tests.swift"}, | |
} | |
writeFiles(tests) | |
} | |
if proj.HasUITests { | |
uitests := map[string]templateContext{ | |
fmt.Sprintf("%s/Tests/%sUITests/Info.plist", *name, *name): infoBNDL, | |
fmt.Sprintf("%s/Tests/%sUITests/Tests.swift", *name, *name): fileContext{*name, "Tests/UITests.swift"}, | |
} | |
writeFiles(uitests) | |
} | |
} | |
func writeFiles(files map[string]templateContext) error { | |
for filename, context := range files { | |
if _, err := os.Stat(filename); os.IsNotExist(err) { | |
file, err := os.Create(filename) | |
if err != nil { | |
return fmt.Errorf("Failed to create file: %s", err) | |
} | |
path, err := FSString(*liveFlag, fmt.Sprintf("/static/%s", context.TemplateName())) | |
if err != nil { | |
return fmt.Errorf("Failed to read template: %s", err) | |
} | |
tmpl, err := template.New(context.TemplateName()).Parse(path) | |
if err != nil { | |
return fmt.Errorf("Failed to parse template: %s", err) | |
} | |
if err := tmpl.Execute(file, context); err != nil { | |
return fmt.Errorf("Failed to execute template: %s", err) | |
} | |
file.Close() | |
} | |
} | |
return nil | |
} | |
type fileContext struct { | |
Name string | |
Template string | |
} | |
type projectContext struct { | |
Name string | |
Template string | |
HasKit bool | |
HasTests bool | |
HasUITests bool | |
} | |
type infoPlistContext struct { | |
Name string | |
Template string | |
Kind string | |
Version string | |
} | |
type templateContext interface { | |
TemplateName() string | |
} | |
func (c fileContext) TemplateName() string { | |
return c.Template | |
} | |
func (c projectContext) TemplateName() string { | |
return c.Template | |
} | |
func (c infoPlistContext) TemplateName() string { | |
return c.Template | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment