github.com/KusionStack/kpm@v0.8.4-0.20240326033734-dc72298a30e5/pkg/runner/runner.go (about) 1 package runner 2 3 import ( 4 "fmt" 5 6 "kcl-lang.io/kcl-go/pkg/kcl" 7 "kcl-lang.io/kcl-go/scripts" 8 "kcl-lang.io/kpm/pkg/opt" 9 ) 10 11 // The pattern of the external package argument. 12 const EXTERNAL_PKGS_ARG_PATTERN = "%s=%s" 13 14 // Compiler is a wrapper of kcl compiler. 15 type Compiler struct { 16 opts *opt.CompileOptions 17 } 18 19 // DefaultCompiler will create a default compiler. 20 func DefaultCompiler() *Compiler { 21 return &Compiler{ 22 opts: opt.DefaultCompileOptions(), 23 } 24 } 25 26 func NewCompilerWithOpts(opts *opt.CompileOptions) *Compiler { 27 return &Compiler{ 28 opts, 29 } 30 } 31 32 // AddKFile will add a k file to the compiler. 33 func (compiler *Compiler) AddKFile(kFilePath string) *Compiler { 34 compiler.opts.Merge(kcl.WithKFilenames(kFilePath)) 35 return compiler 36 } 37 38 // AddKclOption will add a kcl option to the compiler. 39 func (compiler *Compiler) AddKclOption(opt kcl.Option) *Compiler { 40 compiler.opts.Merge(opt) 41 return compiler 42 } 43 44 // AddDep will add a file path to the dependency list. 45 func (compiler *Compiler) AddDepPath(depName string, depPath string) *Compiler { 46 compiler.opts.Merge(kcl.WithExternalPkgs(fmt.Sprintf(EXTERNAL_PKGS_ARG_PATTERN, depName, depPath))) 47 return compiler 48 } 49 50 // Call KCL Compiler and return the result. 51 func (compiler *Compiler) Run() (*kcl.KCLResultList, error) { 52 return kcl.RunWithOpts(*compiler.opts.Option) 53 } 54 55 // GetKclVersion fetches the kcl version 56 func GetKclVersion() string { 57 return string(scripts.KclvmVersionType_latest) 58 }