Class: Debstep::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/debstep/package.rb

Constant Summary collapse

@@required_control_fields =
%w( Package Version Maintainer Description Architecture )
@@optional_control_fields =
%w( Depends Section Priority Pre_Depends )
@@control_fields =
@@required_control_fields + @@optional_control_fields

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}, &block) ⇒ Package

Returns a new instance of Package.

Raises:


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/debstep/package.rb', line 14

def initialize(name, opts={}, &block)
  raise Exception.new('package name must be provided') if name.nil? || name.strip.empty?

  self.Package = name

  workspace_root = opts[:workspace] || '.'

  @workspace = "#{workspace_root}/#{self.Package}"
  FileUtils.rm_r(@workspace) if File.exists?(@workspace)
  FileUtils.mkdir_p(@workspace)

  instance_eval(&block)

  save
end

Instance Method Details

#control_field_specified?(field) ⇒ Boolean

Returns:

  • (Boolean)

63
64
65
66
# File 'lib/debstep/package.rb', line 63

def control_field_specified?(field)
  value = control_field_value(field)
  !value.nil? && !value.strip.empty?
end

#control_field_value(field) ⇒ Object


59
60
61
# File 'lib/debstep/package.rb', line 59

def control_field_value(field)
  send(field.to_sym)
end

#depends(package, version = nil) ⇒ Object


34
35
36
37
38
39
40
41
# File 'lib/debstep/package.rb', line 34

def depends(package, version=nil)
  @depends ||= []
  @depends << case
  when package && version then "#{package} (#{version})"
  when package then package
  else raise Exception.new('must specify a package')
  end
end

#install(path, &block) ⇒ Object


30
31
32
# File 'lib/debstep/package.rb', line 30

def install(path, &block)
  Installation.new(@workspace, path, &block) 
end

#postinst(*args, &block) ⇒ Object


43
44
45
# File 'lib/debstep/package.rb', line 43

def postinst(*args, &block)
  @postinst = ScriptKeeper.new(*args, &block).script
end

#postrm(*args, &block) ⇒ Object


51
52
53
# File 'lib/debstep/package.rb', line 51

def postrm(*args, &block)
  @postrm = ScriptKeeper.new(*args, &block).script
end

#preinst(*args, &block) ⇒ Object


47
48
49
# File 'lib/debstep/package.rb', line 47

def preinst(*args, &block)
  @preinst = ScriptKeeper.new(*args, &block).script
end

#prerm(*args, &block) ⇒ Object


55
56
57
# File 'lib/debstep/package.rb', line 55

def prerm(*args, &block)
  @prerm = ScriptKeeper.new(*args, &block).script
end

#saveObject


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
# File 'lib/debstep/package.rb', line 83

def save
  @@required_control_fields.each do |field|
    raise Exception.new("#{field} is required") unless control_field_specified?(field)
  end

  FileUtils.mkdir("#{@workspace}/DEBIAN")

  self.Depends = case
  when self.Depends && @depends then "#{self.Depends}, #{@depends.join(', ')}"
  when self.Depends then self.Depends
  when @depends then @depends.join(', ')
  end

  File.open("#{@workspace}/DEBIAN/control", 'w') do |file|
    @@control_fields.select do |field|
      control_field_specified?(field)
    end.each do |field|
      file.write("#{field.gsub('_', '-')}: #{control_field_value(field)}\n")
    end
  end

  %w( preinst postinst prerm postrm ).each do |script|
    write_script(script) if script_given?(script)
  end

  `dpkg-deb --build #{@workspace} .`
end

#script_contents(script) ⇒ Object


79
80
81
# File 'lib/debstep/package.rb', line 79

def script_contents(script)
  instance_variable_get(:"@#{script}")
end

#script_given?(script) ⇒ Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/debstep/package.rb', line 75

def script_given?(script)
  !!script_contents(script)
end

#write_script(script) ⇒ Object


68
69
70
71
72
73
# File 'lib/debstep/package.rb', line 68

def write_script(script)
  File.open("#{@workspace}/DEBIAN/#{script}", 'w') do |f|
    f.write(script_contents(script))
    f.chmod(0775)
  end
end