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

Returns a new instance of Buildar.



68
69
70
71
72
73
74
75
76
# File 'lib/buildar.rb', line 68

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.



103
104
105
# File 'lib/buildar.rb', line 103

def gemspec_filename
  @gemspec_filename
end

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/buildar.rb', line 61

def name
  @name
end

#publishObject

Returns the value of attribute publish.



61
62
63
# File 'lib/buildar.rb', line 61

def publish
  @publish
end

#rootObject

Returns the value of attribute root.



61
62
63
# File 'lib/buildar.rb', line 61

def root
  @root
end

#use_gemspec_fileObject

Returns the value of attribute use_gemspec_file.



61
62
63
# File 'lib/buildar.rb', line 61

def use_gemspec_file
  @use_gemspec_file
end

#use_gitObject

Returns the value of attribute use_git.



61
62
63
# File 'lib/buildar.rb', line 61

def use_git
  @use_git
end

#use_version_fileObject

Returns the value of attribute use_version_file.



61
62
63
# File 'lib/buildar.rb', line 61

def use_version_file
  @use_version_file
end

#version_filenameObject

Returns the value of attribute version_filename.



61
62
63
# File 'lib/buildar.rb', line 61

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)



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/buildar.rb', line 41

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:



21
22
23
24
25
26
27
# File 'lib/buildar.rb', line 21

def self.conf(rakefile = nil)
  unless defined?(@@instance)
    root = rakefile ? File.expand_path('..', rakefile) : nil
    @@instance = Buildar.new root
  end
  yield @@instance if block_given?
end

.instanceObject

Confirming singleton. Only buildar/raketasks should need to call this Use conf inside project/Rakefile



33
34
35
36
# File 'lib/buildar.rb', line 33

def self.instance
  raise "no instance; call Buildar.conf" unless defined?(@@instance)
  @@instance
end

.versionObject



10
11
12
13
# File 'lib/buildar.rb', line 10

def self.version
  file = File.expand_path('../../VERSION', __FILE__)
  File.read(file).chomp
end

Instance Method Details

#available_versionObject



124
125
126
127
128
129
# File 'lib/buildar.rb', line 124

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



137
138
139
140
141
# File 'lib/buildar.rb', line 137

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



78
79
80
# File 'lib/buildar.rb', line 78

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

#gemspec_fileObject



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

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

#gemspec_locationObject



108
109
110
# File 'lib/buildar.rb', line 108

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

#hard_gemspecObject

load every time; cache locally if you must



91
92
93
# File 'lib/buildar.rb', line 91

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

#soft_gemspecObject



82
83
84
85
86
87
# File 'lib/buildar.rb', line 82

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



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

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

#version_fileObject



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

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

#version_locationObject



131
132
133
# File 'lib/buildar.rb', line 131

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

#write_version(new_version) ⇒ Object



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

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