Skip to content

Commit

Permalink
#171 @call
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Dec 29, 2024
1 parent 3618ef8 commit 5d76b18
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
4 changes: 3 additions & 1 deletion docs/@call.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ desugars to

Do we need `--file 'path/to/Makesurefile'`?

No. Even if we run `./makesure path/to/Makesurefile` path resolution is relative to the `Makesurefile` location, so internal makesure invocation now doesn't need explicit Makesurefile reference. But let's add a test for this case.
~~No. Even if we run `./makesure -f path/to/Makesurefile` path resolution is relative to the `Makesurefile` location, so internal makesure invocation now doesn't need explicit Makesurefile reference. But let's add a test for this case.~~

Yes, we must add `--file` because it can be `./makesure -f path/to/anyname`.

Are there other options to passthrough ([Usage](https://makesure.dev/Usage.html))?

Expand Down
31 changes: 29 additions & 2 deletions makesure.awk
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ BEGIN {
Mode = "prelude" # prelude|define|goal|goal_glob|lib
srand()
prepareArgs()
ProgAbs = "" # absolute path to makesure executable
MyDirScript = "MYDIR=" quoteArg(getMyDir(ARGV[1])) ";export MYDIR;cd \"$MYDIR\""
Error = ""
makesure()
Expand All @@ -52,6 +53,7 @@ function makesure( i) {
else if ("@reached_if" == $1) handleReachedIf()
else if ("@lib" == $1) handleLib()
else if ("@use_lib" == $1) handleUseLib()
else if ("@calls" == $1) handleCalls()
else if ($1 ~ /^@/) addError("Unknown directive: " $1)
else handleCodeLine($0)
for (i = 1; i < 10; i++) $i = "" # only for macos 10.15 awk version 20070501
Expand Down Expand Up @@ -185,6 +187,25 @@ function handleUseLib( i) {
registerUseLib(globGoal(i))
}

function handleCalls( i) {
checkGoalOnly()

if (NF < 2)
addError("Provide at least one dependency")

if ("goal" == Mode)
processCalls()
# else TODO
# for (i = 0; i < GlobCnt; i++)
# registerDependsOn(globGoal(i))
}

function processCalls( i) {
for (i = 2; i <= NF; i++)
addCodeLine(quoteArg(ProgAbs) " " quoteArg($i))
# addCodeLine("echo " quoteArg(ProgAbs) " " quoteArg($i))
}

function registerUseLib(goalName) {
if (goalName in GoalToLib)
addError("You can only use one @lib in a @goal")
Expand Down Expand Up @@ -549,8 +570,12 @@ function shellExec(script, comment, res) {
return res
}

function getMyDir(makesurefilePath) {
return executeGetLine("cd \"$(dirname " quoteArg(makesurefilePath) ")\";pwd")
function getMyDir(makesurefilePath, script,res,p) {
script = "echo \"$(cd \"$(dirname "(p=quoteArg(Prog))")\" && pwd)/$(basename "p")\";cd \"$(dirname " quoteArg(makesurefilePath) ")\";pwd"
script | getline ProgAbs
script | getline res
closeErr(script)
return res
}

function handleCodeLine(line) {
Expand Down Expand Up @@ -952,6 +977,7 @@ function reparseCli( res,i,err) {
}
return 1
}
# bash-friendly (non-POSIX-compatible) quoting
function quote2(s,force) {
if (index(s, "'")) {
gsub(/\\/, "\\\\", s)
Expand All @@ -978,6 +1004,7 @@ function commandExists(cmd) { return ok("command -v " cmd " >/dev/null") }
function ok(cmd) { return system(cmd) == 0 }
function isFile(path) { return ok("test -f " quoteArg(path)) }
function rm(f) { system("rm " quoteArg(f)) }
# POSIX-compatible quoting
function quoteArg(a) { gsub("'", "'\\''", a); return "'" a "'" }
function trim(s) { sub(/^[ \t\r\n]+/, "", s); sub(/[ \t\r\n]+$/, "", s); return s }
function copyKey(keySrc,keyDst,arr) { if (keySrc in arr) arr[keyDst] = arr[keySrc] }
15 changes: 15 additions & 0 deletions tests/32_calls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@


@goal 'x-created'
@reached_if test -f /tmp/x
echo 'running x-created'
touch /tmp/x

@goal 'x-deleted'
@reached_if ! test -e /tmp/x
echo 'running x-deleted'
rm /tmp/x

@goal 'x-updated'
@calls 'x-deleted'
@calls 'x-created'
2 changes: 2 additions & 0 deletions tests/32_calls.tush
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

$ ./$MAKESURE -f tests/32_calls.sh x-updated

0 comments on commit 5d76b18

Please sign in to comment.