Class: BPM::Package

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

Direct Known Subclasses

Project

Constant Summary collapse

EXT =
"bpkg"
FIELDS =

All JSON fields

{
  "keywords"    => :array,
  "licenses"    => :array,
  "engines"     => :array,
  "main"        => :string,
  "bin"         => :hash,
  "directories" => :hash,
  "pipeline"    => :hash,
  "name"        => :string,
  "version"     => :string,
  "description" => :string,
  "author"      => :string,
  "homepage"    => :string,
  "summary"     => :string,
  "dependencies"             => :hash,
  "dependencies:development" => :hash,
  "bpm:build"         => :hash,
  "bpm:use:transport" => :string,
  "bpm:provides"      => :hash
}
PLUGIN_TYPES =
%w[minifier]
SPEC_FIELDS =

Fields that can be loaded straight into the gemspec

%w[name email]
METADATA_FIELDS =

Fields that should be bundled up into JSON in the gemspec

%w[keywords licenses engines main bin directories pipeline bpm:build]
REQUIRED_FIELDS =
%w[name version summary]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path = nil, config = {}) ⇒ Package

Returns a new instance of Package.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bpm/package.rb', line 54

def initialize(root_path=nil, config={})
  @root_path   = root_path || Dir.pwd
  @json_path   = File.join @root_path, 'bpm_package.json'
  unless File.exists? @json_path
    @json_path   = File.join @root_path, 'package.json'
  end
  
  @email       = config[:email]
  @standalone  = config[:standalone]
  @errors      = []
  # Set defaults
  FIELDS.keys.each{|f| send("#{c2u(f)}=", fd(f))}
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



41
42
43
# File 'lib/bpm/package.rb', line 41

def email
  @email
end

#errorsObject (readonly)

Returns the value of attribute errors.



42
43
44
# File 'lib/bpm/package.rb', line 42

def errors
  @errors
end

#json_pathObject

Returns the value of attribute json_path.



41
42
43
# File 'lib/bpm/package.rb', line 41

def json_path
  @json_path
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



42
43
44
# File 'lib/bpm/package.rb', line 42

def root_path
  @root_path
end

Class Method Details

.from_spec(spec) ⇒ Object



44
45
46
47
48
# File 'lib/bpm/package.rb', line 44

def self.from_spec(spec)
  pkg = new(spec.full_gem_path)
  pkg.fill_from_gemspec(spec)
  pkg
end

.is_package_root?(path) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/bpm/package.rb', line 50

def self.is_package_root?(path)
  File.exists?(File.join(path, 'bpm_package.json')) || File.exists?(File.join(path, 'package.json'))
end

Instance Method Details

#as_jsonObject



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

def as_json

  seen_keys = @read_keys || []
  json   = {}
  
  # keep order of keys read in
  seen_keys.each do |key|
    if FIELDS.include?(key)
      val = send(c2u(key == 'build' ? 'pipeline' : key))
    else
      val = @attributes[key]
    end
    
    json[key] = val if val && !val.empty?
  end
  
  FIELDS.keys.each do |key|
    next if seen_keys.include?(key)
    val = send(c2u(key))
    key = 'build' if key == 'pipeline'
    json[key] = val if val && !val.empty?
  end

  json
end

#bin_filesObject



172
173
174
# File 'lib/bpm/package.rb', line 172

def bin_files
  bin && bin.values
end

#bin_pathObject



176
177
178
# File 'lib/bpm/package.rb', line 176

def bin_path
  directories["bin"] || "bin"
end

#dependencies_buildObject

Returns a hash of dependencies inferred from the build settings.



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/bpm/package.rb', line 200

def dependencies_build
  ret = {}

  bpm_build.each do |target_name, opts|
    next unless opts.is_a?(Hash)
    
    minifier = opts['minifier']
    case minifier
    when String
      ret[minifier] = '>= 0'
    when Hash
      ret.merge! minifier
    end
  end
  
  bpm_provides.each do |_,opts|
    next unless opts.is_a?(Hash) && opts['dependencies']
    ret.merge! opts['dependencies']
  end

  ret
end

#directory_filesObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/bpm/package.rb', line 145

def directory_files
  dir_names = [bin_path, lib_path, tests_path]
  dir_names += directories.reject { |k,_| dir_names.include?(k) }.values
  dir_names.reject! { |t| t == tests_path }
  
  build_names = bpm_build.values.map do |hash|
    # Directories is deprecated
    hash['files'] || hash['directories'] || hash['assets']
  end

  build_names += PLUGIN_TYPES.map do |type|
    val = bpm_provides[type]
    val = val && val =~ /^#{name}\// ? val[name.size+1..-1]+'.js' : nil
    val
  end

  bpm_provides.each do |_,values|
    val = values['main']
    val = val && val =~ /^#{name}\// ? val[name.size+1..-1]+'.js' : nil
    build_names << val if val
  end

  (dir_names+build_names).flatten.compact.uniq.map do |dir| 
    glob_files(dir)
  end.flatten
