From 49dd1dd1fa6bce5fe3a153aeacf6d8dc8c5fc754 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 8 Mar 2023 10:03:39 +1100 Subject: [PATCH 1/9] Update setup script with newest template I used the script from a new Rails 7 project. --- bin/setup | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/bin/setup b/bin/setup index 94fd4d7977..9301073e3f 100755 --- a/bin/setup +++ b/bin/setup @@ -1,36 +1,36 @@ #!/usr/bin/env ruby -require 'fileutils' -include FileUtils +require "fileutils" # path to your application root. -APP_ROOT = File.expand_path('..', __dir__) +APP_ROOT = File.expand_path("..", __dir__) def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") end -chdir APP_ROOT do - # This script is a starting point to setup your application. +FileUtils.chdir APP_ROOT do + # This script is a way to set up or update your development environment automatically. + # This script is idempotent, so that you can run it at any time and get an expectable outcome. # Add necessary setup steps to this file. - puts '== Installing dependencies ==' - system! 'gem install bundler --conservative' - system('bundle check') || system!('bundle install') + puts "== Installing dependencies ==" + system! "gem install bundler --conservative" + system("bundle check") || system!("bundle install") # Install JavaScript dependencies if using Yarn # system('bin/yarn') # puts "\n== Copying sample files ==" - # unless File.exist?('config/database.yml') - # cp 'config/database.yml.sample', 'config/database.yml' + # unless File.exist?("config/database.yml") + # FileUtils.cp "config/database.yml.sample", "config/database.yml" # end puts "\n== Preparing database ==" - system! 'bin/rails db:setup' + system! "bin/rails db:prepare" puts "\n== Removing old logs and tempfiles ==" - system! 'bin/rails log:clear tmp:clear' + system! "bin/rails log:clear tmp:clear" puts "\n== Restarting application server ==" - system! 'bin/rails restart' + system! "bin/rails restart" end From 93ef95f58e95a4a38393f08389d8825a45cba739 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 8 Mar 2023 10:07:15 +1100 Subject: [PATCH 2/9] Update JS dependencies with setup script --- bin/setup | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/setup b/bin/setup index 9301073e3f..4e0b9a4f76 100755 --- a/bin/setup +++ b/bin/setup @@ -17,8 +17,8 @@ FileUtils.chdir APP_ROOT do system! "gem install bundler --conservative" system("bundle check") || system!("bundle install") - # Install JavaScript dependencies if using Yarn - # system('bin/yarn') + # Install JavaScript dependencies + system("bin/yarn") # puts "\n== Copying sample files ==" # unless File.exist?("config/database.yml") From f12ee186ee836aba00f779259c3c35046f1028c7 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 8 Mar 2023 10:17:35 +1100 Subject: [PATCH 3/9] Speed up setup script when restarting rails --- bin/setup | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/setup b/bin/setup index 4e0b9a4f76..976ae4365c 100755 --- a/bin/setup +++ b/bin/setup @@ -32,5 +32,7 @@ FileUtils.chdir APP_ROOT do system! "bin/rails log:clear tmp:clear" puts "\n== Restarting application server ==" - system! "bin/rails restart" + # system! "bin/rails restart" + # Faster to do it manually: + system! "touch tmp/restart.txt" end From 6b857a5683b8c4b7d3e02a1ccb9f4a5c05ebe21e Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 8 Mar 2023 10:41:08 +1100 Subject: [PATCH 4/9] Make code examples easier to copy without $ sign The $ sign used to indicate shell commands. But with markdown it's obvious that we are entering commands. Github has a quick copy button for all code examples which used to copy the $ sign as well. Removing it allows to copy and paste easier with that button. --- GETTING_STARTED.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index 263b90b75c..f82b982c1d 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -48,14 +48,14 @@ Fetch the latest version of `master` from `upstream` (ie. the main repo): First, you need to create the database user the app will use by manually typing the following in your terminal: ```sh -$ sudo -u postgres psql -c "CREATE USER ofn WITH SUPERUSER CREATEDB PASSWORD 'f00d'" +sudo -u postgres psql -c "CREATE USER ofn WITH SUPERUSER CREATEDB PASSWORD 'f00d'" ``` This will create the "ofn" user as superuser and allowing it to create databases. If this command fails, check the [troubleshooting section](#creating-the-database) for an alternative. Next, it is _strongly recommended_ to run the setup script. ```sh -$ script/setup +./script/setup ``` If the script succeeds you're ready to start developing. If not, take a look at the output as it should be informative enough to help you troubleshoot. @@ -114,13 +114,13 @@ Below are fixes to potential issues that can happen during the installation proc #### Creating the database -If the ```$ sudo -u postgres psql -c "CREATE USER ofn WITH SUPERUSER CREATEDB PASSWORD 'f00d'"``` command doesn't work, you can run the following commands instead: +If the `sudo -u postgres psql -c "CREATE USER ofn WITH SUPERUSER CREATEDB PASSWORD 'f00d'"` command doesn't work, you can run the following commands instead: ``` -$ createuser --superuser --pwprompt ofn -Enter password for new role: f00d -Enter it again: f00d -$ createdb open_food_network_dev --owner=ofn -$ createdb open_food_network_test --owner=ofn +createuser --superuser --pwprompt ofn +# Enter password for new role: f00d +# Enter it again: f00d +createdb open_food_network_dev --owner=ofn +createdb open_food_network_test --owner=ofn ``` If these commands succeed, you should be able to [continue the setup process](#get-it-running). From 4999e231bcf79aec2a4e50833ffcff6591eaea00 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 17 Mar 2023 15:41:29 +1100 Subject: [PATCH 5/9] Avoid permission warning when running sudo as root The command is switching to an unprivileged user which can't access /root and therefore there was a warning when executing. Adding `--login` to the sudo command switches to that user properly and avoids the warning. --- GETTING_STARTED.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GETTING_STARTED.md b/GETTING_STARTED.md index f82b982c1d..65b8cca69d 100644 --- a/GETTING_STARTED.md +++ b/GETTING_STARTED.md @@ -48,7 +48,7 @@ Fetch the latest version of `master` from `upstream` (ie. the main repo): First, you need to create the database user the app will use by manually typing the following in your terminal: ```sh -sudo -u postgres psql -c "CREATE USER ofn WITH SUPERUSER CREATEDB PASSWORD 'f00d'" +sudo --login --user=postgres psql -c "CREATE USER ofn WITH SUPERUSER CREATEDB PASSWORD 'f00d'" ``` This will create the "ofn" user as superuser and allowing it to create databases. If this command fails, check the [troubleshooting section](#creating-the-database) for an alternative. From e5e5bf8c17a206eddf00b3fbbf752555d735697f Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 17 Mar 2023 15:59:31 +1100 Subject: [PATCH 6/9] Install ruby automatically if rbenv is available People may use other ways to provide the right Ruby version but if they use rbenv then we can use it automatically. --- script/rbenv-install.sh | 4 ++++ script/setup | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/script/rbenv-install.sh b/script/rbenv-install.sh index 71347a0ef7..e3a2d52fac 100755 --- a/script/rbenv-install.sh +++ b/script/rbenv-install.sh @@ -2,6 +2,10 @@ # # Install our selected Ruby version defined in the .ruby-version file. # +# Requires: +# - [rbenv](https://github.com/rbenv/rbenv#readme) +# - [ruby-build](https://github.com/rbenv/ruby-build#readme) +# # If our ruby-build version is outdated and it can't build the version we want # then we try upgrading ruby-build and installing again. diff --git a/script/setup b/script/setup index 73100989ad..e43f61b7e4 100755 --- a/script/setup +++ b/script/setup @@ -17,11 +17,13 @@ YELLOW='\033[0;33m' RED='\033[0;31m' NO_COLOR='\033[0m' -RUBY_VERSION=$(cat .ruby-version) - # Check ruby version -if ! ruby --version | grep $RUBY_VERSION > /dev/null; then - printf "${RED}Open Food Network requires ruby ${RUBY_VERSION}${NO_COLOR}. Have a look at: https://github.com/rbenv/rbenv\n" +RUBY_VERSION=$(cat .ruby-version) +if command -v rbenv > /dev/null; then + ./script/rbenv-install.sh +elif ! ruby --version | grep $RUBY_VERSION > /dev/null; then + printf "${RED}Open Food Network requires ruby ${RUBY_VERSION}${NO_COLOR}. " + printf "Have a look at: https://github.com/rbenv/rbenv\n" exit 1 fi From 477524039f757d4a018241f7abcb9979998b82ac Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 17 Mar 2023 16:25:17 +1100 Subject: [PATCH 7/9] Update bundler version when necessary Not just when it's missing. --- script/setup | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/script/setup b/script/setup index e43f61b7e4..64bec13d4f 100755 --- a/script/setup +++ b/script/setup @@ -27,10 +27,8 @@ elif ! ruby --version | grep $RUBY_VERSION > /dev/null; then exit 1 fi -# Set up Ruby dependencies via Bundler -if ! command -v bundle > /dev/null; then - ./script/install-bundler -fi +# Install used Bundler version +./script/install-bundler # Install all dependencies bundle check || bundle install From 08f2254a0e510981b98035fc6daedbe4a467bb1e Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 20 Mar 2023 12:40:45 +1100 Subject: [PATCH 8/9] Leverage bin/setup within script/setup And remove duplicate output. `bin/setup` is the Rails default for updating your environment after code updates and `script/setup` is our convenience script for the initial setup and starting with sample data. --- script/setup | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/script/setup b/script/setup index 64bec13d4f..d0c84566dc 100755 --- a/script/setup +++ b/script/setup @@ -30,14 +30,8 @@ fi # Install used Bundler version ./script/install-bundler -# Install all dependencies -bundle check || bundle install -yarn install - -# Set up the database for both development and test -# Confirming the default user and password -printf '\n\n' | bundle exec rake db:setup db:test:prepare -printf '\n' +# Install dependencies, prepare database and tidy +./bin/setup # Load some default data for your environment bundle exec rake ofn:sample_data From da0ef8531ba245cd688921e19d3750d5e60507e8 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Mon, 20 Mar 2023 12:55:09 +1100 Subject: [PATCH 9/9] Install our used bundler version --- bin/setup | 2 +- script/setup | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/bin/setup b/bin/setup index 976ae4365c..bff486cc34 100755 --- a/bin/setup +++ b/bin/setup @@ -14,7 +14,7 @@ FileUtils.chdir APP_ROOT do # Add necessary setup steps to this file. puts "== Installing dependencies ==" - system! "gem install bundler --conservative" + system! "script/install-bundler" system("bundle check") || system!("bundle install") # Install JavaScript dependencies diff --git a/script/setup b/script/setup index d0c84566dc..d8f03ae63b 100755 --- a/script/setup +++ b/script/setup @@ -27,9 +27,6 @@ elif ! ruby --version | grep $RUBY_VERSION > /dev/null; then exit 1 fi -# Install used Bundler version -./script/install-bundler - # Install dependencies, prepare database and tidy ./bin/setup