Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update setup script with newest template and merge with old script #10529

Merged
merged 9 commits into from
Apr 3, 2023

Conversation

mkllnk
Copy link
Member

@mkllnk mkllnk commented Mar 7, 2023

What? Why?

Rails has a default location for setup scripts and newer versions are idempotent. You can use them to update your app as well. It helps devs to run all needed commands after switching branches.

What should we test?

Dev Test

  • Run ./bin/setup and everything should work.
  • Run ./script/setup and it should work as well.

Release notes

Changelog Category: Technical changes

The title of the pull request will be included in the release notes.

Dependencies

Documentation updates

@mkllnk mkllnk added the dev-test A dev need to test this one label Mar 7, 2023
@mkllnk mkllnk self-assigned this Mar 7, 2023
@mkllnk mkllnk marked this pull request as ready for review March 7, 2023 23:56
Copy link
Contributor

@jibees jibees left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

👌

Thanks! 😎

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea to have a single, modern setup script. However I think we're losing some advantages of the old script.
Can we incorporate them into one?

Or what if we updated script/setup to first call bin/setup, then run the extra improvements on top of that. Then bin/setup can remain as the Rails template.

bin/setup Outdated
# Install JavaScript dependencies if using Yarn
# system('bin/yarn')
# Install JavaScript dependencies
system("yarn")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do have a bin/yarn script, so why not use that as per the template? (although I'm not sure it really provides any benefit)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the next commit provides a clue. It's not quite as quick.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't actually see that we had bin/yarn. I'll revise this commit.

script/setup Show resolved Hide resolved

# Set up Ruby dependencies via Bundler
if ! command -v bundle > /dev/null; then
./script/install-bundler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script actually checks the Bundler version, and installs new versions if needed, whereas bin/setup doesn't. I think it would be good to incorporate that to be more idempotent.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow I thought that newer version of bundler automatically use the right version but that doesn't seem to be true. I misremembered the logic of https://bundler.io/guides/bundler_2_upgrade.html#version-autoswitch.

So yes, we should run this as well.

RUBY_VERSION=$(cat .ruby-version)

# Check ruby version
if ! ruby --version | grep $RUBY_VERSION > /dev/null; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check seems pretty useful. I would suggest we keep it.

I can also see possibility to improve and add a node version check also.
Maybe even conditionally run rbenv and nodenv if installed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. When I removed that I thought that we can just run script/rbenv-install but then I realised that this bin/setup is a Ruby script and won't run before installing ruby. And even when Ruby and rbenv are present then rbenv complaints that it can't find the right version.

So maybe that's another reason to keep script/setup to run the ruby installation.

Copy link
Member Author

@mkllnk mkllnk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the thorough review. I will make some changes and ping you again for re-review before rewriting history.

bin/setup Outdated
# Install JavaScript dependencies if using Yarn
# system('bin/yarn')
# Install JavaScript dependencies
system("yarn")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I didn't actually see that we had bin/yarn. I'll revise this commit.

RUBY_VERSION=$(cat .ruby-version)

# Check ruby version
if ! ruby --version | grep $RUBY_VERSION > /dev/null; then
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. When I removed that I thought that we can just run script/rbenv-install but then I realised that this bin/setup is a Ruby script and won't run before installing ruby. And even when Ruby and rbenv are present then rbenv complaints that it can't find the right version.

So maybe that's another reason to keep script/setup to run the ruby installation.


# Set up Ruby dependencies via Bundler
if ! command -v bundle > /dev/null; then
./script/install-bundler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow I thought that newer version of bundler automatically use the right version but that doesn't seem to be true. I misremembered the logic of https://bundler.io/guides/bundler_2_upgrade.html#version-autoswitch.

So yes, we should run this as well.

script/setup Show resolved Hide resolved
@mkllnk mkllnk force-pushed the setup branch 2 times, most recently from e925887 to ce10adf Compare March 20, 2023 01:44
@mkllnk mkllnk requested a review from dacook March 20, 2023 01:55
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I think I found a bug though.

I note that bin/setup has some variations from the default now, which might not be obvious if updating for a new version in the future. But I doubt this matters.

script/setup Show resolved Hide resolved
@mkllnk mkllnk requested a review from dacook April 3, 2023 01:32
Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

mkllnk added 9 commits April 3, 2023 14:32
I used the script from a new Rails 7 project.
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.
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.
People may use other ways to provide the right Ruby version but if they
use rbenv then we can use it automatically.
Not just when it's missing.
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.
@dacook
Copy link
Member

dacook commented Apr 3, 2023

I'm guessing just rebased?
I just came back to this and tested it, seems to work well.

Except that I think the sample_data task is not quite robust enough. My dev data seems to have broken it:

[ofn:sample_data] - cart order
rake aborted!
NoMethodError: undefined method `id' for nil:NilClass
/Users/dcook/projects/openfoodnetwork/lib/tasks/sample_data/order_factory.rb:66:in `create_order'
/Users/dcook/projects/openfoodnetwork/lib/tasks/sample_data/order_factory.rb:47:in `create_cart_order'

Looks like I have an outgoing exchange with no variants which has tripped it up. But I think that's a problem with that task, so all good! ✅

@mkllnk
Copy link
Member Author

mkllnk commented Apr 3, 2023

Thanks. Merging.

@mkllnk mkllnk merged commit de58aa1 into openfoodfoundation:master Apr 3, 2023
@mkllnk mkllnk deleted the setup branch April 3, 2023 05:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev-test A dev need to test this one
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants