@@ -3,24 +3,57 @@ package utils
33import (
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