diff --git a/bin/setup b/bin/setup index c240b96..5905fe9 100755 --- a/bin/setup +++ b/bin/setup @@ -1,77 +1,107 @@ -#!/usr/bin/env ruby -require "fileutils" -require "socket" -require "timeout" +#!/usr/bin/env bash +set -eo pipefail -REDIS_PORT = 6379 -REDIS_HOST = "localhost" -APP_ROOT = File.expand_path("..", __dir__) +# Prefer app executables +app_root="$( + cd "$(dirname "$0")/.." + pwd +)" +export PATH="$app_root/bin:$PATH" -def system!(*args) - system(*args, exception: true) -end +REDIS_PORT=6379 +REDIS_HOST=localhost -def installed?(tool) - system("command -v #{tool} > /dev/null 2>&1") -end +if [ "$RAILS_ENV" = "production" ]; then + echo "RAILS_ENV is production; bailing out" + exit 1 +fi -def redis_running? - Timeout::timeout(3) do - socket = TCPSocket.new(REDIS_HOST, REDIS_PORT) - socket.close - return true - end -rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError, Timeout::Error - return false -end - -if ENV["RAILS_ENV"] == "production" - puts "RAILS_ENV is production; bailing out" - exit -end - -FileUtils.chdir APP_ROOT do - puts "== Installing dependencies ==" - - if installed?("brew") - system "brew install sqlite ffmpeg mise" - elsif installed?("pacman") - system "sudo pacman -S --noconfirm --needed sqlite ffmpeg mise" - elsif installed?("apt") - system "sudo apt-get install --no-install-recommends -y libsqlite3-0 ffmpeg" - end - - if installed?("mise") - system "mise install" +# Install gum if needed +if ! command -v gum &>/dev/null; then + echo + echo "▸ Installing gum" + if command -v pacman &>/dev/null; then + sudo pacman -S --noconfirm gum + elif command -v brew &>/dev/null; then + brew install gum else - puts "Couldn't install mise" - puts "Install mise using your package manager or via:" - puts "https://mise.jdx.dev/installing-mise.html" + echo "Please install gum: https://github.com/charmbracelet/gum" exit 1 - end + fi + echo +fi - system("bundle check") || system!("bundle install") +step() { + local step_name="$1" + shift - puts "\n== Preparing database ==" - if ARGV.include?("--reset") - system "rm -rf ./storage/{db,files}" - system! "bin/rails db:reset" - end - system! "bin/rails db:prepare" + gum style --foreground 208 --bold "▸ $step_name" + gum style --foreground 240 "$*" - unless redis_running? - if installed?("docker") - system("docker run -d --name campfire-redis -p #{REDIS_PORT}:#{REDIS_PORT} redis:7") + "$@" + + local exit_code=$? + echo + return $exit_code +} + +redis_running() { + nc -z "$REDIS_HOST" "$REDIS_PORT" 2>/dev/null +} + +echo +gum style --foreground 214 " ) " +gum style --foreground 208 " ) \\ campfire" +gum style --foreground 202 " ( ( (" +gum style --foreground 94 " .^^^." +echo + +# Install dependencies +if command -v brew &>/dev/null; then + step "Installing packages" brew install sqlite ffmpeg mise +elif command -v pacman &>/dev/null; then + step "Installing packages" sudo pacman -S --noconfirm --needed sqlite ffmpeg mise +elif command -v apt &>/dev/null; then + step "Installing packages" sudo apt-get install --no-install-recommends -y libsqlite3-0 ffmpeg +fi + +if ! command -v mise &>/dev/null; then + echo "Couldn't install mise" + echo "Install mise using your package manager or via:" + echo "https://mise.jdx.dev/installing-mise.html" + exit 1 +fi + +step "Installing Ruby" mise install --yes +eval "$(mise hook-env)" + +bundle config set --local auto_install true +step "Installing RubyGems" bundle install + +# Prepare database +if [[ $* == *--reset* ]]; then + rm -rf ./storage/{db,files} + step "Resetting the database" rails db:reset +fi +step "Preparing the database" rails db:prepare + +# Start Redis if not running +if ! redis_running; then + if command -v docker &>/dev/null; then + if docker ps -aq -f name=campfire-redis | grep -q .; then + step "Starting Redis" docker start campfire-redis else - puts "Couldn't start Redis" - puts "Install either docker or redis and then run this command again" - exit 1 - end - end + step "Setting up Redis" docker run -d --name campfire-redis -p "$REDIS_PORT:$REDIS_PORT" redis:7 + fi + else + echo "Couldn't start Redis" + echo "Install either docker or redis and then run this command again" + exit 1 + fi +fi - puts "\n== Removing old logs and tempfiles ==" - system! "bin/rails log:clear tmp:clear" +step "Cleaning up logs and tempfiles" rails log:clear tmp:clear - puts "\n== Restarting services ==" - system! "bin/rails restart" -end +step "Restarting services" rails restart + +gum style --foreground 46 "✓ Done (${SECONDS} sec)"