Skip to content

Commit

Permalink
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
JuanitoFatas committed Jun 2, 2016
2 parents 2c1cbab + e1da7d5 commit 17d1962
Show file tree
Hide file tree
Showing 22 changed files with 1,339 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -7,4 +7,4 @@
/pkg/
/spec/reports/
/tmp/
spec/examples.txt
/spec/examples.txt
1 change: 0 additions & 1 deletion .rspec
@@ -1,3 +1,2 @@
--require spec_helper
--format documentation
--color
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -3,8 +3,10 @@ cache: bundler
language: ruby
bundler_args: --without debugging
rvm:
- 2.3.1
- 2.3.0
- 2.2.4
- 2.1.9
- ruby-head
matrix:
allow_failures:
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,14 @@

## Unreleased

- Introduce `LockfilePreserver::RubyVersion` [#2][pr2]
- Introduce `LockfilePreserver::Pipeline` [#2][pr2]
- Refactor spec fixture originations [#2][pr2]
- `.keep` now accepts `:ruby_version` to preserve Ruby Version section from the lockfile [#2][pr2]
- new `.keep_all` method to preserve BUNDLED WITH and RUBY VERSION section from the lockfile [#2][pr2]

[pr2]: https://github.com/jollygoodcode/lockfile_preserver/pull/2

## 1.0.0 - 2016.03.08

Init LockfilePreserver.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -7,4 +7,5 @@ group :development do
gem "bundler"
gem "rake"
gem "rspec", "~> 3.4"
gem "pry"
end
17 changes: 15 additions & 2 deletions lib/lockfile_preserver.rb
@@ -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
10 changes: 5 additions & 5 deletions lib/lockfile_preserver/bundled_with.rb
@@ -1,8 +1,8 @@
module LockfilePreserver
class BundledWith
def initialize(original, current)
def initialize(original, updated)
@original = original
@current = current
@updated = updated
end

def keep
Expand All @@ -15,7 +15,7 @@ def keep

private

attr_reader :original, :current
attr_reader :original, :updated

BUNDLED_WITH = "BUNDLED WITH".freeze
REGEXP = %r{\n\nBUNDLED WITH\n\s+(?<version>#{Gem::Version::VERSION_PATTERN})\n*}
Expand All @@ -26,11 +26,11 @@ def keep
private_constant :NEW_LINE

def keep_bundled_with
current.sub(REGEXP, bundled_with)
updated.sub(REGEXP, bundled_with)
end

def remove_bundled_with
current.sub(REGEXP, NEW_LINE)
updated.sub(REGEXP, NEW_LINE)
end

def bundled_with
Expand Down
21 changes: 21 additions & 0 deletions lib/lockfile_preserver/pipeline.rb
@@ -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
46 changes: 46 additions & 0 deletions lib/lockfile_preserver/ruby_version.rb
@@ -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
Expand Up @@ -41,7 +41,7 @@ GEM
ast (2.0.0)
astrolabe (1.3.0)
parser (>= 2.2.0.pre.3, < 3.0)
autoprefixer-rails (5.2.1)
autoprefixer-rails (5.2.0.1)
execjs
json
better_errors (2.1.1)
Expand Down Expand Up @@ -157,7 +157,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
jwt (1.5.1)
jwt (1.5.0)
launchy (2.4.3)
addressable (~> 2.3)
listen (2.10.1)
Expand Down Expand Up @@ -204,11 +204,11 @@ GEM
omniauth-github (1.1.2)
omniauth (~> 1.0)
omniauth-oauth2 (~> 1.1)
omniauth-oauth2 (1.3.1)
omniauth-oauth2 (1.3.0)
oauth2 (~> 1.0)
omniauth (~> 1.2)
open4 (1.3.4)
parser (2.2.2.5)
parser (2.3.0.pre.2)
ast (>= 1.1, < 3.0)
pg (0.18.2)
powerpack (0.1.1)
Expand Down Expand Up @@ -309,7 +309,7 @@ GEM
httparty (>= 0.10.0)
rake
safe_yaml (1.0.4)
sass (3.4.15)
sass (3.4.14)
sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0)
sass (~> 3.1)
Expand Down Expand Up @@ -434,5 +434,8 @@ DEPENDENCIES
uri_template
webmock

BUNDLED WITH
1.10.3
PLATFORMS
ruby

DEPENDENCIES
rails

0 comments on commit 17d1962

Please sign in to comment.