From 2d990b0bee998597b154e59f3e125fd183c7b0e7 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 12 Jun 2024 12:10:29 +0200 Subject: [PATCH] Added support for single file builds --- src/build/Build.go | 20 ++++++++++++------ src/build/Compile.go | 3 +-- src/build/Scan.go | 50 +++++++++++++++++++++++++++++++------------- src/cli/Build.go | 8 +++++-- 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/src/build/Build.go b/src/build/Build.go index c7029cb..0132eb4 100644 --- a/src/build/Build.go +++ b/src/build/Build.go @@ -2,25 +2,27 @@ package build import ( "path/filepath" + "strings" ) // Build describes a compiler build. type Build struct { - Directory string + Files []string WriteExecutable bool } // New creates a new build. -func New(directory string) *Build { +func New(files ...string) *Build { return &Build{ - Directory: directory, + Files: files, WriteExecutable: true, } } // Run parses the input files and generates an executable file. func (build *Build) Run() error { - functions, err := Compile(build.Directory) + functions, errors := Scan(build.Files) + allFunctions, err := Compile(functions, errors) if err != nil { return err @@ -31,11 +33,17 @@ func (build *Build) Run() error { } path := build.Executable() - code, data := Finalize(functions) + code, data := Finalize(allFunctions) return Write(path, code, data) } // Executable returns the path to the executable. func (build *Build) Executable() string { - return filepath.Join(build.Directory, filepath.Base(build.Directory)) + directory, _ := filepath.Abs(build.Files[0]) + + if strings.HasSuffix(directory, ".q") { + directory = filepath.Dir(directory) + } + + return filepath.Join(directory, filepath.Base(directory)) } diff --git a/src/build/Compile.go b/src/build/Compile.go index 1d1ffc1..a40ab2c 100644 --- a/src/build/Compile.go +++ b/src/build/Compile.go @@ -5,8 +5,7 @@ import ( ) // Compile compiles all the functions. -func Compile(directory string) (map[string]*Function, error) { - functions, errors := Scan(directory) +func Compile(functions <-chan *Function, errors <-chan error) (map[string]*Function, error) { wg := sync.WaitGroup{} allFunctions := map[string]*Function{} diff --git a/src/build/Scan.go b/src/build/Scan.go index ae24220..86f161e 100644 --- a/src/build/Scan.go +++ b/src/build/Scan.go @@ -11,12 +11,12 @@ import ( ) // Scan scans the directory. -func Scan(path string) (<-chan *Function, <-chan error) { +func Scan(files []string) (<-chan *Function, <-chan error) { functions := make(chan *Function) errors := make(chan error) go func() { - scan(path, functions, errors) + scan(files, functions, errors) close(functions) close(errors) }() @@ -25,29 +25,51 @@ func Scan(path string) (<-chan *Function, <-chan error) { } // scan scans the directory without channel allocations. -func scan(path string, functions chan<- *Function, errors chan<- error) { +func scan(files []string, functions chan<- *Function, errors chan<- error) { wg := sync.WaitGroup{} - err := directory.Walk(path, func(name string) { - if !strings.HasSuffix(name, ".q") { + for _, file := range files { + stat, err := os.Stat(file) + + if err != nil { + errors <- err return } - fullPath := filepath.Join(path, name) - wg.Add(1) + if stat.IsDir() { + err = directory.Walk(file, func(name string) { + if !strings.HasSuffix(name, ".q") { + return + } - go func() { - defer wg.Done() - err := scanFile(fullPath, functions) + fullPath := filepath.Join(file, name) + wg.Add(1) + + go func() { + defer wg.Done() + err := scanFile(fullPath, functions) + + if err != nil { + errors <- err + } + }() + }) if err != nil { errors <- err } - }() - }) + } else { + wg.Add(1) - if err != nil { - errors <- err + go func() { + defer wg.Done() + err := scanFile(file, functions) + + if err != nil { + errors <- err + } + }() + } } wg.Wait() diff --git a/src/cli/Build.go b/src/cli/Build.go index 2eb6cf2..8f53e6a 100644 --- a/src/cli/Build.go +++ b/src/cli/Build.go @@ -10,7 +10,7 @@ import ( // Build builds an executable. func Build(args []string) int { - b := build.New(".") + b := build.New() for i := 0; i < len(args); i++ { switch args[i] { @@ -26,10 +26,14 @@ func Build(args []string) int { return 2 } - b.Directory = args[i] + b.Files = append(b.Files, args[i]) } } + if len(b.Files) == 0 { + b.Files = append(b.Files, ".") + } + err := b.Run() if err != nil {