Class: VersionGemfile::Versioner
- Inherits:
-
Object
- Object
- VersionGemfile::Versioner
- Defined in:
- lib/version_gemfile/versioner.rb
Constant Summary collapse
- IS_GEM_LINE =
/^\s* gem \s+ ['|"] /ix
- HAS_VERSION =
/^\s*gem \s+ ['|"] \s* [\w|-]+ \s* ['|"]\s*,\s*['|"]/ix
- GET_GEM_NAME =
/^\s*gem \s+ ['|"] \s* ([\w|-]+) \s* ['|"]/ix
- GET_VERSION_NUMBER =
/^\s+[\w|-]+ \s \( ([\w|\.]+) \)/ix
Instance Attribute Summary collapse
-
#gemfile_content ⇒ Object
readonly
Returns the value of attribute gemfile_content.
-
#gemfile_path ⇒ Object
readonly
Returns the value of attribute gemfile_path.
-
#lock_contents ⇒ Object
readonly
Returns the value of attribute lock_contents.
-
#lockfile_path ⇒ Object
readonly
Returns the value of attribute lockfile_path.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_versions ⇒ Object
TODO: Clean this up!.
- #build_gem_line(gem_line, version = nil) ⇒ Object
-
#initialize(options = {}) ⇒ Versioner
constructor
A new instance of Versioner.
- #is_gem_line?(gem_line) ⇒ Boolean
- #match_gem(gem_line) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Versioner
Returns a new instance of Versioner.
17 18 19 20 21 22 23 24 25 |
# File 'lib/version_gemfile/versioner.rb', line 17 def initialize( = {}) = normalize_hash(.clone) @gemfile_path = .fetch('gemfile'){ 'Gemfile' } @lockfile_path = "#{@gemfile_path}.lock" @lock_contents = File.read(lockfile_path) @gemfile_content = File.readlines(gemfile_path) @orig_gemfile = File.read(gemfile_path) end |
Instance Attribute Details
#gemfile_content ⇒ Object (readonly)
Returns the value of attribute gemfile_content.
5 6 7 |
# File 'lib/version_gemfile/versioner.rb', line 5 def gemfile_content @gemfile_content end |
#gemfile_path ⇒ Object (readonly)
Returns the value of attribute gemfile_path.
5 6 7 |
# File 'lib/version_gemfile/versioner.rb', line 5 def gemfile_path @gemfile_path end |
#lock_contents ⇒ Object (readonly)
Returns the value of attribute lock_contents.
5 6 7 |
# File 'lib/version_gemfile/versioner.rb', line 5 def lock_contents @lock_contents end |
#lockfile_path ⇒ Object (readonly)
Returns the value of attribute lockfile_path.
5 6 7 |
# File 'lib/version_gemfile/versioner.rb', line 5 def lockfile_path @lockfile_path end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/version_gemfile/versioner.rb', line 5 def end |
Class Method Details
.add_versions!(options = {}) ⇒ Object
13 14 15 |
# File 'lib/version_gemfile/versioner.rb', line 13 def self.add_versions!( = {}) new().add_versions end |
Instance Method Details
#add_versions ⇒ Object
TODO: Clean this up!
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/version_gemfile/versioner.rb', line 28 def add_versions new_gemfile = Tempfile.new("Gemfile.versioned") begin gemfile_content.each do |gem_line| if is_gem_line?(gem_line) new_gemfile.puts(build_gem_line(gem_line)) else new_gemfile.puts(gem_line) end end File.truncate(gemfile_path, 0) new_gemfile.rewind File.open(gemfile_path, "w") {|f| f.write(new_gemfile.read)} rescue Exception => e puts "ERROR: #{e}" puts "Restoring Gemfile at #{gemfile_path}" File.open(gemfile_path, "w") {|f| f.write(@orig_gemfile)} ensure new_gemfile.close new_gemfile.unlink end end |
#build_gem_line(gem_line, version = nil) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/version_gemfile/versioner.rb', line 59 def build_gem_line(gem_line, version = nil) return gem_line if gem_line.match(HAS_VERSION) gem_name = gem_line.match(GET_GEM_NAME) { $1 } spaces = gem_line.match(/^(\s+)/){ $1 } version ||= get_version(gem_name) "#{spaces}gem '#{gem_name}', '~> #{version}'" end |
#is_gem_line?(gem_line) ⇒ Boolean
55 56 57 |
# File 'lib/version_gemfile/versioner.rb', line 55 def is_gem_line?(gem_line) gem_line =~ IS_GEM_LINE end |
#match_gem(gem_line) ⇒ Object
51 52 53 |
# File 'lib/version_gemfile/versioner.rb', line 51 def match_gem(gem_line) gem_line.match(HAS_VERSION) end |