forked from clio/ten_years_rails
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from fastruby/add-next_rails--init-command
- Loading branch information
Showing
6 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |