From d37bbd2d4cbbfc1ee0cfe67ad90b93057ea3d265 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 18 Jun 2025 23:05:50 +0200 Subject: [PATCH] Added build package --- src/build/Arch.go | 10 ++++++++++ src/build/Build.go | 44 +++++++++++++++++++++++++++++++++++++++++ src/build/Build_test.go | 31 +++++++++++++++++++++++++++++ src/build/New.go | 28 ++++++++++++++++++++++++++ src/build/OS.go | 11 +++++++++++ 5 files changed, 124 insertions(+) create mode 100644 src/build/Arch.go create mode 100644 src/build/Build.go create mode 100644 src/build/Build_test.go create mode 100644 src/build/New.go create mode 100644 src/build/OS.go diff --git a/src/build/Arch.go b/src/build/Arch.go new file mode 100644 index 0000000..b9e3b84 --- /dev/null +++ b/src/build/Arch.go @@ -0,0 +1,10 @@ +package build + +// Arch is a CPU architecture. +type Arch uint8 + +const ( + UnknownArch Arch = iota + ARM + X86 +) \ No newline at end of file diff --git a/src/build/Build.go b/src/build/Build.go new file mode 100644 index 0000000..c9aba8f --- /dev/null +++ b/src/build/Build.go @@ -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")) +} \ No newline at end of file diff --git a/src/build/Build_test.go b/src/build/Build_test.go new file mode 100644 index 0000000..1418d51 --- /dev/null +++ b/src/build/Build_test.go @@ -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") + } +} \ No newline at end of file diff --git a/src/build/New.go b/src/build/New.go new file mode 100644 index 0000000..6013623 --- /dev/null +++ b/src/build/New.go @@ -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 +} \ No newline at end of file diff --git a/src/build/OS.go b/src/build/OS.go new file mode 100644 index 0000000..0451648 --- /dev/null +++ b/src/build/OS.go @@ -0,0 +1,11 @@ +package build + +// OS is an operating system. +type OS uint8 + +const ( + UnknownOS OS = iota + Linux + Mac + Windows +) \ No newline at end of file