Skip to content

Commit 87c8c02

Browse files
author
Florian Heinze
committed
TASK: moved to github
1 parent cf2f6b8 commit 87c8c02

File tree

11 files changed

+318
-0
lines changed

11 files changed

+318
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
test/
3+
dist/
4+
.DS_Store

.goreleaser.yaml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
env_files:
2+
github_token: ~/.config/goreleaser/github_token
3+
before:
4+
hooks:
5+
- go mod download
6+
builds:
7+
- id: "dev-script-runner"
8+
main: ./main.go
9+
goos:
10+
- darwin
11+
goarch:
12+
- amd64
13+
- arm
14+
- arm64
15+
archives:
16+
- replacements:
17+
darwin: Darwin
18+
linux: Linux
19+
windows: Windows
20+
386: i386
21+
amd64: x86_64
22+
checksum:
23+
name_template: 'checksums.txt'
24+
snapshot:
25+
name_template: "{{ .Tag }}-next"
26+
changelog:
27+
sort: asc
28+
filters:
29+
exclude:
30+
- '^docs:'
31+
- '^test:'
32+
brews:
33+
-
34+
name: dev-script-runner
35+
tap:
36+
owner: sandstorm
37+
name: homebrew-tap
38+
# Git author used to commit to the repository.
39+
# Defaults are shown.
40+
commit_author:
41+
name: Sandstorm Release Bot
42+
43+
# Folder inside the repository to put the formula.
44+
# Default is the root folder.
45+
folder: Formula
46+
# Your app's homepage.
47+
# Default is empty.
48+
homepage: "https://github.com/sandstorm/dev-script-runner"
49+
# Your app's description.
50+
# Default is empty.
51+
description: "Sandstorm Dev Script Runner"
52+
# This is a common pattern in Homebrew formulae. We install the directory
53+
# under bin/, as that directory is supposed to contains executables only,
54+
# but rather to install both the script and the directory in some place,
55+
# then create a wrapper in bin/ that calls the script from that place.
56+
install: |
57+
libexec.install Dir["*"]
58+
bin.write_exec_script libexec/"dev-script-runner"

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
set -ex
4+
5+
go build main.go

dev.sh

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
# This script is used to document and run recurring tasks in development. #
4+
# #
5+
# You can run your tasks using the script `./dev some-task`. #
6+
# You can install the Sandstorm Dev Script Runner and run your tasks from any #
7+
# nested folder using `dev run some-task`. #
8+
###############################################################################
9+
10+
set -e
11+
12+
######### TASKS #########
13+
14+
function test() {
15+
echo "#### TEST ####"
16+
_log_success "SUCCESS"
17+
_log_warning "WARNING"
18+
cat dev.sh
19+
}
20+
21+
function setup() {
22+
# As the setup typically is more complex we recommend using a separate
23+
# file `dev_setup.sh`
24+
./dev_setup.sh
25+
}
26+
27+
function sometask() {
28+
# Most task will only require some steps. We recommend implementing them here
29+
_log_success "First Step of some task"
30+
_log_warning "TODO: implement more steps"
31+
}
32+
33+
####### Utilities #######
34+
35+
_log_success() {
36+
printf "\033[0;32m${1}\033[0m\n"
37+
}
38+
_log_warning() {
39+
printf "\033[1;33m%s\033[0m\n" "${1}"
40+
}
41+
_log_error() {
42+
printf "\033[0;31m%s\033[0m\n" "${1}"
43+
}
44+
45+
# THIS NEEDS TO BE LAST!!!
46+
# this will run your tasks
47+
"$@"

dev_setup.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
3#!/bin/bash
2+
3+
# Setup Script to provide a great first run experience ;)
4+
5+
set -e
6+
7+
brew install go
8+
# se our homebrew tap to always get the latest updates.
9+
brew install goreleaser/tap/goreleaser
10+

