Skip to content

Commit

Permalink
Revamp @define #140 : adjust minify.awk
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Sep 24, 2023
1 parent 022feed commit 507071c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
40 changes: 20 additions & 20 deletions makesure_candidate
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ function prepareArgs(i,arg){
selfUpdate()
realExit(0)}
if(!isFile(ARGV[1])){
if(isFile(ARGV[1] "/Makesurefile"))
ARGV[1]=ARGV[1] "/Makesurefile"
if(isFile(ARGV[1]"/Makesurefile"))
ARGV[1]=ARGV[1]"/Makesurefile"
else
die("makesure file not found: " ARGV[1])}
if("-s" in Args||"--silent" in Args)
Expand All @@ -83,7 +83,7 @@ function prepareArgs(i,arg){
if("-t" in Args||"--timing" in Args)
Options["timing"]}
function splitKV(arg,kv,n){
n=index(arg, "=")
n=index(arg,"=")
kv[0]=trim(substr(arg,1,n-1))
kv[1]=trim(substr(arg,n+1))}
function handleOptionDefineOverride(arg,kv){
Expand Down Expand Up @@ -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 @@ -307,7 +307,7 @@ body,goalBody,goalBodies,resolvedGoals,exitCode,t0,t1,t2,goalTimed,list){
if("-d" in Args||"--resolved" in Args){
printf "Resolved goals to reach for"
for(i=0;i in ArgGoals;i++)
printf " %s", quote2(ArgGoals[i],1)
printf " %s",quote2(ArgGoals[i],1)
print ":"
for(i=0;i in resolvedGoals;i++)
if(!reachedGoals[goalName=resolvedGoals[i]]&&!emptyGoals[goalName])
Expand All @@ -325,11 +325,11 @@ body,goalBody,goalBodies,resolvedGoals,exitCode,t0,t1,t2,goalTimed,list){
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 @@ -339,7 +339,7 @@ function topologicalSort(includeReachedIf,requestedGoals,result,reachedGoals,i,j
for(j=0;j<depCnt;j++)
topologicalSortAddConnection(goalName,Dependencies[goalName,j])}
if(arrLen(requestedGoals)==0)
arrPush(requestedGoals, "default")
arrPush(requestedGoals,"default")
for(i=0;i in requestedGoals;i++){
if(!((goalName=requestedGoals[i])in GoalsByName))
die("Goal not found: " goalName)
Expand All @@ -354,7 +354,7 @@ 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 @@ -366,7 +366,7 @@ function checkConditionReached(goalName,conditionStr,script){
if(goalName in GoalToLib)
script=script "\n" Lib[GoalToLib[goalName]]
script=script "\n" conditionStr
return shellExec(script, goalName "@reached_if")==0}
return shellExec(script,goalName "@reached_if")==0}
function shellExec(script,comment,res){
if("tracing" in Options){
script=": " quoteArg(comment)"\n" script
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]}
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,7 +460,7 @@ 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])):\
depArg in Vars?Vars[depArg]:addErrorDedup("wrong arg '\''" depArg "'\''",DependenciesLineNo[gi])):\
die("wrong depArgType: " depArgType)}
gii=goalNameInstantiated SUBSEP i
Dependencies[gii]=instantiate(dep,newArgs)
Expand All @@ -477,7 +477,7 @@ function currentTimeMillis(res){
if(Gawk)
return int(gettimeofday()*1000)
res=executeGetLine("date +%s%3N")
sub(/%?3N/, "000", res)
sub(/%?3N/,"000",res)
return +res}
function selfUpdate(url,tmp,err,newVer){
url="https://raw.githubusercontent.com/xonixx/makesure/main/makesure?token=" rand()
Expand Down Expand Up @@ -634,7 +634,7 @@ function quote2(s,force){
}else
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 @@ -645,11 +645,11 @@ function arrDel(arr,v,l,i,e,resArr){
delete arr
for(i in resArr)
arr[i]=resArr[i]}
function arrLast(arr, l){return (l=arrLen(arr))>0?arr[l - 1]:""}
function arrLast(arr,l){return (l=arrLen(arr))>0?arr[l-1]:""}
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 trim(s){sub(/^[ \t\r\n]+/, "", s);sub(/[ \t\r\n]+$/, "", s);return s}
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 "$@"
22 changes: 17 additions & 5 deletions minify.awk
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ function trim(s) { sub(/^[ \t\r\n]+/, "", s); sub(/[ \t\r\n]+$/, "", s); return
in_begin && /^}/ { in_begin = 0 }
in_begin && $1 ~ /^delete/{ next }
{ if (!/"#"/ && !/\*#\// && !/\*\(#/) gsub("[ \t\r\n]*#.*$", "")
notAString = !/"/
gsub(/ == /, "==")
gsub(/ = /, "=")
gsub(/ != /, "!=")
gsub(/ >= /, ">=")
gsub(/ <= /, "<=")
gsub(/; +/, ";")
if (notAString) gsub(/, +/, ",") # don't change strings
gsubKeepStrings(", +", ",")
gsub(/ ~ /, "~")
gsub(/ > /, ">")
gsub(/ < /, "<")
gsub(/ \/ /, "/")
gsub(/ \* /, "*")
gsub(/ \+ /, "+")
if (notAString) gsub(/ - /, "-")
gsubKeepStrings(" - ", "-")
gsub(/ \|\| /, "||")
gsub(/ \| /, "|")
if (/ \? /) gsub(/ : /, ":")
Expand All @@ -32,9 +31,22 @@ in_begin && $1 ~ /^delete/{ next }
gsub(/[{] +/, "{")
gsub(/} +/, "}")
gsub(/[)] +/, ")")
if (notAString) gsub(/] +/, "]")
gsubKeepStrings("] +", "]")
if (!/^ +}/) gsub(/ +}/, "}")
##gsub(/" in/, "\"in")
gsub(Q, Q "\\" Q Q)
if (l = trim($0)) { printf "%s", (l == "}" ? l : (NR == 1 ? "" : "\n") $0) }
}
}
function gsubKeepStrings(regex, replacement, cnt,parts,i,s) {
if ((cnt = split($0,parts,"\"")) == 1) {
gsub(regex,replacement)
return
}
gsub(regex,replacement,parts[1])
gsub(regex,replacement,parts[cnt])
for (i = 1; i < cnt; i++)
s = s parts[i] "\""
s = s parts[cnt]
$0 = s
}
#BEGIN { $0 = "aaa\"aaa\"bbb"; gsubKeepStrings("aaa","AAA"); print }

0 comments on commit 507071c

Please sign in to comment.