Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 894bc3e

Browse files
committed
Add homebrew_tap native type
1 parent 7042755 commit 894bc3e

File tree

3 files changed

+116
-19
lines changed

3 files changed

+116
-19
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
require "fileutils"
2+
require "pathname"
3+
require "puppet/util/execution"
4+
5+
Puppet::Type.type(:homebrew_tap).provide :default do
6+
include Puppet::Util::Execution
7+
8+
def self.home
9+
@home ||= if boxen_home = Facter.value(:boxen_home)
10+
"#{boxen_home}/homebrew"
11+
else
12+
"/usr/local/homebrew"
13+
end
14+
end
15+
16+
def self.taps_dir
17+
@taps_dir ||= "#{home}/Library/Taps"
18+
end
19+
20+
def self.instances
21+
Dir.entries(taps_dir).map { |t| t.gsub('-', '/') }
22+
end
23+
24+
def exists?
25+
File.directory? install_dir
26+
end
27+
28+
def create
29+
execute [ "brew", "tap", @resource[:source] ], command_opts
30+
end
31+
32+
def destroy
33+
FileUtils.rm_rf install_dir
34+
end
35+
36+
private
37+
38+
def install_dir
39+
@install_dir ||= "#{self.class.taps_dir}/#{hyphenated_source}"
40+
end
41+
42+
def hyphenate(s)
43+
s.gsub('/', '-')
44+
end
45+
46+
def hyphenated_source
47+
@hyphenated_source ||= hyphenate(@resource[:source])
48+
end
49+
50+
# Override default `execute` to run super method in a clean
51+
# environment without Bundler, if Bundler is present
52+
def execute(*args)
53+
if Puppet.features.bundled_environment?
54+
Bundler.with_clean_env do
55+
super
56+
end
57+
else
58+
super
59+
end
60+
end
61+
62+
# Override default `execute` to run super method in a clean
63+
# environment without Bundler, if Bundler is present
64+
def self.execute(*args)
65+
if Puppet.features.bundled_environment?
66+
Bundler.with_clean_env do
67+
super
68+
end
69+
else
70+
super
71+
end
72+
end
73+
74+
def default_user
75+
Facter.value(:boxen_user) || Facter.value(:id) || "root"
76+
end
77+
78+
def command_opts
79+
@command_opts ||= {
80+
:combine => true,
81+
:custom_environment => {
82+
"HOME" => "/#{homedir_prefix}/#{default_user}",
83+
"PATH" => "#{self.class.home}/bin:/usr/bin:/usr/sbin:/bin:/sbin",
84+
"CFLAGS" => "-O2",
85+
"CPPFLAGS" => "-O2",
86+
"CXXFLAGS" => "-O2"
87+
},
88+
:failonfail => true,
89+
:uid => default_user
90+
}
91+
end
92+
end

lib/puppet/type/homebrew_tap.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Puppet::Type.newtype(:homebrew_tap) do
2+
ensurable do
3+
newvalue :present do
4+
provider.create
5+
end
6+
7+
newvalue :absent do
8+
provider.destroy
9+
end
10+
11+
defaultto :present
12+
end
13+
14+
newparam(:source) do
15+
isnamevar
16+
17+
validate do |v|
18+
if v.nil?
19+
raise Puppet::ParseError, "Homebrew_tap requires a source parameter!"
20+
end
21+
end
22+
end
23+
end

manifests/tap.pp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,5 @@
88
) {
99
require homebrew
1010

11-
$source_with_hyphen = regsubst($source, '\/', '-')
12-
13-
case $ensure {
14-
present: {
15-
exec { "brew tap ${source}":
16-
creates => "${homebrew::tapsdir}/${source_with_hyphen}"
17-
}
18-
}
19-
20-
absent: {
21-
exec { "rm -rf ${homebrew::tapsdir}/${source_with_hyphen}":
22-
onlyif => "test -d ${homebrew::tapsdir}/${source_with_hyphen}"
23-
}
24-
}
25-
26-
default: {
27-
fail('Ensure must be present or absent!')
28-
}
29-
}
11+
ensure_resource('homebrew_tap', $source, { 'ensure' => $ensure })
3012
}

0 commit comments

Comments
 (0)