Class: Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/ratatui_ruby/devtools/tasks/bump/manifest.rb

Overview

Represents a file that contains a version number.

Gems have version numbers in multiple places: version.rb, Cargo.toml, etc. Finding and updating them by hand risks inconsistency. One file says 1.2.3, another says 1.2.2.

This class wraps a file path with a regex pattern. It reads the current version and writes new versions. Use lookaround patterns to match precisely.

path

The file path.

pattern

A regex with lookarounds to match the version string.

primary

Whether this is the primary source of truth.

Example

manifest = Manifest.new(
  path: "lib/my_gem/version.rb",
  pattern: /(?<=VERSION = ")[^"]+(?=")/,
  primary: true
)
manifest.version.to_s  # => "1.2.3"

Instance Method Summary collapse

Constructor Details

#initialize(path:, pattern:, primary: false) ⇒ Manifest

Creates a new Manifest.

path

The file path.

pattern

A regex with lookarounds to match the version string.

primary

Whether this is the primary source of truth.



36
37
38
# File 'lib/ratatui_ruby/devtools/tasks/bump/manifest.rb', line 36

def initialize(path:, pattern:, primary: false)
  super
end

Instance Method Details

#readObject

Reads the file content.



41
42
43
# File 'lib/ratatui_ruby/devtools/tasks/bump/manifest.rb', line 41

def read
  File.read(path)
end

#versionObject

Returns the current version from this manifest.



46
47
48
49
50
51
52
# File 'lib/ratatui_ruby/devtools/tasks/bump/manifest.rb', line 46

def version
  content = read
  match = content.match(pattern)
  raise "Version missing in manifest #{path}" unless match

  SemVer.parse(match[0])
end

#write(version) ⇒ Object

Writes a new version to this manifest.

version

The SemVer to write.



57
58
59
60
61
62
# File 'lib/ratatui_ruby/devtools/tasks/bump/manifest.rb', line 57

def write(version)
  return unless File.exist?(path)

  new_content = read.gsub(pattern, version.to_s)
  File.write(path, new_content)
end