lib/aladdin/commands/new.rb in aladdin-0.0.4 vs lib/aladdin/commands/new.rb in aladdin-0.0.5

- old
+ new

@@ -1,16 +1,66 @@ # ~*~ encoding: utf-8 ~*~ require 'aladdin' require 'optparse' +require 'aladdin/constants' -# Array of skeleton files to be copied over. -SKELETON_FILES = %w(.genie.yml index.md images) +module Aladdin -opt_parser = OptionParser.new do |opts| - opts.banner = "Usage: aladdin new [options] [LESSON_PATH]" -end -opt_parser.parse! + module Commands -root = ARGV[0] || Dir.pwd + # @example + # $> aladdin new path/to/lesson/root + module New -Dir.chdir File.join File.dirname(__FILE__), *%w(.. .. .. skeleton) -FileUtils.cp_r SKELETON_FILES, root, verbose: true + # Array of skeleton files to be copied over. + FILES = %w(index.md images) << Aladdin::Config::FILE + + # Array of dot files to be copied over and renamed. + DOT_FILES = %w(gitignore) + + # Flags for {FileUtils.cp_r} + COPY_FLAGS = {verbose: true} + + # Copies skeleton files to given destination. + # @param [String] dest destination path + # @param [Hash] flags options for {FileUtils.cp_r} + # @return [Void] + def copy_files(dest, flags={}) + flags = COPY_FLAGS.merge flags + paths = FILES.map { |file| path_to file } + FileUtils.cp_r paths, dest, flags + DOT_FILES.each do |file| + FileUtils.cp_r path_to(file), File.join(dest, '.' + file), flags + end + end + + # Prefixes +filename+ with the skeleton directory. + # @param [String] filename name of file to resolve + # @return [String] path + def path_to(file) + File.expand_path file, Aladdin::PATHS.skeleton + end + + # Parses the command line arguments. + # @param [Array] argv command line arguments + # @return [Void] + def parse!(argv) + opt_parser = OptionParser.new do |opts| + opts.banner = "Usage: aladdin new [options] [LESSON_PATH]" + end + opt_parser.parse! argv + end + + extend self + + Commands.register do + def new(argv=ARGV, opts={}) + New.parse! argv + New.copy_files(argv[0] || Dir.pwd, opts) + end + end + + end + + end + +end