-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprojects.go
More file actions
485 lines (424 loc) · 11.6 KB
/
Copy pathprojects.go
File metadata and controls
485 lines (424 loc) · 11.6 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
package main
import (
"fmt"
"net/url"
"os"
"path"
"strings"
"github.com/fatih/color"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/urfave/cli/v2"
"github.com/vyPal/CaffeineC/lib/cache"
"github.com/vyPal/CaffeineC/lib/project"
"github.com/vyPal/CaffeineC/util"
)
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,
}, &cli.Command{
Name: "install",
Aliases: []string{"i"},
Usage: "Installs a package and adds it to the project",
Description: "Clones a package from a remote git repository to the package cache." +
"\nIf the package is already in the cache, it only adds a reference to it to the project's config file." +
"\n\nIf the command is run without the url argument, it installs all package fro the current project.",
Category: "project",
ArgsUsage: "<url>",
Action: install,
}, &cli.Command{
Name: "library",
Aliases: []string{"lib"},
Usage: "Manages theCaffeineC libraries",
Subcommands: []*cli.Command{
{
Name: "info",
Usage: "Displays information about a package",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "The version of the package",
},
},
Action: libInfo,
},
{
Name: "update",
Usage: "Updates a package",
Action: libUpdate,
},
},
Category: "project",
Action: libInfo,
})
}
func install(c *cli.Context) error {
liburl := c.Args().First()
conf, err := project.GetCfConf("")
if err != nil {
return err
}
pcache := cache.PackageCache{}
err = pcache.Init()
if err != nil {
return err
}
var cnf project.CfConf
if liburl == "" {
for _, dep := range conf.Dependencies {
pkg, found, err := pcache.FindPackage(dep.Package, dep.Version, dep.Identifier)
if err != nil {
return err
}
if !found {
color.Green("Package not found locally, cloning...")
conf, _, _, err = cache.InstallLibrary(pcache, dep.Identifier, dep.Version)
if err != nil {
return err
}
} else {
conf, err = project.GetCfConf(pkg.Path)
if err != nil {
return err
}
}
}
} else {
err = pcache.CacheScan(true)
if err != nil {
return err
}
liburl, version, err := PrepUrl(liburl)
if err != nil {
return err
}
pkg, f, err := pcache.FindPackage("", version, strings.TrimPrefix(liburl, "https://"))
if err != nil {
return err
}
var ident, ver string
if !f {
color.Green("Package not found locally, cloning...")
cnf, ident, ver, err = cache.InstallLibrary(pcache, liburl, version)
if err != nil {
return err
}
} else {
ident = pkg.Identifier
ver = pkg.Version
cnf, err = project.GetCfConf(pkg.Path)
if err != nil {
return err
}
}
dep := project.CFConfDependency{
Package: cnf.Name,
Version: ver,
Identifier: ident,
}
found := false
for _, dependency := range conf.Dependencies {
if dependency == dep {
found = true
break
}
}
if !found {
conf.Dependencies = append(conf.Dependencies, dep)
err = conf.Save("cfconf.yaml", true)
if err != nil {
return err
}
}
fmt.Println("Added package", cnf.Name, "to the project.")
}
fmt.Println("--------------------------------------------------")
fmt.Println(" Package Details ")
fmt.Println("--------------------------------------------------")
fmt.Printf("Name : %s\n", cnf.Name)
fmt.Printf("Description : %s\n", cnf.Description)
fmt.Printf("Version : %s\n", cnf.Version)
fmt.Printf("Main File : %s\n", cnf.Main)
fmt.Printf("Author : %s\n", cnf.Author)
fmt.Printf("License : %s\n", cnf.License)
fmt.Println("--------------------------------------------------")
return nil
}
func PrepUrl(liburl string) (u, ver string, e error) {
version := "main"
if strings.Contains(liburl, "@") {
split := strings.Split(liburl, "@")
liburl = split[0]
version = split[1]
} else {
color.Yellow("Branch name not specified, defaulting to 'main'")
}
parsedUrl, err := url.Parse(liburl)
if err != nil {
return "", "", err
}
if parsedUrl.Hostname() == "" {
liburl = "https://github.com/" + liburl
}
if !strings.HasPrefix(liburl, "http://") && !strings.HasPrefix(liburl, "https://") {
liburl = "https://" + liburl
}
return liburl, version, nil
}
func libInfo(c *cli.Context) error {
liburl := c.Args().First()
var conf project.CfConf
var err error
if liburl == "" {
conf, err = project.GetCfConf("")
if err != nil {
return err
}
} else {
pcache := cache.PackageCache{}
err = pcache.Init()
if err != nil {
return err
}
err = pcache.CacheScan(true)
if err != nil {
return err
}
pkg, found, err := pcache.FindPackage("", "", liburl)
if err != nil {
return err
}
if !found {
color.Green("Package not found locally, cloning...")
liburl, version, err := PrepUrl(liburl)
if err != nil {
return err
}
// Create a temporary directory
tempDir, err := os.MkdirTemp("", "cclib*")
if err != nil {
return err
}
defer os.RemoveAll(tempDir) // clean up
// Clone the repository to the temporary directory
_, err = git.PlainClone(tempDir, false, &git.CloneOptions{
URL: liburl,
SingleBranch: true,
Depth: 1,
ReferenceName: plumbing.NewBranchReferenceName(version),
})
if err != nil {
return err
}
// Get the configuration file from the cloned repository
conf, err = project.GetCfConf(tempDir)
if err != nil {
return err
}
} else {
conf, err = project.GetCfConf(pkg.Path)
if err != nil {
return err
}
}
}
fmt.Println("--------------------------------------------------")
fmt.Println(" Package Details ")
fmt.Println("--------------------------------------------------")
fmt.Printf("Name : %s\n", conf.Name)
fmt.Printf("Description : %s\n", conf.Description)
fmt.Printf("Version : %s\n", conf.Version)
fmt.Printf("Main File : %s\n", conf.Main)
fmt.Printf("Author : %s\n", conf.Author)
fmt.Printf("License : %s\n", conf.License)
fmt.Println("--------------------------------------------------")
return nil
}
func libUpdate(c *cli.Context) error {
liburl := c.Args().First()
conf, err := project.GetCfConf("")
if err != nil {
return err
}
pcache := cache.PackageCache{}
err = pcache.Init()
if err != nil {
return err
}
var cnf project.CfConf
if liburl == "" {
for _, dep := range conf.Dependencies {
_, found, err := pcache.FindPackage(dep.Package, dep.Version, dep.Identifier)
if err != nil {
return err
}
if !found {
color.Green("Package not found locally, cloning...")
cnf, _, _, err = cache.InstallLibrary(pcache, dep.Identifier, dep.Version)
if err != nil {
return err
}
} else {
color.Green("Package found locally, updating...")
cnf, _, _, err = cache.UpdateLibrary(pcache, dep.Identifier, dep.Version)
if err != nil {
return err
}
}
}
} else {
err = pcache.CacheScan(true)
if err != nil {
return err
}
liburl, version, err := PrepUrl(liburl)
if err != nil {
return err
}
_, found, err := pcache.FindPackage("", version, strings.TrimPrefix(liburl, "https://"))
if err != nil {
return err
}
var ident, ver string
if !found {
color.Green("Package not found locally, cloning...")
cnf, ident, ver, err = cache.InstallLibrary(pcache, liburl, version)
if err != nil {
return err
}
} else {
color.Green("Package found locally, updating...")
cnf, ident, ver, err = cache.UpdateLibrary(pcache, liburl, version)
if err != nil {
return err
}
}
dep := project.CFConfDependency{
Package: cnf.Name,
Version: ver,
Identifier: ident,
}
conf.Dependencies = append(conf.Dependencies, dep)
err = conf.Save("cfconf.yaml", true)
if err != nil {
return err
}
fmt.Println("Updated package", cnf.Name, "in the project.")
}
fmt.Println("--------------------------------------------------")
fmt.Println(" Package Details ")
fmt.Println("--------------------------------------------------")
fmt.Printf("Name : %s\n", cnf.Name)
fmt.Printf("Description : %s\n", cnf.Description)
fmt.Printf("Version : %s\n", cnf.Version)
fmt.Printf("Main File : %s\n", cnf.Main)
fmt.Printf("Author : %s\n", cnf.Author)
fmt.Printf("License : %s\n", cnf.License)
fmt.Println("--------------------------------------------------")
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 !util.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 util.PromptYN("Use default configuration?", false) {
conf := project.CfConf{}
conf.CreateDefault(rootDir)
err := conf.Save(path.Join(rootDir, "cfconf.yaml"), false)
if err != nil {
return err
}
fmt.Println("Created file:", path.Join(rootDir, "cfconfig.yaml"))
} else {
conf := project.CfConf{}
conf.Name = util.PromptString("Project name", "NewProject")
conf.Description = util.PromptString("Project description", "A new CaffeineC project")
conf.Version = util.PromptString("Project version", "1.0.0")
conf.Main = util.PromptString("Main file", "src/main.cffc")
conf.Author = util.PromptString("Author", "Anonymous")
conf.License = util.PromptString("License", "MIT")
err := conf.Save(path.Join(rootDir, "cfconf.yaml"), false)
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
}