debstep 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/debstep.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'debstep/package.rb'
2
+ require 'debstep/exception.rb'
3
+ require 'debstep/installation.rb'
4
+ require 'debstep/script_keeper.rb'
@@ -0,0 +1,4 @@
1
+ module Debstep
2
+ class Exception < ::Exception
3
+ end
4
+ end
@@ -0,0 +1,37 @@
1
+ module Debstep
2
+ class Installation
3
+ def initialize(workspace, path, &block)
4
+ raise Exception.new('path is a required variable') if path.nil? || path.strip.empty?
5
+ raise Exception.new('install path must be absolute (start with a "/")') unless path[0] == '/'
6
+
7
+ @path = "#{workspace}#{path}"
8
+ FileUtils.mkdir_p(@path)
9
+
10
+ instance_eval(&block)
11
+ end
12
+
13
+ def folder(path, opts={})
14
+ FileUtils.cp_r(path, target(path, opts))
15
+ end
16
+
17
+ def template(path, opts={})
18
+ File.open(target(path, opts), 'w') do |file|
19
+ erb = File.open(path, 'r').read
20
+ out = ERB.new(erb).result(opts[:binding] || TOPLEVEL_BINDING)
21
+ file.write(out)
22
+ end
23
+ end
24
+
25
+ alias :file :folder
26
+ alias :erb :template
27
+
28
+ private
29
+
30
+ # gets the install destination given the source path and the options
31
+ def target(path, opts)
32
+ basename = opts[:as] || File.basename(path)
33
+ "#{@path}/#{basename}"
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,111 @@
1
+ require 'fileutils'
2
+
3
+ module Debstep
4
+ class Package
5
+
6
+ @@required_control_fields = %w( Package Version Maintainer Description Architecture )
7
+ @@optional_control_fields = %w( Depends )
8
+
9
+ @@control_fields = @@required_control_fields + @@optional_control_fields
10
+ @@control_fields.each do |a|
11
+ attr_accessor a.to_sym
12
+ end
13
+
14
+ def initialize(name, opts={}, &block)
15
+ raise Exception.new('package name must be provided') if name.nil? || name.strip.empty?
16
+
17
+ self.Package = name
18
+
19
+ workspace_root = opts[:workspace] || '.'
20
+
21
+ @workspace = "#{workspace_root}/#{self.Package}"
22
+ FileUtils.rm_r(@workspace) if File.exists?(@workspace)
23
+ FileUtils.mkdir_p(@workspace)
24
+
25
+ instance_eval(&block)
26
+
27
+ save("#{@workspace}.deb")
28
+ end
29
+
30
+ def install(path, &block)
31
+ Installation.new(@workspace, path, &block)
32
+ end
33
+
34
+ def depends(package, version=nil)
35
+ @depends ||= []
36
+ @depends << case
37
+ when package && version then "#{package} (#{version})"
38
+ when package then package
39
+ else raise Exception.new('must specify a package')
40
+ end
41
+ end
42
+
43
+ def postinst(*args, &block)
44
+ @postinst = ScriptKeeper.new(*args, &block).script
45
+ end
46
+
47
+ def preinst(*args, &block)
48
+ @preinst = ScriptKeeper.new(*args, &block).script
49
+ end
50
+
51
+ def postrm(*args, &block)
52
+ @postrm = ScriptKeeper.new(*args, &block).script
53
+ end
54
+
55
+ def prerm(*args, &block)
56
+ @prerm = ScriptKeeper.new(*args, &block).script
57
+ end
58
+
59
+ def control_field_value(field)
60
+ send(field.to_sym)
61
+ end
62
+
63
+ def control_field_specified?(field)
64
+ value = control_field_value(field)
65
+ !value.nil? && !value.strip.empty?
66
+ end
67
+
68
+ def write_script(script)
69
+ File.open("#{@workspace}/DEBIAN/#{script}", 'w') do |f|
70
+ f.write(script_contents(script))
71
+ f.chmod(0775)
72
+ end
73
+ end
74
+
75
+ def script_given?(script)
76
+ !!script_contents(script)
77
+ end
78
+
79
+ def script_contents(script)
80
+ instance_variable_get(:"@#{script}")
81
+ end
82
+
83
+ def save(path)
84
+ @@required_control_fields.each do |field|
85
+ raise Exception.new("#{field} is required") unless control_field_specified?(field)
86
+ end
87
+
88
+ FileUtils.mkdir("#{@workspace}/DEBIAN")
89
+
90
+ self.Depends = case
91
+ when self.Depends && @depends then "#{self.Depends}, #{@depends.join(', ')}"
92
+ when self.Depends then self.Depends
93
+ when @depends then @depends.join(', ')
94
+ end
95
+
96
+ File.open("#{@workspace}/DEBIAN/control", 'w') do |file|
97
+ @@control_fields.select do |field|
98
+ control_field_specified?(field)
99
+ end.each do |field|
100
+ file.write("#{field}: #{control_field_value(field)}\n")
101
+ end
102
+ end
103
+
104
+ %w( preinst postinst prerm postrm ).each do |script|
105
+ write_script(script) if script_given?(script)
106
+ end
107
+
108
+ `dpkg-deb --build #{@workspace}`
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,22 @@
1
+ require 'erb'
2
+
3
+ module Debstep
4
+ class ScriptKeeper
5
+ attr_reader :script
6
+
7
+ def initialize(init="#!/usr/bin/env bash", &block)
8
+ @script = init + "\n"
9
+ instance_eval(&block)
10
+ end
11
+
12
+ def file(path)
13
+ @script += File.open(path, 'r').read.strip + "\n"
14
+ end
15
+
16
+ def template(path)
17
+ @script += ERB.new(File.open(path, 'r').read.strip).result + "\n"
18
+ end
19
+
20
+ alias :run :file
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: debstep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian Goodman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-10 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Front-end for the dpkg-deb command, used to build and configure packages
15
+ procedurally with a ruby DSL.
16
+ email: sebastian@foodocs.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/debstep/package.rb
22
+ - lib/debstep/script_keeper.rb
23
+ - lib/debstep/installation.rb
24
+ - lib/debstep/exception.rb
25
+ - lib/debstep.rb
26
+ homepage: http://foodocs.com/
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: configure debian packages with a ruby DSL
50
+ test_files: []