Skip to content

Commit

Permalink
add more functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-the-programmer committed Jul 28, 2022
1 parent b0838c0 commit f51b736
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 26 deletions.
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,44 @@ Download the latest release from our [GitHub Release](https://github.com/neuron-

To build and run, simply use...

```bash
```powershell
castle
```

Where castle.yaml is similar to...

```yaml
build:
- build_script arg1 arg2
- second_build_script arg1
- build-script arg1 arg2
- second-build-script arg1

run:
- run_section arg1
- run_more_sections arg1
- run-section arg1
- run-more-sections arg1

test:
- test-script
- test-script-2
- test-script-3

tasks:
format:
- format-script
admin-stuff:
- admin-script
- tidy-workspace
```
## CLI Arguments
```bash
Usage of castle.exe:
```powershell
Usage of castle:
-b, -build Build the project.
-c, -config Config YAML file to parse. (default "castle.yml")
-r, -run Run the project.
-v, -version Show version.
-t, -test Run the tests.

task <task-name> Run a task.
```
61 changes: 46 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,93 @@ import (
const (
LIGHTBLUE = "\033[1;34m"
LIGHTCYAN = "\033[1;36m"
LIGHTGREY = "\033[1;37m"
LIGHTPURPLE = "\033[1;35m"
LIGHTRED = "\033[1;31m"
LIGHTYELLOW = "\033[1;33m"
RESET = "\033[0m"

VERSION = "v0.0.1"
VERSION = "v0.1.0"
)

var (
filename = flag.String("config", "castle.yaml", "Config YAML file to parse.")
showVersion = flag.Bool("version", false, "Show version and exit.")
shouldBuild = flag.Bool("build", false, "Build the project.")
shouldRun = flag.Bool("run", false, "Run the project.")
shouldTest = flag.Bool("test", false, "test the project.")
)

func init() {
flag.StringVar(filename, "c", "castle.yml", "Config YAML file to parse.")
flag.BoolVar(showVersion, "v", false, "Show version.")
flag.BoolVar(shouldBuild, "b", false, "Build the project.")
flag.BoolVar(shouldRun, "r", false, "Run the projecte.")
flag.BoolVar(shouldRun, "r", false, "Run the project.")
flag.BoolVar(shouldTest, "t", false, "Test the project.")
}

func building() { fmt.Println(LIGHTBLUE, "\bBuilding... 🔨", RESET) }
func running() { fmt.Println(LIGHTCYAN, "\bRunning... 🚀", RESET) }
func testing() { fmt.Println(LIGHTYELLOW, "\bTesting... 🧪", RESET) }
func none() bool {
return !*shouldBuild && !*shouldRun && !*shouldTest && !*showVersion
}

func main() {
flag.Parse()

fmt.Println(LIGHTPURPLE, "\bCastle", VERSION, RESET)

if *showVersion {
fmt.Println(LIGHTPURPLE, "\bCastle", VERSION, RESET)
os.Exit(0)
}

fmt.Println(LIGHTPURPLE, "\bCastle", VERSION, RESET)
config := parse.Parse(*filename)
if none() {
// Remove the -c and -config and following argument from os.Args
for i, v := range os.Args {
if v == "-c" || v == "-config" {
os.Args = append(os.Args[:i], os.Args[i+2:]...)
}
}

if os.Args[1] == "task" {
fmt.Println(LIGHTRED, "\bTask:", os.Args[2], "... 📝", RESET)
RunSection(config.Tasks[os.Args[2]])
}
}

if *shouldBuild {
fmt.Println("Building...")
building()
RunSection(config.Build)
os.Exit(0)
}

if *shouldRun {
fmt.Println("Running...")
running()
RunSection(config.Run)
}

if *shouldTest {
testing()
RunSection(config.Test)
}

if !*shouldBuild && !*shouldRun && !*shouldTest {
building()
RunSection(config.Build)
running()
RunSection(config.Run)
os.Exit(0)
}

fmt.Println(LIGHTBLUE, "\bBuilding... 🔨", RESET)
RunSection(config.Build)
fmt.Println(LIGHTCYAN, "\bRunning... 🚀", RESET)
RunSection(config.Run)
os.Exit(0)
}

func RunSection(iter []string) {
for _, cmd := range iter {
fmt.Println("→ ", cmd)
fmt.Println(LIGHTGREY, "\b→", RESET, cmd)

c := strings.Split(cmd, " ")
d := strings.Join(c[1:], " ")
cmd := exec.Command(c[0], d)
cmd := exec.Command(c[0], c[1:]...)

out, err := cmd.CombinedOutput()
fmt.Println(string(out))
Expand Down
8 changes: 5 additions & 3 deletions parse/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
)

type T struct {
Build []string `yaml:"build"`
Run []string `yaml:"run"`
Build []string `yaml:"build"`
Run []string `yaml:"run"`
Test []string `yaml:"test"`
Tasks map[string][]string `yaml:"tasks"`
}

func Parse(filename string) T {
fmt.Println("File → ", filename)
fmt.Println("Config File → ", filename)
fmt.Println()

t := T{}
Expand Down
2 changes: 1 addition & 1 deletion run.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off
go build -o=./bin/castle.exe main.go
cls
.\bin\castle.exe -h
.\bin\castle.exe -c test/castle.yaml task admin
9 changes: 9 additions & 0 deletions test/castle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ build:

run:
- ./file.exe run

test:
- ./file.exe test

tasks:
format:
- ./file.exe format
admin:
- ./file.exe admin

0 comments on commit f51b736

Please sign in to comment.