From 32afe57e18955ccb6d9679b1fceebee129fad6f2 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Wed, 31 Mar 2021 08:32:57 +0200 Subject: [PATCH] :sparkles: Improve build scripts. --- backend/scripts/build | 81 +++++++++++++++++++ backend/scripts/build.sh | 75 ----------------- backend/scripts/import-generic-collections.sh | 4 - backend/scripts/manage.template.sh | 19 +++++ backend/scripts/prepare-release.sh | 16 ---- backend/scripts/psql.sh | 2 - backend/scripts/run-tests-in-docker.sh | 4 - backend/scripts/run.template.sh | 20 +++++ backend/scripts/smtpd.sh | 2 - backend/scripts/tests.sh | 2 - backend/src/app/config.clj | 8 +- exporter/scripts/{build.sh => build} | 1 - frontend/scripts/build | 17 ++++ frontend/scripts/build-and-run-tests.sh | 8 -- frontend/scripts/build.sh | 21 ----- manage.sh | 38 +++++++-- 16 files changed, 174 insertions(+), 144 deletions(-) create mode 100755 backend/scripts/build delete mode 100755 backend/scripts/build.sh delete mode 100755 backend/scripts/import-generic-collections.sh create mode 100644 backend/scripts/manage.template.sh delete mode 100755 backend/scripts/prepare-release.sh delete mode 100755 backend/scripts/psql.sh delete mode 100755 backend/scripts/run-tests-in-docker.sh create mode 100644 backend/scripts/run.template.sh delete mode 100755 backend/scripts/smtpd.sh delete mode 100755 backend/scripts/tests.sh rename exporter/scripts/{build.sh => build} (93%) create mode 100755 frontend/scripts/build delete mode 100755 frontend/scripts/build-and-run-tests.sh delete mode 100755 frontend/scripts/build.sh diff --git a/backend/scripts/build b/backend/scripts/build new file mode 100755 index 000000000..ec65f4ea3 --- /dev/null +++ b/backend/scripts/build @@ -0,0 +1,81 @@ +#!/usr/bin/env bb + +;; This Source Code Form is subject to the terms of the Mozilla Public +;; License, v. 2.0. If a copy of the MPL was not distributed with this +;; file, You can obtain one at http://mozilla.org/MPL/2.0/. +;; +;; This Source Code Form is "Incompatible With Secondary Licenses", as +;; defined by the Mozilla Public License, v. 2.0. +;; +;; Copyright (c) UXBOX Labs SL + +(ns build + (:require + [clojure.string :as str] + [clojure.java.io :as io] + [clojure.pprint :refer [pprint]] + [babashka.fs :as fs] + [babashka.process :refer [$ check]])) + +(defn split-cp + [data] + (str/split data #":")) + +(def classpath + (->> ($ clojure -Spath) + (check) + (:out) + (slurp) + (split-cp) + (map str/trim))) + +(def classpath-jars + (let [xfm (filter #(str/ends-with? % ".jar"))] + (into #{} xfm classpath))) + +(def classpath-paths + (let [xfm (comp (remove #(str/ends-with? % ".jar")) + (filter #(.isDirectory (io/file %))))] + (into #{} xfm classpath))) + +(def version + (or (first *command-line-args*) "%version%")) + +;; Clean previous dist +(-> ($ rm -rf "./target/dist") check) + +;; Create a new dist +(-> ($ mkdir -p "./target/dist/deps") check) + +;; Copy all jar deps into dist +(run! (fn [item] (-> ($ cp ~item "./target/dist/deps/") check)) classpath-jars) + +;; Create the application jar +(spit "./target/dist/version.txt" version) +(-> ($ jar cvf "./target/dist/deps/app.jar" -C ~(first classpath-paths) ".") check) +(-> ($ jar uvf "./target/dist/deps/app.jar" -C "./target/dist" "version.txt") check) +(run! (fn [item] + (-> ($ jar uvf "./target/dist/deps/app.jar" -C ~item ".") check)) + (rest classpath-paths)) + +;; Copy logging configuration +(-> ($ cp "./resources/log4j2.xml" "./target/dist/") check) + +;; Create classpath file +(let [jars (->> (into ["app.jar"] classpath-jars) + (map fs/file-name) + (map #(fs/path "deps" %)) + (map str))] + (spit "./target/dist/classpath" (str/join ":" jars))) + +;; Copy run script template +(-> ($ cp "./scripts/run.template.sh" "./target/dist/run.sh") check) + +;; Copy run script template +(-> ($ cp "./scripts/manage.template.sh" "./target/dist/manage.sh") check) + +;; Add exec permisions to scripts. +(-> ($ chmod +x "./target/dist/run.sh") check) +(-> ($ chmod +x "./target/dist/manage.sh") check) + +nil diff --git a/backend/scripts/build.sh b/backend/scripts/build.sh deleted file mode 100755 index 04865cb3d..000000000 --- a/backend/scripts/build.sh +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env bash - -CLASSPATH=`(clojure -Spath)` -NEWCP="./main:./common" - -rm -rf ./target/dist -mkdir -p ./target/dist/deps - -for item in $(echo $CLASSPATH | tr ":" "\n"); do - if [ "${item: -4}" == ".jar" ]; then - cp $item ./target/dist/deps/; - BN="$(basename -- $item)" - NEWCP+=":./deps/$BN" - fi -done - -cp ./resources/log4j2.xml ./target/dist/log4j2.xml -cp -r ./src ./target/dist/main -cp -r ./resources/emails ./target/dist/main/ -cp -r ./resources/error-report.tmpl ./target/dist/main/ -cp -r ../common ./target/dist/common - -echo $NEWCP > ./target/dist/classpath; - -tee -a ./target/dist/run.sh >> /dev/null <&2 echo "Couldn't find 'java'. Please set JAVA_HOME." - exit 1 - fi -fi - -if [ -f ./environ ]; then - source ./environ -fi - -set -x -exec \$JAVA_CMD \$JVM_OPTS -classpath \$CP -Dlog4j2.configurationFile=./log4j2.xml "\$@" clojure.main -m app.main -EOF - -tee -a ./target/dist/manage.sh >> /dev/null <&2 echo "Couldn't find 'java'. Please set JAVA_HOME." - exit 1 - fi -fi - -if [ -f ./environ ]; then - source ./environ -fi - -exec \$JAVA_CMD \$JVM_OPTS -classpath \$CP -Dlog4j2.configurationFile=./log4j2.xml clojure.main -m app.cli.manage "\$@" -EOF - -chmod +x ./target/dist/run.sh -chmod +x ./target/dist/manage.sh diff --git a/backend/scripts/import-generic-collections.sh b/backend/scripts/import-generic-collections.sh deleted file mode 100755 index b28c41296..000000000 --- a/backend/scripts/import-generic-collections.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash - -clojure -Adev -m app.cli.collimp $@ - diff --git a/backend/scripts/manage.template.sh b/backend/scripts/manage.template.sh new file mode 100644 index 000000000..31ccabffe --- /dev/null +++ b/backend/scripts/manage.template.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set +e +JAVA_CMD=$(type -p java) + +set -e +if [[ ! -n "$JAVA_CMD" ]]; then + if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then + JAVA_CMD="$JAVA_HOME/bin/java" + else + >&2 echo "Couldn't find 'java'. Please set JAVA_HOME." + exit 1 + fi +fi + +if [ -f ./environ ]; then + source ./environ +fi + +exec $JAVA_CMD $JVM_OPTS -classpath $(cat classpath) -Dlog4j2.configurationFile=./log4j2.xml clojure.main -m app.cli.manage "\$@" diff --git a/backend/scripts/prepare-release.sh b/backend/scripts/prepare-release.sh deleted file mode 100755 index 2eb2b353e..000000000 --- a/backend/scripts/prepare-release.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env bash - -if [ "$#" -e 0 ]; then - echo "Expecting parameters: 1=path to backend; 2=destination directory" - exit 1 -fi - -rm -rf $2 || exit 1; - -rsync -avr \ - --exclude="/test" \ - --exclude="/resources/public/media" \ - --exclude="/target" \ - --exclude="/scripts" \ - --exclude="/.*" \ - $1 $2; diff --git a/backend/scripts/psql.sh b/backend/scripts/psql.sh deleted file mode 100755 index e54149fcd..000000000 --- a/backend/scripts/psql.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env bash -PGPASSWORD=$PENPOT_DATABASE_PASSWORD psql $PENPOT_DATABASE_URI -U $PENPOT_DATABASE_USERNAME diff --git a/backend/scripts/run-tests-in-docker.sh b/backend/scripts/run-tests-in-docker.sh deleted file mode 100755 index bcb8e50ec..000000000 --- a/backend/scripts/run-tests-in-docker.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash - -set -xe -clojure -Adev -m app.tests.main; diff --git a/backend/scripts/run.template.sh b/backend/scripts/run.template.sh new file mode 100644 index 000000000..2742fe9fe --- /dev/null +++ b/backend/scripts/run.template.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +set +e +JAVA_CMD=$(type -p java) + +set -e +if [[ ! -n "$JAVA_CMD" ]]; then + if [[ -n "$JAVA_HOME" ]] && [[ -x "$JAVA_HOME/bin/java" ]]; then + JAVA_CMD="$JAVA_HOME/bin/java" + else + >&2 echo "Couldn't find 'java'. Please set JAVA_HOME." + exit 1 + fi +fi + +if [ -f ./environ ]; then + source ./environ +fi + +set -x +exec $JAVA_CMD $JVM_OPTS -classpath "$(cat classpath)" -Dlog4j2.configurationFile=./log4j2.xml "$@" clojure.main -m app.main diff --git a/backend/scripts/smtpd.sh b/backend/scripts/smtpd.sh deleted file mode 100755 index a4aa39cc6..000000000 --- a/backend/scripts/smtpd.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env bash -python -m smtpd -n -c DebuggingServer localhost:25 diff --git a/backend/scripts/tests.sh b/backend/scripts/tests.sh deleted file mode 100755 index 4a5166496..000000000 --- a/backend/scripts/tests.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env sh -exec clojure -M:dev:tests "$@" diff --git a/backend/src/app/config.clj b/backend/src/app/config.clj index aff4989a0..ebb33d417 100644 --- a/backend/src/app/config.clj +++ b/backend/src/app/config.clj @@ -5,7 +5,7 @@ ;; This Source Code Form is "Incompatible With Secondary Licenses", as ;; defined by the Mozilla Public License, v. 2.0. ;; -;; Copyright (c) 2020-2021 UXBOX Labs SL +;; Copyright (c) UXBOX Labs SL (ns app.config "A configuration management." @@ -15,6 +15,7 @@ [app.common.version :as v] [app.util.time :as dt] [clojure.core :as c] + [clojure.java.io :as io] [clojure.spec.alpha :as s] [cuerdas.core :as str] [environ.core :refer [env]])) @@ -251,7 +252,10 @@ :migrations-verbose false} (read-config env))) -(def version (v/parse "%version%")) +(def version (v/parse (or (some-> (io/resource "version.txt") + (slurp) + (str/trim)) + "%version%"))) (def config (read-config env)) (def test-config (read-test-config env)) diff --git a/exporter/scripts/build.sh b/exporter/scripts/build similarity index 93% rename from exporter/scripts/build.sh rename to exporter/scripts/build index 521bcca47..61cc021d1 100755 --- a/exporter/scripts/build.sh +++ b/exporter/scripts/build @@ -1,6 +1,5 @@ #!/usr/bin/env bash -source ~/.bashrc set -ex yarn install diff --git a/frontend/scripts/build b/frontend/scripts/build new file mode 100755 index 000000000..94f7dfeb3 --- /dev/null +++ b/frontend/scripts/build @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -ex + +CURRENT_VERSION=$1; +CURRENT_HASH=$(git rev-parse --short HEAD); +EXTRA_PARAMS=$SHADOWCLJS_EXTRA_PARAMS; + +yarn install || exit 1; +npx gulp clean || exit 1; +npx shadow-cljs release main --config-merge "{:release-version \"${CURRENT_HASH}\"}" $EXTRA_PARAMS || exit 1 +npx gulp build || exit 1; +npx gulp dist:clean || exit 1; +npx gulp dist:copy || exit 1; + +sed -i -re "s/\%version\%/$CURRENT_VERSION/g" ./target/dist/index.html; + diff --git a/frontend/scripts/build-and-run-tests.sh b/frontend/scripts/build-and-run-tests.sh deleted file mode 100755 index 57f2478cb..000000000 --- a/frontend/scripts/build-and-run-tests.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash -source ~/.bashrc - -set -ex; - -yarn install -clojure -Adev tools.clj build:tests -node ./target/tests/main diff --git a/frontend/scripts/build.sh b/frontend/scripts/build.sh deleted file mode 100755 index a55a93979..000000000 --- a/frontend/scripts/build.sh +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env bash - -source ~/.bashrc - -set -ex - -if [ -z "${TAG}" ]; then - export TAG=$(git log -n 1 --pretty=format:%H -- ./); -fi - -yarn install - -export NODE_ENV=production; - -# Clean the output directory -npx gulp clean || exit 1; - -npx shadow-cljs release main --config-merge "{:release-version \"${TAG}\"}" $SHADOWCLJS_EXTRA_PARAMS -npx gulp build || exit 1; -npx gulp dist:clean || exit 1; -npx gulp dist:copy || exit 1; diff --git a/manage.sh b/manage.sh index b1e2546dc..d92eb5b4f 100755 --- a/manage.sh +++ b/manage.sh @@ -71,6 +71,9 @@ function run-devenv { } function build { + echo ">> build start: $1" + local version=$(print-current-version); + pull-devenv-if-not-exists; docker volume create ${DEVENV_PNAME}_user_data; docker run -t --rm \ @@ -79,10 +82,28 @@ function build { -e EXTERNAL_UID=$CURRENT_USER_ID \ -e SHADOWCLJS_EXTRA_PARAMS=$SHADOWCLJS_EXTRA_PARAMS \ -w /home/penpot/penpot/$1 \ - $DEVENV_IMGNAME:latest sudo -EH -u penpot ./scripts/build.sh + $DEVENV_IMGNAME:latest sudo -EH -u penpot ./scripts/build $version + + echo ">> build end: $1" +} + +function put-license-file { + local target=$1; + tee -a $target/LICENSE >> /dev/null <> bundle app start"; + local version=$(print-current-version); local bundle_dir="./bundle-app"; @@ -91,25 +112,28 @@ function build-app-bundle { rm -rf $bundle_dir mkdir -p $bundle_dir; - cp -r ./frontend/target/dist $bundle_dir/frontend; - cp -r ./backend/target/dist $bundle_dir/backend; + mv ./frontend/target/dist $bundle_dir/frontend; + mv ./backend/target/dist $bundle_dir/backend; echo $version > $bundle_dir/version.txt - - sed -i -re "s/\%version\%/$version/g" $bundle_dir/frontend/index.html; - sed -i -re "s/\%version\%/$version/g" $bundle_dir/backend/main/app/config.clj; + put-license-file $bundle_dir; + echo ">> bundle app end"; } function build-exporter-bundle { + echo ">> bundle exporter start"; local version=$(print-current-version); local bundle_dir="./bundle-exporter"; build "exporter"; rm -rf $bundle_dir; - cp -r ./exporter/target $bundle_dir; + mv ./exporter/target $bundle_dir; echo $version > $bundle_dir/version.txt + put-license-file $bundle_dir; + + echo ">> bundle exporter end"; } function usage {