Class: Buildar
- Inherits:
-
Object
- Object
- Buildar
- Defined in:
- lib/buildar.rb
Overview
Buildar is effectively a hand-rolled singleton. Yes, a NIH miserable excuse for a global. Look, we want to be able to call Buildar.conf in the project rakefile, but we need that data accessible here and inside lib/buildar/tasks. So we need a “global”. But if we use a class-based singleton, it’s namespaced. And it can’t be set to nil, for example.
Instance Attribute Summary collapse
-
#gemspec_filename ⇒ Object
it’s common to set the name after intialization.
-
#manifest_filename ⇒ Object
Returns the value of attribute manifest_filename.
-
#name ⇒ Object
Returns the value of attribute name.
-
#publish ⇒ Object
Returns the value of attribute publish.
-
#root ⇒ Object
Returns the value of attribute root.
-
#use_gemspec_file ⇒ Object
Returns the value of attribute use_gemspec_file.
-
#use_git ⇒ Object
Returns the value of attribute use_git.
-
#use_manifest_file ⇒ Object
Returns the value of attribute use_manifest_file.
-
#use_version_file ⇒ Object
Returns the value of attribute use_version_file.
-
#version_filename ⇒ Object
Returns the value of attribute version_filename.
Class Method Summary collapse
-
.bump(position, version) ⇒ Object
e.g.
-
.conf(rakefile = nil) {|@@instance| ... } ⇒ Object
Call this from the rakefile, like: Buildar.conf(__FILE__) do |b| b.name = ‘foo’ # …
- .dir(file) ⇒ Object
-
.instance ⇒ Object
Confirming singleton.
- .version ⇒ Object
Instance Method Summary collapse
- #available_manifest ⇒ Object
- #available_version ⇒ Object
-
#gemfile ⇒ Object
where we expect a built gem to land.
- #gemspec ⇒ Object
- #gemspec_file ⇒ Object
- #gemspec_location ⇒ Object
-
#hard_gemspec ⇒ Object
load every time; cache locally if you must.
-
#initialize(root = nil) ⇒ Buildar
constructor
A new instance of Buildar.
- #manifest ⇒ Object
- #manifest_file ⇒ Object
- #manifest_location ⇒ Object
- #soft_gemspec ⇒ Object
- #version ⇒ Object
- #version_file ⇒ Object
- #version_location ⇒ Object
- #write_version(new_version) ⇒ Object
Constructor Details
#initialize(root = nil) ⇒ Buildar
Returns a new instance of Buildar.
72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/buildar.rb', line 72 def initialize(root = nil) @root = root ? File.(root) : Dir.pwd @name = File.split(@root).last @use_git = false @publish = { rubygems: false } @use_gemspec_file = true @use_version_file = false @version_filename = 'VERSION' @use_manifest_file = false @manifest_filename = 'MANIFEST.txt' end |
Instance Attribute Details
#gemspec_filename ⇒ Object
it’s common to set the name after intialization. e.g. via Buildar.conf so set the default on first invocation. After that, it’s an accessor.
110 111 112 |
# File 'lib/buildar.rb', line 110 def gemspec_filename @gemspec_filename end |
#manifest_filename ⇒ Object
Returns the value of attribute manifest_filename.
64 65 66 |
# File 'lib/buildar.rb', line 64 def manifest_filename @manifest_filename end |
#name ⇒ Object
Returns the value of attribute name.
64 65 66 |
# File 'lib/buildar.rb', line 64 def name @name end |
#publish ⇒ Object
Returns the value of attribute publish.
64 65 66 |
# File 'lib/buildar.rb', line 64 def publish @publish end |
#root ⇒ Object
Returns the value of attribute root.
64 65 66 |
# File 'lib/buildar.rb', line 64 def root @root end |
#use_gemspec_file ⇒ Object
Returns the value of attribute use_gemspec_file.
64 65 66 |
# File 'lib/buildar.rb', line 64 def use_gemspec_file @use_gemspec_file end |
#use_git ⇒ Object
Returns the value of attribute use_git.
64 65 66 |
# File 'lib/buildar.rb', line 64 def use_git @use_git end |
#use_manifest_file ⇒ Object
Returns the value of attribute use_manifest_file.
64 65 66 |
# File 'lib/buildar.rb', line 64 def use_manifest_file @use_manifest_file end |
#use_version_file ⇒ Object
Returns the value of attribute use_version_file.
64 65 66 |
# File 'lib/buildar.rb', line 64 def use_version_file @use_version_file end |
#version_filename ⇒ Object
Returns the value of attribute version_filename.
64 65 66 |
# File 'lib/buildar.rb', line 64 def version_filename @version_filename end |
Class Method Details
.bump(position, version) ⇒ Object
e.g. bump(:minor, ‘1.2.3’) #=> ‘1.3.0’ only works for versions consisting of integers delimited by periods (dots)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/buildar.rb', line 44 def self.bump(position, version) pos = [:major, :minor, :patch, :build].index(position) || position places = version.split('.') if pos >= places.length and pos <= places.length + 2 # add zeroes to places up to pos # allows bump(:build, '0') #=> '0.0.0.1' places.length.upto(pos) { |i| places[i] = 0 } end raise "bad position: #{pos} (for version #{version})" unless places[pos] places.map.with_index { |place, i| if i < pos place elsif i == pos place.to_i + 1 else 0 end }.join('.') end |
.conf(rakefile = nil) {|@@instance| ... } ⇒ Object
Call this from the rakefile, like:
Buildar.conf(__FILE__) do |b|
b.name = 'foo'
# ...
end
24 25 26 27 28 29 30 |
# File 'lib/buildar.rb', line 24 def self.conf(rakefile = nil) unless defined?(@@instance) args = rakefile ? [File.dirname(rakefile)] : [] @@instance = Buildar.new(*args) end yield @@instance if block_given? end |
.dir(file) ⇒ Object
10 11 12 |
# File 'lib/buildar.rb', line 10 def self.dir(file) File.('..', file) end |
.instance ⇒ Object
Confirming singleton. Only buildar/raketasks should need to call this Use conf inside project/Rakefile
36 37 38 39 |
# File 'lib/buildar.rb', line 36 def self.instance raise "no instance; call Buildar.conf" unless defined?(@@instance) @@instance end |
.version ⇒ Object
14 15 16 |
# File 'lib/buildar.rb', line 14 def self.version File.read(File.join(dir(__FILE__), '..', 'VERSION')).chomp end |
Instance Method Details
#available_manifest ⇒ Object
150 151 152 153 154 155 |
# File 'lib/buildar.rb', line 150 def available_manifest return self.manifest if @use_manifest_file manifest = self.gemspec.files raise "gemspec.files is missing" if !manifest or manifest.to_s.empty? manifest end |
#available_version ⇒ Object
131 132 133 134 135 136 |
# File 'lib/buildar.rb', line 131 def available_version return self.version if @use_version_file version = self.gemspec.version raise "gemspec.version is missing" if !version or version.to_s.empty? version end |
#gemfile ⇒ Object
where we expect a built gem to land
144 145 146 147 148 |
# File 'lib/buildar.rb', line 144 def gemfile path = File.join(@root, 'pkg', "#{@name}-#{self.available_version}.gem") raise "gemfile #{path} does not exist" unless File.exists?(path) path end |
#gemspec ⇒ Object
84 85 86 |
# File 'lib/buildar.rb', line 84 def gemspec @use_gemspec_file ? self.hard_gemspec : self.soft_gemspec end |
#gemspec_file ⇒ Object
102 103 104 |
# File 'lib/buildar.rb', line 102 def gemspec_file File.join(@root, self.gemspec_filename) end |
#gemspec_location ⇒ Object
115 116 117 |
# File 'lib/buildar.rb', line 115 def gemspec_location @use_gemspec_file ? self.gemspec_filename : 'Rakefile' end |
#hard_gemspec ⇒ Object
load every time; cache locally if you must
98 99 100 |
# File 'lib/buildar.rb', line 98 def hard_gemspec Gem::Specification.load self.gemspec_file end |
#manifest ⇒ Object
157 158 159 |
# File 'lib/buildar.rb', line 157 def manifest File.readlines(self.manifest_file).map { |line| line.chomp } end |
#manifest_file ⇒ Object
161 162 163 |
# File 'lib/buildar.rb', line 161 def manifest_file File.join(@root, @manifest_filename) end |
#manifest_location ⇒ Object
165 166 167 |
# File 'lib/buildar.rb', line 165 def manifest_location @use_manifest_file ? self.manifest_filename : self.gemspec_filename end |
#soft_gemspec ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/buildar.rb', line 88 def soft_gemspec @soft_gemspec ||= Gem::Specification.new @soft_gemspec.name = @name @soft_gemspec.version = self.version if @use_version_file @soft_gemspec.files = self.manifest if @use_manifest_file @soft_gemspec end |
#version ⇒ Object
119 120 121 |
# File 'lib/buildar.rb', line 119 def version File.read(self.version_file).chomp end |
#version_file ⇒ Object
127 128 129 |
# File 'lib/buildar.rb', line 127 def version_file File.join(@root, @version_filename) end |
#version_location ⇒ Object
138 139 140 |
# File 'lib/buildar.rb', line 138 def version_location @use_version_file ? self.version_filename : self.gemspec_filename end |
#write_version(new_version) ⇒ Object
123 124 125 |
# File 'lib/buildar.rb', line 123 def write_version new_version File.open(self.version_file, 'w') { |f| f.write(new_version) } end |