Class: Ian::Control

Inherits:
Object
  • Object
show all
Defined in:
lib/ian/control.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**fields) ⇒ Control

Returns a new instance of Control.



7
8
9
# File 'lib/ian/control.rb', line 7

def initialize(**fields)
  @fields = fields
end

Class Method Details

.defaultObject



200
201
202
# File 'lib/ian/control.rb', line 200

def self.default
  self.new(self.defaults)
end

.defaultsObject

default values for a new control file



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
# File 'lib/ian/control.rb', line 88

def self.defaults
  {
    package:          "name",
    priority:         "optional",
    section:          "misc",
    essential:        "no",
    size:             0,
    maintainer:       Utils.guess_maintainer,   # TODO: pass the maintainer in somehow
    homepage:         "http://example.com",
    arch:             "all",
    version:          "0.0.1",
    replaces:         [],
    conflicts:        [],
    breaks:           [],
    recommends:       [],
    suggests:         [],
    enhances:         [],
    predepends:       [],
    depends:          [],
    desc:            "This is a description",
    long_desc:       [
      "This is a longer description that can take",
      "up multiple lines"
    ]
  }
end

.fieldsObject

a map of field symbols to field names



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ian/control.rb', line 116

def self.fields
  {
    package:     "Package",
    depends:     "Depends",
    replaces:    "Replaces",
    breaks:      "Breaks",
    conflicts:   "Conflicts",
    recommends:  "Recommends",
    suggests:    "Suggests",
    enhances:    "Enhances",
    predepends:  "Pre-Depends",
    version:     "Version",
    priority:    "Priority",
    section:     "Section",
    essential:   "Essential",
    size:        "Installed-Size",
    maintainer:  "Maintainer",
    homepage:    "Homepage",
    arch:        "Architecture",
    desc:        "Description",
    long_desc:   "  "
  }
end

.load_file(path) ⇒ Object



175
176
177
# File 'lib/ian/control.rb', line 175

def self.load_file(path)
  self.new(self.parse(File.read(path)))
end

.parse(text) ⇒ Object

parse this control file into the fields hash



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ian/control.rb', line 180

def self.parse(text)
  fields = {}

  self.fields.each do |f, name|
    m = text.match(/^#{name}: (.*)$/)
    next unless m
    fields[f] = m[1]
  end

  # for the relations fields, split the string out into an array
  self.relationship_fields.each do |key|
    next unless fields[key]
    fields[key] = fields[key].split(",").map! {|d| d.strip }
  end

  fields[:long_desc] = text.scan(/^  (.*)$/).flatten

  return fields
end

.relationship_fieldsObject



142
143
144
# File 'lib/ian/control.rb', line 142

def self.relationship_fields
  [:replaces, :conflicts, :recommends, :suggests, :enhances, :predepends, :depends, :breaks]
end

.save(ctrl, path) ⇒ Object

save the control file to disk raises Ian::ValidationError



82
83
84
85
# File 'lib/ian/control.rb', line 82

def self.save(ctrl, path)
  ctrl.valid!
  File.write(path, ctrl.to_s)
end

Instance Method Details

#[](field) ⇒ Object

allow reading fields directly



18
19
20
# File 'lib/ian/control.rb', line 18

def [](field)
  @fields[field]
end

#[]=(field, value) ⇒ Object

allow setting fields directly



12
13
14
15
# File 'lib/ian/control.rb', line 12

def []=(field, value)
  valid_field!(field)
  @fields[field] = value
end

#delete(field) ⇒ Object

deletes a field from the hash

Raises:

  • (ArgumentError)


33
34
35
36
37
38
# File 'lib/ian/control.rb', line 33

def delete(field)
  field.to_sym if field.is_a? String
  valid_field!(field)
  raise ArgumentError, "Cannot remove mandatory field #{field.to_s}" if mandatory_fields.include?(field)
  @fields.delete(field)
end

#fieldsObject



140
# File 'lib/ian/control.rb', line 140

def fields; self.class.fields; end

#mandatory_fieldsObject

an array of mandatory fields for a control file



154
155
156
# File 'lib/ian/control.rb', line 154

def mandatory_fields
  [:package, :version, :arch, :maintainer, :desc, :long_desc]
end

#missing_mandatory_fieldsObject

return the mandatory fields that are missing from the control file



147
148
149
150
151
# File 'lib/ian/control.rb', line 147

def missing_mandatory_fields
  mandatory_fields.map do |f|
    return f unless @fields.keys.include?(f)
  end.reject {|f| f.nil? }
end

#pkgnameObject



22
23
24
25
26
27
28
29
30
# File 'lib/ian/control.rb', line 22

def pkgname
  parts = [
    @fields[:package],
    @fields[:version],
    @fields[:arch]
  ]

  "%s_%s_%s" % parts
end

#to_sObject

output the control file as a string



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ian/control.rb', line 55

def to_s
  lines = []

  # build the standard fields
  [:package, :version, :section, :priority, :arch, :essential, :size, :maintainer, :homepage].each do |key|
    lines << "#{fields[key]}: #{@fields[key]}"
  end

  # build the relationship fields that have been exploded into an array
  self.class.relationship_fields.each do |key|
    next unless @fields[key] and @fields[key].any?
    lines << "%s: %s" % [ self.class.fields[key], @fields[key].join(", ") ]
  end

  lines << "Description: #{@fields[:desc]}"

  # build the long description with the double space at the start for each line
  lines += @fields[:long_desc].map do |ld|
    "  #{ld}"
  end

  lines << "" # blank line as per debian control spec
  lines.join("\n")
end

#update(hash) ⇒ Object

update a bunch of fields at a time



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ian/control.rb', line 41

def update(hash)
  hash.each do |key, value|
    valid_field!(key)
    raise ArgumentError, "Value for #{key} was empty" if value.nil? or value == ""

    if self.class.relationship_fields.include?(key)
      @fields[key] = value.split(",").map {|d| d.strip }
    else
      @fields[key] = value
    end
  end
end

#valid!Object

Raises:



163
164
165
# File 'lib/ian/control.rb', line 163

def valid!
  raise ValidationError, "Missing mandatory control fields: #{missing_mandatory_fields.join(",")}" unless valid?
end

#valid?Boolean

checks if the control file is valid

Returns:

  • (Boolean)


159
160
161
# File 'lib/ian/control.rb', line 159

def valid?
  missing_mandatory_fields.empty?
end

#valid_field!(key) ⇒ Object

Raises:

  • (ArgumentError)


171
172
173
# File 'lib/ian/control.rb', line 171

def valid_field!(key)
  raise ArgumentError, "Invalid field: #{key}" unless valid_field?(key)
end

#valid_field?(key) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/ian/control.rb', line 167

def valid_field?(key)
  self.class.fields.keys.include? key
end