Skip to content

Commit e7e40f3

Browse files
author
Florian Heinze
committed
FEATURE: parse tasks from source files
1 parent a9de557 commit e7e40f3

11 files changed

+179
-45
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ script directly `./dev.sh some-task`.
1010
* easy initialization of your project
1111
* autocompletion
1212
* documentation of your tasks will be used to provide help
13+
* structure your tasks in different files
1314

1415
## Motivation
1516

@@ -112,6 +113,22 @@ function sometask() {
112113
}
113114
```
114115

116+
### Structuring tasks into different files
117+
118+
```bash
119+
#!/bin/bash
120+
############################## DEV_SCRIPT_MARKER ##############################
121+
122+
# You can structure your tasks into different files using `source`. We will also
123+
# parse these files. The order will be:
124+
# 1. tasks of the dev.sh
125+
# 2. tasks of sourced files
126+
source ./dev_utilities.sh
127+
source ./dev_tasks_testing.sh
128+
source ./dev_tasks_release.sh
129+
```
130+
131+
115132
## Usage
116133

117134
run `dev` for more information.

dev.sh

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,29 @@
77
# nested folder using `dev run some-task`. #
88
###############################################################################
99

10+
source ./dev_utilities.sh
11+
source ./dev_tasks_testing.sh
12+
source ./dev_tasks_release.sh
13+
1014
set -e
1115

1216
######### TASKS #########
1317

18+
# install dev dependencies
19+
function setup() {
20+
# As the setup typically is more complex we recommend using a separate
21+
# file `dev_setup.sh`
22+
./dev_setup.sh
23+
}
24+
25+
# build
26+
function build() {
27+
go build main
28+
}
29+
1430
# exposes ./main binary globally
1531
# we rename the original file and copy a fresh build to `/usr/local/bin/`
16-
function switch-dev-binary() {
32+
function switch-binary() {
1733
echo "-----------> creating build"
1834
build
1935
echo "-----------> replacing binary"
@@ -29,7 +45,7 @@ function switch-dev-binary() {
2945
#
3046
# we check if the dev binary was backed up and replace the current dev with
3147
# this backup ;)
32-
function restore-dev-binary() {
48+
function restore-binary() {
3349
if test -f "/usr/local/bin/dev_back"; then
3450
echo "/usr/local/bin/dev_back exists."
3551
echo "restoring /usr/local/bin/dev "
@@ -39,45 +55,6 @@ function restore-dev-binary() {
3955
echo "no /usr/local/bin/dev_back found. Nothing to restore!"
4056
fi
4157
}
42-
# build
43-
function build() {
44-
go build main
45-
}
46-
47-
# install dev dependencies
48-
function setup() {
49-
# As the setup typically is more complex we recommend using a separate
50-
# file `dev_setup.sh`
51-
./dev_setup.sh
52-
}
53-
54-
# running tests
55-
function run-test() {
56-
pushd utils
57-
go test "$@"
58-
popd
59-
_log_success "All Tests finished successfully ;)"
60-
}
61-
62-
# releasing a new version
63-
function release() {
64-
run-test
65-
build
66-
goreleaser release --rm-dist
67-
_log_success "Release finished successfully ;)"
68-
}
69-
70-
####### Utilities #######
71-
72-
_log_success() {
73-
printf "\033[0;32m${1}\033[0m\n"
74-
}
75-
_log_warning() {
76-
printf "\033[1;33m%s\033[0m\n" "${1}"
77-
}
78-
_log_error() {
79-
printf "\033[0;31m%s\033[0m\n" "${1}"
80-
}
8158

8259
# THIS NEEDS TO BE LAST!!!
8360
# this will run your tasks

dev_tasks_release.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# releasing a new version
6+
function release() {
7+
run-test
8+
build
9+
goreleaser release --rm-dist
10+
_log_success "Release finished successfully ;)"
11+
}

dev_tasks_testing.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# running tests
6+
function run-test() {
7+
pushd utils
8+
go test "$@"
9+
popd
10+
_log_success "All Tests finished successfully ;)"
11+
}

dev_utilities.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
####### Utilities #######
6+
7+
_log_success() {
8+
printf "\033[0;32m${1}\033[0m\n"
9+
}
10+
_log_warning() {
11+
printf "\033[1;33m%s\033[0m\n" "${1}"
12+
}
13+
_log_error() {
14+
printf "\033[0;31m%s\033[0m\n" "${1}"
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
4+
set -e
5+
6+
######### TASKS #########
7+
8+
function nested-import-task1 {
9+
echo "Some Code"
10+
}
11+
12+
function nested-import-task2 {
13+
echo "Some Code"
14+
}

utils/fixtures/tasks_from_imports.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
4+
source ./tasks_from_imports__import1.sh
5+
source ./tasks_from_imports__import2.sh
6+
source ./nested_imports/tasks_from_imports__import3.sh
7+
8+
set -e
9+
10+
######### TASKS #########
11+
12+
function task() {
13+
echo "Some Code"
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
4+
set -e
5+
6+
######### TASKS #########
7+
8+
function import1-task1 {
9+
echo "Some Code"
10+
}
11+
12+
function import1-task2 {
13+
echo "Some Code"
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
############################## DEV_SCRIPT_MARKER ##############################
3+
4+
set -e
5+
6+
######### TASKS #########
7+
8+
function import2-task1 {
9+
echo "Some Code"
10+
}
11+
12+
function import2-task2 {
13+
echo "Some Code"
14+
}

utils/parse_dev_script_tasks.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,57 @@ package utils
33
import (
44
"log"
55
"os"
6+
"path"
7+
"path/filepath"
68
"regexp"
79
"sort"
810
"strings"
911
)
1012

11-
func ParseDevScriptTasks(devScriptPath string) []DevScriptTask {
12-
// https://regex101.com/r/ZFB8ml/1
13+
func getSourcedScriptContent(sourcePath string, devScriptPath string) string {
14+
absolutePath := ""
15+
if filepath.IsAbs(sourcePath) {
16+
// sourcePath is absolute, we can ignore the devScriptPath
17+
absolutePath = sourcePath
18+
} else {
19+
// sourcePath is relative
20+
absolutePath = path.Join(path.Dir(devScriptPath), sourcePath)
21+
}
22+
sourceBytes, err := os.ReadFile(absolutePath)
23+
if err != nil {
24+
return ""
25+
}
26+
return string(sourceBytes)
27+
}
28+
29+
func collectSourcedScripts(devScriptPath string) string {
1330
devScriptBytes, err := os.ReadFile(devScriptPath)
1431
if err != nil {
1532
log.Fatalf("Failed to execute: '%s'", err.Error())
1633
}
34+
result := string(devScriptBytes)
35+
// https://regex101.com/r/POMvPQ/1
36+
compiledRegex := regexp.MustCompile(`source (.*\.sh)`)
37+
matches := compiledRegex.FindAllStringSubmatch(result, -1)
1738

18-
devScriptString := string(devScriptBytes)
39+
for _, match := range matches {
40+
if len(match) > 1 {
41+
// capture group `(.*\.sh)`
42+
result += "\n"
43+
result += getSourcedScriptContent(match[1], devScriptPath)
44+
}
45+
}
46+
47+
return result
48+
}
49+
50+
func ParseDevScriptTasks(devScriptPath string) []DevScriptTask {
51+
// https://regex101.com/r/ZFB8ml/1
1952
compiledRegex := regexp.MustCompile(`(?m)(?P<comments>(?:^#.*(?:\n|\r\n|\r))*)^(?:function )?(?P<name>[a-zA-Z0-9_-]+)\s?(?:\(\))?\s?{`)
2053
captureGroupNames := compiledRegex.SubexpNames()
2154
commentsIndex := sort.StringSlice(captureGroupNames).Search("comments")
2255
taskIndex := sort.StringSlice(captureGroupNames).Search("name")
23-
matches := compiledRegex.FindAllStringSubmatch(devScriptString, -1)
56+
matches := compiledRegex.FindAllStringSubmatch(collectSourcedScripts(devScriptPath), -1)
2457

2558
var results = []DevScriptTask{}
2659
for _, match := range matches {

0 commit comments

Comments
 (0)