From cae6696c3e8a8ef76ff76ad71bf4ed43cae8a09a Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 17 Oct 2023 15:10:35 +0200 Subject: [PATCH] Added build command --- README.md | 14 ++++++++---- build/Build.go | 34 ++++++++++++++++++++++++++++ cli/Build.go | 50 ++++++++++++++++++++++++++++++++++++++++++ cli/Help.go | 2 +- cli/System.go | 5 ----- cli_test.go | 7 ++++-- examples/hello/hello.q | 3 +++ 7 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 build/Build.go create mode 100644 examples/hello/hello.q diff --git a/README.md b/README.md index 860cdd4..99d512d 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,12 @@ A simple programming language. +## Features + +* Fast compilation +* High performance +* Small binaries + ## Installation ```shell @@ -10,11 +16,11 @@ cd q go build ``` -## Features +## Usage -* Fast compilation -* High performance -* Small binaries +```shell +q build +``` ## License diff --git a/build/Build.go b/build/Build.go new file mode 100644 index 0000000..9a838b9 --- /dev/null +++ b/build/Build.go @@ -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 +} diff --git a/cli/Build.go b/cli/Build.go index 5c7ea6e..19f7544 100644 --- a/cli/Build.go +++ b/cli/Build.go @@ -1,5 +1,55 @@ package cli +import ( + "os" + + "git.akyoto.dev/cli/q/build" + "git.akyoto.dev/cli/q/log" +) + 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 } diff --git a/cli/Help.go b/cli/Help.go index 464cb35..87da5c3 100644 --- a/cli/Help.go +++ b/cli/Help.go @@ -9,5 +9,5 @@ func Help(args []string) int { log.Error.Println("") log.Error.Println(" build [directory]") log.Error.Println(" system") - return 1 + return 2 } diff --git a/cli/System.go b/cli/System.go index 524e0fb..4bde621 100644 --- a/cli/System.go +++ b/cli/System.go @@ -10,13 +10,8 @@ import ( func System(args []string) int { line := "%-19s%s\n" - // Platform log.Info.Printf(line, "Platform:", runtime.GOOS) - - // Architecture log.Info.Printf(line, "Architecture:", runtime.GOARCH) - - // Go log.Info.Printf(line, "Go:", runtime.Version()) // Directory diff --git a/cli_test.go b/cli_test.go index b99942f..c1aef37 100644 --- a/cli_test.go +++ b/cli_test.go @@ -14,9 +14,12 @@ func TestCLI(t *testing.T) { } tests := []cliTest{ - {[]string{}, 1}, - {[]string{"invalid"}, 1}, + {[]string{}, 2}, + {[]string{"invalid"}, 2}, {[]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 { diff --git a/examples/hello/hello.q b/examples/hello/hello.q new file mode 100644 index 0000000..d974d48 --- /dev/null +++ b/examples/hello/hello.q @@ -0,0 +1,3 @@ +main() { + +}