Skip to content

Commit 9e470b8

Browse files
authored
Merge pull request #139 from fastruby/add-next_rails--init-command
2 parents 8cdd756 + d678853 commit 9e470b8

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# main [(unreleased)](https://github.com/fastruby/next_rails/compare/v1.4.2...main)
22

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

56
* Your changes/patches go here.

exe/next.sh

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22
if [[ "${@}" == "--init" ]]; then
3+
echo "The next --init command is deprecated. Please use the next_rails --init command instead."
34
# Add next? top of Gemfile
45
cat <<-STRING > Gemfile.tmp
56
def next?

exe/next_rails

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env ruby
22
require "optparse"
33
require "next_rails/version"
4+
require "next_rails"
45

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

15+
opts.on("--init", "Setup the dual-boot configuration") do
16+
options[:init] = true
17+
end
18+
1419
opts.on("--version", "show version of the gem") do
1520
options[:version] = true
1621
end
@@ -23,6 +28,8 @@ end
2328

2429
option_parser.parse!
2530

31+
puts NextRails::Init.call if options.fetch(:init, false)
32+
2633
if options.fetch(:version, false)
2734
puts NextRails::VERSION
2835
exit 2

lib/next_rails.rb

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "next_rails/gem_info"
44
require "next_rails/version"
5+
require "next_rails/init"
56
require "next_rails/bundle_report"
67
require "next_rails/bundle_report/ruby_version_compatibility"
78
require "deprecation_tracker"

lib/next_rails/init.rb

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# frozen_string_literal: true
2+
3+
require 'fileutils'
4+
5+
module NextRails
6+
# This class is responsible for installing the dual-boot files for your.
7+
class Init
8+
def self.call
9+
new.call
10+
end
11+
12+
def call
13+
return gemfiles_message unless gemfiles?
14+
return next_gemfiles_message if next_gemfiles?
15+
16+
add_next_conditional
17+
create_sym_link
18+
copy_gemfile_lock
19+
message
20+
end
21+
22+
private
23+
24+
def gemfiles?
25+
%w[Gemfile Gemfile.lock].any? { |file| File.exist?(file) }
26+
end
27+
28+
def gemfiles_message
29+
'You must have a Gemfile and Gemfile.lock to run the next_rails --init command.'
30+
end
31+
32+
def next_gemfiles?
33+
%w[Gemfile.next Gemfile.next.lock].any? { |file| File.exist?(file) }
34+
end
35+
36+
def next_gemfiles_message
37+
'The next_rails --init command has already been run.'
38+
end
39+
40+
def add_next_conditional
41+
File.open('Gemfile.tmp', 'w') do |file|
42+
file.write <<-STRING
43+
def next?
44+
File.basename(__FILE__) == "Gemfile.next"
45+
end
46+
STRING
47+
end
48+
49+
File.open('Gemfile', 'r') do |original|
50+
File.open('Gemfile.tmp', 'a') do |temp|
51+
temp.write(original.read)
52+
end
53+
end
54+
55+
File.rename('Gemfile.tmp', 'Gemfile')
56+
end
57+
58+
def create_sym_link
59+
File.symlink('Gemfile', 'Gemfile.next')
60+
end
61+
62+
def copy_gemfile_lock
63+
FileUtils.cp('Gemfile.lock', 'Gemfile.next.lock')
64+
end
65+
66+
def message
67+
<<-MESSAGE
68+
Created Gemfile.next (a symlink to your Gemfile). Your Gemfile has been modified to support dual-booting!
69+
70+
There's just one more step: modify your Gemfile to use a newer version of Rails using the \`next?\` helper method.
71+
72+
For example, here's how to go from 5.2.8.1 to 6.0.6.1:
73+
74+
if next?
75+
gem "rails", "6.0.6.1"
76+
else
77+
gem "rails", "5.2.8.1"
78+
end
79+
MESSAGE
80+
end
81+
end
82+
end

spec/next_rails/init_spec.rb

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
require 'fileutils'
5+
6+
RSpec.describe NextRails::Init do
7+
let(:gemfile_content) { "source 'https://rubygems.org'\ngem 'rails'\n" }
8+
9+
before(:all) do
10+
FileUtils.cp('Gemfile', 'Gemfile.original')
11+
end
12+
13+
after(:all) do
14+
FileUtils.cp('Gemfile.original', 'Gemfile')
15+
FileUtils.rm_f('Gemfile.original')
16+
end
17+
18+
before do
19+
FileUtils.rm_f('Gemfile')
20+
FileUtils.rm_f('Gemfile.lock')
21+
FileUtils.rm_f('Gemfile.next')
22+
FileUtils.rm_f('Gemfile.next.lock')
23+
end
24+
25+
after do
26+
FileUtils.rm_f('Gemfile')
27+
FileUtils.rm_f('Gemfile.lock')
28+
FileUtils.rm_f('Gemfile.next')
29+
FileUtils.rm_f('Gemfile.next.lock')
30+
end
31+
32+
describe '.call' do
33+
it 'already has next Gemfile files' do
34+
File.write('Gemfile', gemfile_content)
35+
FileUtils.touch('Gemfile.lock')
36+
File.write('Gemfile.next', gemfile_content)
37+
38+
expect(described_class.call).to eq('The next_rails --init command has already been run.')
39+
end
40+
41+
it 'does not have Gemfile files' do
42+
expect(described_class.call).to eq('You must have a Gemfile and Gemfile.lock to run the next_rails --init command.')
43+
end
44+
45+
it 'creates Gemfile.next and Gemfile.next.lock' do
46+
File.write('Gemfile', gemfile_content)
47+
FileUtils.touch('Gemfile.lock')
48+
49+
expect do
50+
described_class.call
51+
end.to change { File.exist?('Gemfile.next') }.from(false).to(true)
52+
.and change { File.exist?('Gemfile.next.lock') }.from(false).to(true)
53+
end
54+
55+
it 'returns a success message' do
56+
File.write('Gemfile', gemfile_content)
57+
FileUtils.touch('Gemfile.lock')
58+
59+
message = described_class.call
60+
expect(message).to include('Created Gemfile.next (a symlink to your Gemfile).')
61+
expect(message).to include("For example, here's how to go from 5.2.8.1 to 6.0.6.1:")
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)