-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprojects.go
More file actions
249 lines (205 loc) · 5.21 KB
/
Copy pathprojects.go
File metadata and controls
249 lines (205 loc) · 5.21 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"bufio"
"fmt"
"os"
"path"
"strings"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
)
func init() {
commands = append(commands, &cli.Command{
Name: "init",
Usage: "Initialize a new CaffeineC project",
Category: "project",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "The name of the project",
},
&cli.StringFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "The version of the project",
},
&cli.StringFlag{
Name: "main",
Aliases: []string{"m"},
Usage: "The main file of the project",
},
&cli.StringFlag{
Name: "author",
Aliases: []string{"a"},
Usage: "The author of the project",
},
&cli.StringFlag{
Name: "license",
Aliases: []string{"l"},
Usage: "The license of the project",
},
},
Action: initProject,
})
}
type CfConf struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Version string `yaml:"version"`
Main string `yaml:"main"`
Dependencies []string `yaml:"dependencies"`
Author string `yaml:"author"`
License string `yaml:"license"`
}
func (c *CfConf) CreateDefault() {
c.Name = "NewProject"
c.Description = "A new CaffeineC project"
c.Version = "1.0.0"
c.Main = "src/main.cffc"
c.Author = "Anonymous"
c.License = "MIT"
}
func (c *CfConf) Save(filepath string) error {
if _, err := os.Stat(filepath); !os.IsNotExist(err) {
if promptYN(filepath+" already exists. Overwrite?", false) {
os.Remove(filepath)
} else {
return nil
}
}
_, err := os.Create(filepath)
if err != nil {
return err
}
file, err := os.OpenFile(filepath, os.O_WRONLY, 0644)
if err != nil {
return err
}
yml, err := yaml.Marshal(c)
if err != nil {
return err
}
_, err = file.Write(yml)
if err != nil {
return err
}
return nil
}
func initProject(c *cli.Context) error {
rootDir := c.Args().First()
if rootDir == "" {
rootDir = "."
}
// Check if the directory exists
if _, err := os.Stat(rootDir); !os.IsNotExist(err) {
// The directory exists, check if it has any contents
files, err := os.ReadDir(rootDir)
if err != nil {
return err
}
if len(files) > 0 {
if !promptYN("The directory is not empty, continue?", false) {
return nil
}
}
} else {
err := os.Mkdir(rootDir, 0755)
if err != nil {
return err
}
fmt.Println("Created directory:", rootDir)
}
if _, err := os.Stat(path.Join(rootDir, "src")); os.IsNotExist(err) {
err := os.Mkdir(path.Join(rootDir, "src"), 0755)
if err != nil {
return err
}
fmt.Println("Created directory:", path.Join(rootDir, "src"))
}
if _, err := os.Stat(path.Join(rootDir, "src", "main.cffc")); os.IsNotExist(err) {
_, err := os.Create(path.Join(rootDir, "src", "main.cffc"))
if err != nil {
return err
}
file, err := os.OpenFile(path.Join(rootDir, "src", "main.cffc"), os.O_WRONLY, 0644)
if err != nil {
return err
}
_, err = file.WriteString("package main;\n\nextern func printf(format: *i8): void;\n\nfunc main(): i64 {\n\tprintf(\"Hello, world!\\n\");\n\treturn 0;\n}\n")
if err != nil {
return err
}
fmt.Println("Created file:", path.Join(rootDir, "src", "main.cffc"))
}
if promptYN("Use default configuration?", false) {
conf := CfConf{}
conf.CreateDefault()
err := conf.Save(path.Join(rootDir, "cfconf.yaml"))
if err != nil {
return err
}
fmt.Println("Created file:", path.Join(rootDir, "cfconfig.yaml"))
} else {
conf := CfConf{}
conf.Name = promptString("Project name", "NewProject")
conf.Description = promptString("Project description", "A new CaffeineC project")
conf.Version = promptString("Project version", "1.0.0")
conf.Main = promptString("Main file", "src/main.cffc")
conf.Author = promptString("Author", "Anonymous")
conf.License = promptString("License", "MIT")
err := conf.Save(path.Join(rootDir, "cfconf.yaml"))
if err != nil {
return err
}
fmt.Println("Created file:", path.Join(rootDir, "cfconf.yaml"))
}
fmt.Println("----------------------------------------")
fmt.Println("Project initialized successfully!")
fmt.Println("Run 'cd", rootDir, "&& caffeinec build' to build the project.'")
fmt.Println("----------------------------------------")
return nil
}
func GetCfConf(dir string) (CfConf, error) {
var conf CfConf
file, err := os.Open(path.Join(dir, "cfconf.yaml"))
if err != nil {
return CfConf{}, err
}
decoder := yaml.NewDecoder(file)
err = decoder.Decode(&conf)
if err != nil {
return CfConf{}, err
}
return conf, nil
}
func promptYN(prompt string, def bool) bool {
reader := bufio.NewReader(os.Stdin)
if def {
fmt.Printf("%s (Y/n): ", prompt)
} else {
fmt.Printf("%s (y/N): ", prompt)
}
response, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
response = strings.TrimSpace(response)
if response == "" {
return def
}
return strings.ToLower(response) == "y"
}
func promptString(prompt string, def string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("%s (%s): ", prompt, def)
response, err := reader.ReadString('\n')
if err != nil {
panic(err)
}
response = strings.TrimSpace(response)
if response == "" {
return def
}
return response
}