dep

Package manager for embedded C libraries
git clone git://git.finwo.net/app/dep
Log | Files | Refs | README | LICENSE

commit 9c6a1ca8d23178e8bb4a8395c8d3a7f0787fa59f
parent cff1c2388e9e914a2089abf4a29d47e9d26cb1eb
Author: finwo <finwo@pm.me>
Date:   Wed, 25 Jan 2023 22:03:16 +0100

Split commands into src subdir; replaced bashpp by preprocess

Diffstat:
M.gitignore | 1-
MMakefile | 14++++++--------
Mpackage.ini | 6------
Asrc/command/help/index.sh | 31+++++++++++++++++++++++++++++++
Asrc/command/help/topic/global.txt | 12++++++++++++
Asrc/command/install.sh | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dsrc/ini.sh | 76----------------------------------------------------------------------------
Msrc/main.sh | 36++++++++++++++++++++++++++++++------
Dsrc/shopt.sh | 7-------
Asrc/util/ini.sh | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/util/shopt.sh | 8++++++++
11 files changed, 226 insertions(+), 104 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,4 +1,3 @@ /bak/ -/bashpp /util/ /dep diff --git a/Makefile b/Makefile @@ -3,23 +3,21 @@ SRC=$(wildcard src/*.sh) # Idea: # include lib/.dep/libraries.mk -default: main +PREPROCESS=preprocess --substitute -bashpp: - curl https://raw.githubusercontent.com/iwonbigbro/bashpp/master/bin/bashpp > bashpp - chmod +x bashpp +default: main -util/ini.sh: src/ini.sh src/shopt.sh +util/ini.sh: src/util/ini.sh src/util/shopt.sh mkdir -p util echo '#!/usr/bin/env bash' > $@ - ./bashpp -I src -v src/ini.sh | tee -a $@ > /dev/null + $(PREPROCESS) -I src -v src/util/ini.sh | tee -a $@ > /dev/null chmod +x $@ .PHONY: main -main: $(SRC) bashpp util/ini.sh +main: $(SRC) util/ini.sh @$(eval NAME=$(shell util/ini.sh package.ini package.name)) echo '#!/usr/bin/env bash' > "$(NAME)" - ./bashpp -D __NAME=$(NAME) -I src -v src/main.sh | tee -a "$(NAME)" > /dev/null + $(PREPROCESS) -D __NAME=$(NAME) -I src src/main.sh | tee -a $(NAME) > /dev/null chmod +x "$(NAME)" .PHONY: clean diff --git a/package.ini b/package.ini @@ -2,11 +2,5 @@ name=dep deps=lib -src=http://tarball - [dependencies] bashpp=https://github.com/iwonbigbro/bashpp/archive/refs/heads/master.tar.gz - -# TEST - -; test diff --git a/src/command/help/index.sh b/src/command/help/index.sh @@ -0,0 +1,31 @@ +declare -A help_topics + +read -r -d '' help_topics[global] <<- EOF +# #include "topic/global.txt" +EOF + +HELP_TOPIC=global +function arg_h { + arg_help "$@" + return $? +} +function arg_help { + HELP_TOPIC=$1 + shift +} + +function cmd_h { + cmd_help "$@" + return $? +} +function cmd_help { + if [ -z "${help_topics[$HELP_TOPIC]}" ]; then + echo "Unknown topic: $HELP_TOPIC" >&2 + exit 1 + fi + + echo "${help_topics[$HELP_TOPIC]}" +} + +cmds[${#cmds[*]}]="h" +cmds[${#cmds[*]}]="help" diff --git a/src/command/help/topic/global.txt b/src/command/help/topic/global.txt @@ -0,0 +1,12 @@ + +Usage: ${NAME} [global options] <command> [options] [-- ...args] + +Global options: + n/a + +Commands: + i(nstall) Install all the project's dependencies + h(elp) [topic] Show this help or the top-level info about a command + +Help topics: + global This help text diff --git a/src/command/install.sh b/src/command/install.sh @@ -0,0 +1,57 @@ +# #include "util/ini.sh" + +function arg_i { + arg_install "$@" + return $? +} +function arg_install { + return 0 +} + +function cmd_i { + cmd_install "$@" + return $? +} +function cmd_install { + + # Sanity check + PACKAGE_PATH="$(pwd)/package.ini" + if [ ! -f "${PACKAGE_PATH}" ]; then + echo "No package.ini in the working directory!" >&2 + exit 1 + fi + + ini_foreach cmd_install_parse_ini "${PACKAGE_PATH}" + cmd_install_execute +} + +cmds[${#cmds[*]}]="i" +cmds[${#cmds[*]}]="install" + +CMD_INSTALL_PKG_NAME= +CMD_INSTALL_PKG_DEST="$(pwd)/lib" +declare -A CMD_INSTALL_DEPS +function cmd_install_parse_ini { + case "$1" in + package.) + case "$2" in + name) + CMD_INSTALL_PKG_NAME="$3" + ;; + deps) + CMD_INSTALL_PKG_DEST="$(pwd)/$3" + ;; + esac + ;; + dependencies.) + CMD_INSTALL_DEPS["$2"]="$3" + ;; + esac +} + +function cmd_install_execute { + echo "PKG_NAME: $CMD_INSTALL_PKG_NAME" + echo "LIB_DEST: $CMD_INSTALL_PKG_DEST" + echo "@: ${CMD_INSTALL_DEPS[@]}" + echo "!: ${!CMD_INSTALL_DEPS[@]}" +} diff --git a/src/ini.sh b/src/ini.sh @@ -1,76 +0,0 @@ -#include "shopt.sh" - -# Arguments: -# $0 <fn_keyHandler> <str_filename> [section[.key]] -function ini_foreach { - - # No file = no data - inifile="${2}" - if [[ ! -f "$inifile" ]]; then - exit 1 - fi - - # Process the file line-by-line - SECTION= - while read line; do - - # Remove surrounding whitespace - line=${line##*( )} # From the beginning - line=${line%%*( )} # From the end - - # Remove comments and empty lines - if [[ "${line:0:1}" == '#' ]] || [[ "${line:0:1}" == ';' ]] || [[ "${#line}" == 0 ]]; then - continue - fi - - # Handle section markers - if [[ "${line:0:1}" == "[" ]]; then - SECTION=$(echo $line | sed -e 's/\[\(.*\)\]/\1/') - SECTION=${SECTION##*( )} - SECTION=${SECTION%%*( )} - SECTION="${SECTION}." - continue - fi - - # Output found variable - NAME=${line%%=*} - NAME=${NAME%%*( )} - VALUE=${line#*=} - VALUE=${VALUE##*( )} - - # Output searched or all - if [[ -z "${3}" ]]; then - $1 "$SECTION" "$NAME" "${VALUE}" - elif [[ "${SECTION}" == "${3}." ]] || [[ "${SECTION}${NAME}" == "${3}" ]]; then - $1 "$SECTION" "$NAME" "${VALUE}" - fi - - done < "${inifile}" - -} - -function ini_output_full { - echo "$1$2=$3" -} -function ini_output_section { - echo "$2=$3" -} -function ini_output_value { - echo "$3" -} - -# Allow this file to be called stand-alone -# ini.sh <filename> [section[.key]] [sectionmode] -if [ $(basename $0) == "ini.sh" ]; then - fullMode=full - sectionMode=value - if [[ ! -z "${3}" ]]; then - fullMode=${3} - sectionMode=${3} - fi - if [[ -z "${2}" ]]; then - ini_foreach ini_output_${fullMode} "$@" - else - ini_foreach ini_output_${sectionMode} "$@" - fi -fi diff --git a/src/main.sh b/src/main.sh @@ -1,17 +1,41 @@ -#include "ini.sh" +cmds=("") +# #include "command/help/index.sh" +# #include "command/install.sh" -# Filled by bashpp +# Filled by preprocess NAME=__NAME function main { + cmd=help - # Commands: - # install + while [ "$#" -gt 0 ]; do + # If argument is a command, pass parsing on to it & stop main parser + if [[ " ${cmds[*]} " =~ " $1 " ]]; then + cmd=$1 + shift + arg_$cmd "$@" + break + fi - echo "Hello World" + # Main parser + case "$1" in + --) + shift + break 2 + ;; + *) + echo "Unknown argument: $1" >&2 + exit 1 + ;; + esac + shift + + done + + cmd_$cmd } if [ $(basename $0) == "${NAME}" ]; then - main + main "$@" fi diff --git a/src/shopt.sh b/src/shopt.sh @@ -1,7 +0,0 @@ -#ifndef __SHOPT_SH__ -#define __SHOPT_SH__ - -# Required for the whitespace trimming -shopt -s extglob - -#endif __SHOPT_SH__ diff --git a/src/util/ini.sh b/src/util/ini.sh @@ -0,0 +1,82 @@ +# #ifndef __INI_SH__ +# #define __INI_SH__ + +# #include "shopt.sh" + +# Arguments: +# $0 <fn_keyHandler> <str_filename> [section[.key]] +function ini_foreach { + + # No file = no data + inifile="${2}" + if [[ ! -f "$inifile" ]]; then + exit 1 + fi + + # Process the file line-by-line + SECTION= + while read line; do + + # Remove surrounding whitespace + line=${line##*( )} # From the beginning + line=${line%%*( )} # From the end + + # Remove comments and empty lines + if [[ "${line:0:1}" == '#' ]] || [[ "${line:0:1}" == ';' ]] || [[ "${#line}" == 0 ]]; then + continue + fi + + # Handle section markers + if [[ "${line:0:1}" == "[" ]]; then + SECTION=$(echo $line | sed -e 's/\[\(.*\)\]/\1/') + SECTION=${SECTION##*( )} + SECTION=${SECTION%%*( )} + SECTION="${SECTION}." + continue + fi + + # Output found variable + NAME=${line%%=*} + NAME=${NAME%%*( )} + VALUE=${line#*=} + VALUE=${VALUE##*( )} + + # Output searched or all + if [[ -z "${3}" ]]; then + $1 "$SECTION" "$NAME" "${VALUE}" + elif [[ "${SECTION}" == "${3}." ]] || [[ "${SECTION}${NAME}" == "${3}" ]]; then + $1 "$SECTION" "$NAME" "${VALUE}" + fi + + done < "${inifile}" + +} + +function ini_output_full { + echo "$1$2=$3" +} +function ini_output_section { + echo "$2=$3" +} +function ini_output_value { + echo "$3" +} + +# Allow this file to be called stand-alone +# ini.sh <filename> [section[.key]] [sectionmode] +if [ $(basename $0) == "ini.sh" ]; then + fullMode=full + sectionMode=value + if [[ ! -z "${3}" ]]; then + fullMode=${3} + sectionMode=${3} + fi + if [[ -z "${2}" ]]; then + ini_foreach ini_output_${fullMode} "$@" + else + ini_foreach ini_output_${sectionMode} "$@" + fi +fi + +# __INI_SH__ +# #endif diff --git a/src/util/shopt.sh b/src/util/shopt.sh @@ -0,0 +1,8 @@ +# #ifndef __SHOPT_SH__ +# #define __SHOPT_SH__ + +# Required for the whitespace trimming +shopt -s extglob + +# __SHOPT_SH__ +# #endif