From 1c8ef992f18de71ea3f78143f6c6bc526e163341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 24 Sep 2019 16:31:50 +0200 Subject: [PATCH 1/6] plugin loading bug (globs) --- build/usr/local/bin/autodoc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/build/usr/local/bin/autodoc b/build/usr/local/bin/autodoc index 6c92403..03b9c00 100755 --- a/build/usr/local/bin/autodoc +++ b/build/usr/local/bin/autodoc @@ -15,9 +15,12 @@ declare -A g_build_systems_glob source "${g_lib}/globals" source "${g_lib}/logging" -source "${g_lib}/plugins/"*".sh" source "${g_lib}/handle_inotify" +for plugin in "${g_lib}/plugins/"*".sh"; do + source "${plugin}" +done + # # MAIN # From 078cafdc3916b712482ee710df49d6b172b65b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 18 Feb 2020 06:05:14 +0100 Subject: [PATCH 2/6] move build folder --- docker-compose.yml | 6 +++--- {build => src}/Dockerfile | 0 {build => src}/usr/local/bin/autodoc | 0 {build => src}/usr/local/lib/autodoc/globals | 0 {build => src}/usr/local/lib/autodoc/handle_inotify | 0 {build => src}/usr/local/lib/autodoc/logging | 0 {build => src}/usr/local/lib/autodoc/plugins/make.sh | 0 7 files changed, 3 insertions(+), 3 deletions(-) rename {build => src}/Dockerfile (100%) rename {build => src}/usr/local/bin/autodoc (100%) rename {build => src}/usr/local/lib/autodoc/globals (100%) rename {build => src}/usr/local/lib/autodoc/handle_inotify (100%) rename {build => src}/usr/local/lib/autodoc/logging (100%) rename {build => src}/usr/local/lib/autodoc/plugins/make.sh (100%) diff --git a/docker-compose.yml b/docker-compose.yml index e51149f..cad3529 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,11 +9,11 @@ services: image: ldericher/autodoc build: - context: ./build + context: ./src command: "bash" volumes: - - "${PWD}/build/usr/local/bin/autodoc:/usr/local/bin/autodoc:ro" - - "${PWD}/build/usr/local/lib/autodoc:/usr/local/lib/autodoc:ro" + - "${PWD}/src/usr/local/bin/autodoc:/usr/local/bin/autodoc:ro" + - "${PWD}/src/usr/local/lib/autodoc:/usr/local/lib/autodoc:ro" - "${PWD}/examples:/docs" diff --git a/build/Dockerfile b/src/Dockerfile similarity index 100% rename from build/Dockerfile rename to src/Dockerfile diff --git a/build/usr/local/bin/autodoc b/src/usr/local/bin/autodoc similarity index 100% rename from build/usr/local/bin/autodoc rename to src/usr/local/bin/autodoc diff --git a/build/usr/local/lib/autodoc/globals b/src/usr/local/lib/autodoc/globals similarity index 100% rename from build/usr/local/lib/autodoc/globals rename to src/usr/local/lib/autodoc/globals diff --git a/build/usr/local/lib/autodoc/handle_inotify b/src/usr/local/lib/autodoc/handle_inotify similarity index 100% rename from build/usr/local/lib/autodoc/handle_inotify rename to src/usr/local/lib/autodoc/handle_inotify diff --git a/build/usr/local/lib/autodoc/logging b/src/usr/local/lib/autodoc/logging similarity index 100% rename from build/usr/local/lib/autodoc/logging rename to src/usr/local/lib/autodoc/logging diff --git a/build/usr/local/lib/autodoc/plugins/make.sh b/src/usr/local/lib/autodoc/plugins/make.sh similarity index 100% rename from build/usr/local/lib/autodoc/plugins/make.sh rename to src/usr/local/lib/autodoc/plugins/make.sh From 76aa31c0c645e5db8c0f1f731ee789e4dafc5e50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 18 Feb 2020 07:58:11 +0100 Subject: [PATCH 3/6] Add "do_${build_system}_all" and "do_build_all" --- src/usr/local/bin/autodoc | 11 ++-- src/usr/local/lib/autodoc/globals | 11 ++++ src/usr/local/lib/autodoc/handle_inotify | 77 ++++++++++++++++++++--- src/usr/local/lib/autodoc/logging | 2 + src/usr/local/lib/autodoc/plugins/make.sh | 62 +++++++++++------- 5 files changed, 127 insertions(+), 36 deletions(-) diff --git a/src/usr/local/bin/autodoc b/src/usr/local/bin/autodoc index 03b9c00..4da2f9a 100755 --- a/src/usr/local/bin/autodoc +++ b/src/usr/local/bin/autodoc @@ -1,13 +1,13 @@ #!/bin/bash +echo -n "Loading '${0}': " + # # hard globals # g_bin="$(readlink -f "$(which "$0")")" g_lib=${g_bin/"bin"/"lib"} -declare -a g_build_systems -declare -A g_build_systems_glob # # load base program @@ -16,16 +16,19 @@ declare -A g_build_systems_glob source "${g_lib}/globals" source "${g_lib}/logging" source "${g_lib}/handle_inotify" - for plugin in "${g_lib}/plugins/"*".sh"; do source "${plugin}" done +echo "program parsed." + # # MAIN # +echo "Building everything in '${g_watchroot}'." +do_build_all -echo "Booting '${g_bin}' in '${g_watchroot}'." +echo "Watching '${g_watchroot}'." # setup inotify: # -mrq monitor, recursive, quiet # -e events diff --git a/src/usr/local/lib/autodoc/globals b/src/usr/local/lib/autodoc/globals index bbe9c83..5bea44e 100644 --- a/src/usr/local/lib/autodoc/globals +++ b/src/usr/local/lib/autodoc/globals @@ -1,4 +1,15 @@ #!/bin/bash +# array of available build systems +declare -a g_build_systems + +# map of file globs for build systems +declare -A g_build_systems_glob + +# map of annotation patterns for build systems +declare -A g_build_systems_annotations + # $WATCHROOT (default: ".") g_watchroot="$(readlink -f "${1:-.}")" + +echo -n "globals ... " \ No newline at end of file diff --git a/src/usr/local/lib/autodoc/handle_inotify b/src/usr/local/lib/autodoc/handle_inotify index 63e7f94..bdfc845 100644 --- a/src/usr/local/lib/autodoc/handle_inotify +++ b/src/usr/local/lib/autodoc/handle_inotify @@ -29,7 +29,7 @@ do_compile() { # $DIR $OBJECT $DONE # build systems for build_system in ${g_build_systems[@]}; do - do_build_system "${dir}" "${object}" "${build_system}" \ + do_build_system "${dir}" "${build_system}" "${object}" \ && local done="1" done @@ -44,25 +44,59 @@ do_compile() { # $DIR $OBJECT $DONE fi } -# use given BUILD_SYSTEM to process an OBJECT from a DIRectory -do_build_system() { # $DIR $OBJECT $BUILD_SYSTEM +# check if defined source pattern matches OBJECT +do_check_srcpat() { # $DIR $BUILD_DESC $ANNOTATION $OBJECT # extract params local dir="$1" - local object="$2" - local build_system="$3" + local build_desc="$2" + local annotation="$3" + local object="$4" + + # check 'source pattern' + local srcpat="$(grep -E "^${annotation}" "${dir}/${build_desc}" | tail -n 1 | sed -r "s/^${annotation}\s+//")" + + if [ -z "${srcpat}" ]; then + # empty srcpat => fail + logline_append "Empty source pattern, check for '${annotation}' annotation!" + return 1 + + elif [ -z "${object}" ]; then + # empty object = "no specific object" => success + return 0 + + elif [[ "${object}" =~ ${srcpat} ]]; then + # nonempty object matches srcpat => success + return 0 + + else + # nonempty object does not match srcpat => fail + logline_append "SRCPAT '${srcpat}' mismatch." + return 1 + + fi +} + +# use given BUILD_SYSTEM to process an OBJECT from a DIRectory +do_build_system() { # $DIR $BUILD_SYSTEM $OBJECT + # extract params + local dir="$1" + local build_system="$2" + local object="$3" # not done yet local result=1 - # get glob pattern for plugin + # get glob patterns for plugin for glob in ${g_build_systems_glob[${build_system}]}; do - # match glob in directory + # match each glob in directory for file in "${dir}"/${glob}; do # check file readability if [ -r "${file}" ]; then # actually call into build system logline_append "Found '${file}':" - do_${build_system} "${dir}" "${object}" "$(basename "${file}")" \ + + local file="$(basename "${file}")" + do_${build_system} "${dir}" "${file}" "${object}" \ && local result=0 fi done @@ -70,3 +104,30 @@ do_build_system() { # $DIR $OBJECT $BUILD_SYSTEM return ${result} } + +# force build using all systems +do_build_all() { # + # build systems + for build_system in ${g_build_systems[@]}; do + echo "Build system '${build_system}'." + for glob in ${g_build_systems_glob[${build_system}]}; do + + # match each glob recursively + find "${g_watchroot}" -iname "${glob}" | \ + while read file; do + if [ -r "${file}" ]; then + # force call into build system + logline_append "Found '${file}':" + + local dir="$(dirname "${file}")" + local file="$(basename "${file}")" + do_${build_system}_all "${dir}" "$(basename "${file}")" + + logline_flush + fi + done + done + done +} + +echo -n "main ... " diff --git a/src/usr/local/lib/autodoc/logging b/src/usr/local/lib/autodoc/logging index 802d576..8d2a632 100644 --- a/src/usr/local/lib/autodoc/logging +++ b/src/usr/local/lib/autodoc/logging @@ -10,3 +10,5 @@ logline_flush() { echo "${g_logline}" g_logline="" } + +echo -n "logging ... " diff --git a/src/usr/local/lib/autodoc/plugins/make.sh b/src/usr/local/lib/autodoc/plugins/make.sh index 6ee1ef8..e4ecab1 100644 --- a/src/usr/local/lib/autodoc/plugins/make.sh +++ b/src/usr/local/lib/autodoc/plugins/make.sh @@ -6,40 +6,54 @@ g_build_systems+=(make) # build instruction file globs for this plugin g_build_systems_glob[make]="Makefile *.mk" -# compile using bare make command +# srcpat annotation prefix for this plugin +g_build_systems_annotations[make]='#%SRCPAT%' + +# try to compile file OBJECT do_make() { # $DIR $OBJECT $MAKEFILE # extract params local dir="$1" - local object="$2" - local makefile="$3" + local makefile="$2" + local object="$3" - # check Makefile 'source pattern' - local srcpat="$(grep -E "^#%SRCPAT%" "${dir}/${makefile}" | tail -n 1 | sed -r "s/^#%SRCPAT%\s+//")" + # only run if "object" is source file + if do_check_srcpat "${dir}" "${makefile}" "${g_build_systems_annotations[make]}" "${object}"; then + do_run_make "${dir}" "${makefile}" + fi +} - if [ -z "${srcpat}" ]; then - logline_append "Empty source pattern, check '#%SRCPAT%' annotation!" - return 1 +# try running make for MAKEFILE inside DIRectory +do_make_all() { # $DIR $MAKEFILE + # extract params + local dir="$1" + local makefile="$2" - elif [[ "${object}" =~ ${srcpat} ]]; then - # check for autodoc target - local target="$(grep -E "^autodoc:" "${dir}/${makefile}" | sed -r "s/:.*$//")" + # only run if "makefile" is relevant for autodoc + if do_check_srcpat "${dir}" "${makefile}" "${g_build_systems_annotations[make]}" ""; then + do_run_make "${dir}" "${makefile}" + fi +} - if [ -z "${target}" ]; then - logline_append "Running 'make'!" - else - logline_append "Making '${target}'!" - fi +# actually run GNU Make with MAKEFILE inside DIRectory +do_run_make() { # $DIR $MAKEFILE + # extract params + local dir="$1" + local makefile="$2" - # actually run "make" and save (truncated) output - local makelog="$(make --no-print-directory -C "${dir}" -f "${makefile}" -j ${target})" - logline_append "$(echo "${makelog}" | head -n 10 | sed 's/$/;/g' | tr '\n' ' ' | sed 's/ *$//')" - - logline_append "Done." + # check for autodoc target + local target="$(grep -E "^autodoc:" "${dir}/${makefile}" | sed -r "s/:.*$//")" + if [ -z "${target}" ]; then + logline_append "Running 'make'!" else - logline_append "SRCPAT '${srcpat}' mismatch." - return 1 + logline_append "Making '${target}'!" fi - return 0 + # actually run "make" and save (truncated) output + local makelog="$(make --no-print-directory -C "${dir}" -f "${makefile}" -j ${target})" + logline_append "$(echo "${makelog}" | head -n 10 | sed 's/$/;/g' | tr '\n' ' ' | sed 's/ *$//')" + + logline_append "Done." } + +echo -n "GNU Make plugin ... " From 0022106d97d31b1c61e94e7eddf6cf0e50f104b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 18 Feb 2020 08:00:43 +0100 Subject: [PATCH 4/6] Add "do_build_watch" --- src/usr/local/bin/autodoc | 16 +++------------- .../local/lib/autodoc/{handle_inotify => main} | 17 ++++++++++++++++- 2 files changed, 19 insertions(+), 14 deletions(-) rename src/usr/local/lib/autodoc/{handle_inotify => main} (89%) diff --git a/src/usr/local/bin/autodoc b/src/usr/local/bin/autodoc index 4da2f9a..6699d98 100755 --- a/src/usr/local/bin/autodoc +++ b/src/usr/local/bin/autodoc @@ -15,7 +15,8 @@ g_lib=${g_bin/"bin"/"lib"} source "${g_lib}/globals" source "${g_lib}/logging" -source "${g_lib}/handle_inotify" +source "${g_lib}/main" + for plugin in "${g_lib}/plugins/"*".sh"; do source "${plugin}" done @@ -29,15 +30,4 @@ echo "Building everything in '${g_watchroot}'." do_build_all echo "Watching '${g_watchroot}'." -# setup inotify: -# -mrq monitor, recursive, quiet -# -e events -# --format %e eventlist csv, %w workdir, %f filename -inotifywait -mrq \ - -e create -e delete -e moved_to -e close_write \ - --format '%e %w%f' \ - "${g_watchroot}" | \ -\ -while read NOTIFICATION; do - do_handle ${NOTIFICATION} -done +do_build_watch diff --git a/src/usr/local/lib/autodoc/handle_inotify b/src/usr/local/lib/autodoc/main similarity index 89% rename from src/usr/local/lib/autodoc/handle_inotify rename to src/usr/local/lib/autodoc/main index bdfc845..d2ffd23 100644 --- a/src/usr/local/lib/autodoc/handle_inotify +++ b/src/usr/local/lib/autodoc/main @@ -1,7 +1,7 @@ #!/bin/bash # process an inotify event -do_handle() { # $FLAGS $OBJECT +do_handle_inotify() { # $FLAGS $OBJECT # extract params local flags="$1" shift 1 @@ -130,4 +130,19 @@ do_build_all() { # done } +do_build_watch() { # + # setup inotify: + # -mrq monitor, recursive, quiet + # -e events + # --format %e eventlist csv, %w workdir, %f filename + inotifywait -mrq \ + -e create -e delete -e moved_to -e close_write \ + --format '%e %w%f' \ + "${g_watchroot}" | \ + \ + while read NOTIFICATION; do + do_handle_inotify ${NOTIFICATION} + done +} + echo -n "main ... " From 661e94aa13ec8a0106fd166c200f126ac538ff04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 18 Feb 2020 09:19:17 +0100 Subject: [PATCH 5/6] better CLI options --- src/usr/local/bin/autodoc | 28 +++++++---- src/usr/local/lib/autodoc/globals | 58 +++++++++++++++++++++-- src/usr/local/lib/autodoc/logging | 2 - src/usr/local/lib/autodoc/main | 12 ++--- src/usr/local/lib/autodoc/plugins/make.sh | 4 +- 5 files changed, 81 insertions(+), 23 deletions(-) diff --git a/src/usr/local/bin/autodoc b/src/usr/local/bin/autodoc index 6699d98..3be9df9 100755 --- a/src/usr/local/bin/autodoc +++ b/src/usr/local/bin/autodoc @@ -1,7 +1,5 @@ #!/bin/bash -echo -n "Loading '${0}': " - # # hard globals # @@ -21,13 +19,27 @@ for plugin in "${g_lib}/plugins/"*".sh"; do source "${plugin}" done -echo "program parsed." - # # MAIN # -echo "Building everything in '${g_watchroot}'." -do_build_all -echo "Watching '${g_watchroot}'." -do_build_watch +# show debug info +if [ ${g_verbose} -eq 1 ]; then + logline_append "Variables:" + logline_append "build:${g_build}" + logline_append "watch:${g_watch}" + logline_append "root:${g_root}" + logline_flush +fi + +if [ ${g_build} -eq 1 ]; then + echo "Building everything in '${g_root}'." + do_build_all +fi + +if [ ${g_watch} -eq 1 ]; then + echo "Watching '${g_root}'." + do_build_watch +fi + +echo "Done." diff --git a/src/usr/local/lib/autodoc/globals b/src/usr/local/lib/autodoc/globals index 5bea44e..595aba1 100644 --- a/src/usr/local/lib/autodoc/globals +++ b/src/usr/local/lib/autodoc/globals @@ -9,7 +9,59 @@ declare -A g_build_systems_glob # map of annotation patterns for build systems declare -A g_build_systems_annotations -# $WATCHROOT (default: ".") -g_watchroot="$(readlink -f "${1:-.}")" +# simple help page +do_show_help() { + echo "Usage: ${0} [-h?bwv] [-r ROOT] [ROOT]" + echo + echo "Options:" + echo " -h, -? Show this help and exit" + echo " -b Build ROOT directory on startup" + echo " -w Keep watching ROOT directory for changes" + echo " -v Verbose output" + echo " -r ROOT Set ROOT directory" + echo + echo "ROOT directory can be set via '-r' argument or positionally." + echo "If ROOT directory is undefined, it defaults to the current working directory." + exit 0 +} -echo -n "globals ... " \ No newline at end of file +# reset in case getopts has been used previously in the shell +OPTIND=1 + +# initialize variables +g_build=0 +g_watch=0 +g_root="" + +while getopts "h?bwvr:" opt; do + case "$opt" in + h|\?) + do_show_help + ;; + b) + g_build=1 + ;; + w) + g_watch=1 + ;; + v) + g_verbose=1 + ;; + r) + g_root="${OPTARG}" + ;; + esac +done + +# default to help +[ ${g_build} -eq 0 ] && [ ${g_watch} -eq 0 ] && [ ${g_verbose} -eq 0 ] && do_show_help + +# shift off getopts parsed options +shift $((OPTIND-1)) +[ "${1:-}" = "--" ] && shift + +# if g_root undefined by getopt, +[ -z "${g_root:-}" ] && g_root="$1" + +# get actual $ROOT directory (default: ".") +g_root="$(readlink -f "${g_root:-.}")" \ No newline at end of file diff --git a/src/usr/local/lib/autodoc/logging b/src/usr/local/lib/autodoc/logging index 8d2a632..802d576 100644 --- a/src/usr/local/lib/autodoc/logging +++ b/src/usr/local/lib/autodoc/logging @@ -10,5 +10,3 @@ logline_flush() { echo "${g_logline}" g_logline="" } - -echo -n "logging ... " diff --git a/src/usr/local/lib/autodoc/main b/src/usr/local/lib/autodoc/main index d2ffd23..d9b874b 100644 --- a/src/usr/local/lib/autodoc/main +++ b/src/usr/local/lib/autodoc/main @@ -33,13 +33,13 @@ do_compile() { # $DIR $OBJECT $DONE && local done="1" done - # never leave $g_watchroot - if [ "${dir}" != "${g_watchroot}" ]; then + # never leave $g_root + if [ "${dir}" != "${g_root}" ]; then # search parent dir for more build instructions do_compile "$(dirname "${dir}")" "${object}" "${done}" elif [ "${done}" == "0" ]; then - # hit $g_watchroot + # hit $g_root logline_append "Not a source file." fi } @@ -113,7 +113,7 @@ do_build_all() { # for glob in ${g_build_systems_glob[${build_system}]}; do # match each glob recursively - find "${g_watchroot}" -iname "${glob}" | \ + find "${g_root}" -iname "${glob}" | \ while read file; do if [ -r "${file}" ]; then # force call into build system @@ -138,11 +138,9 @@ do_build_watch() { # inotifywait -mrq \ -e create -e delete -e moved_to -e close_write \ --format '%e %w%f' \ - "${g_watchroot}" | \ + "${g_root}" | \ \ while read NOTIFICATION; do do_handle_inotify ${NOTIFICATION} done } - -echo -n "main ... " diff --git a/src/usr/local/lib/autodoc/plugins/make.sh b/src/usr/local/lib/autodoc/plugins/make.sh index e4ecab1..d836faa 100644 --- a/src/usr/local/lib/autodoc/plugins/make.sh +++ b/src/usr/local/lib/autodoc/plugins/make.sh @@ -54,6 +54,4 @@ do_run_make() { # $DIR $MAKEFILE logline_append "$(echo "${makelog}" | head -n 10 | sed 's/$/;/g' | tr '\n' ' ' | sed 's/ *$//')" logline_append "Done." -} - -echo -n "GNU Make plugin ... " +} \ No newline at end of file From 56db54e11a52a96f98662fbc435736cc0c435934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Tue, 18 Feb 2020 10:42:11 +0100 Subject: [PATCH 6/6] autodoc default parameters --- src/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Dockerfile b/src/Dockerfile index c014a7a..c74c5d8 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -6,4 +6,4 @@ RUN apt-get update && apt-get -y install \ COPY usr /usr -CMD ["autodoc"] +CMD ["autodoc", "-bw"]