class Boilerpl8::Operation

Constants

ALL

Public Class Methods

create(boilerplate_name) click to toggle source
# File lib/boilerpl8.rb, line 134
def self.create(boilerplate_name)
  #; [!xr4c6] reports error when argument has no schema.
  boilerplate_name =~ /\A(\w+:)/  or
    err("#{boilerplate_name}: expected 'github:' or 'file:' schema.")
  schema = $1
  #; [!95h3f] reports error when argument has unknown schema.
  klass = ALL.find {|cls| cls.const_get(:SCHEMA) == schema }  or
    err("#{boilerplate_name}: unknown schema (expected 'github:' or 'file:').")
  return klass.new()
end
inherited(klass) click to toggle source
# File lib/boilerpl8.rb, line 130
def self.inherited(klass)
  ALL << klass
end

Protected Class Methods

err(msg) click to toggle source
# File lib/boilerpl8.rb, line 122
def self.err(msg)
  raise CommandOptionError.new(msg)
end

Public Instance Methods

do_everything(boilerplate_name, target_dir, options) click to toggle source
# File lib/boilerpl8.rb, line 60
def do_everything(boilerplate_name, target_dir, options)
  url, filename = resolve(boilerplate_name, options)
  filepath = download(url, filename)
  basedir = extract(filepath, target_dir)
  kick_initializer(basedir)
end

Protected Instance Methods

download(url, filename) click to toggle source
# File lib/boilerpl8.rb, line 73
def download(url, filename)
  print "Download from #{url} ..."
  content = open(url) {|f| f.read }
  puts " done."
  File.open(filename, 'wb') {|f| f.write(content) }
  return filename
end
err(msg) click to toggle source
# File lib/boilerpl8.rb, line 118
def err(msg)
  raise CommandOptionError.new(msg)
end
extract(filename, basedir) click to toggle source
# File lib/boilerpl8.rb, line 81
def extract(filename, basedir)
  filename =~ /\.(zip|tgz|tar\.(gz|bz2|xz))\z/  or
    err("#{filename}: expected '*.zip' or '*.tar.gz'")
  base = File.basename($`)
  basedir ||= base
  rm_rf basedir if File.exist?(basedir)
  #
  case filename
  when /\.zip\z/
    tmpdir = basedir + ".tmp"
    sys "unzip -q -d #{tmpdir} #{filename}"
    paths = Dir.glob("#{tmpdir}/*")
    if paths.length == 1 && File.directory?(paths[0])
      mv paths[0], basedir
      rm_rf tmpdir
    else
      mv tmpdir, basedir
    end
  else
    sys "tar xf #{filename}"
    mv base, basedir if base != basedir
  end
  #
  return basedir
end
kick_initializer(basedir) click to toggle source
# File lib/boilerpl8.rb, line 107
def kick_initializer(basedir)
  chdir(basedir) do
    INITIALIZER_SCRIPTS.each do |script, lang|
      if File.exist?(script)
        sys "#{lang} #{script}"
        break
      end
    end
  end
end
resolve(arg, options) click to toggle source
# File lib/boilerpl8.rb, line 69
def resolve(arg, options)
  raise NotImplementedError.new("#{self.class.name}#resolve(): not implemented yet.")
end