-
Notifications
You must be signed in to change notification settings - Fork 44
/
run.go
554 lines (484 loc) · 14.4 KB
/
run.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
This file provides the api for running a kcl package.
You can use `RunOptions` to set the options for running a kcl package.
Before running a kcl package, you should create a instance for `KpmClient`.
```go
kpmcli := client.NewKpmClient()
```
You can use the method `Run` for `KpmClient` with options `RunOptions` to run a kcl package.
```go
kpmcli.Run(
WithWorkDir("path/to/workdir"),
WithRunSourceUrl("path/to/source"),
)
```
You can set the KCL package sources by `WithRunSourceUrls` or `WithRunSourceUrl`.
The KCL package sources include the local path, the remote git/oci path, etc.
For remote git/oci path, you can set the package sources
```go
// The KCL package sources are the remote git repo.
kpmcli.Run(
WithRunSourceUrl("git://github.com/test/test.git"),
)
// The KCL package sources are the remote oci registry.
kpmcli.Run(
WithRunSourceUrl("oci://ghcr.com/test/test"),
)
```
For local paths, you can set multiple *.k files or directories.
likg:
```go
kpmcli.Run(
WithRunSourceUrl("local/usr/test1/main.k"),
WithRunSourceUrl("local/usr/test2/base.k"),
WithRunSourceUrl("local/usr/test3/"),
)
```
For the source above, `kpmcli.Run()` will do :
1.find a package root path from the sources.
2.load the package from the package root path.
3.take all the sources as the compile entry to compile the package.
NOTE: `kpmcli.Run()` do not support compiling multiple packages at the same time. so, all the sources should belong to the same package root path.
`kpmcli.Run()` will iterate all the sources and find the source root path.
For source `local/usr/test1/main.k`, `kpmcli.Run()` will start from the path `local/usr/test1` and iterate all the parent directories.
If `kcl.mod` are found, the path of `kcl.mod` will be used as the source root path.
If `kcl.mod` are not found, the path of the source will be used as the source root path.
So if the kcl.mod is located in the path `local/usr/`, `kpmcli.Run()` will load package from `local/usr/` and load dependencies from `local/usr/kcl.mod`.
And take all the KCL program files in the sources as the compile entry to compile the package.
*/
package client
import (
"errors"
"fmt"
"os"
"path/filepath"
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/constants"
"kcl-lang.io/kpm/pkg/downloader"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/utils"
)
// RunOptions contains the options for running a kcl package.
type RunOptions struct {
settingYamlFiles []string
vendor bool
// Sources is the sources of the package.
// It can be a local *.k path, a local *.tar/*.tgz path, a local directory, a remote git/oci path,.
Sources []*downloader.Source
*kcl.Option
}
type RunOption func(*RunOptions) error
// WithWorkDir sets the work directory for running the kcl package.
func WithWorkDir(workDir string) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.WorkDir = workDir
return nil
}
}
// WithRunSources sets the sources for running the kcl package.
func WithRunSources(sources []*downloader.Source) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.Sources = sources
return nil
}
}
// WithRunSources sets the source for running the kcl package.
func WithRunSource(source *downloader.Source) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if ro.Sources == nil {
ro.Sources = make([]*downloader.Source, 0)
}
ro.Sources = append(ro.Sources, source)
return nil
}
}
// WithRunSourceUrls sets the source urls for running the kcl package.
func WithRunSourceUrls(sourceUrls []string) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
var sources []*downloader.Source
for _, sourceUrl := range sourceUrls {
source, err := downloader.NewSourceFromStr(sourceUrl)
if err != nil {
return err
}
sources = append(sources, source)
}
ro.Sources = sources
return nil
}
}
// WithRunSourceUrl sets the source url for running the kcl package.
func WithRunSourceUrl(sourceUrl string) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if ro.Sources == nil {
ro.Sources = make([]*downloader.Source, 0)
}
source, err := downloader.NewSourceFromStr(sourceUrl)
if err != nil {
return err
}
ro.Sources = append(ro.Sources, source)
return nil
}
}
// WithSettingFiles sets the setting files for running the kcl package.
func WithSettingFiles(settingFiles []string) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.settingYamlFiles = settingFiles
return nil
}
}
// WithArguments sets the arguments for running the kcl package.
func WithArguments(args []string) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.Merge(kcl.WithOptions(args...))
return nil
}
}
// WithOverrides sets the overrides for running the kcl package.
func WithOverrides(overrides []string, debug bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.Merge(kcl.WithOverrides(overrides...))
ro.PrintOverrideAst = debug
return nil
}
}
// WithPathSelectors sets the path selectors for running the kcl package.
func WithPathSelectors(pathSelectors []string) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.Merge(kcl.WithSelectors(pathSelectors...))
return nil
}
}
// WithDebug sets the debug mode for running the kcl package.
func WithDebug(debug bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if debug {
ro.Debug = 1
}
return nil
}
}
// WithDisableNone sets the disable none mode for running the kcl package.
func WithDisableNone(disableNone bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if disableNone {
ro.Merge(kcl.WithDisableNone(disableNone))
}
return nil
}
}
// WithExternalPkgs sets the external packages for running the kcl package.
func WithExternalPkgs(externalPkgs []string) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.Merge(kcl.WithExternalPkgs(externalPkgs...))
return nil
}
}
// WithSortKeys sets the sort keys for running the kcl package.
func WithSortKeys(sortKeys bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if sortKeys {
ro.Merge(kcl.WithSortKeys(sortKeys))
}
return nil
}
}
// WithShowHidden sets the show hidden mode for running the kcl package.
func WithShowHidden(showHidden bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if showHidden {
ro.Merge(kcl.WithShowHidden(showHidden))
}
return nil
}
}
// WithStrictRange sets the strict range mode for running the kcl package.
func WithStrictRange(strictRange bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if strictRange {
ro.StrictRangeCheck = strictRange
}
return nil
}
}
// WithCompileOnly sets the compile only mode for running the kcl package.
func WithCompileOnly(compileOnly bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if compileOnly {
ro.CompileOnly = compileOnly
}
return nil
}
}
// WithVendor sets the vendor mode for running the kcl package.
func WithVendor(vendor bool) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
if vendor {
ro.vendor = vendor
}
return nil
}
}
// applyCompileOptionsFromYaml applies the compile options from the kcl.yaml file.
func (o *RunOptions) applyCompileOptionsFromYaml(workdir string) bool {
succeed := false
// load the kcl.yaml from cli
if len(o.settingYamlFiles) != 0 {
for _, settingYamlFile := range o.settingYamlFiles {
o.Merge(kcl.WithSettings(settingYamlFile))
succeed = true
}
} else {
// load the kcl.yaml from the workdir
// If the workdir is not empty, try to find the settings.yaml file in the workdir.
settingsYamlPath := filepath.Join(workdir, constants.KCL_YAML)
if utils.DirExists(settingsYamlPath) {
o.Merge(kcl.WithSettings(settingsYamlPath))
succeed = true
}
}
// transform the relative path to the absolute path in kcl.yaml by workdir
var updatedKFilenameList []string
for _, kfile := range o.KFilenameList {
if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) {
kfile = filepath.Join(workdir, kfile)
}
updatedKFilenameList = append(updatedKFilenameList, kfile)
}
o.KFilenameList = updatedKFilenameList
return succeed
}
// applyCompileOptionsFromKclMod applies the compile options from the kcl.mod file.
func (o *RunOptions) applyCompileOptionsFromKclMod(kclPkg *pkg.KclPkg) bool {
o.Merge(*kclPkg.GetKclOpts())
var updatedKFilenameList []string
// transform the relative path to the absolute path in kcl.yaml by kcl.mod path
for _, kfile := range o.KFilenameList {
if !filepath.IsAbs(kfile) && !utils.IsModRelativePath(kfile) {
kfile = filepath.Join(kclPkg.HomePath, kfile)
}
updatedKFilenameList = append(updatedKFilenameList, kfile)
}
o.KFilenameList = updatedKFilenameList
return len(o.KFilenameList) != 0
}
// applyCompileOptions applies the compile options from cli, kcl.yaml and kcl.mod.
func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.KclPkg, workDir string) error {
o.Merge(kcl.WithWorkDir(workDir))
// If the sources from cli is not empty, use the sources from cli.
if len(o.Sources) != 0 {
var compiledFiles []string
// All the cli relative path should be transformed to the absolute path by workdir
for _, source := range o.Sources {
if source.IsLocalPath() {
sPath := source.Path
if !filepath.IsAbs(sPath) && !utils.IsModRelativePath(sPath) {
sPath = filepath.Join(workDir, sPath)
}
compiledFiles = append(compiledFiles, sPath)
}
}
o.KFilenameList = compiledFiles
if len(o.KFilenameList) == 0 {
if !o.applyCompileOptionsFromKclMod(kclPkg) {
o.KFilenameList = []string{kclPkg.HomePath}
}
}
} else {
// If the sources from cli is empty, try to apply the compile options from kcl.yaml
if !o.applyCompileOptionsFromYaml(workDir) {
// If the sources from kcl.yaml is empty, try to apply the compile options from kcl.mod
if !o.applyCompileOptionsFromKclMod(kclPkg) {
// If the sources from kcl.mod is empty, compile the current sources.
if source.IsLocalPath() {
o.KFilenameList = []string{workDir}
} else {
o.KFilenameList = []string{kclPkg.HomePath}
}
}
}
}
return nil
}
// getPkgSource returns the package source.
// Compiling multiple packages at the same time will cause an error.
func (o *RunOptions) getPkgSource() (*downloader.Source, error) {
workDir := o.WorkDir
var pkgSource *downloader.Source
if len(o.Sources) == 0 {
workDir, err := filepath.Abs(workDir)
if err != nil {
return nil, err
}
// If no sources set by options, return a localSource to facilitate searching for
// compilation entries from configurations such as kcl.yaml and kcl.mod files.
return &downloader.Source{
Local: &downloader.Local{
Path: workDir,
},
}, nil
} else {
var rootPath string
var err error
for _, source := range o.Sources {
if pkgSource == nil {
pkgSource = source
rootPath, err = source.FindRootPath()
if err != nil {
return nil, err
}
} else {
rootSourceStr, err := pkgSource.ToString()
if err != nil {
return nil, err
}
sourceStr, err := source.ToString()
if err != nil {
return nil, err
}
if pkgSource.IsPackaged() || source.IsPackaged() {
return nil, reporter.NewErrorEvent(
reporter.CompileFailed,
fmt.Errorf("cannot compile multiple packages %s at the same time", []string{rootSourceStr, sourceStr}),
"only allows one package to be compiled at a time",
)
}
if !pkgSource.IsPackaged() && !source.IsPackaged() {
tmpRootPath, err := source.FindRootPath()
if err != nil {
return nil, err
}
if tmpRootPath != rootPath {
return nil, reporter.NewErrorEvent(
reporter.CompileFailed,
fmt.Errorf("cannot compile multiple packages %s at the same time", []string{tmpRootPath, rootPath}),
"only allows one package to be compiled at a time",
)
}
}
}
}
// A local k file or folder
if pkgSource.IsLocalKPath() || pkgSource.IsDir() {
rootPath, err = pkgSource.FindRootPath()
if err != nil {
return nil, err
}
pkgSource, err = downloader.NewSourceFromStr(rootPath)
if err != nil {
return nil, err
}
}
}
if pkgSource == nil {
return nil, errors.New("no source provided")
}
return pkgSource, nil
}
// Run runs the kcl package.
func (c *KpmClient) Run(options ...RunOption) (*kcl.KCLResultList, error) {
opts := &RunOptions{}
for _, option := range options {
if err := option(opts); err != nil {
return nil, err
}
}
// Set the work directory to pwd if not set the workdir
var err error
if opts.WorkDir == "" {
opts.WorkDir, err = os.Getwd()
if err != nil {
return nil, err
}
}
// Find the package source, note is maybe local compile entries or paths that contains `kcl.mod`
pkgSource, err := opts.getPkgSource()
if err != nil {
return nil, err
}
// Visit the root package source.
var res *kcl.KCLResultList
err = NewVisitor(*pkgSource, c).Visit(pkgSource, func(kclPkg *pkg.KclPkg) error {
// Apply the compile options from cli, kcl.yaml or kcl.mod
err = opts.applyCompileOptions(*pkgSource, kclPkg, opts.WorkDir)
if err != nil {
return err
}
kclPkg.SetVendorMode(opts.vendor)
// Resolve and update the dependencies into a map.
pkgMap, err := c.ResolveDepsIntoMap(kclPkg)
if err != nil {
return err
}
// Fill the dependency path.
for dName, dPath := range pkgMap {
if !filepath.IsAbs(dPath) {
dPath = filepath.Join(c.homePath, dPath)
}
opts.Merge(kcl.WithExternalPkgs(fmt.Sprintf(constants.EXTERNAL_PKGS_ARG_PATTERN, dName, dPath)))
}
// Compile the kcl package.
res, err = kcl.RunWithOpts(*opts.Option)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
return res, nil
}