This commit is contained in:
parent
c35f8ff05c
commit
aae75197e9
6 changed files with 87 additions and 90 deletions
|
@ -1,45 +1,9 @@
|
|||
package build
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Build describes the parameters for the "build" command.
|
||||
type Build struct {
|
||||
Files []string
|
||||
Arch Arch
|
||||
OS OS
|
||||
Dry bool
|
||||
}
|
||||
|
||||
// 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"))
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package build_test
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.urbach.dev/cli/q/src/build"
|
||||
"git.urbach.dev/go/assert"
|
||||
)
|
||||
|
||||
func TestExecutableFromDirectoryLinux(t *testing.T) {
|
||||
b := build.New("../../examples/hello")
|
||||
b.OS = build.Linux
|
||||
exe := filepath.Base(b.Executable())
|
||||
assert.Equal(t, exe, "hello")
|
||||
}
|
||||
|
||||
func TestExecutableFromFileLinux(t *testing.T) {
|
||||
b := build.New("../../examples/hello/hello.q")
|
||||
b.OS = build.Linux
|
||||
exe := filepath.Base(b.Executable())
|
||||
assert.Equal(t, exe, "hello")
|
||||
}
|
||||
|
||||
func TestExecutableFromDirectoryMac(t *testing.T) {
|
||||
b := build.New("../../examples/hello")
|
||||
b.OS = build.Mac
|
||||
exe := filepath.Base(b.Executable())
|
||||
assert.Equal(t, exe, "hello")
|
||||
}
|
||||
|
||||
func TestExecutableFromFileMac(t *testing.T) {
|
||||
b := build.New("../../examples/hello/hello.q")
|
||||
b.OS = build.Mac
|
||||
exe := filepath.Base(b.Executable())
|
||||
assert.Equal(t, exe, "hello")
|
||||
}
|
||||
|
||||
func TestExecutableFromDirectoryWindows(t *testing.T) {
|
||||
b := build.New("../../examples/hello")
|
||||
b.OS = build.Windows
|
||||
exe := filepath.Base(b.Executable())
|
||||
assert.Equal(t, exe, "hello.exe")
|
||||
}
|
||||
|
||||
func TestExecutableFromFileWindows(t *testing.T) {
|
||||
b := build.New("../../examples/hello/hello.q")
|
||||
b.OS = build.Windows
|
||||
exe := filepath.Base(b.Executable())
|
||||
assert.Equal(t, exe, "hello.exe")
|
||||
}
|
37
src/build/Executable.go
Normal file
37
src/build/Executable.go
Normal file
|
@ -0,0 +1,37 @@
|
|||
package build
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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"))
|
||||
}
|
||||
|
||||
// fromDirectoryName returns the executable path based on the directory name.
|
||||
func fromDirectoryName(path string) string {
|
||||
return filepath.Join(path, filepath.Base(path))
|
||||
}
|
40
src/build/Executable_test.go
Normal file
40
src/build/Executable_test.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package build_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"testing"
|
||||
|
||||
"git.urbach.dev/cli/q/src/build"
|
||||
"git.urbach.dev/cli/q/src/global"
|
||||
"git.urbach.dev/go/assert"
|
||||
)
|
||||
|
||||
func TestExecutable(t *testing.T) {
|
||||
osList := []string{"linux", "darwin", "windows"}
|
||||
archList := []string{"amd64", "arm64"}
|
||||
fileList := []string{"../../examples/hello", "../../examples/hello/hello.q"}
|
||||
|
||||
for _, os := range osList {
|
||||
for _, arch := range archList {
|
||||
t.Run(fmt.Sprintf("%s-%s", os, arch), func(t *testing.T) {
|
||||
for _, file := range fileList {
|
||||
global.HostOS = os
|
||||
global.HostArch = arch
|
||||
b := build.New(file)
|
||||
exe := filepath.Base(b.Executable())
|
||||
|
||||
if os == "windows" {
|
||||
assert.Equal(t, exe, "hello.exe")
|
||||
} else {
|
||||
assert.Equal(t, exe, "hello")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
global.HostOS = runtime.GOOS
|
||||
global.HostArch = runtime.GOARCH
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
package build
|
||||
|
||||
import "runtime"
|
||||
import (
|
||||
"git.urbach.dev/cli/q/src/global"
|
||||
)
|
||||
|
||||
// New creates a new build.
|
||||
func New(files ...string) *Build {
|
||||
|
@ -8,14 +10,14 @@ func New(files ...string) *Build {
|
|||
Files: files,
|
||||
}
|
||||
|
||||
switch runtime.GOARCH {
|
||||
switch global.HostArch {
|
||||
case "amd64":
|
||||
b.Arch = X86
|
||||
case "arm64":
|
||||
b.Arch = ARM
|
||||
}
|
||||
|
||||
switch runtime.GOOS {
|
||||
switch global.HostOS {
|
||||
case "linux":
|
||||
b.OS = Linux
|
||||
case "darwin":
|
||||
|
|
|
@ -3,6 +3,7 @@ package global
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
)
|
||||
|
||||
|
@ -12,12 +13,16 @@ var (
|
|||
Library string
|
||||
Root string
|
||||
WorkingDirectory string
|
||||
HostOS string
|
||||
HostArch string
|
||||
)
|
||||
|
||||
// init is the very first thing that's executed.
|
||||
// It disables the GC and initializes global variables.
|
||||
func init() {
|
||||
debug.SetGCPercent(-1)
|
||||
HostOS = runtime.GOOS
|
||||
HostArch = runtime.GOARCH
|
||||
|
||||
var err error
|
||||
Executable, err = os.Executable()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue