-
Notifications
You must be signed in to change notification settings - Fork 2
/
process.templates.sh
executable file
·83 lines (79 loc) · 3.2 KB
/
process.templates.sh
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/sh
# Takes a set of templates stored in files and creates Go code that specifies the
# same templates inline. Normally the scaffolder uses these inline versions and so
# the user doesn't need to specify where to find the tenplates when they run the
# scaffolder.
#
# usage:
# ./process_templates.sh
process() {
echo 'package main' >$1
echo >>$1
echo 'import (' >>$1
echo ' "io/ioutil"' >>$1
echo ' "strings"' >>$1
echo ' "text/template"' >>$1
echo ' "log"' >>$1
echo ' "os"' >>$1
echo ')' >>$1
echo >>$1
echo '// substituteGraves replaces each occurence of the sequence "%%GRAVE%%" with a' >>$1
echo '// single grave (backtick) rune. In this source file, all templates are quoted in' >>$1
echo '// graves, but some templates contain graves, and a grave within a grave causes a' >>$1
echo '// syntax error. The solution is to replace the graves in the template with' >>$1
echo '// "%%GRAVE%% and then pre-process the template before use.' >>$1
echo 'func substituteGraves(s string) string {' >>$1
echo ' return strings.Replace(s, "%%GRAVE%%", "\x60", -1)' >>$1
echo '}' >>$1
echo >>$1
echo '// createTemplateFromFile creates a template from a file. The file is in the' >>$1
echo '// templates directory wherever the scaffolder is installed, and that is out of our' >>$1
echo '// control, so this should only be called when the "templatedir" command line' >>$1
echo '// argument is specified. ' >>$1
echo 'func createTemplateFromFile(templateName string) *template.Template {' >>$1
echo ' log.SetPrefix("createTemplate() ")' >>$1
echo ' templateFile := templateDir + templateName' >>$1
echo ' buf, err := ioutil.ReadFile(templateFile)' >>$1
echo ' if err != nil {' >>$1
echo ' log.Printf("cannot open template file %s - %s ",' >>$1
echo ' templateFile, err.Error())' >>$1
echo ' os.Exit(-1)' >>$1
echo ' }' >>$1
echo ' tp := string(buf)' >>$1
echo ' tp = substituteGraves(tp)' >>$1
echo ' return template.Must(template.New(templateName).Parse(tp))' >>$1
echo '}' >>$1
echo >>$1
echo 'func createTemplates(useBuiltIn bool) {' >>$1
first=1
for file in *
do
echo
if test $first -eq 1
then
echo 'templateName := "'$file'"'
else
echo 'templateName = "'$file'"'
fi
first=0
echo ' if useBuiltIn {'
echo ' if verbose {'
echo ' log.Printf("creating template %s from builtin template", templateName)'
echo ' }'
echo ' templateText := `'
cat $file
echo '`'
echo ' templateText = substituteGraves(templateText)'
echo ' templateMap[templateName] ='
echo ' template.Must(template.New(templateName).Parse(templateText))'
echo ' } else {'
echo ' if verbose {'
echo ' log.Printf("creating template %s from file %s", templateName, templateDir+templateName)'
echo ' }'
echo ' templateMap[templateName] = createTemplateFromFile(templateName)'
echo ' }'
done >>$1
echo '}' >>$1
}
cd $GOPATH/src/github.com/goblimey/scaffolder/templates
process "../create_templates.go"