end

#expanded_deps(project) ⇒ Object

Collects an expanded list of all dependencies this package depends on directly or indirectly. This assume the project has already resolved any needed dependencies and therefore will raise an exception if a dependency cannot be found locally in the project.



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/bpm/package.rb', line 314

def expanded_deps(project)
  ret  = []
  seen = []
  todo = [self]
  while todo.size > 0
    pkg = todo.shift
    pkg.dependencies.each do |dep_name, dep_vers|
      next if seen.include? dep_name
      seen << dep_name
      found = project.local_deps.find { |x| x.name == dep_name }
      if found
        todo << found
        ret  << found
      else
        raise "Required local dependency not found #{dep_name}"
      end
    end
  end
  ret
end

#file_nameObject



141
142
143
# File 'lib/bpm/package.rb', line 141

def file_name
  "#{full_name}.#{EXT}"
end

#fill_from_gemspec(spec) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/bpm/package.rb', line 286

def fill_from_gemspec(spec)
  unless spec.is_a?(LibGems::Specification)
    spec = LibGems::Format.from_file_by_path(spec.to_s).spec
  end

  SPEC_FIELDS.each{|f| send("#{f}=", spec.send(f) || fd(field)) }

  self.author = spec.authors.first
  self.version = spec.version.to_s

  self.description = spec.description
  self.summary     = spec.summary
  self.homepage    = spec.homepage

   = spec.requirements.first
  if 
     = JSON.parse()
    METADATA_FIELDS.each{|f| send("#{c2u(f)}=", [f] || fd(f))}
  end

  self.dependencies = Hash[spec.dependencies.map{|d| [d.name, d.requirement.to_s ]}]
  self.dependencies_development = Hash[spec.development_dependencies.map{|d| [d.name, d.requirement.to_s ]}]
end

#find_transport_plugins(project) ⇒ Object



188
189
190
191
192
193
194
195
196
197
# File 'lib/bpm/package.rb', line 188

def find_transport_plugins(project)
  dependencies.keys.map do |pkg_name|
    dep = project.local_deps.find do |pkg|
      pkg.load_json
      pkg.name == pkg_name
    end
    raise "Could not find dependency: #{pkg_name}" unless dep
    dep.provided_transport
  end.compact
end

#full_nameObject



137
138
139
# File 'lib/bpm/package.rb', line 137

def full_name
  "#{name}-#{version}"
end

#generator_for(type) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/bpm/package.rb', line 228

def generator_for(type)
  unless generator = BPM.generator_for(name, type, false)
    path = File.join(root_path, 'templates', "#{type}_generator.rb")
    load path if File.exist?(path)
    generator = BPM.generator_for(name, type)
  end
  generator
end

#has_json?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/bpm/package.rb', line 249

def has_json?
  !!json_path && File.exist?(json_path)
end

#lib_pathObject



180
181
182
# File 'lib/bpm/package.rb', line 180

def lib_path
  directories["lib"] || "lib"
end

#load_jsonObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/bpm/package.rb', line 263

def load_json
  begin
    json = JSON.parse(File.read(@json_path))
  rescue JSON::ParserError, Errno::EACCES, Errno::ENOENT => ex
    raise BPM::InvalidPackageError.new self, ex.message
  end

  @read_keys = json.keys.dup # to retain order on save
  @attributes = json # save for saving
  
  FIELDS.keys.each do |field|
    if field == 'pipeline'
      self.pipeline = json['build'] || fd(field)
    else
      send("#{c2u(field)}=", json[field] || fd(field))
    end
  end

  validate_name_and_path
  
  true
end

#local_deps(search_paths = nil) ⇒ Object

TODO: Make better errors TODO: This might not work well with conflicting versions



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/bpm/package.rb', line 403

def local_deps(search_paths=nil)
  search_paths ||= [File.join(root_path, "vendor"), File.join(root_path, "packages")]

  dependencies.inject([]) do |list, (name, version)|
    packages = search_paths.map{|p| Package.new(File.join(p, name)) }.select{|p| p.has_json? }

    raise "Can't find package #{name} required in #{self.name}" if packages.empty?

    packages.each{|p| p.load_json }

    unless satisfied_by?(version, package.version)
      raise "#{name} (#{package.version}) doesn't match #{version} required in #{self.name}"
    end

    (package.local_deps(search_paths) << package).each do |dep|
      list << dep unless list.any?{|d| d.name == dep.name }
    end
    list
  end
end

#merged_dependencies(*kinds) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/bpm/package.rb', line 335

def merged_dependencies(*kinds)
  kinds.inject({}) do |ret, kind|
    deps = case kind
    when :runtime
      dependencies
    when :development
      dependencies_development
    when :build
      dependencies_build
    end
    ret.merge! deps
  end
end

#provided_formatsObject



357
358
359
360
361
362
363
# File 'lib/bpm/package.rb', line 357

