-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (90 loc) · 2.97 KB
/
main.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"embed"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/moeenn/projects/internal/templates"
"github.com/moeenn/projects/internal/templates/c"
"github.com/moeenn/projects/internal/templates/cppCmake"
"github.com/moeenn/projects/internal/templates/cppMake"
"github.com/moeenn/projects/internal/templates/golang"
"github.com/moeenn/projects/internal/templates/javaGradle"
"github.com/moeenn/projects/internal/templates/js"
"github.com/moeenn/projects/internal/templates/python"
"github.com/moeenn/projects/internal/templates/ts"
)
//go:embed stubs
var stubFS embed.FS
var (
TEMPLATE_NAMES = [8]string{
"go", "c", "cpp-cmake", "cpp-make", "javascript (or 'js')", "typescript (or 'ts')", "java-gradle", "python (or 'py')",
}
)
func run() error {
templatePtr := flag.String("template", TEMPLATE_NAMES[0], "Project template to use")
projectNamePtr := flag.String("name", "sandbox", "Name of project being initialized")
listTemplatesPtr := flag.Bool("list", false, "Print list of available template names")
flag.Parse()
if *listTemplatesPtr {
fmt.Printf("Valid templates include: \n - %s\n", strings.Join(TEMPLATE_NAMES[:], "\n - "))
return nil
}
cwd, err := os.Getwd()
if err != nil {
return fmt.Errorf("failed to detect project current directory: %s", err.Error())
}
stubTemplates := template.Must(template.ParseFS(stubFS, "stubs/**/*.stub"))
templateArgs := &templates.TemplateArgs{
Templates: stubTemplates,
ProjectName: *projectNamePtr,
RootPath: filepath.Join(cwd, *projectNamePtr),
}
var config *templates.TemplateConfig
switch *templatePtr {
case "c":
config = c.NewCConfig(templateArgs)
err = templateArgs.Initialize("C", config)
case "cpp-make":
config = cppMake.NewCPPCmakeConfig(templateArgs)
err = templateArgs.Initialize("C++ (Make)", config)
case "cpp-cmake":
config = cppCmake.NewCPPCmakeConfig(templateArgs)
err = templateArgs.Initialize("C++ (Cmake)", config)
case "go":
config = golang.NewGolangConfig(templateArgs)
err = templateArgs.Initialize("Golang", config)
case "js", "javascript":
config = js.NewJSConfig(templateArgs)
err = templateArgs.Initialize("Javascript (vanilla)", config)
case "ts", "typescript":
config = ts.NewTSConfig(templateArgs)
err = templateArgs.Initialize("Typescript", config)
case "java-gradle":
config = javaGradle.NewJavaGradleConfig(templateArgs)
err = templateArgs.Initialize("Java (Gradle)", config)
case "py", "python":
config = python.NewPythonConfig(templateArgs)
err = templateArgs.Initialize("Python", config)
default:
err = fmt.Errorf("invalid project template name: %s", *templatePtr)
}
if err != nil {
// cleanup: remove any created files in case of error
err = os.RemoveAll(templateArgs.RootPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
}
return err
}
return nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
os.Exit(1)
}
}