Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Fix gem-ification bits (now installs+runs)

- Factor out 'deb' package assembly into a library
  • Loading branch information...
commit 5b942f27106eb8965460c6835eacf5026d0abbca 1 parent 63494e9
@jordansissel authored
View
112 bin/fpm 100644 → 100755
@@ -1,9 +1,11 @@
#!/usr/bin/env ruby
#
+require "rubygems"
require "optparse"
require "ostruct"
require "erb"
+require "fpm"
def main(args)
settings = OpenStruct.new
@@ -11,6 +13,7 @@ def main(args)
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{$0} [options]"
+ # TODO(sissel): Maybe this should be '-o OUTPUT' ?
opts.on("-p PACKAGEFILE", "--package PACKAGEFILE",
"The package file to manage") do |path|
if path =~ /^\//
@@ -50,109 +53,22 @@ def main(args)
opts.parse!(args)
- # Actions:
- # create package
- #
- # files: add, remove
- # scripts: add, remove
- # metadata: set, add, remove
-
if !settings.package_path
$stderr.puts "No package file given to manage. Give with -p PACKAGEFILE"
return 1
end
- paths = args
- type = settings.package_path.split(".")[-1]
-
- mkbinding = lambda do
- package = settings.package_name
- version = settings.version
- package_iteration = 1
-
- maintainer = (settings.maintainer or "<#{ENV["USER"]}>")
- category = (settings.category or "X11")
- summary = (settings.summary or "no summary given for #{package}-#{version}")
-
- architecture = (settings.architecture or %x(uname -m).chomp)
- if architecture == "x86_64" && type == "deb"
- architecture = "amd64"
- end
-
-
- url = (settings.url or "http://example.com/")
- dependencies = (settings.dependencies or [])
- return binding
- end
-
- metadata = mkbinding.call
-
- package_path = settings.package_path
- package_path.gsub!(/VERSION/, eval('"#{version}-#{package_iteration}"', metadata))
- package_path.gsub!(/ARCH/, eval("architecture", metadata))
-
-
- if type != "deb"
- $stderr.puts "Unsupported package type '#{type}'"
- return 1
- end
-
-
- builddir = "#{Dir.pwd}/build-#{type}-#{File.basename(package_path)}"
- Dir.mkdir(builddir) if !File.directory?(builddir)
- template = File.new("#{File.dirname(__FILE__)}/../templates/#{type}.erb").read()
-
- Dir.chdir(settings.chdir || ".") do
- puts Dir.pwd
-
- # Add directories first.
- dirs = []
- paths.each do |path|
- while path != "/" and path != "."
- if !dirs.include?(path) or File.symlink?(path)
- dirs << path
- else
- #puts "Skipping: #{path}"
- #puts !dirs.include?(path)
- #puts !File.symlink?(path)
- end
- path = File.dirname(path)
- end
- end
- dirs = dirs.sort { |a,b| a.length <=> b.length}
- puts dirs.join("\n")
- system(*["tar", "--owner=root", "--group=root", "-cf", "#{builddir}/data.tar", "--no-recursion", *dirs])
- puts paths.join("\n")
- #paths = paths.reject { |p| File.symlink?(p) }
- system(*["tar", "--owner=root", "--group=root", "-rf", "#{builddir}/data.tar", *paths])
- system(*["gzip", "-f", "#{builddir}/data.tar"])
-
- # Generate md5sums
- md5sums = []
- paths.each do |path|
- md5sums += %x{find #{path} -type f -print0 | xargs -0 md5sum}.split("\n")
- end
- File.open("#{builddir}/md5sums", "w") { |f| f.puts md5sums.join("\n") }
-
- # Generate 'control' file
- control = ERB.new(template).result(metadata)
- File.open("#{builddir}/control", "w") { |f| f.puts control }
- end
-
- # create control.tar.gz
- Dir.chdir(builddir) do
- system("tar -zcf control.tar.gz control md5sums")
- end
-
- Dir.chdir(builddir) do
- # create debian-binary
- File.open("debian-binary", "w") { |f| f.puts "2.0" }
- end
-
- Dir.chdir(builddir) do
- system("ar -qc #{package_path} debian-binary control.tar.gz data.tar.gz")
- end
-end
+ package = FPM::Deb.new
+ package.name = settings.package_name
+ package.version = settings.version
+ package.maintainer = settings.maintainer if settings.maintainer
+ package.architecture = settings.architecture or %x{uname -m}.chomp
+ package.assemble({
+ "output" => settings.package_path,
+ "root" => settings.chdir,
+ "paths" => args, # Remainder of args are paths.
+ })
+end # def main
ret = main(ARGV)
exit(ret != nil ? ret : 0)
View
2  fpm.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |spec|
files = []
- dirs = %w{lib bin}
+ dirs = %w{lib bin templates}
dirs.each do |dir|
files += Dir["#{dir}/**/*"]
end
View
3  lib/fpm.rb
@@ -0,0 +1,3 @@
+require "fpm/namespace"
+require "fpm/package"
+require "fpm/deb"
View
74 lib/fpm/deb.rb
@@ -0,0 +1,74 @@
+#!/usr/bin/env ruby
+#
+
+require "erb"
+require "fpm/namespace"
+require "fpm/package"
+
+class FPM::Deb < FPM::Package
+
+ # Assemble the package.
+ # params:
+ # "root" => "/some/path" # the 'root' of your package directory
+ # "paths" => [ "/some/path" ...] # paths to icnlude in this package
+ # "output" => "foo.deb" # what to output to.
+ #
+ # The 'output' file path will have 'VERSION' and 'ARCH' replaced with
+ # the appropriate values if if you want the filename generated.
+ def assemble(params)
+ raise "No package name given. Can't assemble package" if !@name
+
+ root = params["root"]
+ paths = params["paths"]
+ output = params["output"]
+ type = "deb"
+
+ p self.inspect
+
+ # Debian calls x86_64 "amd64"
+ @architecture = "amd64" if @architecture == "x86_64"
+
+ output.gsub!(/VERSION/, "#{@version}-#{@iteration}")
+ output.gsub!(/ARCH/, @architecture)
+
+ builddir = "#{Dir.pwd}/build-#{type}-#{File.basename(output)}"
+ @garbage << builddir
+
+ Dir.mkdir(builddir) if !File.directory?(builddir)
+
+ Dir.chdir(root || ".") do
+ self.tar("#{builddir}/data.tar", paths)
+ # TODO(sissel): Make a helper method.
+ system(*["gzip", "-f", "#{builddir}/data.tar"])
+
+ # Generate md5sums
+ md5sums = self.checksum(paths)
+ File.open("#{builddir}/md5sums", "w") { |f| f.puts md5sums.join("\n") }
+
+ # Generate 'control' file
+ template = File.new("#{File.dirname(__FILE__)}/../../templates/deb.erb").read()
+ control = ERB.new(template).result(binding)
+ File.open("#{builddir}/control", "w") { |f| f.puts control }
+ end
+
+ # create control.tar.gz
+ Dir.chdir(builddir) do
+ # Make the control
+ system("tar -zcf control.tar.gz control md5sums")
+
+ # create debian-binary
+ File.open("debian-binary", "w") { |f| f.puts "2.0" }
+
+ # pack up the .deb
+ system("ar -qc #{output} debian-binary control.tar.gz data.tar.gz")
+ end
+ end # def assemble
+
+ def checksum(paths)
+ md5sums = []
+ paths.each do |path|
+ md5sums += %x{find #{path} -type f -print0 | xargs -0 md5sum}.split("\n")
+ end
+ end # def checksum
+end # class FPM::Deb
+
View
1  lib/fpm/namespace.rb
@@ -0,0 +1 @@
+module FPM; end
View
65 lib/fpm/package.rb
@@ -0,0 +1,65 @@
+require "fpm/namespace"
+require "socket" # for Socket.gethostname
+
+class FPM::Package
+ # The name of this package
+ attr_accessor :name
+
+ # The version of this package (the upstream version)
+ attr_accessor :version
+
+ # The iteration of this package.
+ # Debian calls this 'release' and is the last '-NUMBER' in the version
+ # RedHat has this as 'Release' in the .spec file
+ # FreeBSD calls this 'PORTREVISION'
+ # If left unpicked, it defaults to 1.
+ attr_accessor :iteration
+
+ # Who maintains this package? This could be the upstream author
+ # or the package maintainer. You pick.
+ attr_accessor :maintainer
+
+ # URL for this package.
+ # Could be the homepage. Could be the download url. You pick.
+ attr_accessor :url
+
+ # The category of this package.
+ # RedHat calls this 'Group'
+ # Debian calls this 'Section'
+ # FreeBSD would put this in /usr/ports/<category>/...
+ attr_accessor :category
+
+ # A identifier representing the license. Any string is fine.
+ attr_accessor :license
+
+ # What architecture is this package for?
+ attr_accessor :architecture
+
+ # TODO(sissel): Dependencies?
+
+ def initialize
+ @iteration = 1
+ @url = ""
+ @category = "default"
+ @license = "unknown"
+ @maintainer = "<#{ENV["USER"]}@#{Socket.gethostname}>"
+ @architecture = nil
+
+ # Garbage is stuff you may want to clean up.
+ @garbage = []
+ end
+
+ def tar(output, paths)
+ dirs = []
+ paths.each do |path|
+ while path != "/" and path != "."
+ dirs << path if !dirs.include?(path)
+ path = File.dirname(path)
+ end
+ end # paths.each
+ dirs = dirs.sort { |a,b| a.length <=> b.length}
+ system(*["tar", "--owner=root", "--group=root", "-cf", output, "--no-recursion", *dirs])
+ system(*["tar", "--owner=root", "--group=root", "-rf", output, *paths])
+ end
+
+end
View
18 templates/deb.erb
@@ -1,12 +1,12 @@
-Package: <%= package %>
-Version: <%= version %>-<%= package_iteration %>
-Architecture: <%= architecture %>
-Maintainer: <%= maintainer %>
+Package: <%= @package %>
+Version: <%= @version %>-<%= @package_iteration %>
+Architecture: <%= @architecture %>
+Maintainer: <%= @maintainer %>
Installed-Size: 12345
-Depends: <%= dependencies.join(", ") %>
-Section: <%= category %>
+Depends: <%= @dependencies.join(", ") rescue "" %>
+Section: <%= @category %>
Priority: extra
-Homepage: <%= url %>
-Description: <%= summary %>
- <%= summary %>
+Homepage: <%= @url %>
+Description: <%= @summary %>
+ <%= @summary %>
View
2  tools/npm2pkg.rb
@@ -56,7 +56,7 @@
maintainer = "#{m["name"]} <#{m["email"]}>"
end
- pkgcmd = [ "ruby", "bin/pkg.rb",
+ pkgcmd = [ "fpm",
"-n", "nodejs-#{package["name"]}",
"-v", package["version"],
"-m", maintainer,
Please sign in to comment.
Something went wrong with that request. Please try again.