def provided_formats
  ret = {}
  bpm_provides.each do | key, opts |
    ret[key[7..-1]] = opts if key =~ /^format:/
  end
  ret
end

#provided_minifierObject



397
398
399
# File 'lib/bpm/package.rb', line 397

def provided_minifier
  bpm_provides['minifier']
end

#provided_postprocessorsObject



379
380
381
# File 'lib/bpm/package.rb', line 379

def provided_postprocessors
  bpm_provides['postprocessors'] || []
end

#provided_preprocessorsObject



370
371
372
# File 'lib/bpm/package.rb', line 370

def provided_preprocessors
  bpm_provides['preprocessors'] || []
end

#provided_transportObject



388
389
390
# File 'lib/bpm/package.rb', line 388

def provided_transport
  bpm_provides['transport']
end

#say(*args) ⇒ Object



133
134
135
# File 'lib/bpm/package.rb', line 133

def say(*args)
  shell.say *args
end

#shellObject



129
130
131
# File 'lib/bpm/package.rb', line 129

def shell
  @shell ||= Thor::Base.shell.new
end

#standalone?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/bpm/package.rb', line 237

def standalone?
  !!@standalone
end

#template_path(name) ⇒ Object



223
224
225
226
# File 'lib/bpm/package.rb', line 223

def template_path(name)
  path = File.join(root_path, 'templates', name.to_s)
  File.exist?(path) ? path : nil
end

#tests_pathObject



184
185
186
# File 'lib/bpm/package.rb', line 184

def tests_path
  directories["tests"] || "tests"
end

#to_jsonObject



125
126
127
# File 'lib/bpm/package.rb', line 125

def to_json
  as_json.to_json
end

#to_specObject



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

def to_spec
  return unless valid?
  LibGems::Specification.new do |spec|
    SPEC_FIELDS.each{|f| spec.send("#{f}=", send(f)) }
    spec.version      = version
    spec.authors      = [author] if author
    spec.files        = directory_files + ["package.json"]
    spec.test_files   = glob_files(tests_path)
    spec.bindir       = bin_path
    spec.licenses     = licenses.map{|l| l["type"]}
    spec.executables  = bin_files.map{|p| File.basename(p) } if bin_path

    spec.homepage     = self.homepage
    spec.summary      = self.summary
    spec.description  = self.description if self.description

     = Hash[METADATA_FIELDS.map{|f| [f, send(c2u(f)) ] }]
    spec.requirements = [.to_json]

    # TODO: Is this right?
    spec.rubyforge_project = "bpm"

    def spec.file_name
      "#{full_name}.#{EXT}"
    end

    dependencies.each{|d,v| spec.add_dependency(d,v) }
    dependencies_development.each{|d,v| spec.add_development_dependency(d,v) }
  end
end

#used_dependencies(project) ⇒ Object



349
350
351
352
353
354
355
# File 'lib/bpm/package.rb', line 349

def used_dependencies(project)
  if project.has_local_package?(self.name) 
    merged_dependencies(:runtime, :development)
  else
    merged_dependencies(:runtime)
  end
end

#used_formats(project) ⇒ Object



365
366
367
368
# File 'lib/bpm/package.rb', line 365

def used_formats(project)
  pkgs=project.map_to_packages used_dependencies(project)
  pkgs.inject({}) { |ret, pkg| ret.merge!(pkg.provided_formats) }
end

#used_postprocessors(project) ⇒ Object



383
384
385
386
# File 'lib/bpm/package.rb', line 383

def used_postprocessors(project)
  pkgs=project.map_to_packages used_dependencies(project)
  pkgs.map { |pkg| pkg.provided_postprocessors }.flatten
end

#used_preprocessors(project) ⇒ Object



374
375
376
377
# File 'lib/bpm/package.rb', line 374

def used_preprocessors(project)
  pkgs=project.map_to_packages used_dependencies(project)
  pkgs.map { |pkg| pkg.provided_postprocessors }.flatten
end

#used_transports(project) ⇒ Object



392
393
394
395
# File 'lib/bpm/package.rb', line 392

def used_transports(project)
  pkgs=project.map_to_packages used_dependencies(project)
  pkgs.map { |pkg| pkg.provided_transport }.compact.flatten
end

#valid?Boolean

Returns:

  • (Boolean)


245
246
247
# File 'lib/bpm/package.rb', line 245

def valid?
  load_json && validate
end

#validateObject



241
242
243
# File 'lib/bpm/package.rb', line 241

def validate
  validate_fields && validate_version && validate_paths
end

#validate_name_and_pathObject



253
254
255
256
257
258
259
260
261
# File 'lib/bpm/package.rb', line 253

def validate_name_and_path
  # Currently we're only validating this for vendored packages
  return if standalone?

  dirname = File.basename(root_path)
  unless name.nil? || name.empty? || dirname == name || (version && dirname == "#{name}-#{version}")
    raise BPM::InvalidPackagePathError.new(self)
  end
end