Skip to content

Commit

Permalink
Add @lib for code reuse #36
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Aug 24, 2021
1 parent 3622f81 commit 1d52cb1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makesurefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
@depends_on tests/13_errors.tush
@depends_on tests/14_doc.tush
@depends_on tests/15_private.tush
@depends_on tests/16_lib.tush

@goal stable
@doc rebuilds makesure_stable & README.md for release
Expand Down
44 changes: 42 additions & 2 deletions makesure.awk
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ BEGIN {
split("",ReachedIf) # name -> condition line
split("",GlobFiles) # list
split("",GlobGoals) # list
Mode = "prelude" # prelude/goal/goal_glob
split("",LibNames) # list
split("",Lib) # name -> code
split("",GoalToLib)# goal name -> lib name
Mode = "prelude" # prelude|goal|goal_glob|lib
srand()
prepareArgs()
MyDirScript = "MYDIR=" quoteArg(getMyDir(ARGV[1])) ";export MYDIR;cd \"$MYDIR\""
Expand All @@ -36,6 +39,8 @@ BEGIN {
"@doc" == $1 { handleDoc(); next }
"@depends_on" == $1 { handleDependsOn(); next }
"@reached_if" == $1 { handleReachedIf(); next }
"@lib" == $1 { handleLib(); next }
"@use_lib" == $1 { handleUseLib(); next }
$1 ~ /^@/ { addError("Unknown directive: " $1); next }
{ handleCodeLine($0); next }

Expand Down Expand Up @@ -156,6 +161,36 @@ function started(mode) {
Mode = mode
}

function handleLib() {
started("lib")

libName = trim($2)
if (libName in Lib) {
die("Lib '" libName "' is already defined")
}
arrPush(LibNames, script_name)
Lib[libName]
}

function handleUseLib( goal_name) {
checkGoalOnly()

if ("goal" == Mode)
registerUseLib(currentGoalName())
else {
for (i=0; i<arrLen(GlobGoals); i++){
registerUseLib(GlobGoals[i])
}
}
}

function registerUseLib(goal_name) {
if (goal_name in GoalToLib)
die("You can only use one @lib in a @goal")

GoalToLib[goal_name] = $2
}

function handleGoal( priv) {
started("goal")
priv = parseGoalLine()
Expand Down Expand Up @@ -408,6 +443,7 @@ function isPrelude() { return "prelude"==Mode }
function checkPreludeOnly() { if (!isPrelude()) addError("Only use " $1 " in prelude") }
function checkGoalOnly() { if ("goal" != Mode && "goal_glob" != Mode) addError("Only use " $1 " in @goal") }
function currentGoalName() { return isPrelude() ? "" : arrLast(GoalNames) }
function currentLibName() { return arrLast(LibNames) }

function realExit(code, i) {
Died = 1
Expand Down Expand Up @@ -445,7 +481,11 @@ function getMyDir(makesurefilePath) {
}

function handleCodeLine(line, goal_name) {
if ("goal_glob" == Mode) {
if ("lib" == Mode) {
name = currentLibName()
#print "Append line for '" name "': " line
Lib[name] = addL(Lib[name], line)
} else if ("goal_glob" == Mode) {
for (i=0; i<arrLen(GlobGoals); i++){
if (!Code[goal_name = GlobGoals[i]])
addCodeLine(goal_name, makeGlobVarsCode(i))
Expand Down
19 changes: 19 additions & 0 deletions tests/16_lib.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@


@lib
f1 () {
echo "Hello $1"
}

@lib lib_name
f2 () {
echo "Hello lib_name $1"
}

@goal g1
@use_lib
f1 World

@goal g2
@use_lib lib_name
f1 World
12 changes: 12 additions & 0 deletions tests/16_lib.tush
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


$ cd "$MYDIR"; ./makesure -f tests/16_lib.sh -l
| Available goals:
| a
| 15_private.sh

$ cd "$MYDIR"; ./makesure -f tests/16_lib.sh g1

$ cd "$MYDIR"; ./makesure -f tests/16_lib.sh g2


0 comments on commit 1d52cb1

Please sign in to comment.