diff --git a/Gemfile b/Gemfile index 363ad13..f4507c2 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,13 @@ -source :rubygems +#source :rubygems +source "http://ruby.taobao.org" gem "tagen", "~>1.1.0" +gem "pd" gem "pa", "~>1.2.0" #gem "optimism", "~>3.1.0" -gem "optimism", :path => "/home/guten/dev/optimsim" +gem "optimism", :path => "/home/guten/dev/one/optimism" gem "thor" +gem "tilt" group :development do gem "rspec" diff --git a/Gemfile.lock b/Gemfile.lock index 3b2e764..2d8175c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,25 +1,36 @@ +PATH + remote: /home/guten/dev/one/optimism + specs: + optimism (3.1.2) + hike (~> 1.2.0) + GEM - remote: http://rubygems.org/ + remote: http://ruby.taobao.org/ specs: - activesupport (3.1.3) + activesupport (3.1.4) multi_json (~> 1.0) + awesome_print (1.0.2) diff-lcs (1.1.3) hike (1.2.1) - multi_json (1.0.4) - optimism (3.1.0) - hike (~> 1.2.0) - pa (1.2.1) - rspec (2.8.0) - rspec-core (~> 2.8.0) - rspec-expectations (~> 2.8.0) - rspec-mocks (~> 2.8.0) - rspec-core (2.8.0) - rspec-expectations (2.8.0) - diff-lcs (~> 1.1.2) - rspec-mocks (2.8.0) - tagen (1.1.4) + i18n (0.6.0) + multi_json (1.2.0) + pa (1.2.3) + pd (1.0.5) + awesome_print + rspec (2.9.0) + rspec-core (~> 2.9.0) + rspec-expectations (~> 2.9.0) + rspec-mocks (~> 2.9.0) + rspec-core (2.9.0) + rspec-expectations (2.9.1) + diff-lcs (~> 1.1.3) + rspec-mocks (2.9.0) + tagen (1.1.7) activesupport (~> 3.1.0) + i18n + pd thor (0.14.6) + tilt (1.3.3) watchr (0.7) yard (0.7.5) @@ -27,10 +38,12 @@ PLATFORMS ruby DEPENDENCIES - optimism (~> 3.1.0) + optimism! pa (~> 1.2.0) + pd rspec tagen (~> 1.1.0) thor + tilt watchr yard diff --git a/data/home_config/_ragrc b/data/home_config/_ragrc deleted file mode 100644 index bc0dd35..0000000 --- a/data/home_config/_ragrc +++ /dev/null @@ -1,8 +0,0 @@ -author = 'XX' -email = 'XX' -license = 'XX' - -github: - username = 'XX' - -# vim: filetype=ruby diff --git a/data/template/test/file_name.erb b/data/template/test/file_name.erb deleted file mode 100644 index 325363b..0000000 --- a/data/template/test/file_name.erb +++ /dev/null @@ -1 +0,0 @@ -<%=author%> diff --git a/hello/hello b/hello/hello new file mode 100644 index 0000000..b6fc4c6 --- /dev/null +++ b/hello/hello @@ -0,0 +1 @@ +hello \ No newline at end of file diff --git a/lib/rag.rb b/lib/rag.rb index 8395bad..3f923fb 100644 --- a/lib/rag.rb +++ b/lib/rag.rb @@ -1,6 +1,9 @@ libdir = File.dirname(__FILE__); $LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir) -require "tagen/core" +#require "tagen/core" +require "tagen/core/array/extract_options" +require "active_support/core_ext/string/inflections" +require "pd" require "pa" require "optimism" require "thor" @@ -11,6 +14,7 @@ class Rag < Thor autoload :Util, "rag/util" Error = Class.new Exception + ENoTemplate = Class.new Error RagError = Class.new Exception Rc = Optimism.require "rag/rc", "~/.ragrc" @@ -27,23 +31,44 @@ def ui end end - def initialize(*) - super - the_shell = (options["no-color"] ? Thor::Shell::Basic.new : shell) - Rag.ui = UI::Shell.new(the_shell) - Rag.ui.debug! if options["verbose"] - - # generate Rc.o - o = Rc.o = OpenOption.new - if gemspec_file=Dir["*.gemspec"][0] - gemspec = Gem::Specification.load(gemspec_file) - o.project = gemspec.name - o.version = gemspec.version + chainable = Module.new do + def initialize(*) + super + the_shell = (options["no-color"] ? Thor::Shell::Basic.new : shell) + Rag.ui = UI::Shell.new(the_shell) + Rag.ui.debug! if options["verbose"] end end + include chainable end -require "rag/new" -require "rag/gem" -require "rag/test" -require "rag/doc" +# new +class Rag + desc "new ", "create a new project" + method_option "name", :aliases => "-n", :type => :string, :banner => "NAME", :desc => "new project name" + method_option "klass", :aliases => "-k", :type => :string, :banner => "CLASS_NAME", :desc => "new class name" + def new(template_name, app_path) + o = options.dup + template_name = template_name.dup + app_path = Pa(app_path.dup) + require "rag/new" + + root = Pa("#{Rc.p.home}/#{template_name}") + raise ENoTemplate, "can't find '#{template_name}' at #{Rc.p.home}" unless root.exists? + + o["name"] ||= Pa.absolute(app_path).fname + o["klass"] ||= o["name"].classify + Rc << { + template_name: template_name, + app_path: app_path, + project: o["name"], + klass: o["klass"], + } + + source = File.read("#{root}/Buildfile") + DSL.source_root root.p + dsl = DSL.new(template_name, app_path, o) + dsl.destination_root = "" + dsl.instance_eval source + end +end diff --git a/lib/rag/ext.rb b/lib/rag/ext.rb new file mode 100644 index 0000000..52ecab6 --- /dev/null +++ b/lib/rag/ext.rb @@ -0,0 +1,50 @@ +class Thor + module Actions + def directory2(source, *args, &block) + config = args.last.is_a?(Hash) ? args.pop : {} + destination = args.first || source + action Directory2.new(self, source, destination || source, config, &block) + end + + # use tilt + # :scope => nil + # :locals => {} + # &block is for yield. + def template2(source, *args, &block) + config = args.last.is_a?(Hash) ? args.pop : {} + destination = args.first || source.sub(/\.tt$/, '') + template_options = {source_root: self.class.source_root} + + source = File.expand_path(find_in_source_paths(source.to_s)) + + create_file destination, nil, config do + Rag::Template.render(source, template_options, &block) + end + end + + class Directory2 < Directory #:nodoc: + protected + def execute! + lookup = config[:recursive] ? File.join(source, '**') : source + lookup = File.join(lookup, '{*,.[a-z]*}') + + Dir[lookup].sort.each do |file_source| + next if File.directory?(file_source) + file_destination = File.join(given_destination, file_source.gsub(source, '.')) + file_destination.gsub!('/./', '/') + + case file_source + when /\.empty_directory$/ + dirname = File.dirname(file_destination).gsub(/\/\.$/, '') + next if dirname == given_destination + base.empty_directory(dirname, config) + when /\.tt$/ + destination = base.template2(file_source, file_destination[0..-4], config, &@block) + else + destination = base.copy_file(file_source, file_destination, config, &@block) + end + end + end + end + end +end diff --git a/lib/rag/new.rb b/lib/rag/new.rb index f8794bf..9bd3e62 100644 --- a/lib/rag/new.rb +++ b/lib/rag/new.rb @@ -1,4 +1,7 @@ -class Rag # ::Project +require "tilt" +require "rag/ext" + +class Rag # Project Template DSL =begin == ERB support variables @@ -122,35 +125,43 @@ def get_erb_config config._data end end -end -class Rag < Thor - desc "new