Added build command

This commit is contained in:
Eduard Urbach 2023-10-17 15:10:35 +02:00
parent c3925e86b3
commit cae6696c3e
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
7 changed files with 103 additions and 12 deletions

View File

@ -2,6 +2,12 @@
A simple programming language. A simple programming language.
## Features
* Fast compilation
* High performance
* Small binaries
## Installation ## Installation
```shell ```shell
@ -10,11 +16,11 @@ cd q
go build go build
``` ```
## Features ## Usage
* Fast compilation ```shell
* High performance q build
* Small binaries ```
## License ## License

34
build/Build.go Normal file
View File

@ -0,0 +1,34 @@
package build
import "path/filepath"
// Build describes a compiler build.
type Build struct {
ExecutableName string
ExecutablePath string
WriteExecutable bool
}
// New creates a new build.
func New(directory string) (*Build, error) {
directory, err := filepath.Abs(directory)
if err != nil {
return nil, err
}
executableName := filepath.Base(directory)
build := &Build{
ExecutableName: executableName,
ExecutablePath: filepath.Join(directory, executableName),
WriteExecutable: true,
}
return build, nil
}
// Run parses the input files and generates an executable file.
func (build *Build) Run() error {
return nil
}

View File

@ -1,5 +1,55 @@
package cli package cli
import (
"os"
"git.akyoto.dev/cli/q/build"
"git.akyoto.dev/cli/q/log"
)
func Build(args []string) int { func Build(args []string) int {
directory := "."
if len(args) > 0 {
directory = args[0]
}
stat, err := os.Stat(directory)
if err != nil {
log.Error.Println(err)
return 1
}
if !stat.IsDir() {
log.Error.Println("Build path must be a directory")
return 2
}
b, err := build.New(directory)
if err != nil {
log.Error.Println(err)
return 1
}
for i := 1; i < len(args); i++ {
switch args[i] {
case "--dry":
b.WriteExecutable = false
default:
log.Error.Printf("Unknown parameter: %s\n", args[i])
return 2
}
}
err = b.Run()
if err != nil {
log.Error.Println(err)
return 1
}
return 0 return 0
} }

View File

@ -9,5 +9,5 @@ func Help(args []string) int {
log.Error.Println("") log.Error.Println("")
log.Error.Println(" build [directory]") log.Error.Println(" build [directory]")
log.Error.Println(" system") log.Error.Println(" system")
return 1 return 2
} }

View File

@ -10,13 +10,8 @@ import (
func System(args []string) int { func System(args []string) int {
line := "%-19s%s\n" line := "%-19s%s\n"
// Platform
log.Info.Printf(line, "Platform:", runtime.GOOS) log.Info.Printf(line, "Platform:", runtime.GOOS)
// Architecture
log.Info.Printf(line, "Architecture:", runtime.GOARCH) log.Info.Printf(line, "Architecture:", runtime.GOARCH)
// Go
log.Info.Printf(line, "Go:", runtime.Version()) log.Info.Printf(line, "Go:", runtime.Version())
// Directory // Directory

View File

@ -14,9 +14,12 @@ func TestCLI(t *testing.T) {
} }
tests := []cliTest{ tests := []cliTest{
{[]string{}, 1}, {[]string{}, 2},
{[]string{"invalid"}, 1}, {[]string{"invalid"}, 2},
{[]string{"system"}, 0}, {[]string{"system"}, 0},
{[]string{"build", "non-existing-directory"}, 1},
{[]string{"build", "examples/hello/hello.q"}, 2},
{[]string{"build", "examples/hello", "--invalid"}, 2},
} }
for _, test := range tests { for _, test := range tests {

3
examples/hello/hello.q Normal file
View File

@ -0,0 +1,3 @@
main() {
}