forked from timsutton/brew-pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrew-pkg.rb
executable file
·162 lines (133 loc) · 5.16 KB
/
brew-pkg.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Builds an OS X installer package from an installed formula.
require 'formula'
require 'optparse'
require 'tmpdir'
module HomebrewArgvExtension extend self
def with_deps?
flag? '--with-deps'
end
end
# cribbed Homebrew module code from brew-unpack.rb
module Homebrew extend self
def pkg
unpack_usage = <<-EOS
Usage: brew pkg [--identifier-prefix] [--with-deps] [--without-kegs] formula
Build an OS X installer package from a formula. It must be already
installed; 'brew pkg' doesn't handle this for you automatically. The
'--identifier-prefix' option is strongly recommended in order to follow
the conventions of OS X installer packages.
Options:
--identifier-prefix set a custom identifier prefix to be prepended
to the built package's identifier, ie. 'org.nagios'
makes a package identifier called 'org.nagios.nrpe'
--with-deps include all the package's dependencies in the built package
--without-kegs exclude package contents at /usr/local/Cellar/packagename
--scripts set the path to custom preinstall and postinstall scripts
EOS
abort unpack_usage if ARGV.empty?
identifier_prefix = if ARGV.include? '--identifier-prefix'
ARGV.next.chomp(".")
else
'org.homebrew'
end
f = Formulary.factory ARGV.last
# raise FormulaUnspecifiedError if formulae.empty?
# formulae.each do |f|
name = f.name
identifier = identifier_prefix + ".#{name}"
version = f.version.to_s
version += "_#{f.revision}" if f.revision.to_s != '0'
# Make sure it's installed first
if not f.any_version_installed?
onoe "#{f.name} is not installed. First install it with 'brew install #{f.name}'."
abort
end
# Setup staging dir
pkg_root = Dir.mktmpdir 'brew-pkg'
staging_root = pkg_root + HOMEBREW_PREFIX
ohai "Creating package staging root using Homebrew prefix #{HOMEBREW_PREFIX}"
FileUtils.mkdir_p staging_root
pkgs = [f]
# Add deps if we specified --with-deps
pkgs += f.recursive_dependencies if ARGV.include? '--with-deps'
pkgs.each do |pkg|
formula = Formulary.factory(pkg.to_s)
dep_version = formula.version.to_s
dep_version += "_#{formula.revision}" if formula.revision.to_s != '0'
ohai "Staging formula #{formula.name}"
# Get all directories for this keg, rsync to the staging root
if File.exists?(File.join(HOMEBREW_CELLAR, formula.name, dep_version))
dirs = Pathname.new(File.join(HOMEBREW_CELLAR, formula.name, dep_version)).children.select { |c| c.directory? }.collect { |p| p.to_s }
dirs.each {|d| safe_system "rsync", "-a", "#{d}", "#{staging_root}/" }
if File.exists?("#{HOMEBREW_CELLAR}/#{formula.name}/#{dep_version}") and not ARGV.include? '--without-kegs'
ohai "Staging directory #{HOMEBREW_CELLAR}/#{formula.name}/#{dep_version}"
safe_system "mkdir", "-p", "#{staging_root}/Cellar/#{formula.name}/"
safe_system "rsync", "-a", "#{HOMEBREW_CELLAR}/#{formula.name}/#{dep_version}", "#{staging_root}/Cellar/#{formula.name}/"
end
end
# Write out a LaunchDaemon plist if we have one
if formula.plist
ohai "Plist found at #{formula.plist_name}, staging for /Library/LaunchDaemons/#{formula.plist_name}.plist"
launch_daemon_dir = File.join staging_root, "Library", "LaunchDaemons"
FileUtils.mkdir_p launch_daemon_dir
fd = File.new(File.join(launch_daemon_dir, "#{formula.plist_name}.plist"), "w")
fd.write formula.plist
fd.close
end
end
# Add scripts if we specified --scripts
found_scripts = false
if ARGV.include? '--scripts'
scripts_path = ARGV.next
if File.directory?(scripts_path)
pre = File.join(scripts_path,"preinstall")
post = File.join(scripts_path,"postinstall")
if File.exists?(pre)
File.chmod(0755, pre)
found_scripts = true
ohai "Adding preinstall script"
end
if File.exists?(post)
File.chmod(0755, post)
found_scripts = true
ohai "Adding postinstall script"
end
end
if not found_scripts
opoo "No scripts found in #{scripts_path}"
end
end
# Custom ownership
found_ownership = false
if ARGV.include? '--ownership'
custom_ownership = ARGV.next
if ['recommended', 'preserve', 'preserve-other'].include? custom_ownership
found_ownership = true
ohai "Setting pkgbuild option --ownership with value #{custom_ownership}"
else
opoo "#{custom_ownership} is not a valid value for pkgbuild --ownership option, ignoring"
end
end
# Build it
pkgfile = "#{name}-#{version}.pkg"
ohai "Building package #{pkgfile}"
args = [
"--quiet",
"--root", "#{pkg_root}",
"--identifier", identifier,
"--version", version
]
if found_scripts
args << "--scripts"
args << scripts_path
end
if found_ownership
args << "--ownership"
args << custom_ownership
end
args << "#{pkgfile}"
safe_system "pkgbuild", *args
FileUtils.rm_rf pkg_root
end
end
Homebrew.pkg