Added build package
All checks were successful
/ test (push) Successful in 13s

This commit is contained in:
Eduard Urbach 2025-06-18 23:05:50 +02:00
parent bd74b95155
commit d37bbd2d4c
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
5 changed files with 124 additions and 0 deletions

10
src/build/Arch.go Normal file
View file

@ -0,0 +1,10 @@
package build
// Arch is a CPU architecture.
type Arch uint8
const (
UnknownArch Arch = iota
ARM
X86
)

44
src/build/Build.go Normal file
View file

@ -0,0 +1,44 @@
package build
import (
"path/filepath"
"strings"
)
// Build describes the parameters for the "build" command.
type Build struct {
Files []string
Arch Arch
OS OS
}
// Executable returns the path to the executable.
func (build *Build) Executable() string {
path, err := filepath.Abs(build.Files[0])
if err != nil {
panic(err)
}
if strings.HasSuffix(path, ".q") {
path = fromFileName(path)
} else {
path = fromDirectoryName(path)
}
if build.OS == Windows {
path += ".exe"
}
return path
}
// fromDirectoryName returns the executable path based on the directory name.
func fromDirectoryName(path string) string {
return filepath.Join(path, filepath.Base(path))
}
// fromFileName returns the executable path based on the file name.
func fromFileName(path string) string {
return filepath.Join(filepath.Dir(path), strings.TrimSuffix(filepath.Base(path), ".q"))
}

31
src/build/Build_test.go Normal file
View file

@ -0,0 +1,31 @@
package build_test
import (
"path/filepath"
"testing"
"git.urbach.dev/cli/q/src/build"
"git.urbach.dev/go/assert"
)
func TestExecutableNameFromDirectory(t *testing.T) {
b := build.New("../../examples/hello")
exe := filepath.Base(b.Executable())
if b.OS == build.Windows {
assert.Equal(t, exe, "hello.exe")
} else {
assert.Equal(t, exe, "hello")
}
}
func TestExecutableNameFromFile(t *testing.T) {
b := build.New("../../examples/hello/hello.q")
exe := filepath.Base(b.Executable())
if b.OS == build.Windows {
assert.Equal(t, exe, "hello.exe")
} else {
assert.Equal(t, exe, "hello")
}
}

28
src/build/New.go Normal file
View file

@ -0,0 +1,28 @@
package build
import "runtime"
// New creates a new build.
func New(files ...string) *Build {
b := &Build{
Files: files,
}
switch runtime.GOARCH {
case "amd64":
b.Arch = X86
case "arm64":
b.Arch = ARM
}
switch runtime.GOOS {
case "linux":
b.OS = Linux
case "darwin":
b.OS = Mac
case "windows":
b.OS = Windows
}
return b
}

11
src/build/OS.go Normal file
View file

@ -0,0 +1,11 @@
package build
// OS is an operating system.
type OS uint8
const (
UnknownOS OS = iota
Linux
Mac
Windows
)