mattbrictson / tomo

A friendly CLI for deploying Rails apps ✨

Home Page:https://tomo.mattbrictson.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Take rbenv_ruby_version from .ruby-version file by default

uxxman opened this issue Β· comments

It is a common convention in rbenv (even rvm) projects to define .ruby-version file for the project to automatically switch to the right ruby version. Rails projects also generates this file for you by default. So, instead of setting this value at 2 different places and risking them to get out of sync during an upgrade, can we make the default value of rbenv_ruby_version to consider .ruby-version file if it exists? For example

rbenv_ruby_version: nil

with

rbenv_ruby_version: File.exist?(".ruby-version") ? File.read(".ruby-version").strip : nil

I think this could work. In fact, tomo does something similar for bundler where if bundler_version is nil, it will read the bundler version from the Gemfile.lock:

needed_bundler_ver = version_setting || extract_bundler_ver_from_lockfile

Generally speaking I try to avoid that kind of magic, though. Different deployment tools use different files. Heroku, for example, looks for a ruby declaration inside the Gemfile and does not use .ruby-version. So that inconsistent magic might confuse people.

But, considering that rails new generates a .ruby-version by default, this does seem reasonable. So I am willing to try it.

I guess that means that going forward, tomo init would no longer automatically put the Ruby version in the .tomo/config.yml file for you? In other words, we'd remove this line:

set rbenv_ruby_version: <%= RUBY_VERSION.inspect %>

In terms of reading the .ruby-version file, I think that needs to be done server-side. Because you want the version of Ruby installed to match the code that is being deployed, not what you have locally. In some cases they might be different; e.g. if you have the develop branch checked out locally but you are asking tomo to deploy main.

So something like the bundler plugin does server-side:

def extract_bundler_ver_from_lockfile
lockfile_tail = remote.capture(
"tail", "-n", "10", paths.release.join("Gemfile.lock"),
raise_on_error: false
)
version = lockfile_tail[/BUNDLED WITH\n (\S+)$/, 1]
return version if version || dry_run?
die <<~REASON
Could not guess bundler version from Gemfile.lock.
Use the :bundler_version setting to specify the version of bundler to install.
REASON
end

Does that sound right?

If you want to put together a PR, feel free to give it a shot. I'd appreciate more contributors. πŸ™‚

I guess that means that going forward, tomo init would no longer automatically put the Ruby version in the .tomo/config.yml file for you? In other words, we'd remove this line:

Exactly πŸ‘ One less config to worry about πŸ˜‰

In terms of reading the .ruby-version file, I think that needs to be done server-side. Because you want the version of Ruby installed to match the code that is being deployed, not what you have locally. In some cases they might be different; e.g. if you have the develop branch checked out locally but you are asking tomo to deploy main.

Again agreed πŸ‘ They can surely differ in some scenarios and the server side value is important, not the local one.

If you want to put together a PR, feel free to give it a shot. I'd appreciate more contributors. πŸ™‚

Sure, would love to ❀️ I will checkout the code in bundler plugin and try to put together a PR using something similar for rbenv ruby version config πŸ‘

Thanks for the merge πŸ‘ πŸ˜„ closing the issue