Skip to content

Commit

Permalink
Add module name handling and improve header formatting
Browse files Browse the repository at this point in the history
Introduce module name processing to prevent improper imports during preprocessing and improve header formatting logic using module paths. Add a module reader utility and update Korin to derive or override module names as needed. Enhance examples to utilize the new struct import approach.
  • Loading branch information
ShindouMihou committed Jan 10, 2025
1 parent c344b66 commit 9df37f0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 8 deletions.
10 changes: 4 additions & 6 deletions examples/sample_two.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions examples/test/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package test

type Statistics struct {
RunningMemory int // +k:named(json)
}
3 changes: 2 additions & 1 deletion internal/kcomp/compilation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Configuration struct {
BuildDirectory string
Plugins []kplugins.Plugin
BuildCommand string
ModuleName string
Logger func(args ...any)
}

Expand Down Expand Up @@ -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
}
34 changes: 34 additions & 0 deletions internal/kmod/module_reader.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 21 additions & 0 deletions pkg/korin/external_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -22,17 +23,35 @@ type Korin struct {
BuildDirectory string
Plugins []kplugins.Plugin
BuildCommand string
ModuleName string
ModulePath string
Logger Logger
}

// Process processes the directory specified in the `dir` parameter.
// 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)
}
Expand Down Expand Up @@ -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...)
Expand Down
15 changes: 14 additions & 1 deletion pkg/kplugins/external_headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kplugins
import (
"github.com/ShindouMihou/korin/internal/kslices"
"slices"
"strings"
)

type Headers struct {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9df37f0

Please sign in to comment.