From 31ac40bfd3046192a8506b788e5b95c0f73889c3 Mon Sep 17 00:00:00 2001 From: kondratyev-nickolay <152752796+kondratyev-nickolay@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:18:43 -0800 Subject: [PATCH] Adding ./setup.sh to automate development setup, (also added ./watch.sh wrapper) (#3973) --- README.md | 2 +- setup.sh | 11 +++++ shell/_setup_nvm_source_me.sh | 17 ++++++++ shell/_util.sh | 47 +++++++++++++++++++++ shell/_verify_env_variables.sh | 9 +++++ shell/_verify_node_version.sh | 19 +++++++++ shell/_verify_npm.sh | 10 +++++ shell/_verify_nvm_source_me.sh | 10 +++++ shell/_verify_yarn.sh | 10 +++++ shell/setup.sh | 74 ++++++++++++++++++++++++++++++++++ watch.sh | 6 +++ 11 files changed, 214 insertions(+), 1 deletion(-) create mode 100755 setup.sh create mode 100755 shell/_setup_nvm_source_me.sh create mode 100755 shell/_util.sh create mode 100755 shell/_verify_env_variables.sh create mode 100755 shell/_verify_node_version.sh create mode 100755 shell/_verify_npm.sh create mode 100755 shell/_verify_nvm_source_me.sh create mode 100755 shell/_verify_yarn.sh create mode 100755 shell/setup.sh create mode 100755 watch.sh diff --git a/README.md b/README.md index 543bcb9987..f9bd95e23c 100644 --- a/README.md +++ b/README.md @@ -196,7 +196,7 @@ There are a variety of ways to connect with Dendron devs, contributors, and othe Dendron wouldn't be what it is today without help from the wonderful gardeners πŸ‘¨β€πŸŒΎπŸ‘©β€πŸŒΎ -If you would like to contribute (docs, code, finance, or advocacy), you can find instructions to do so [here](https://wiki.dendron.so/notes/125c990b-6fe7-4ada-a65f-44cbde8b33f0.html). +If you would like to contribute (docs, code, finance, or advocacy), you can find instructions to do so [here](https://wiki.dendron.so/notes/125c990b-6fe7-4ada-a65f-44cbde8b33f0.html). For setup of local development environment run `./setup.sh` which automates the setup. diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000000..370f4bd057 --- /dev/null +++ b/setup.sh @@ -0,0 +1,11 @@ +# Description: Setup the environment for Dendron development. +# +# For further documentation refer to [Dendron Plugin Quickstart]: +# https://docs.dendron.so/notes/64f0e2d5-2c83-43df-9144-40f2c68935aa/ +main() { + export DENDRON_MONOREPO="${PWD:?}" + + "${DENDRON_MONOREPO:?}"/shell/setup.sh +} + +main "${@}" || exit 1 diff --git a/shell/_setup_nvm_source_me.sh b/shell/_setup_nvm_source_me.sh new file mode 100755 index 0000000000..a61e03fc32 --- /dev/null +++ b/shell/_setup_nvm_source_me.sh @@ -0,0 +1,17 @@ +main() { + if [[ ! -f "$HOME/.nvm/nvm.sh" ]] + then + echo "File $HOME/.nvm/nvm.sh does NOT exists. Setup NVM refer to https://github.com/nvm-sh/nvm" + exit 1 + fi + + # Source nvm script - adjust the path if it's different on your machine + # + # -s check if file exists and has a size greater than zero + [ -s "$HOME/.nvm/nvm.sh" ] && { + source "$HOME/.nvm/nvm.sh" + echo "Sourced $HOME/.nvm/nvm.sh" + } +} + +main "${@}" || exit 1 diff --git a/shell/_util.sh b/shell/_util.sh new file mode 100755 index 0000000000..fe4692e262 --- /dev/null +++ b/shell/_util.sh @@ -0,0 +1,47 @@ +# -------------------------------------------------------------------------------- +# ANSI escape code for green color +export COLOR_GREEN="\033[0;32m" +# ANSI escape code to reset color back to default +export COLOR_RESET="\033[0m" # No Color +# ANSI escape code for red color +export COLOR_RED="\033[0;31m" + +echo_green(){ + echo -e "${COLOR_GREEN:?}${*}${COLOR_RESET:?}" +} + +# Check if the file exists and source it. +source_robust(){ + local file="${1:?}" + + if [ ! -f "${file:?}" ]; then + echo -e "${COLOR_RED:?}File not found: ${file:?}${COLOR_RESET:?}" + exit 1 + fi + + # shellcheck disable=SC1090 + source "${file:?}" +} + +# Announces command (prints out the command that is going to be executed). +# Executes the command. +# If the command fails, prints out error and exits. +# +# Note this function will use 'source' with a temporary file, to allow +# the functions that are executed to modify environment variables. +eae(){ + local execution_file=/tmp/execution_file_dendron_setup.sh + echo "${@:?}" > "${execution_file:?}" + + echo "" + echo -e "Executing: ${COLOR_GREEN}$(cat "${execution_file:?}")${COLOR_RESET:?}" + + # We use source_robust instead of 'eval' to be able to modify environment variables + # from the commands that we are running with eae. + # + # shellcheck disable=SC1090 + if ! source "${execution_file:?}"; then + echo -e "${COLOR_RED:?}Error executing: $(cat "${execution_file:?}")${COLOR_RESET:?}" + exit 1 + fi +} diff --git a/shell/_verify_env_variables.sh b/shell/_verify_env_variables.sh new file mode 100755 index 0000000000..8835af6629 --- /dev/null +++ b/shell/_verify_env_variables.sh @@ -0,0 +1,9 @@ +main() { + # -z: returns true when value is empty. + if [[ -z "${DENDRON_MONOREPO}" ]]; then + echo "DENDRON_MONOREPO environment variable is not set." + exit 1 + fi +} + +main "${@}" || exit 1 diff --git a/shell/_verify_node_version.sh b/shell/_verify_node_version.sh new file mode 100755 index 0000000000..81a1bcaab0 --- /dev/null +++ b/shell/_verify_node_version.sh @@ -0,0 +1,19 @@ +main() { + MIN_VERSION="14.0.0" + + # Get the current Node.js version + CURRENT_VERSION=$(node -v | sed 's/v//') # Removes the 'v' prefix from version + + # Function to compare versions + version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; } + + # Check if the current version is greater than or equal to the minimum version + if version_gt "$CURRENT_VERSION" "$MIN_VERSION"; then + echo "Current Node.js version is $CURRENT_VERSION. Proceeding..." + else + echo "Error: Node.js version must be $MIN_VERSION or greater. Current version is $CURRENT_VERSION." + exit 1 + fi +} + +main "${@}" || exit 1 diff --git a/shell/_verify_npm.sh b/shell/_verify_npm.sh new file mode 100755 index 0000000000..d6a6ae0002 --- /dev/null +++ b/shell/_verify_npm.sh @@ -0,0 +1,10 @@ +main() { + if command -v npm > /dev/null 2>&1; then + echo "Verified npm is installed." + else + echo "npm is not installed. Please install Node.js and npm." + exit 1 + fi +} + +main "${@}" || exit 1 diff --git a/shell/_verify_nvm_source_me.sh b/shell/_verify_nvm_source_me.sh new file mode 100755 index 0000000000..8217b03a56 --- /dev/null +++ b/shell/_verify_nvm_source_me.sh @@ -0,0 +1,10 @@ +main() { + if type nvm > /dev/null 2>&1; then + echo "nvm is installed." + else + echo "nvm is not installed. Please install nvm. (https://github.com/nvm-sh/nvm)" + exit 1 + fi +} + +main "${@}" || exit 1 diff --git a/shell/_verify_yarn.sh b/shell/_verify_yarn.sh new file mode 100755 index 0000000000..ea85cbc419 --- /dev/null +++ b/shell/_verify_yarn.sh @@ -0,0 +1,10 @@ +main() { + if command -v yarn > /dev/null 2>&1; then + echo "Verified Yarn is installed." + else + echo "Yarn is not installed. Please install Yarn." + exit 1 + fi +} + +main "${@}" || exit 1 diff --git a/shell/setup.sh b/shell/setup.sh new file mode 100755 index 0000000000..13b22e8e0f --- /dev/null +++ b/shell/setup.sh @@ -0,0 +1,74 @@ +# Description: Setup the environment for Dendron development. +# +# Pre-requisites: +# - DENDRON_MONOREPO environment variable must be set. +# It should point to dendron's monorepo +# (directory where you cloned https://github.com/dendronhq/dendron.git into) +# +# This script is based off of https://docs.dendron.so/notes/64f0e2d5-2c83-43df-9144-40f2c68935aa/ + +# -z: returns true when value is empty. +if [[ -z "${DENDRON_MONOREPO}" ]]; then + echo "DENDRON_MONOREPO environment variable is not set. Please set it to dendron's monorepo directory." + exit 1 +fi + +if [[ -f "${DENDRON_MONOREPO:?}"/shell/_util.sh ]] +then + source "${DENDRON_MONOREPO:?}"/shell/_util.sh +else + echo "File not found: ${DENDRON_MONOREPO:?}/shell/_util.sh" + exit 1 +fi + +_setup_node_version(){ + # NVM is often not propagated to subshells. This is a workaround to + # allow usage of NVM within the script. + source_robust "${DENDRON_MONOREPO:?}"/shell/_setup_nvm_source_me.sh + + # We need to source verification of NVM due to subshell issue mentioned above. + source_robust "${DENDRON_MONOREPO:?}"/shell/_verify_nvm_source_me.sh + + # There is an issue with node 17+ and `yarn setup` that causes an error. + # Node 16 is the latest version that works with `yarn setup` with + # current dendron setup. + # + # Another option to try is to use later node version with: + # export NODE_OPTIONS=--openssl-legacy-provider + # + # However, it seems more robust to pick a node version that is known to work. + # Hence, we are setting node version to 16. + eae nvm install 16 + eae nvm use 16 +} + +main_impl(){ + eae _setup_node_version + + eae npm install -g yarn + eae npm install -g lerna + + eae cd "${DENDRON_MONOREPO:?}" + + echo "install workspace dependencies..." + eae yarn + + echo "install package dependencies..." + eae yarn setup +} + +main() { + echo_green "Starting ${0}..." + + eae "${DENDRON_MONOREPO:?}"/shell/_verify_env_variables.sh + eae "${DENDRON_MONOREPO:?}"/shell/_verify_node_version.sh + eae "${DENDRON_MONOREPO:?}"/shell/_verify_npm.sh + eae "${DENDRON_MONOREPO:?}"/shell/_verify_yarn.sh + + main_impl + + echo "--------------------------------------------------------------------------------" + echo_green "Finished ${0} successfully. For further documentation refer to https://docs.dendron.so/notes/64f0e2d5-2c83-43df-9144-40f2c68935aa/ . Particularly look for the part that talks about 'dendron-main.code-workspace' (And use File->Open Workspace from file... to open 'dendron/dendron-main.code-workspace'). Also look for './watch.sh' which wraps the watch command." +} + +main "${@}" || exit 1 diff --git a/watch.sh b/watch.sh new file mode 100755 index 0000000000..7e9b42371a --- /dev/null +++ b/watch.sh @@ -0,0 +1,6 @@ +# Related documentation here https://docs.dendron.so/notes/64f0e2d5-2c83-43df-9144-40f2c68935aa/ +main() { + ./bootstrap/scripts/watch.sh +} + +main "${@}" || exit 1