3 ms·
https://github.com/DaveJarvis/keenwrite/blob/main/scripts/build-template https://github.com/DaveJarvis/keenwrite/blob/main/scripts/bu... My template script pro
by thangalin 3y ago
https://github.com/DaveJarvis/keenwrite/blob/main/scripts/build-template https://github.com/DaveJarvis/keenwrite/blob/main/scripts/bu...
My template script provides a way to make user-friendly shell scripts. In a script that uses the template, you define the dependencies and their sources as comma-separated values:
DEPENDENCIES=(
"gradle,https://gradle.org"
"warp-packer,https://github.com/Reisz/warp/releases"
"tar,https://www.gnu.org/software/tar"
"wine,https://www.winehq.org"
"unzip,http://infozip.sourceforge.net"
)
You define the command-line arguments:
ARGUMENTS+=(
"a,arch,Target operating system architecture (amd64)"
"o,os,Target operating system (linux, windows, mac)"
"u,update,Java update version number (${ARG_JAVA_UPDATE})"
"v,version,Full Java version (${ARG_JAVA_VERSION})"
)
You define the "execute()" method that is called after the arguments are parsed:
execute() {
// Make the computer do the work.
return 1
}
If the script takes arguments, handle each one individually:
argument() {
local consume=2
case "$1" in
-a|--arch)
ARG_JAVA_ARCH="$2"
;;
-o|--os)
ARG_JAVA_OS="$2"
;;
esac
return ${consume}
}
Then call the template's main to start the script rolling:
main "$@"
For 99% of the scripts I write, this provides:
* Built-in software dependencies verification.
* Instructions to the user when requirements are missing.
* Simple command-line argument parsing.
* Help and logging using ANSI colour.
Here's a complete script that builds the Windows, Linux, and Mac installers for my Markdown editor:
https://github.com/DaveJarvis/KeenWrite/blob/main/installer.sh https://github.com/DaveJarvis/KeenWrite/blob/main/installer....
There's a write-up about creating the script that has a lot more details about how the template works:
https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdown-part-1/ https://dave.autonoma.ca/blog/2019/05/22/typesetting-markdow...
Note that it is technically possible to improve the scripts such that handling individual arguments can be done in the template itself. This would require a slightly different argument definition semantics:
ARGUMENTS+=(
"ARG_JAVA_ARCH,a,arch,Target operating system architecture (amd64)"
"ARG_JAVA_OS,o,os,Target operating system (linux, windows, mac)"
"usage=utile_usage,h,help,Show this help message then exit"
)
By detecting an `=` symbol for the first item in the lists, it's possible to know whether a command-line argument is assigning a variable value, or whether it means to perform additional functionality. (PR welcome!)