From cd3ecd81c44913891520a31a761db551d43c6d04 Mon Sep 17 00:00:00 2001 From: b1ek Date: Wed, 14 Feb 2024 11:44:10 +1000 Subject: [PATCH] (feat) proper file downloading mechanism --- brick.sh | 68 ++++++++++++++++++++++++++++++++++++++++---- bricks/example.brick | 11 +++---- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/brick.sh b/brick.sh index a71849b..73da283 100755 --- a/brick.sh +++ b/brick.sh @@ -60,6 +60,19 @@ user_confirm() { done } +bricksenv() { + echo "export BRICKS_DIR=$BRICKS_DIR" + echo "export BRICKS_DB=$BRICKS_DB" + echo "export AUTOMATIC=$AUTOMATIC" +} + +tmpdir() { + if ! [ -f $BRICKS_DIR/.tmp ]; then + mkdir -p $BRICKS_DB/.tmp + fi + echo $BRICKS_DB/.tmp +} + # some vars BRICKS_DIR=${BRICKS_DIR:-} PKGS=() @@ -170,6 +183,7 @@ if [ "${#PACDEPS[@]}" != "0" ]; then inf "removed $removed installed pacman deps" fi + # then there might actually be no work to be done if [ "$TO_INSTALL" != "" ]; then inf "installing pacman packages:" inf "$TO_INSTALL" @@ -190,17 +204,61 @@ else inf "no pacman deps to install" fi +DOWNLOADED_FILES=() + i=1 # pacman deps installed, lets install the actual shit for pkg in "${PKGS[@]}"; do inf "installing $pkg [$i/${#PKGS[@]}]" + + + exec_brick() { + echo -e "$(bricksenv)$1" | bash + } + + # first check if there is stuff we need to download for that brick + SCRIPT=" - export BRICKS_DIR=$BRICKS_DIR - export BRICKS_DB=$BRICKS_DB - source $(getbrick $pkg)\n - install" - echo -e "$SCRIPT" | bash > $BRICKS_DB/.$pkg-install-log + source $(getbrick $pkg) + if [ -z ${var+x} ]; then + echo \${downloads[@]} + fi + " + + DOWNLOADS=$(exec_brick "$SCRIPT") + DOWNLOADS_MAP=() + if ! [ "$DOWNLOADS" == "" ]; then + DOWNLOADS=($DOWNLOADS) + inf "$(inf "downloading files for $pkg...")" + i=1 + for download in $DOWNLOADS; do + curl -s $download > $(tmpdir)/$pkg-file-$i & + DOWNLOADS_MAP=(${DOWNLOADS_MAP[@]} $(tmpdir)/$pkg-file-$i) + i=$(($i + 1)) + done + wait + inf "$(inf "downloaded")" + fi + + DOWNLOADED_FILES=(${DOWNLOADED_FILES[@]} ${DOWNLOADS_MAP[@]}) + + SCRIPT=" + source $(getbrick $pkg)\n + downloads=(${DOWNLOADS_MAP[@]})\n + install + " + + exec_brick "$SCRIPT" > $BRICKS_DB/.$pkg-install-log + i=$(($i + 1)) done +if ! [ "${#DOWNLOADED_FILES[@]}" == "0" ]; then + inf "$(inf 'cleaning up downloaded files')" + for file in $DOWNLOADED_FILES; do + rm $file & + done + wait +fi + inf "done" diff --git a/bricks/example.brick b/bricks/example.brick index 2319b9e..729ccc9 100644 --- a/bricks/example.brick +++ b/bricks/example.brick @@ -4,6 +4,9 @@ # GPL3 only # uwu +downloads=( + https://example.com/script.sh +) info() { echo this brick does this and that @@ -23,11 +26,9 @@ pkgs() { } install() { - # these 2 lines are to prevent it from being installed - errcho '`example` brick is not an installable brick, its for packaging reference only' - exit 1 - - sh -c "$(curl https://example.com/script.sh)" + echo this is a dummy package, it is not meant to actually be installed, silly + return + sh -c "$(cat ${downloads[0]})" } uninstall() {