go.mod

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module main
2+
3+
go 1.19
4+
5+
require (
6+
github.com/logrusorgru/aurora/v3 v3.0.0 // indirect
7+
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
2+
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
3+
github.com/logrusorgru/aurora/v3 v3.0.0 h1:R6zcoZZbvVcGMvDCKo45A9U/lzYyzl5NfYIvznmDfE4=
4+
github.com/logrusorgru/aurora/v3 v3.0.0/go.mod h1:vsR12bk5grlLvLXAYrBsb5Oc/N+LxAlxggSjiwMnCUc=
5+
github.com/logrusorgru/aurora/v4 v4.0.0 h1:sRjfPpun/63iADiSvGGjgA1cAYegEWMPCJdUpJYn9JA=
6+
github.com/logrusorgru/aurora/v4 v4.0.0/go.mod h1:lP0iIa2nrnT/qoFXcOZSrZQpJ1o6n2CUf/hyHi2Q4ZQ=

main

1.83 MB
Binary file not shown.

main.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
// main.go
2+
package main
3+
4+
import (
5+
"embed"
6+
"github.com/logrusorgru/aurora/v3"
7+
"log"
8+
"os"
9+
"os/exec"
10+
"path/filepath"
11+
"regexp"
12+
)
13+
14+
const DEV_SCRIPT_MARKER = "DEV_SCRIPT_MARKER"
15+
const DEV_SCRIPT_NAME = "dev.sh"
16+
const MAX_DEPTH = 100
17+
const INIT_ARGUMENT = "INIT"
18+
19+
// Assets represents the embedded files.
20+
//
21+
//go:embed templates/dev.sh templates/dev_setup.sh
22+
var Assets embed.FS
23+
24+
func main() {
25+
// TODO: remove later
26+
// os.Chdir("/Users/florian/src/solarwatt-home-app-flutter/app/lib/features/counter_example")
27+
os.Chdir("/Users/florian/src/go-dev-script-runner/test")
28+
if os.Args[1] == INIT_ARGUMENT {
29+
runInit()
30+
} else {
31+
runTask()
32+
}
33+
}
34+
35+
func runInit() {
36+
log.Println(aurora.Green("RUNNING INIT"))
37+
currentDirectory, err := os.Getwd()
38+
if err != nil {
39+
log.Fatalf("Failed to execute: '%s'", err.Error())
40+
}
41+
42+
devShTargetPath := filepath.Join(currentDirectory, "dev.sh")
43+
devSetupShTargetPath := filepath.Join(currentDirectory, "dev_setup.sh")
44+
45+
if !fileExists(devShTargetPath) {
46+
copyAssetToPath("templates/dev.sh", devShTargetPath)
47+
if !fileExists(devSetupShTargetPath) {
48+
copyAssetToPath("templates/dev_setup.sh", devSetupShTargetPath)
49+
} else {
50+
log.Println("dev_setup.sh is already present.")
51+
}
52+
} else {
53+
log.Println("dev.sh is already present. Skipping INIT")
54+
}
55+
56+
os.Exit(0)
57+
}
58+
59+
func runTask() {
60+
currentDirectory, err := os.Getwd()
61+
if err != nil {
62+
log.Fatalf("Failed to execute: '%s'", err.Error())
63+
}
64+
65+
steps := 0
66+
for {
67+
devScriptPath := filepath.Join(currentDirectory, DEV_SCRIPT_NAME)
68+
if fileExists(devScriptPath) {
69+
log.Println("Found " + DEV_SCRIPT_NAME + " in path " + currentDirectory)
70+
if fileContains(devScriptPath, DEV_SCRIPT_MARKER) {
71+
log.Println(DEV_SCRIPT_NAME + " contains marker " + DEV_SCRIPT_MARKER)
72+
execDevScriptWithArguments(devScriptPath, os.Args[1:])
73+
break
74+
} else {
75+
log.Println("Marker is missing in " + DEV_SCRIPT_NAME)
76+
}
77+
} else {
78+
currentDirectory = filepath.Dir(currentDirectory)
79+
log.Println("MOVING UP:", currentDirectory)
80+
steps += 1
81+
}
82+
if currentDirectory == "/" || steps >= MAX_DEPTH {
83+
break
84+
}
85+
}
86+
}
87+
88+
func fileExists(path string) bool {
89+
info, err := os.Stat(path)
90+
if os.IsNotExist(err) {
91+
return false
92+
}
93+
return !info.IsDir()
94+
}
95+
96+
func fileContains(filePath string, needle string) bool {
97+
b, err := os.ReadFile(filePath)
98+
if err != nil {
99+
log.Fatalf("Failed to execute: '%s'", err.Error())
100+
}
101+
isContained, err := regexp.Match(needle, b)
102+
if err != nil {
103+
log.Fatalf("Failed to execute: '%s'", err.Error())
104+
}
105+
return isContained
106+
}
107+
108+
func copyAssetToPath(embedPath string, targetPath string) {
109+
data, err := Assets.ReadFile(embedPath)
110+
if err != nil {
111+
log.Fatalf("Failed to execute: '%s'", err.Error())
112+
}
113+
err = os.WriteFile(targetPath, []byte(data), 0755)
114+
if err != nil {
115+
log.Fatalf("Failed to execute: '%s'", err.Error())
116+
}
117+
log.Println(targetPath + " was created.")
118+
}
119+
120+
func execDevScriptWithArguments(devScriptPath string, arguments []string) {
121+
err := os.Chdir(filepath.Dir(devScriptPath))
122+
if err != nil {
123+
log.Fatalf("Failed to execute: '%s'", err.Error())
124+
}
125+
126+
cmd := exec.Command(devScriptPath, arguments...)
127+
cmd.Stdout = os.Stdout
128+
cmd.Stderr = os.Stderr
129+
// Run wil wait for the process to finish
130+
err = cmd.Run()
131+
if err != nil {
132+
log.Fatalf("Failed to execute: '%s'", err.Error())
133+
}
134+
}

templates/dev.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
# This script is used to document and run recurring tasks in development. #
4+
# #
5+
# You can run your tasks using the script `./dev some-task`. #
6+
# You can install the Sandstorm Dev Script Runner and run your tasks from any #
7+
# nested folder using `dev run some-task`. #
8+
###############################################################################
9+
10+
set -e
11+
12+
######### TASKS #########
13+
14+
function setup() {
15+
# As the setup typically is more complex we recommend using a separate
16+
# file `dev_setup.sh`
17+
./dev_setup.sh
18+
}
19+
20+
function sometask() {
21+
# Most task will only require some steps. We recommend implementing them here
22+
_log_success "First Step of some task"
23+
_log_warning "TODO: implement more steps"
24+
}
25+
26+
####### Utilities #######
27+
28+
_log_success() {
29+
printf "\033[0;32m${1}\033[0m\n"
30+
}
31+
_log_warning() {
32+
printf "\033[1;33m%s\033[0m\n" "${1}"
33+
}
34+
_log_error() {
35+
printf "\033[0;31m%s\033[0m\n" "${1}"
36+
}
37+
38+
# THIS NEEDS TO BE LAST!!!
39+
# this will run your tasks
40+
"$@"

templates/dev_setup.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
# Setup Script to provide a great first run experience ;)
4+
5+
set -e
6+
7+
echo "TODO: implement ./dev_setup.sh"

0 commit comments

Comments
 (0)