Class: Buildar

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = nil) ⇒ Buildar



71
72
73
74
75
76
77
78
79
# File 'lib/buildar.rb', line 71

def initialize(root = nil)
  @root = root ? File.expand_path(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'
end

Instance Attribute Details

#gemspec_filenameObject

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.



106
107
108
# File 'lib/buildar.rb', line 106

def gemspec_filename
  @gemspec_filename
end

#nameObject

Returns the value of attribute name.



64
65
66
# File 'lib/buildar.rb', line 64

def name
  @name
end

#publishObject

Returns the value of attribute publish.



64
65
66
# File 'lib/buildar.rb', line 64

def publish
  @publish
end

#rootObject

Returns the value of attribute root.



64
65
66
# File 'lib/buildar.rb', line 64

def root
  @root
end

#use_gemspec_fileObject

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_gitObject

Returns the value of attribute use_git.



64
65
66
# File 'lib/buildar.rb', line 64

def use_git
  @use_git
end

#use_version_fileObject

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_filenameObject

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

Yields:



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.expand_path('..', file)
end

.instanceObject

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

.versionObject



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_versionObject



127
128
129
130
131
132
# File 'lib/buildar.rb', line 127

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

#gemfileObject

where we expect a built gem to land



140
141
142
143
144
# File 'lib/buildar.rb', line 140

def gemfile
  path = File.join(@root, 'pkg', "#{@name}-#{self.available_version}.gem")
  raise "gemfile #{path} does not exist" unless File.exists?(path)
  path
end

#gemspecObject



81
82
83
# File 'lib/buildar.rb', line 81

def gemspec
  @use_gemspec_file ? self.hard_gemspec : self.soft_gemspec
end

#gemspec_fileObject



98
99
100
# File 'lib/buildar.rb', line 98

def gemspec_file
  File.join(@root, self.gemspec_filename)
end

#gemspec_locationObject



111
112
113
# File 'lib/buildar.rb', line 111

def gemspec_location
  @use_gemspec_file ? self.gemspec_filename : 'Rakefile'
end

#hard_gemspecObject

load every time; cache locally if you must



94
95
96
# File 'lib/buildar.rb', line 94

def hard_gemspec
  Gem::Specification.load self.gemspec_file
end

#soft_gemspecObject



85
86
87
88
89
90
# File 'lib/buildar.rb', line 85

def soft_gemspec
  @soft_gemspec ||= Gem::Specification.new
  @soft_gemspec.name = @name
  @soft_gemspec.version = self.version if @use_version_file
  @soft_gemspec
end

#versionObject



115
116
117
# File 'lib/buildar.rb', line 115

def version
  File.read(self.version_file).chomp
end

#version_fileObject



123
124
125
# File 'lib/buildar.rb', line 123

def version_file
  File.join(@root, @version_filename)
end

#version_locationObject



134
135
136
# File 'lib/buildar.rb', line 134

def version_location
  @use_version_file ? self.version_filename : self.gemspec_filename
end

#write_version(new_version) ⇒ Object



119
120
121
# File 'lib/buildar.rb', line 119

def write_version new_version
  File.open(self.version_file, 'w') { |f| f.write(new_version) }
end