Skip to content

Commit

Permalink
Working spin new and spin init
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydrogers committed Dec 15, 2023
1 parent aa0e2c8 commit 8cce7de
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bin/spin
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ main() {
;;
init)
source "$SPIN_HOME/lib/actions/init.sh"
action_init
action_init "$@"
;;
kill)
source "$SPIN_HOME/lib/actions/kill.sh"
Expand Down
129 changes: 128 additions & 1 deletion lib/actions/init.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,131 @@
#!/usr/bin/env bash
action_init() {
echo "Initializing spin..."
set -e
local force=0
local project_directory="$(pwd)"

for arg in "$@"; do
case $arg in
--force)
force=1
;;
--template=*)
template="${arg#*=}"
;;
--project-directory=*)
project_directory="${arg#*=}"
;;
*)
echo "${BOLD}${RED}❌ Invalid argument: $arg ${RESET}"
return 1
;;
esac
done

# Assume we're in the project root if we can find a composer.json file or a package.json file
if [[ ! -f "$project_directory/composer.json" && ! -f "$project_directory/package.json" ]]; then
echo "${BOLD}${RED}❌ Unable to find a composer.json or package.json file in \"$project_directory\". Please run \"spin init\" from the root of your project.${RESET}"
return 1
fi

if [[ $force = 0 ]]; then
echo -n "${BOLD}${YELLOW}👉 Heads up: We're about to add our templates to your project.${RESET} Are you good with that? [y/n]: "
read -n 1 add_files_response
echo # move to a new line
if [[ $add_files_response =~ ^[Yy]$ ]]; then
echo "We will add our templates to your project."
elif [[ $add_files_response =~ ^[Nn]$ ]]; then
echo "${BOLD}${BLUE}👋 Ok, we won't add any files to your project.${RESET} You can always configure your infrastructure manually or run \"spin init\" again in the future."
exit 2
else
echo "${BOLD}${RED}❌ Invalid response. Please respond with \"y\" or \"n\".${RESET} Run \"spin init\" to try again."
return 1
fi
fi

if [[ -z $template ]]; then
echo "Select your project type:"
echo "1) Laravel"
echo "2) Nuxt"
echo -n "Enter the number of your choice (1 for Laravel, 2 for Nuxt): "
read -r project_type_number
echo # move to a new line

case $project_type_number in
1)
template="laravel"
;;
2)
template="node"
;;
*)
echo "Invalid selection."
return 1
;;
esac
fi

if [[ -d "$SPIN_HOME/templates/$template" ]]; then
echo "${BOLD}${BLUE}⚡️ Copying spin templates to our project, \"$project_directory\"...${RESET}"

# Check to see if the template file already exists in the project directory
find "$SPIN_HOME/templates/$template" -type f -exec bash -c '
for file do
target_file="$2/${file#$1/}"
if [[ -d "$file" ]]; then
continue
fi
if [[ -f "$target_file" ]]; then
echo "${BOLD}${YELLOW}⚠️ \"$target_file\" already exists. Skipping...${RESET}"
else
mkdir -p "$(dirname "$target_file")"
if cp "$file" "$target_file"; then
echo "${BOLD}${GREEN}✅ \"$target_file\" has been created.${RESET}"
else
echo "${BOLD}${RED}❌ Error copying \"$file\" to \"$target_file\".${RESET}"
fi
fi
done
' bash "$SPIN_HOME/templates/$template" "$project_directory" {} +
fi

echo "${BOLD}${BLUE}⚡️ Adding items to your .gitignore for best security...${RESET}"

while IFS= read -r line || [[ -n "$line" ]]; do
# Check if the line is not already in the .gitignore
if ! grep -Fxq "$line" "$project_directory/.gitignore"; then
# If the line is not in .gitignore, append it
echo "$line" >> "$project_directory/.gitignore"
fi
done < "$SPIN_HOME/templates/common/.gitignore.example"

if [[ -f "$project_directory/.spin.yml" ]]; then
echo "${BOLD}${YELLOW}⚠️ \"$project_directory/.spin.yml\" already exists. Skipping...${RESET}"
else
echo "${BOLD}${BLUE}⚡️ Creating \"$project_directory/.spin.yml\"...${RESET}"
cp "$SPIN_HOME/templates/common/.spin.example.yml" "$project_directory/.spin.yml"
fi

