module Hen::Commands

Constants

COMMANDS
SKELETON
USAGE

Public Instance Methods

[](arg) click to toggle source
# File lib/hen/commands.rb, line 61
def [](arg)
  arg = arg.sub(/\A-+/, '')
  arg !~ /\W/ && public_method_defined?(arg) ? send(arg) : method_missing(arg)
end
config() click to toggle source
# File lib/hen/commands.rb, line 96
def config
  render(File.join(SKELETON, '_henrc'), henrc = Hen.default_henrc)

  puts
  puts "Your .henrc has been created: #{henrc}. Now adjust it to your needs."
end
create() click to toggle source
# File lib/hen/commands.rb, line 103
def create
  abort 'Path argument missing!' unless path = ARGV.shift

  skel = ARGV.first !~ /^-/ && ARGV.shift || File.join(SKELETON, 'project')
  abort "Project skeleton not found: #{skel}" unless File.directory?(skel)

  create_path(path = File.expand_path(path), created = [])
  create_skel(path, skel = File.expand_path(skel), created, replace = {})

  puts
  puts "Your new project directory has been created: #{path}. Have fun!"

  replace.each { |target, details|
    puts ["\n#{target}:", *details].join("\n  ") unless details.empty?
  }.clear
end
h()
Alias for: help
help() click to toggle source
# File lib/hen/commands.rb, line 70
def help
  puts USAGE
  puts
  puts 'Commands:'

  max = COMMANDS.keys.map(&:length).max

  COMMANDS.sort.each { |cmd, desc|
    puts "  %-#{max}s - %s" % [cmd, (desc = [*desc]).shift]
    desc.each { |extra| puts "  %#{max}s   + %s" % [' ', extra] }
  }
end
Also aliased as: h
list() click to toggle source
# File lib/hen/commands.rb, line 85
def list
  # How to achieve? Has to list *all* hens and tasks made available therein,
  # *regardless* of any missing prerequisites (preferably indicating whether
  # a particular hen/task is currently available).
  abort 'Sorry, not yet available...'
end
usage() click to toggle source
# File lib/hen/commands.rb, line 66
def usage
  abort USAGE
end
version() click to toggle source
# File lib/hen/commands.rb, line 92
def version
  puts "hen v#{VERSION}"
end

Private Instance Methods

create_git(path, created) click to toggle source
# File lib/hen/commands.rb, line 164
def create_git(path, created)
  ARGV.option!(:g, :git) { |remote|
    Dir.chdir(path) {
      if system('git', 'init')
        created << File.join(path, '.git')

        if remote.nil? && githubuser
          remote = "git@github.com:#{githubuser}/#{progname}"
        end

        unless remote.nil? || remote.empty?
          url, label = remote.split('=', 2).reverse
          system('git', 'remote', 'add', label ||= 'origin', url)

          system('git', 'config', 'branch.master.remote', label)
          system('git', 'config', 'branch.master.merge', 'refs/heads/master')
        end
      end
    }

    true
  }
end
create_path(path, created) click to toggle source
# File lib/hen/commands.rb, line 126
def create_path(path, created)
  if File.directory?(path)
    abort "Target directory already exists: #{path}. Won't touch."
  else
    Dir.mkdir(path)
    created << path
  end
end
create_skel(path, skel, created, replace) click to toggle source
# File lib/hen/commands.rb, line 135
def create_skel(path, skel, created, replace)
  progname(File.basename(path))  # pre-fill

  git = create_git(path, created)

  Dir.chdir(skel) {
    Dir['**/*'].each { |sample|
      next unless target = mangle_target(path, sample, git)

      created << target

      File.directory?(sample) ? FileUtils.mkdir_p(target) :
        replace[target] = render(sample, target).scan(/### .+ ###/)
    }
  }

  created.clear
ensure
  created.reverse_each { |item|
    if File.exist?(item)
      begin
        (File.directory?(item) ? Dir : File).unlink(item)
      rescue Errno::ENOTEMPTY
        File.basename(item) == '.git' ? FileUtils.rm_rf(item) : raise
      end
    end
  }.clear
end
mangle_target(path, sample, git) click to toggle source
# File lib/hen/commands.rb, line 188
def mangle_target(path, sample, git)
  target = sample.gsub(/__(.+?)__/) { send($1) }

  dir, name = File.split(target)
  name.sub!(/\A_/, '.')

  File.join(path, dir, name) if git || !name.start_with?('.git')
end
method_missing(method, *) click to toggle source
# File lib/hen/commands.rb, line 122
def method_missing(method, *)
  abort "Illegal command: #{method}\n#{USAGE}"
end