Class: Bake::Gem::Helper

Inherits:
Object
  • Object
show all
Includes:
Shell
Defined in:
lib/bake/gem/helper.rb

Constant Summary collapse

VERSION_PATTERN =
/VERSION = ['"](?<value>\d+\.\d+\.\d+)(?<pre>.*?)['"]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shell

#execute, #readlines, #system

Constructor Details

#initialize(root = Dir.pwd, gemspec: nil) ⇒ Helper

Returns a new instance of Helper.



17
18
19
20
# File 'lib/bake/gem/helper.rb', line 17

def initialize(root = Dir.pwd, gemspec: nil)
	@root = root
	@gemspec = gemspec || find_gemspec
end

Instance Attribute Details

#gemspecObject (readonly)

Returns the value of attribute gemspec.



23
24
25
# File 'lib/bake/gem/helper.rb', line 23

def gemspec
  @gemspec
end

#rootObject (readonly)

Returns the value of attribute root.



22
23
24
# File 'lib/bake/gem/helper.rb', line 22

def root
  @root
end

Instance Method Details

#build_gem(root: "pkg", signing_key: nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bake/gem/helper.rb', line 83

def build_gem(root: "pkg", signing_key: nil)
	# Ensure the output directory exists:
	FileUtils.mkdir_p("pkg")
	
	output_path = File.join('pkg', @gemspec.file_name)
	
	if signing_key == false
		@gemspec.signing_key = nil
	elsif signing_key.is_a?(String)
		@gemspec.signing_key = signing_key
	elsif signing_key == true and @gemspec.signing_key.nil?
		raise ArgumentError, "Signing key is required for signing the gem, but none was specified by the gemspec."
	end
	
	::Gem::Package.build(@gemspec, false, false, output_path)
end

#find_gemspec(glob = "*.gemspec") ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bake/gem/helper.rb', line 108

def find_gemspec(glob = "*.gemspec")
	paths = Dir.glob(glob, base: @root).sort
	
	if paths.size > 1
		raise "Multiple gemspecs found: #{paths}, please specify one!"
	end
	
	if path = paths.first
		return ::Gem::Specification.load(path)
	end
end

#guard_cleanObject



63
64
65
66
67
68
69
70
71
# File 'lib/bake/gem/helper.rb', line 63

def guard_clean
	lines = readlines("git", "status", "--porcelain", chdir: @root)
	
	if lines.any?
		raise "Repository has uncommited changes!\n#{lines.join('')}"
	end
	
	return true
end

#guard_default_branchObject



73
74
75
76
77
78
# File 'lib/bake/gem/helper.rb', line 73

def guard_default_branch
	branch = readlines("git", "branch", "--show-current", chdir: @root).first.chomp
	remote_head_branch = readlines("git", "symbolic-ref", "refs/remotes/origin/HEAD", chdir: @root).first.chomp.split('/').last
	
	return branch == remote_head_branch
end

#install_gem(*arguments, path: @gemspec.file_name) ⇒ Object



100
101
102
# File 'lib/bake/gem/helper.rb', line 100

def install_gem(*arguments, path: @gemspec.file_name)
	system("gem", "install", path, *arguments)
end

#push_gem(*arguments, path: @gemspec.file_name) ⇒ Object



104
105
106
# File 'lib/bake/gem/helper.rb', line 104

def push_gem(*arguments, path: @gemspec.file_name)
	system("gem", "push", path, *arguments)
end

#update_version(bump, version_path = self.version_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bake/gem/helper.rb', line 31

def update_version(bump, version_path = self.version_path)
	return false unless version_path
	
	lines = File.readlines(version_path)
	version = nil
	
	lines.each do |line|
		if match = line.match(VERSION_PATTERN)
			version = match[:value].split(/\./).map(&:to_i)
			bump.each_with_index do |increment, index|
				if increment == 1
					version[index] += 1
				elsif increment == 0
					version[index] = 0
				end
			end
			
			line.sub!(match[:value], version.join('.'))
		end
	end
	
	if version
		if block_given?
			yield version
		end
		
		File.write(version_path, lines.join)
		
		return version_path
	end
end

#version_pathObject



25
26
27
# File 'lib/bake/gem/helper.rb', line 25

def version_path
	@gemspec&.files.grep(/lib(.*?)\/version.rb/).first
end