diff --git a/build.sh b/build.sh index 0250436..675bab9 100755 --- a/build.sh +++ b/build.sh @@ -1,23 +1,8 @@ #!/usr/bin/env bash -build::rdlink() { - readonly build_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" - readonly rdlinkRelease="${build_dir}/release/rdlink.sh" - readonly color_green='\033[1;32m' - readonly color_red='\033[1;31m' - readonly color_less='\033[0m' - readonly shellcheck_cmd="$(command -v shellcheck)" - readonly fileHeader="#!/usr/bin/env bash - -# rdlink [OPTIONS] -# [OPTION] -# -- : End of options marker -# -* : Other options are ignored -# -# Version: $(git describe --long --tags --always --dirty) -# Author: Martin Winkler -# Origin: https://winklerfamilie.eu/git/efelon/rdlink.git -# +build::addLicense() { + local target="${1:?}" + local licenseText="# # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or @@ -32,51 +17,111 @@ build::rdlink() { # along with this program. If not, see . " + printf '%s' "${licenseText}" >> "${target}" +} + +build::headerRdlink() { + local target="${1:?}" + local fileHeader="#!/usr/bin/env bash + +# rdlink.sh [OPTIONS] +# [OPTION] +# -- : End of options marker +# -* : Other options are ignored +# +# Version: $(git describe --long --tags --always --dirty) +# Author: Martin Winkler +# Origin: https://winklerfamilie.eu/git/efelon/rdlink.git +" + printf '%s' "${fileHeader}" > "${target}" + build::addLicense "${target}" +} + +build::headerStarter() { + local target="${1:?}" + local fileHeader="#!/usr/bin/env bash + +# starter.sh +# +# Start or source your application at the end to have the +# environment variable \$_origin available. +# +# Version: $(git describe --long --tags --always --dirty) +# Author: Martin Winkler +# Origin: https://winklerfamilie.eu/git/efelon/rdlink.git +" + printf '%s' "${fileHeader}" > "${target}" + build::addLicense "${target}" +} + +build::rdlink() { + readonly build_dir="$(cd "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" + readonly rdlinkRelease="${build_dir}/release/rdlink.sh" + readonly starterRelease="${build_dir}/release/starter.sh" + readonly color_green='\033[1;32m' + readonly color_red='\033[1;31m' + readonly color_less='\033[0m' + readonly shellcheck_cmd="$(command -v shellcheck)" local testresult_graphic= testresult_graphic="${color_green}✔${color_less}" # alt. symbol ✓ testresult_graphic_fail="${color_red}✗${color_less}" # alt. symbol ≠ if ! mkdir -p "$(dirname -- "${rdlinkRelease}")" ; then - printf " [ERROR] Cannot create release directory\n" + printf ' [ERROR] Cannot create release directory\n' return 1 fi - printf "# Building rdlink release\n" + printf '# Building rdlink release\n' - # Write header - printf "%s" "${fileHeader}" > "${rdlinkRelease}" - - printf " Extracting script part and removing debug information..." + build::headerRdlink "${rdlinkRelease}" + printf ' Extracting script part and removing debug information...' # Get essential script content # * extract script part # * remove script marker # * remove debug function calls # * remove debug options from rdlink # * replace multiple empty lines with one - #if cat "${build_dir}/rdlink.sh" \ - # | sed -n '/### Script.*/,/### Script EOF/ p' \ - # | sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \ - # | sed '/^$/N; /^\n$/D' \ - # >> "${rdlinkRelease}" 2>/dev/null ; then if sed -n '/### Script.*/,/### Script EOF/ p' < "${build_dir}/rdlink.sh" \ | sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \ | sed '/^$/N; /^\n$/D' \ >> "${rdlinkRelease}" 2>/dev/null ; then - printf "%b" "${testresult_graphic}\n" + printf '%b' "${testresult_graphic}\n" else - printf "%b" "${testresult_graphic_fail}\n" + printf '%b' "${testresult_graphic_fail}\n" return 1 fi + { + build::headerStarter "${starterRelease}" + printf ' Building starter...' + if sed -n '/### Script.*/,/### Script Starter EOF/ p' < "${build_dir}/rdlink.sh" \ + | sed '/### Script.*/d; /^[[:blank:]]*info/d; /-d\{1,2\}/,/;;/d' \ + | sed '/^$/N; /^\n$/D' \ + >> "${starterRelease}" 2>/dev/null ; then + printf '%b' "${testresult_graphic}\n" + else + printf '%b' "${testresult_graphic_fail}\n" + return 1 + fi + + printf '_origin="$(cd "$(dirname -- "$(rl::rdlink "${BASH_SOURCE[0]}")")" >/dev/null 2>&1 && pwd)"\n' >> "${starterRelease}" + printf 'export _origin\n\n' >> "${starterRelease}" + + printf '# Uncomment one of the next lines and replace "tostart.sh" with your script\n' >> "${starterRelease}" + printf '# to have _origin available even on platform not supporting readlink -f.\n\n' >> "${starterRelease}" + printf '#bash "${_origin}/tostart.sh"\n' >> "${starterRelease}" + printf '#. "${_origin}/tostart.sh"\n' >> "${starterRelease}" + printf '#"${_origin}/tostart.sh"\n' >> "${starterRelease}" + } + if [[ "${shellcheck_cmd}" ]] ; then - printf "%b\n" " Running shellcheck...\033[1A\033[s\n" + printf '%b\n' " Running shellcheck...\033[1A\033[s\n" if "${shellcheck_cmd}" "${rdlinkRelease}" ; then - printf "%b" "\033[u${testresult_graphic}\n" + printf '%b' "\033[u${testresult_graphic}\n" else return 1 fi fi - chmod +x "${rdlinkRelease}" return 0 }