Sha256: ca0cd562a8e41ad5a04c28a05217cf2f4620adb4f808b62d96d290c92eeb8942

Contents?: true

Size: 1.96 KB

Versions: 14

Compression:

Stored size: 1.96 KB

Contents

require "shellwords"

# Generator to create a new HTML project based on an existing skeleton
class Roger::Generators::NewGenerator < Thor::Group
  include Thor::Actions

  desc "Create a new HTML roger project based on an existing skeleton"

  argument :path, type: :string, required: true, desc: "Path to generate project into"

  class_option(
    :template,
    type: :string,
    aliases: ["-t"],
    desc: "Template to use, can be a path or a git remote, uses built in minimal as default"
  )

  attr_reader :source_paths

  def setup_variables
    self.destination_root = path

    @source_paths = []

    # Stuff to rm -rf later
    @cleanup = []
  end

  def validate_path_is_empty
    return unless File.directory?(destination_root)

    say "Directory #{destination_root} already exists, please only use this to create new projects"
    exit(1)
  end

  def validate_template_path
    template = options[:template] || File.dirname(__FILE__) + "/../../../examples/default_template"

    if File.exist?(template)
      say "Taking template from #{template}"
      @source_paths << template
    else
      say "Getting template from git remote #{template}"
      @source_paths << git_clone_template(template)
    end
  rescue StandardError => e
    puts e
    puts e.backtrace.join("\n")
  end

  def create_project
    directory(".", ".")
  end

  protected

  def git_clone_template(template)
    tmp_dir = temp_directory

    if run("git clone --depth=1 #{Shellwords.escape(template)} #{tmp_dir}")
      say "Cloned template from #{template}"
      run("rm -rf #{tmp_dir + '.git'}")
      @cleanup << tmp_dir.to_s
    else
      say "Template path #{template} doesn't seem to be a git remote or a local path"
      exit(1)
    end

    tmp_dir
  end

  def temp_directory
    # Hack to create temp directory
    t = Tempfile.new("roger-generate-new")
    tmp_dir = Pathname.new(t.path)
    t.close
    t.unlink
    tmp_dir
  end
end

Roger::Generators.register Roger::Generators::NewGenerator

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
roger-1.7.0 lib/roger/generators/new.rb
roger-1.6.4 lib/roger/generators/new.rb
roger-1.6.3 lib/roger/generators/new.rb
roger-1.6.2 lib/roger/generators/new.rb
roger-1.6.1 lib/roger/generators/new.rb
roger-1.6.0 lib/roger/generators/new.rb
roger-1.5.0 lib/roger/generators/new.rb
roger-1.4.6 lib/roger/generators/new.rb
roger-1.4.5 lib/roger/generators/new.rb
roger-1.4.4 lib/roger/generators/new.rb
roger-1.4.3 lib/roger/generators/new.rb
roger-1.4.2 lib/roger/generators/new.rb
roger-1.4.1 lib/roger/generators/new.rb
roger-1.4.0 lib/roger/generators/new.rb