Skip to content

Commit

Permalink
Implement xctemplate generate <Template Name>
Browse files Browse the repository at this point in the history
  • Loading branch information
ngtk committed May 23, 2018
1 parent 1c10519 commit aa0c76b
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"

[prune]
go-tests = true
unused-packages = true
22 changes: 22 additions & 0 deletions xctemplate/templates/Swift.xctemplate/TemplateInfo.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Kind</key>
<string>Xcode.IDEFoundation.TextSubstitutionFileTemplateKind</string>
<key>Description</key>
<string>An empty Swift file.</string>
<key>Summary</key>
<string>An empty Swift file</string>
<key>SortOrder</key>
<string>30</string>
<key>AllowedTypes</key>
<array>
<string>public.swift-source</string>
</array>
<key>DefaultCompletionName</key>
<string>File</string>
<key>MainTemplateFile</key>
<string>___FILEBASENAME___.swift</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//___FILEHEADER___

import Foundation
58 changes: 58 additions & 0 deletions xctemplate/xctemplate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"log"
"os"

"github.com/gobuffalo/packr"
"github.com/urfave/cli"
)

func main() {
app := cli.NewApp()
app.Name = "xcode-template"
app.Usage = "Create a template and share with your project"

app.Commands = []cli.Command{
{
Name: "generate",
Aliases: []string{"g"},
Usage: "generate a new template",
Action: func(c *cli.Context) error {
box := packr.NewBox("./templates")

currentDir, _ := os.Getwd()
filename := c.Args().First()
fileDir := fmt.Sprintf("%s/Templates/%s.xctemplate", currentDir, filename)
templateInfoPath := fmt.Sprintf("%s/TemplateInfo.plist", fileDir)
swiftFilePath := fmt.Sprintf("%s/___FILEBASENAME___.swift", fileDir)

os.MkdirAll(fileDir, 0777)

// Create TemplateInfo.plist
templateInfoFile, err := os.Create(templateInfoPath)
if err != nil {
log.Fatal(err)
}
templateInfoString := box.String("Swift.xctemplate/TemplateInfo.plist")
templateInfoFile.Write(([]byte)(templateInfoString))

// Create Swift File
swiftFile, err := os.Create(swiftFilePath)
if err != nil {
log.Fatal(err)
}
templateString := box.String("Swift.xctemplate/___FILEBASENAME___.swift")
swiftFile.Write(([]byte)(templateString))

return nil
},
},
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}

0 comments on commit aa0c76b

Please sign in to comment.