Skip to content

Add next_rails --init to replace next --init #139

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

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# main [(unreleased)](https://github.com/fastruby/next_rails/compare/v1.4.2...main)

- [Add next_rails --init](https://github.com/fastruby/next_rails/pull/139)
- [Add Ruby 3.4 support](https://github.com/fastruby/next_rails/pull/133)

* Your changes/patches go here.
Expand Down
1 change: 1 addition & 0 deletions exe/next.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
if [[ "${@}" == "--init" ]]; then
echo "The next --init command is deprecated. Please use the next_rails --init command instead."
# Add next? top of Gemfile
cat <<-STRING > Gemfile.tmp
def next?
Expand Down
7 changes: 7 additions & 0 deletions exe/next_rails
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env ruby
require "optparse"
require "next_rails/version"
require "next_rails"

options = {}
option_parser = OptionParser.new do |opts|
Expand All @@ -11,6 +12,10 @@ option_parser = OptionParser.new do |opts|
bin/next_rails --version info # Show the version of the gem installed
MESSAGE

opts.on("--init", "Setup the dual-boot configuration") do
options[:init] = true
end

opts.on("--version", "show version of the gem") do
options[:version] = true
end
Expand All @@ -23,6 +28,8 @@ end

option_parser.parse!

puts NextRails::Init.call if options.fetch(:init, false)

if options.fetch(:version, false)
puts NextRails::VERSION
exit 2
Expand Down
1 change: 1 addition & 0 deletions lib/next_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "next_rails/gem_info"
require "next_rails/version"
require "next_rails/init"
require "next_rails/bundle_report"
require "next_rails/bundle_report/ruby_version_compatibility"
require "deprecation_tracker"
Expand Down
82 changes: 82 additions & 0 deletions lib/next_rails/init.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'fileutils'

module NextRails
# This class is responsible for installing the dual-boot files for your.
class Init
def self.call
new.call
end

def call
return gemfiles_message unless gemfiles?
return next_gemfiles_message if next_gemfiles?

add_next_conditional
create_sym_link
copy_gemfile_lock
message
end

private

def gemfiles?
%w[Gemfile Gemfile.lock].any? { |file| File.exist?(file) }
end

def gemfiles_message
'You must have a Gemfile and Gemfile.lock to run the next_rails --init command.'
end

def next_gemfiles?
%w[Gemfile.next Gemfile.next.lock].any? { |file| File.exist?(file) }
end

def next_gemfiles_message
'The next_rails --init command has already been run.'
end

def add_next_conditional
File.open('Gemfile.tmp', 'w') do |file|
file.write <<-STRING
def next?
File.basename(__FILE__) == "Gemfile.next"
end
STRING
end

File.open('Gemfile', 'r') do |original|
File.open('Gemfile.tmp', 'a') do |temp|
temp.write(original.read)
end
end

File.rename('Gemfile.tmp', 'Gemfile')
end

def create_sym_link
File.symlink('Gemfile', 'Gemfile.next')
end

def copy_gemfile_lock
FileUtils.cp('Gemfile.lock', 'Gemfile.next.lock')
end

def message
<<-MESSAGE
Created Gemfile.next (a symlink to your Gemfile). Your Gemfile has been modified to support dual-booting!

There's just one more step: modify your Gemfile to use a newer version of Rails using the \`next?\` helper method.

For example, here's how to go from 5.2.8.1 to 6.0.6.1:

if next?
gem "rails", "6.0.6.1"
else
gem "rails", "5.2.8.1"
end
MESSAGE
end
end
end
64 changes: 64 additions & 0 deletions spec/next_rails/init_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# frozen_string_literal: true

require 'spec_helper'
require 'fileutils'

RSpec.describe NextRails::Init do
let(:gemfile_content) { "source 'https://rubygems.org'\ngem 'rails'\n" }

before(:all) do
FileUtils.cp('Gemfile', 'Gemfile.original')
end

after(:all) do
FileUtils.cp('Gemfile.original', 'Gemfile')
FileUtils.rm_f('Gemfile.original')
end

before do
FileUtils.rm_f('Gemfile')
FileUtils.rm_f('Gemfile.lock')
FileUtils.rm_f('Gemfile.next')
FileUtils.rm_f('Gemfile.next.lock')
end

after do
FileUtils.rm_f('Gemfile')
FileUtils.rm_f('Gemfile.lock')
FileUtils.rm_f('Gemfile.next')
FileUtils.rm_f('Gemfile.next.lock')
end

describe '.call' do
it 'already has next Gemfile files' do
File.write('Gemfile', gemfile_content)
FileUtils.touch('Gemfile.lock')
File.write('Gemfile.next', gemfile_content)

expect(described_class.call).to eq('The next_rails --init command has already been run.')
end

it 'does not have Gemfile files' do
expect(described_class.call).to eq('You must have a Gemfile and Gemfile.lock to run the next_rails --init command.')
end

it 'creates Gemfile.next and Gemfile.next.lock' do
File.write('Gemfile', gemfile_content)
FileUtils.touch('Gemfile.lock')

expect do
described_class.call
end.to change { File.exist?('Gemfile.next') }.from(false).to(true)
.and change { File.exist?('Gemfile.next.lock') }.from(false).to(true)
end

it 'returns a success message' do
File.write('Gemfile', gemfile_content)
FileUtils.touch('Gemfile.lock')

message = described_class.call
expect(message).to include('Created Gemfile.next (a symlink to your Gemfile).')
expect(message).to include("For example, here's how to go from 5.2.8.1 to 6.0.6.1:")
end
end
end