Skip to content

Commit

Permalink
auto-format using the latest intellij-awk : adjust minify.awk
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Dec 21, 2023
1 parent c6a2324 commit 36689f1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 70 deletions.
130 changes: 65 additions & 65 deletions makesure_candidate
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ BEGIN {
Mode="prelude"
srand()
prepareArgs()
MyDirScript="MYDIR=" quoteArg(getMyDir(ARGV[1]))";export MYDIR;cd \"$MYDIR\""
MyDirScript="MYDIR="quoteArg(getMyDir(ARGV[1]))";export MYDIR;cd \"$MYDIR\""
Error=""
makesure()}
function makesure(i){
Expand All @@ -29,7 +29,7 @@ function makesure(i){
else if("@reached_if"==$1)handleReachedIf()
else if("@lib"==$1)handleLib()
else if("@use_lib"==$1)handleUseLib()
else if($1~/^@/)addError("Unknown directive: " $1)
else if($1~/^@/)addError("Unknown directive: "$1)
else handleCodeLine($0)
for(i=1;i<10;i++)$i=""}
doWork()
Expand All @@ -49,7 +49,7 @@ function prepareArgs(i,arg){
arrPush(ArgGoals,arg)
delete ARGV[i]}
if("-h"in Args||"--help"in Args){
print"makesure ver. " Version
print"makesure ver. "Version
print"Usage: makesure [options...] [-f buildfile] [goals...]"
print" -f,--file buildfile"
print" set buildfile to use (default Makesurefile)"
Expand All @@ -75,7 +75,7 @@ function prepareArgs(i,arg){
if(isFile(ARGV[1]"/Makesurefile"))
ARGV[1]=ARGV[1]"/Makesurefile"
else
die("makesure file not found: " ARGV[1])}
die("makesure file not found: "ARGV[1])}
if("-s"in Args||"--silent"in Args)
Options["silent"]
if("-x"in Args||"--tracing"in Args)
Expand All @@ -96,30 +96,30 @@ function handleOptions(i){
addError("Provide at least one option")
for(i=2;i<=NF;i++){
if(!($i in SupportedOptions))
addError("Option '\''" $i"'\'' is not supported")
addError("Option '\''"$i"'\'' is not supported")
Options[$i]}}
function handleDefine(){
started("define")
if(NF!=3){
addError("Invalid @define syntax, should be @define VAR_NAME '\''value'\''")
return}
if($2 !~ /^[A-Za-z_][A-Za-z0-9_]*$/){
addError("Wrong variable name: '\''" $2"'\''")
addError("Wrong variable name: '\''"$2"'\''")
return}
if(!($2 in DefineOverrides))
Vars[$2]=$3}
function handleShell(){
checkPreludeOnly()
if(!((Shell=$2)in SupportedShells))
addError("Shell '\''" Shell"'\'' is not supported")}
addError("Shell '\''"Shell"'\'' is not supported")}
function timingOn(){
return"timing"in Options&&!("silent"in Options)}
function started(mode){
Mode=mode}
function handleLib(libName){
started("lib")
if((libName=$2)in Lib){
addError("Lib '\''" libName"'\'' is already defined")}
addError("Lib '\''"libName"'\'' is already defined")}
arrPush(LibNames,libName)
Lib[libName]}
function handleUseLib(i){
Expand All @@ -143,13 +143,13 @@ function handleGoal(i,goalName){
GoalParams[goalName,GoalParamsCnt[goalName]++]=validateParamName($i)
}else if(NF>2)addError("nothing allowed after goal name")}
function validateParamName(p){
if(p !~ /^[A-Z_][A-Z0-9_]*$/)addError("@param name should match /^[A-Z_][A-Z0-9_]*$/: '\''" p"'\''")
if(p !~ /^[A-Z_][A-Z0-9_]*$/)addError("@param name should match /^[A-Z_][A-Z0-9_]*$/: '\''"p"'\''")
return p}
function registerGoal(priv,goalName){
if(""==goalName||"@params"==goalName)
addError("Goal must have a name")
else if(goalName in GoalsByName)
addError("Goal " quote2(goalName,1)" is already defined")
addError("Goal "quote2(goalName,1)" is already defined")
else {
arrPush(GoalNames,goalName)
GoalsByName[goalName]=priv
Expand All @@ -159,9 +159,9 @@ function calcGlob(goalName,pattern,script,file){
GlobCnt=0
GlobGoalName=goalName
delete GlobFiles
script=MyDirScript";for f in " pattern";do test -e \"$f\"&&echo \"$f\"||:;done"
script=MyDirScript";for f in "pattern";do test -e \"$f\"&&echo \"$f\"||:;done"
if("sh"!=Shell)
script=Shell" -c " quoteArg(script)
script=Shell" -c "quoteArg(script)
while((script|getline file)>0){
GlobCnt++
arrPush(GlobFiles,file)}
Expand Down Expand Up @@ -204,7 +204,7 @@ function handleDoc(i){
registerDoc(globGoal(i))}}
function registerDoc(goalName){
if(goalName in Doc)
addError("Multiple " $1" not allowed for a goal")
addError("Multiple "$1" not allowed for a goal")
$1=""
Doc[goalName]=trim($0)}
function handleDependsOn(i){
Expand Down Expand Up @@ -241,10 +241,10 @@ function handleReachedIf(i){
for(i=0;i<GlobCnt;i++)
registerReachedIf(globGoal(i),makeGlobVarsCode(i))}
function makeGlobVarsCode(i){
return"ITEM=" quoteArg(GlobFiles[i])";INDEX=" i";TOTAL=" GlobCnt";"}
return"ITEM="quoteArg(GlobFiles[i])";INDEX="i";TOTAL="GlobCnt";"}
function registerReachedIf(goalName,preScript){
if(goalName in ReachedIf)
addError("Multiple " $1" not allowed for a goal")
addError("Multiple "$1" not allowed for a goal")
trimDirective()
ReachedIf[goalName]=preScript trim($0)}
function trimDirective(){
Expand All @@ -254,13 +254,13 @@ function checkBeforeRun(i,j,dep,depCnt,goalName){
depCnt=DependenciesCnt[goalName=GoalNames[i]]
for(j=0;j<depCnt;j++)
if(!((dep=Dependencies[goalName,j])in GoalsByName))
addError("Goal " quote2(goalName,1)" has unknown dependency '\''" dep"'\''",DependenciesLineNo[goalName,j])
addError("Goal "quote2(goalName,1)" has unknown dependency '\''"dep"'\''",DependenciesLineNo[goalName,j])
if(goalName in GoalToLib&&!(GoalToLib[goalName]in Lib))
addError("Goal " quote2(goalName,1)" uses unknown lib '\''" GoalToLib[goalName]"'\''",UseLibLineNo[goalName])}}
addError("Goal "quote2(goalName,1)" uses unknown lib '\''"GoalToLib[goalName]"'\''",UseLibLineNo[goalName])}}
function getPreludeCode(a,k){
addLine(a,MyDirScript)
for(k in Vars)
addLine(a,k"=" quoteArg(Vars[k])";export " k)
addLine(a,k"="quoteArg(Vars[k])";export "k)
return a[0]}
function doWork(\
i,goalName,gnLen,gnMaxLen,reachedGoals,emptyGoals,preludeCode,
Expand All @@ -286,7 +286,7 @@ body,goalBody,goalBodies,resolvedGoals,exitCode,t0,t1,t2,goalTimed,list){
continue
printf" "
if(goalName in Doc)
printf"%-" gnMaxLen"s : %s\n",quote2(goalName),Doc[goalName]
printf"%-"gnMaxLen"s : %s\n",quote2(goalName),Doc[goalName]
else
print quote2(goalName)}
}else {
Expand All @@ -311,25 +311,25 @@ body,goalBody,goalBodies,resolvedGoals,exitCode,t0,t1,t2,goalTimed,list){
print":"
for(i=0;i in resolvedGoals;i++)
if(!reachedGoals[goalName=resolvedGoals[i]]&&!emptyGoals[goalName])
print" " quote2(goalName)
print" "quote2(goalName)
}else {
for(i=0;i in resolvedGoals;i++){
goalName=resolvedGoals[i]
goalTimed=timingOn()&&!reachedGoals[goalName]&&!emptyGoals[goalName]
if(goalTimed)
t1=t2?t2:currentTimeMillis()
if(!("silent"in Options))
print" goal " quote2(goalName,1)" " (reachedGoals[goalName]?"[already satisfied].":emptyGoals[goalName]?"[empty].":"...")
print" goal "quote2(goalName,1)" "(reachedGoals[goalName]?"[already satisfied].":emptyGoals[goalName]?"[empty].":"...")
exitCode=(reachedGoals[goalName]||emptyGoals[goalName])?0:shellExec(goalBodies[goalName],goalName)
if(exitCode!=0)
print" goal " quote2(goalName,1)" failed"
print" goal "quote2(goalName,1)" failed"
if(goalTimed){
t2=currentTimeMillis()
print" goal " quote2(goalName,1)" took " renderDuration(t2-t1)}
print" goal "quote2(goalName,1)" took "renderDuration(t2-t1)}
if(exitCode!=0)
break}
if(timingOn())
print" total time " renderDuration((t2?t2:currentTimeMillis())-t0)
print" total time "renderDuration((t2?t2:currentTimeMillis())-t0)
if(exitCode!=0)
realExit(exitCode)}}}
function topologicalSort(includeReachedIf,requestedGoals,result,reachedGoals,i,j,goalName,loop,depCnt){
Expand All @@ -342,19 +342,19 @@ function topologicalSort(includeReachedIf,requestedGoals,result,reachedGoals,i,j
arrPush(requestedGoals,"default")
for(i=0;i in requestedGoals;i++){
if(!((goalName=requestedGoals[i])in GoalsByName))
die("Goal not found: " goalName)
die("Goal not found: "goalName)
topologicalSortPerform(includeReachedIf,reachedGoals,goalName,result,loop)}
if(loop[0]==1)
die("There is a loop in goal dependencies via " loop[1]" -> " loop[2])}
die("There is a loop in goal dependencies via "loop[1]" -> "loop[2])}
function isCodeAllowed(){return"goal"==Mode||"goal_glob"==Mode||"lib"==Mode}
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 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 arrLast(GoalNames)}
function currentLibName(){return arrLast(LibNames)}
function realExit(code){
exit code}
function addError(err,n){if(!n)n=NR;Error=addL(Error,err":\n" ARGV[1]":" n": " Lines[n])}
function addError(err,n){if(!n)n=NR;Error=addL(Error,err":\n"ARGV[1]":"n": "Lines[n])}
function addErrorDedup(err,n){if((err,n)in AddedErrors)return;AddedErrors[err,n];addError(err,n)}
function die(msg,out){
out="cat 1>&2"
Expand All @@ -364,20 +364,20 @@ function die(msg,out){
function checkConditionReached(goalName,conditionStr,script){
script=getPreludeCode()
if(goalName in GoalToLib)
script=script"\n" Lib[GoalToLib[goalName]]
script=script"\n" conditionStr
script=script"\n"Lib[GoalToLib[goalName]]
script=script"\n"conditionStr
return shellExec(script,goalName"@reached_if")==0}
function shellExec(script,comment,res){
if("tracing"in Options){
script=": " quoteArg(comment)"\n" script
script=Shell" -x -e -c " quoteArg(script)
script=": "quoteArg(comment)"\n"script
script=Shell" -x -e -c "quoteArg(script)
}else
script=Shell" -e -c " quoteArg(script)
script="trap '\''exit 7'\'' INT;" script
script=Shell" -e -c "quoteArg(script)
script="trap '\''exit 7'\'' INT;"script
res=system(script)
return res}
function getMyDir(makesurefilePath){
return executeGetLine("cd \"$(dirname " quoteArg(makesurefilePath)")\";pwd")}
return executeGetLine("cd \"$(dirname "quoteArg(makesurefilePath)")\";pwd")}
function handleCodeLine(line){
if(!isCodeAllowed()&&line !~ /^[ \t]*#/&&trim(line)!=""){
if(!ShellInPreludeErrorShown++)
Expand Down Expand Up @@ -443,15 +443,15 @@ function instantiate(goal,args,newArgs,i,j,depArg,depArgType,dep,goalNameInstant
copyKey(goal,goalNameInstantiated,ReachedIf)
copyKey(goal,goalNameInstantiated,GoalToLib)
for(i in args)
argsCode=addL(argsCode,i"=" quoteArg(args[i]))
argsCode=addL(argsCode,i"="quoteArg(args[i]))
CodePre[goalNameInstantiated]=addL(CodePre[goalNameInstantiated],argsCode)
if(goalNameInstantiated in ReachedIf)
ReachedIf[goalNameInstantiated]=argsCode"\n" ReachedIf[goalNameInstantiated]}
ReachedIf[goalNameInstantiated]=argsCode"\n"ReachedIf[goalNameInstantiated]}
for(i=0;i<DependenciesCnt[goal];i++){
dep=Dependencies[gi=goal SUBSEP i]
argsCnt=+DependencyArgsCnt[gi]
if(dep in GoalsByName&&argsCnt!=GoalParamsCnt[dep])
addErrorDedup("wrong args count for '\''" dep"'\''",DependenciesLineNo[gi])
addErrorDedup("wrong args count for '\''"dep"'\''",DependenciesLineNo[gi])
for(j=0;j<argsCnt;j++){
depArg=DependencyArgs[gi,j]
depArgType=DependencyArgsType[gi,j]
Expand All @@ -460,8 +460,8 @@ function instantiate(goal,args,newArgs,i,j,depArg,depArgType,dep,goalNameInstant
depArg : \
depArgType=="var"?\
(depArg in args?args[depArg]:\
depArg in Vars?Vars[depArg]:addErrorDedup("wrong arg '\''" depArg"'\''",DependenciesLineNo[gi])):\
die("wrong depArgType: " depArgType)}
depArg in Vars?Vars[depArg]:addErrorDedup("wrong arg '\''"depArg"'\''",DependenciesLineNo[gi])):\
die("wrong depArgType: "depArgType)}
gii=goalNameInstantiated SUBSEP i
Dependencies[gii]=instantiate(dep,newArgs)
DependenciesLineNo[gii]=DependenciesLineNo[gi]
Expand All @@ -471,7 +471,7 @@ function instantiateGoalName(goal,args,res,cnt,i){
if((cnt=GoalParamsCnt[goal])==0)return goal
res=goal
for(i=0;i<cnt;i++)
res=res"@" args[GoalParams[goal,i]]
res=res"@"args[GoalParams[goal,i]]
return res}
function currentTimeMillis(res){
if(Gawk)
Expand All @@ -480,17 +480,17 @@ function currentTimeMillis(res){
sub(/%?3N/,"000",res)
return +res}
function selfUpdate(url,tmp,err,newVer){
url="https://raw.githubusercontent.com/xonixx/makesure/main/makesure?token=" rand()
url="https://raw.githubusercontent.com/xonixx/makesure/main/makesure?token="rand()
tmp=executeGetLine("mktemp /tmp/makesure_new.XXXXXXXXXX")
err=dl(url,tmp)
if(!err&&!ok("chmod +x " tmp))err="can'\''t chmod +x " tmp
if(!err&&!ok("chmod +x "tmp))err="can'\''t chmod +x "tmp
if(!err){
newVer=executeGetLine(tmp" -v")
if(Version!=newVer){
if(!ok("cp " tmp" " quoteArg(Prog)))
err="can'\''t overwrite " Prog
else print"updated " Version" -> " newVer
}else print"you have latest version " Version" installed"}
if(!ok("cp "tmp" "quoteArg(Prog)))
err="can'\''t overwrite "Prog
else print"updated "Version" -> "newVer
}else print"you have latest version "Version" installed"}
rm(tmp)
if(err)die(err)}
function renderDuration(deltaMillis,\
Expand All @@ -514,26 +514,26 @@ function renderDuration(deltaMillis,\
secS=deltaSec>0?deltaSec" s":""
secSI=deltaSec>0?int(deltaSec)" s":""
if(dayS!="")
res=dayS" " (hrS==""?"0 h":hrS)
res=dayS" "(hrS==""?"0 h":hrS)
else if(deltaHr>0)
res=hrS" " (minS==""?"0 m":minS)
res=hrS" "(minS==""?"0 m":minS)
else if(deltaMin>0)
res=minS" " (secSI==""?"0 s":secSI)
res=minS" "(secSI==""?"0 s":secSI)
else
res=deltaSec>0?secS:"0 s"
return res}
function executeGetLine(script,res){
script|getline res
closeErr(script)
return res}
function closeErr(script){if(close(script)!=0)die("Error executing: " script)}
function closeErr(script){if(close(script)!=0)die("Error executing: "script)}
function dl(url,dest,verbose){
verbose="VERBOSE"in ENVIRON
if(commandExists("wget")){
if(!ok("wget " (verbose?"":"-q")" " quoteArg(url)" -O" quoteArg(dest)))
if(!ok("wget "(verbose?"":"-q")" "quoteArg(url)" -O"quoteArg(dest)))
return"error with wget"
}else if(commandExists("curl")){
if(!ok("curl " (verbose?"":"-s")" " quoteArg(url)" -o " quoteArg(dest)))
if(!ok("curl "(verbose?"":"-s")" "quoteArg(url)" -o "quoteArg(dest)))
return"error with curl"
}else return"wget/curl not found"}
function natOrder(s1,s2,i1,i2,c1,c2,n1,n2){
Expand Down Expand Up @@ -600,7 +600,7 @@ function parseCli_2(line,vars,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){
for(;(c=substr(line,pos+1,1))~/[_A-Za-z0-9]/;pos++)
var=var c
if(var !~ /^[_A-Za-z][_A-Za-z0-9]*$/)
return"wrong var: '\''" var"'\''"
return"wrong var: '\''"var"'\''"
w=(w)((val=var in vars?vars[var]:ENVIRON[var])!=""?val:defVal)
continue}
w=w c}
Expand All @@ -613,32 +613,32 @@ function parseCli_2(line,vars,res,pos,c,c1,isDoll,q,var,inDef,defVal,val,w,i){
while((c=substr(line,++pos,1))!=""&&c!=" "&&c!="\t"){
w=w c}
if(w !~ /^[_A-Za-z0-9@.]+$/)
return"wrong unquoted: '\''" w"'\''"
return"wrong unquoted: '\''"w"'\''"
res[i=+res[-7]++,"quote"]="u"
res[i]=w}}}}
function reparseCli(res,i,err){
err=parseCli_2($0,Vars,res)
if(err){
addError("Syntax error: " err)
addError("Syntax error: "err)
return -1}
$0=""
for(i=NF=0;i in res;i++){
$(++NF)=res[i]
Quotes[NF]=res[i,"quote"]}
for(i=2;i<=NF;i++)
if("\""==Quotes[i]&&!("@define"==$1&&3==i||"@depends_on"==$1&&"@args"==$3&&i>3)){
addError("Wrong quoting: " $i)
addError("Wrong quoting: "$i)
return -1}
return 0}
function quote2(s,force){
if(index(s,"'\''")){
gsub(/\\/,"\\\\",s)
gsub(/'\''/,"\\'\''",s)
return"$'\''" s"'\''"
return"$'\''"s"'\''"
}else
return force||s~/[^a-zA-Z0-9.,@_\/=+-]/?"'\''" s"'\''":s}
return force||s~/[^a-zA-Z0-9.,@_\/=+-]/?"'\''"s"'\''":s}
function addLine(target,line){target[0]=addL(target[0],line)}
function addL(s,l){return s?s"\n" l:l}
function addL(s,l){return s?s"\n"l:l}
function arrPush(arr,elt){arr[arr[-7]++]=elt}
function arrLen(arr){return +arr[-7]}
function arrDel(arr,v,l,i,e,resArr){
Expand All @@ -650,10 +650,10 @@ function arrDel(arr,v,l,i,e,resArr){
for(i in resArr)
arr[i]=resArr[i]}
function arrLast(arr,l){return (l=arrLen(arr))>0?arr[l-1]:""}
function commandExists(cmd){return ok("command -v " cmd" >/dev/null")}
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))}
function quoteArg(a){gsub("'\''","'\''\\'\'''\''",a);return"'\''" a"'\''"}
function isFile(path){return ok("test -f "quoteArg(path))}
function rm(f){system("rm "quoteArg(f))}
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]}' Makesurefile "$@"
Loading

0 comments on commit 36689f1

Please sign in to comment.