Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Merge pull request #2 from jollygoodcode/preserve-ruby-section
Preserve ruby section
- Loading branch information
Showing
22 changed files
with
1,339 additions
and
58 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,4 @@ | |
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
spec/examples.txt | ||
/spec/examples.txt |
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 |
---|---|---|
@@ -1,3 +1,2 @@ | ||
--require spec_helper | ||
--format documentation | ||
--color |
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 |
---|---|---|
|
@@ -7,4 +7,5 @@ group :development do | |
gem "bundler" | ||
gem "rake" | ||
gem "rspec", "~> 3.4" | ||
gem "pry" | ||
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 |
---|---|---|
@@ -1,12 +1,25 @@ | ||
require "lockfile_preserver/version" | ||
require "lockfile_preserver/pipeline" | ||
require "lockfile_preserver/bundled_with" | ||
require "lockfile_preserver/ruby_version" | ||
|
||
module LockfilePreserver | ||
def self.keep(original, updated, section = :bundled_with) | ||
if section == :bundled_with | ||
LockfilePreserver::BundledWith.new(original, updated).keep | ||
else | ||
abort "We currently only support preserve BUNDLED_WITH section of lockfile." | ||
elsif section == :ruby_version | ||
LockfilePreserver::RubyVersion.new(original, updated).keep | ||
elsif | ||
abort %(We currently only support preserve "BUNDLED WITH" & "RUBY VERSION" section of lockfile.) | ||
end | ||
end | ||
|
||
def self.keep_all(original, updated) | ||
pipeline = Pipeline.new [ | ||
LockfilePreserver::BundledWith, | ||
LockfilePreserver::RubyVersion | ||
] | ||
|
||
pipeline.call(original, updated) | ||
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
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,21 @@ | ||
module LockfilePreserver | ||
class Pipeline | ||
def initialize(preservers) | ||
@preservers = preservers | ||
end | ||
|
||
def call(original, updated) | ||
result = updated | ||
|
||
preservers.each do |preserver| | ||
result = preserver.new(original, result).keep | ||
end | ||
|
||
result | ||
end | ||
|
||
private | ||
|
||
attr_reader :preservers | ||
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,46 @@ | ||
module LockfilePreserver | ||
class RubyVersion | ||
def initialize(original, updated) | ||
@original = original | ||
@updated = updated | ||
end | ||
|
||
def keep | ||
if original.include? RUBY_VERSION | ||
keep_ruby_version | ||
else | ||
remove_ruby_version | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :original, :updated | ||
|
||
RUBY_VERSION = "RUBY VERSION".freeze | ||
VERSION_PATTERN = %r{.+} | ||
REGEXP = %r{\n\nRUBY VERSION\n\s+(?<version>#{VERSION_PATTERN})\n*} | ||
NEW_LINE = "\n".freeze | ||
|
||
private_constant :RUBY_VERSION | ||
private_constant :REGEXP | ||
private_constant :NEW_LINE | ||
|
||
def keep_ruby_version | ||
updated.sub(REGEXP, ruby_version_section) | ||
end | ||
|
||
def remove_ruby_version | ||
updated.sub(REGEXP, NEW_LINE) | ||
end | ||
|
||
def ruby_version_section | ||
"\n\nRUBY VERSION\n" \ | ||
" #{ruby_version}\n" | ||
end | ||
|
||
def ruby_version | ||
@_ruby_version ||= original.match(REGEXP)[:version] | ||
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
Oops, something went wrong.