if [[ ! -f "$project_directory/.vault_password" ]]; then
echo "${BOLD}${YELLOW}⚠️ Your \".spin.yml\" is not encrypted. We HIGHLY reccomend encrypting it. Would you like to encrypt it now?${RESET}"
echo -n "Enter \"y\" or \"n\": "
read -n 1 encrypt_response
echo # move to a new line

if [[ $encrypt_response =~ ^[Yy]$ ]]; then
echo "${BOLD}${BLUE}⚡️ Running Ansible Vault to encrypt \"$project_directory/.spin.yml\"...${RESET}"
echo "${BOLD}${YELLOW}⚠️ NOTE: This password will be required anytime someone needs to change the \".spin.yml\" file.${RESET}"
echo "${BOLD}${YELLOW}We recommend using a RANDOM PASSWORD.${RESET}"
docker run --name spin-ansible --rm --pull always -it -v "$(pwd)/$project_directory":/project -w /project $DEFAULT_ANSIBLE_IMAGE ansible-vault encrypt .spin.yml
echo "${BOLD}${GREEN}\"$project_directory/.spin.yml\" has been encrypted.${RESET}"
echo "${BOLD}${YELLOW}👉 NOTE: You can save this password in \".vault_password\" in the root of your project if you want your secret to be remembered.${RESET}"
elif [[ $encrypt_response =~ ^[Nn]$ ]]; then
echo "${BOLD}${BLUE}👋 Ok, we won't encrypt your \".spin.yml\".${RESET} You can always encrypt it later by running \"spin vault encrypt\"."
else
echo "${BOLD}${RED}❌ Invalid response. Please respond with \"y\" or \"n\".${RESET} Run \"spin init\" to try again."
return 1
fi
fi

echo "${BOLD}${BLUE}🚀 The project, \"$project_name\", is now ready for \"spin up\"!${RESET}"
}
2 changes: 1 addition & 1 deletion lib/actions/latest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ action_latest(){
case $1 in
php)
shift 1
docker run --rm -v $(pwd):/var/www/html $(get_latest_image php) "$@"
docker run --rm -v $(pwd):/var/www/html $DEFAULT_PHP_IMAGE "$@"
;;
*)
echo "\"$1\" is not a valid command. Below are the commands available."
Expand Down
12 changes: 7 additions & 5 deletions lib/actions/new.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ action_new(){
case $1 in
laravel)
shift 1
latest_image=$(get_latest_image php)
latest_image=$DEFAULT_PHP_IMAGE
docker pull $latest_image
docker run --rm -v $(pwd):/var/www/html -e "LOG_LEVEL=off" $latest_image composer create-project laravel/laravel "$@"
install_spin_package_to_project php ${@:-laravel}
docker run --rm -w /var/www/html -v $(pwd):/var/www/html -e "LOG_LEVEL=off" $latest_image composer create-project laravel/laravel "$@"
install_spin_package_to_project php "${@:-laravel}" --force
source "$SPIN_HOME/lib/actions/init.sh"
action_init --template=laravel --project-directory="${@:-laravel}" --force
;;
nuxt)
shift 1
latest_image=$(get_latest_image node)
latest_image=$DEFAULT_NODE_IMAGE
docker pull $latest_image
docker run --rm -it -v $(pwd):/usr/src/app -w /usr/src/app $latest_image npx nuxi@latest init "$@"
install_spin_package_to_project node ${@:-"nuxt-app"}
install_spin_package_to_project node "${@:-nuxt-app}" --force
;;
*)
echo "\"$1\" is not a valid command. Below are the commands available."
Expand Down
110 changes: 63 additions & 47 deletions lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,72 @@

SPIN_CONFIG_FILE_LOCATION="$SPIN_HOME/conf/spin.conf"

### Default Images
DEFAULT_PHP_IMAGE="serversideup/php:beta-cli"
DEFAULT_NODE_IMAGE="node:20"
DEFAULT_ANSIBLE_IMAGE="willhallonline/ansible:alpine"


install_spin_package_to_project() {
read -n 1 -r -p "Do you want to add Spin to your project? (Y/n)"
echo
if [[ $REPLY =~ ^[Yy]$ ]]
then
project_dir="$(pwd)/$2"
case "$1" in
"php")
docker run --rm -v $project_dir:/var/www/html -e "LOG_LEVEL=off" $(get_latest_image php) composer --working-dir=/var/www/html/ require serversideup/spin --dev
;;
"node")
if [[ -f "$project_dir/package-lock.json" && -f "$project_dir/package.json" ]]; then
echo "🧐 I detected a package-lock.json file, so I'll use npm."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $(get_latest_image node) npm install @serversideup/spin --save-dev
elif [[ -f "$project_dir/pnpm-lock.yaml" ]]; then
echo "🧐 I detected a pnpm-lock.yaml file, so I'll use pnpm."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $(get_latest_image node) pnpm add -D @serversideup/spin
elif [[ -f "$project_dir/yarn.lock" ]]; then
echo "🧐 I detected a yarn.lock file, so I'll use yarn."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $(get_latest_image node) yarn add @serversideup/spin --dev
elif [[ -f "$project_dir/Bunfile" || -f "$project_dir/Bunfile.lock" ]]; then
echo "🧐 I detected a Bunfile or Bunfile.lock file, so I'll use bun."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $(get_latest_image node) bun add -d @serversideup/spin
else
echo "Unknown Node project type."
exit 1
fi
;;
*)
echo "Invalid argument. Supported arguments are: php, node."
return 1
;;
esac
# Variables for clearer understanding
framework="$1"
project_name="$2"
force=0

# Loop through arguments
for arg in "$@"; do
case $arg in
--force)
force=1
break
;;
*)
# Handle other arguments or do nothing
;;
esac
done

# Ask for confirmation if not forced
if [[ $force -eq 0 ]]; then
read -n 1 -r -p "Do you want to add Spin to your project? (Y/n) "
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
return 0
fi
fi

echo "${BOLD}${BLUE}⚡️ Adding spin as a $framework development dependency...${RESET}"

project_dir="$(pwd)/$project_name"
case "$framework" in
"php")
docker run --rm -v $project_dir:/var/www/html -e "LOG_LEVEL=off" $DEFAULT_PHP_IMAGE composer --working-dir=/var/www/html/ require serversideup/spin:dev-release/2.0 --dev
;;
"node")
if [[ -f "$project_dir/package-lock.json" && -f "$project_dir/package.json" ]]; then
echo "🧐 I detected a package-lock.json file, so I'll use npm."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $DEFAULT_NODE_IMAGE npm install @serversideup/spin --save-dev
elif [[ -f "$project_dir/pnpm-lock.yaml" ]]; then
echo "🧐 I detected a pnpm-lock.yaml file, so I'll use pnpm."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $DEFAULT_NODE_IMAGE pnpm add -D @serversideup/spin
elif [[ -f "$project_dir/yarn.lock" ]]; then
echo "🧐 I detected a yarn.lock file, so I'll use yarn."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $DEFAULT_NODE_IMAGE yarn add @serversideup/spin --dev
elif [[ -f "$project_dir/Bunfile" || -f "$project_dir/Bunfile.lock" ]]; then
echo "🧐 I detected a Bunfile or Bunfile.lock file, so I'll use bun."
docker run --rm -v $project_dir:/usr/src/app -w /usr/src/app $DEFAULT_NODE_IMAGE bun add -d @serversideup/spin
else
echo "Unknown Node project type."
exit 1
fi
;;
*)
echo "Invalid argument. Supported arguments are: php, node."
return 1
;;
esac

echo "${BOLD}${BLUE}🥳 The project, $project_name, has been created with Spin installed as a development dependency!${RESET}"
}

check_connection_with_cmd() {
Expand Down Expand Up @@ -218,21 +249,6 @@ filter_out_spin_arguments() {
echo "${filtered_args[@]}"
}

get_latest_image() {
case "$1" in
"php")
echo "serversideup/php:beta-cli"
;;
"node")
echo "node:18"
;;
*)
echo "Invalid argument. Supported arguments are: php, node."
return 1
;;
esac
}

is_internet_connected() {
local repo="serversideup/spin"
local branch="main"
Expand Down

0 comments on commit 8cce7de

Please sign in to comment.