Module: Morpheus::Morpkg

Defined in:
lib/morpheus/morpkg.rb

Overview

A module for building .morpkg files

Defined Under Namespace

Classes: ZipFileGenerator

Class Method Summary collapse

Class Method Details

.build_package(source_directory, outfile = nil, do_overwrite = false) ⇒ Object

write a .morpkg file for a package source directory validates manifest data default outfile is code-version.morpkg returns outfile (filename) or raises exception



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/morpheus/morpkg.rb', line 23

def self.build_package(source_directory, outfile=nil, do_overwrite=false)
  source_directory = File.expand_path(source_directory)
  manifest = self.parse_package_manifest(source_directory)
  code = manifest["code"]
  version = manifest["version"]
  org = manifest["org"] || manifest["organization"]
  type = manifest["type"]
  if code.nil? || code.empty?
    raise "Package manifest data missing: code"
  end
  if version.nil? || version.empty?
    raise "Package manifest data missing: version"
  end
  # if org.nil? || org.empty?
  #   raise "Package manifest data missing: org"
  # end
  # if type.nil? || type.empty?
  #   raise "Package manifest data missing: type"
  # end
  if outfile.nil? || outfile.empty?
    # outfile = "#{orig_dir}/#{type}-#{code}-#{version}.morpkg"
    # outfile = File.join(File.dirname(source_directory), "#{type}-#{code}-#{version}.morpkg")
    outfile = File.join(File.dirname(source_directory), "#{code}-#{version}.morpkg")
  elsif File.directory?(outfile)
    outfile = File.join(outfile, "#{code}-#{version}.morpkg")
  end
  if Dir.exist?(outfile)
    raise "Invalid package target. #{outfile} is the name of an existing directory."
  end
  if File.exist?(outfile)
    if do_overwrite
      # don't delete, just overwrite.
      # File.delete(outfile)
    else
      raise "Invalid package target. File already exists: #{outfile}"
    end
  end
  # build directories if needed
  if !Dir.exist?(File.dirname(outfile))
    Dir.mkdir(File.dirname(outfile))
  end

  # write the .morpkg file and return filename
  zf = ZipFileGenerator.new(source_directory, outfile)
  zf.write()
  
  return outfile
end

.parse_package_manifest(source_directory) ⇒ Object

parse the manifest json data for a package source directory



9
10
11
12
13
14
15
16
17
# File 'lib/morpheus/morpkg.rb', line 9

def self.parse_package_manifest(source_directory)
  source_directory = File.expand_path(source_directory)
  manifest_filename = File.join(source_directory, "package-manifest.json")
  if !File.exist?(manifest_filename)
    raise "Package manifest file not found: #{manifest_filename}"
  end
  manifest = JSON.parse(File.read(manifest_filename))
  return manifest
end