diff --git a/build/Dockerfile b/build/Dockerfile index 48b8413..c014a7a 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -4,7 +4,6 @@ RUN apt-get update && apt-get -y install \ inotify-tools \ && rm -rf /var/lib/apt/lists/* -COPY autodoc /usr/local/bin/autodoc -RUN chmod +x /usr/local/bin/autodoc +COPY usr /usr CMD ["autodoc"] diff --git a/build/autodoc b/build/autodoc deleted file mode 100755 index e199e5a..0000000 --- a/build/autodoc +++ /dev/null @@ -1,102 +0,0 @@ -#!/bin/bash - -# $1:WATCHROOT (default: ".") -g_watchroot="$(readlink -f "${1:-.}")" - -# compile using bare make command -do_make() { # $1:DIR $2:MAKEFILE $3:OBJECT - # extract params - local dir="$1" - 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+//")" - - if [ -z "${srcpat}" ]; then - echo -n "Empty source pattern, check '#%SRCPAT%' annotation! " - return 1 - - elif [[ "${object}" =~ ${srcpat} ]]; then - # check for autodoc target - local target="$(grep -E "^autodoc:" "${dir}/${makefile}" | sed -r "s/:.*$//")" - - if [ -z "${target}" ]; then - echo "Running 'make'. " - else - echo "Making '${target}'. " - fi - - make --no-print-directory -C "${dir}" -j ${target} - - else - echo -n "SRCPAT '${srcpat}' mismatch. " - return 1 - fi - - return 0 -} - -# compile a directory -do_compile() { # $1:DIR $2:OBJECT $3:DONE - # extract params - local dir="$1" - local object="$2" - local done="${3:-0}" - - # build systems - - if [ -r "${dir}/Makefile" ]; then - # Makefile found - echo -n "Found '${dir}/Makefile': " - do_make "${dir}" "Makefile" "${object}" \ - && local done="1" - fi - - # never leave $g_watchroot - if [ "${dir}" != "${g_watchroot}" ]; then - # search parent dir for more build instructions - do_compile "$(dirname "${dir}")" "${object}" "${done}" - - elif [ "${done}" == "0" ]; then - # hit $g_watchroot - echo "No build instructions found!" - fi -} - -# process an inotify event -do_handle() { # $1:FLAGS $2:OBJECT - # extract params - local flags="$1" - shift 1 - local dir="$(dirname "$*")" - local object="$(basename "$*")" - - if [[ "${flags}" =~ "ISDIR" ]]; then - # object refers to directory - local dir="${dir}/${object}" - local object="." - fi - - # start using toolchain - echo -n "'${object}': '${flags}' in '${dir}'. " - do_compile "${dir}" "${object}" -} - -# -# MAIN -# - -echo "Booting ${0} in '${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 FILE; do - do_handle ${FILE} -done diff --git a/build/usr/local/bin/autodoc b/build/usr/local/bin/autodoc new file mode 100755 index 0000000..7a643db --- /dev/null +++ b/build/usr/local/bin/autodoc @@ -0,0 +1,34 @@ +#!/bin/bash + +# +# hard globals +# + +g_bin="$(readlink -f "$(which "$0")")" +g_lib=${g_bin/"bin"/"lib"} + +# +# load base program +# + +source "${g_lib}/globals" +source "${g_lib}/plugin_"* +source "${g_lib}/handle_inotify" + +# +# MAIN +# + +echo "Booting '${g_bin}' in '${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 diff --git a/build/usr/local/lib/autodoc/globals b/build/usr/local/lib/autodoc/globals new file mode 100644 index 0000000..ec8cefe --- /dev/null +++ b/build/usr/local/lib/autodoc/globals @@ -0,0 +1,4 @@ +#!/bin/bash + +# $1:WATCHROOT (default: ".") +g_watchroot="$(readlink -f "${1:-.}")" diff --git a/build/usr/local/lib/autodoc/handle_inotify b/build/usr/local/lib/autodoc/handle_inotify new file mode 100644 index 0000000..4146201 --- /dev/null +++ b/build/usr/local/lib/autodoc/handle_inotify @@ -0,0 +1,46 @@ +#!/bin/bash + +# compile a directory +do_compile() { # $1:DIR $2:OBJECT $3:DONE + # extract params + local dir="$1" + local object="$2" + local done="${3:-0}" + + # build systems + if [ -r "${dir}/Makefile" ]; then + # Makefile found + echo -n "Found '${dir}/Makefile': " + do_make "${dir}" "Makefile" "${object}" \ + && local done="1" + fi + + # never leave $g_watchroot + if [ "${dir}" != "${g_watchroot}" ]; then + # search parent dir for more build instructions + do_compile "$(dirname "${dir}")" "${object}" "${done}" + + elif [ "${done}" == "0" ]; then + # hit $g_watchroot + echo "No build instructions found!" + fi +} + +# process an inotify event +do_handle() { # $1:FLAGS $2:OBJECT + # extract params + local flags="$1" + shift 1 + local dir="$(dirname "$*")" + local object="$(basename "$*")" + + if [[ "${flags}" =~ "ISDIR" ]]; then + # object refers to directory + local dir="${dir}/${object}" + local object="." + fi + + # start using toolchain + echo -n "'${object}': '${flags}' in '${dir}'. " + do_compile "${dir}" "${object}" +} diff --git a/build/usr/local/lib/autodoc/plugin_make b/build/usr/local/lib/autodoc/plugin_make new file mode 100644 index 0000000..b9d215a --- /dev/null +++ b/build/usr/local/lib/autodoc/plugin_make @@ -0,0 +1,35 @@ +#!/bin/bash + +# compile using bare make command +do_make() { # $1:DIR $2:MAKEFILE $3:OBJECT + # extract params + local dir="$1" + 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+//")" + + if [ -z "${srcpat}" ]; then + echo -n "Empty source pattern, check '#%SRCPAT%' annotation! " + return 1 + + elif [[ "${object}" =~ ${srcpat} ]]; then + # check for autodoc target + local target="$(grep -E "^autodoc:" "${dir}/${makefile}" | sed -r "s/:.*$//")" + + if [ -z "${target}" ]; then + echo "Running 'make'. " + else + echo "Making '${target}'. " + fi + + make --no-print-directory -C "${dir}" -j ${target} + + else + echo -n "SRCPAT '${srcpat}' mismatch. " + return 1 + fi + + return 0 +} diff --git a/docker-compose.yml b/docker-compose.yml index 426315c..e51149f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,5 +14,6 @@ services: command: "bash" volumes: - - "${PWD}/build/autodoc:/usr/local/bin/autodoc:ro" + - "${PWD}/build/usr/local/bin/autodoc:/usr/local/bin/autodoc:ro" + - "${PWD}/build/usr/local/lib/autodoc:/usr/local/lib/autodoc:ro" - "${PWD}/examples:/docs"