class Extensionator::Creator

Public Class Methods

new(dir, options) click to toggle source
# File lib/extensionator/creator.rb, line 10
def initialize(dir, options)
  @dir = dir
  @opts = options
  @opts[:exclude] ||= /\.crx$/
end

Public Instance Methods

copy(destination) click to toggle source
# File lib/extensionator/creator.rb, line 28
def copy(destination)
  FileUtils.mkdir_p(destination)

  process_directory.each do |p|
    path_in_dir, file = p

    write_path = File.join(destination, path_in_dir)

    if File.directory?(file)
      FileUtils.mkdir_p(write_path)
    else
      FileUtils.cp(file, write_path)
    end
  end
end
crx(destination) click to toggle source
# File lib/extensionator/creator.rb, line 22
def crx(destination)
  with_zip do |zip_str|
    CRX.create(destination, zip_str, private_key)
  end
end
validate() click to toggle source
# File lib/extensionator/creator.rb, line 44
def validate
  manifest.validate(dir_files)
end
zip(destination) click to toggle source
# File lib/extensionator/creator.rb, line 16
def zip(destination)
  with_zip do |zip_str|
    File.open(destination, "wb"){|f| f.write zip_str}
  end
end

Private Instance Methods

dir_files() click to toggle source
# File lib/extensionator/creator.rb, line 82
def dir_files
  unless @dir_files
    @dir_files = []
    Find.find(@dir) do |path|
      case
        when path == @dir
          next
        when (@opts[:exclude] && path =~ @opts[:exclude])
          Find.prune
        else
          @dir_files << [relative_path(@dir, path), path]
      end
    end
  end
  @dir_files
end
manifest() click to toggle source
# File lib/extensionator/creator.rb, line 99
def manifest
  @manifest ||= Manifest.new(@dir)
end
manifest_updated?() click to toggle source
# File lib/extensionator/creator.rb, line 103
def manifest_updated?
  @manifest && @manifest.updated?
end
private_key() click to toggle source
# File lib/extensionator/creator.rb, line 107
def private_key
  @private_key ||=
    if @opts[:identity]
      begin
        OpenSSL::PKey::RSA.new(File.read(@opts[:identity]))
      rescue Error => e
        raise KeyError.new("Couldn't read key file #{@opts[:identity]}: #{e.message}")
      end
    else
      raise ArgumentError.new("You need to specify an identity file for this operation.")
    end
end
process_directory() click to toggle source
# File lib/extensionator/creator.rb, line 55
def process_directory
  new_paths = dir_files

  manifest.validate(new_paths) unless @opts[:skip_validation]
  manifest.inject_key(private_key) if @opts[:inject_key]
  manifest.strip_key if @opts[:strip_key]
  manifest.inject_version(@opts[:inject_version]) if @opts[:inject_version]

  new_paths << manifest.write if manifest_updated?

  new_paths
end
relative_path(base, target) click to toggle source
# File lib/extensionator/creator.rb, line 120
def relative_path(base, target)
  from, to = [base, target].map(&Pathname.method(:new))
  to.relative_path_from(from).to_s
end
with_zip() { |zip_up(files)| ... } click to toggle source
# File lib/extensionator/creator.rb, line 50
def with_zip
  files = process_directory
  yield zip_up(files)
end
zip_up(paths) click to toggle source
# File lib/extensionator/creator.rb, line 68
def zip_up(paths)
  Zip.continue_on_exists_proc = true
  Zip::File.add_buffer do |zip|
    paths.each do |path_combo|
      path_in_zip, file = path_combo
      if File.directory?(file)
        zip.mkdir(path_in_zip)
      else
        zip.add(path_in_zip, file)
      end
    end
  end.string
end