Class: GemKit::Release::VersionFile
- Inherits:
-
Object
- Object
- GemKit::Release::VersionFile
- Defined in:
- lib/gem_kit/release/version_file.rb
Overview
Owns the one file that states the version, and the semver arithmetic for moving it. Two layouts are supported, and which one is in play is decided by whether an ERB template sits beside the file:
lib/x/version.rb.erb present -> render it with `version` in scope
otherwise -> substitute the version string in place
The second is the common case and deliberately surgical: it rewrites the quoted version literal and touches nothing else in the file.
Defined Under Namespace
Classes: Error
Constant Summary collapse
- SEGMENTS =
%w[major minor patch].freeze
- LITERAL =
A quoted semver literal, e.g. VERSION = "4.1.0"
/(["'])(\d+\.\d+\.\d+(?:[-.][0-9A-Za-z.-]+)?)\1/
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Class Method Summary collapse
-
.bump(version, segment) ⇒ Object
"4.1.0", :minor -> "4.2.0".
Instance Method Summary collapse
-
#initialize(path, template: nil) ⇒ VersionFile
constructor
A new instance of VersionFile.
-
#read ⇒ Object
The version currently written in the file.
-
#write(version) ⇒ Object
Write
versioninto the file.
Constructor Details
#initialize(path, template: nil) ⇒ VersionFile
Returns a new instance of VersionFile.
26 27 28 29 |
# File 'lib/gem_kit/release/version_file.rb', line 26 def initialize(path, template: nil) @path = path @template = template end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
24 25 26 |
# File 'lib/gem_kit/release/version_file.rb', line 24 def path @path end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
24 25 26 |
# File 'lib/gem_kit/release/version_file.rb', line 24 def template @template end |
Class Method Details
.bump(version, segment) ⇒ Object
"4.1.0", :minor -> "4.2.0"
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gem_kit/release/version_file.rb', line 32 def self.bump(version, segment) unless SEGMENTS.include?(segment.to_s) raise Error, "unknown segment #{segment.inspect} (expected #{SEGMENTS.join(", ")})" end major, minor, patch = version.to_s.split(".").map(&:to_i) case segment.to_s when "major" then "#{major + 1}.0.0" when "minor" then "#{major}.#{minor + 1}.0" when "patch" then "#{major}.#{minor}.#{patch + 1}" end end |
Instance Method Details
#read ⇒ Object
The version currently written in the file.
46 47 48 49 50 51 52 |
# File 'lib/gem_kit/release/version_file.rb', line 46 def read contents = File.read(path) match = contents.match(LITERAL) raise Error, "no version literal in #{path}" if match.nil? match[2] end |
#write(version) ⇒ Object
Write version into the file. Returns the version written.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gem_kit/release/version_file.rb', line 55 def write(version) if template File.write(path, ERB.new(File.read(template)).result(binding)) else contents = File.read(path) raise Error, "no version literal in #{path}" unless contents.match?(LITERAL) File.write(path, contents.sub(LITERAL) { "#{$1}#{version}#{$1}" }) end version end |