diff --git a/examples/sample_two.go b/examples/sample_two.go index bcebdef..a3cf3ea 100644 --- a/examples/sample_two.go +++ b/examples/sample_two.go @@ -3,15 +3,13 @@ package main import ( "encoding/json" "fmt" + "github.com/ShindouMihou/korin/examples/test" ) func main() { - type Statistics struct { - RunningMemory int // +k:named(json) - } - statistics := Statistics{RunningMemory: 24_000} // +k:println - stats, _ := json.Marshal(statistics) // +k:float - stats = stats // +k:println + statistics := test.Statistics{RunningMemory: 24_000} // +k:println + stats, _ := json.Marshal(statistics) // +k:float + stats = stats // +k:println var name string fmt.Scanln(&name) diff --git a/examples/test/hello.go b/examples/test/hello.go new file mode 100644 index 0000000..08684e3 --- /dev/null +++ b/examples/test/hello.go @@ -0,0 +1,5 @@ +package test + +type Statistics struct { + RunningMemory int // +k:named(json) +} diff --git a/internal/kcomp/compilation.go b/internal/kcomp/compilation.go index 7295cdf..622415d 100644 --- a/internal/kcomp/compilation.go +++ b/internal/kcomp/compilation.go @@ -25,6 +25,7 @@ type Configuration struct { BuildDirectory string Plugins []kplugins.Plugin BuildCommand string + ModuleName string Logger func(args ...any) } @@ -357,5 +358,5 @@ func process(config *Configuration, file *siopao.File) (string, error) { for _, plugin := range config.Plugins { plugin.FreeContext(file.Path()) } - return headers.Format() + contents.Contents(), nil + return headers.Format(config.ModuleName) + contents.Contents(), nil } diff --git a/internal/kmod/module_reader.go b/internal/kmod/module_reader.go new file mode 100644 index 0000000..5413ac3 --- /dev/null +++ b/internal/kmod/module_reader.go @@ -0,0 +1,34 @@ +package kmod + +import ( + "errors" + "github.com/ShindouMihou/korin/internal/kstrings" + "github.com/ShindouMihou/siopao/siopao" + "strings" +) + +type Module struct { + Name string +} + +func ReadModule(path string) (*Module, error) { + if !kstrings.HasSuffix(path, "go.mod") { + return nil, errors.New("path is not a go.mod file") + } + file := siopao.Open(path) + + reader, err := file.TextReader() + if err != nil { + return nil, err + } + + module := Module{} + if err := reader.EachLine(func(line string) { + if kstrings.HasPrefix(line, "module ") { + module.Name = strings.TrimPrefix(line, "module ") + } + }); err != nil { + return nil, err + } + return &module, nil +} diff --git a/pkg/korin/external_builder.go b/pkg/korin/external_builder.go index d32fc1c..1f78cc5 100644 --- a/pkg/korin/external_builder.go +++ b/pkg/korin/external_builder.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/ShindouMihou/go-little-utils/slices" "github.com/ShindouMihou/korin/internal/kcomp" + "github.com/ShindouMihou/korin/internal/kmod" "github.com/ShindouMihou/korin/pkg/kplugins" "github.com/ttacon/chalk" "io" @@ -22,6 +23,8 @@ type Korin struct { BuildDirectory string Plugins []kplugins.Plugin BuildCommand string + ModuleName string + ModulePath string Logger Logger } @@ -29,10 +32,26 @@ type Korin struct { // Unlike `Build`, this function returns the `errors` that occurred during the processing, and won't // print them out. func (ko Korin) Process(dir string) []error { + moduleName := ko.ModuleName + if moduleName == "" { + modulePath := ko.ModulePath + if modulePath == "" { + modulePath = filepath.Join(dir, "go.mod") + } + module, err := kmod.ReadModule(modulePath) + if err != nil { + return []error{err} + } + moduleName = module.Name + if moduleName == "" { + return []error{fmt.Errorf("module name is empty, please either provide the correct path or module name in the Korin struct")} + } + } return kcomp.Process(&kcomp.Configuration{ BuildDirectory: ko.BuildDirectory, Plugins: ko.Plugins, BuildCommand: ko.BuildCommand, + ModuleName: moduleName, Logger: ko.Logger, }, dir) } @@ -159,6 +178,8 @@ func New() *Korin { kplugins.NewPluginSerializerAnnotations(), kplugins.NewPluginEnvironmentKey(), }, + ModuleName: "", + ModulePath: "", BuildCommand: "go build {$BUILD_FOLDER}/{$FILE_NAME}.go", Logger: func(args ...any) { fmt.Println(args...) diff --git a/pkg/kplugins/external_headers.go b/pkg/kplugins/external_headers.go index 5f91282..b3f8063 100644 --- a/pkg/kplugins/external_headers.go +++ b/pkg/kplugins/external_headers.go @@ -3,6 +3,7 @@ package kplugins import ( "github.com/ShindouMihou/korin/internal/kslices" "slices" + "strings" ) type Headers struct { @@ -30,11 +31,23 @@ func (headers *Headers) Package(name string) { // Format returns the formatted headers, this properly formats them, in which case, multiple imports will be // multi-lined and the package will be at the top. -func (headers *Headers) Format() string { +func (headers *Headers) Format(moduleName string, buildDir string) string { text := "package " + headers.pkg text += "\n" if len(headers.imports) > 0 { text += "\n" + + // Important part of headers modification since if we don't do this, then + // all the source files will be imported from the original module path, which makes this preprocessing + // entirely useless. + moduleName = strings.TrimSuffix(moduleName, "/") + buildDir = strings.TrimSuffix(buildDir, "/") + for ind, imp := range headers.imports { + if strings.HasPrefix(imp, moduleName+"/") { + headers.imports[ind] = strings.ReplaceAll(imp, moduleName+"/", moduleName+"/"+buildDir+"/") + } + } + text += SyntaxHelper.Import(headers.imports) } return text