Skip to content

Commit

Permalink
begin to test
Browse files Browse the repository at this point in the history
  • Loading branch information
gutenye committed May 15, 2012
1 parent e86e32c commit b550443
Show file tree
Hide file tree
Showing 21 changed files with 249 additions and 110 deletions.
7 changes: 5 additions & 2 deletions 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"
Expand Down
45 changes: 29 additions & 16 deletions Gemfile.lock
@@ -1,36 +1,49 @@
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)

PLATFORMS
ruby

DEPENDENCIES
optimism (~> 3.1.0)
optimism!
pa (~> 1.2.0)
pd
rspec
tagen (~> 1.1.0)
thor
tilt
watchr
yard
8 changes: 0 additions & 8 deletions data/home_config/_ragrc

This file was deleted.

1 change: 0 additions & 1 deletion data/template/test/file_name.erb

This file was deleted.

1 change: 1 addition & 0 deletions hello/hello
@@ -0,0 +1 @@
hello
59 changes: 42 additions & 17 deletions 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"
Expand All @@ -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"

Expand All @@ -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 <template_name> <app_path>", "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
50 changes: 50 additions & 0 deletions 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
73 changes: 42 additions & 31 deletions 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
Expand Down Expand Up @@ -122,35 +125,43 @@ def get_erb_config
config._data
end
end
end

class Rag < Thor
desc "new <template> <app_path>", "create a new project"
method_option "name", :aliases => "-n", :type => :string, :banner => "NAME", :desc => "another name"
method_option "class_name", :aliases => "-c", :type => :string, :banner => "CLASS_NAME", :desc => "another class name"
def new(template, app_path)
check_first_time!

Project.create template.dup, app_path.dup, options.dup
end

private
def check_first_time!
return unless first_time?

puts "first time run rag"
Pa.each("#{Rc.p.data}/home_config") { |src|
dest = "~/" + src.b.sub(/^_/, ".")
puts "[create] #{dest.short}"
Pa.cp src, dest
}

puts "exit."
puts "please edit ~/.ragrc config file, then run rag again"
exit
end

def first_time?
Rc.p.homerc.exists? ? false : true
end
class Template
# @param [Hash] o options pass to initialize
def self.render(source, o={}, &blk)
scope = Template.new(o)

Tilt["erb"].new(source).render(scope, scope.locals, &blk)
end

attr_reader :source_root

# @param [Hash] o options
# @option o [String] :source_root ("")
def initialize(o={})
@source_root = o["source_root"] || ""
end

def render(path)
path = Pa.absolute?(path) ? path : Pa.join(source_root, path)

Tilt.new(path).render(self, locals)
end

# for tilt
def locals
Rc._to_hash
end
end

class DSL < Thor
include Thor::Actions
attr_reader :options, :template_name, :app_path

def initialize(template_name, app_path, o={})
@template_name = template_name
@app_path = app_path
@options = o
end
end
end
21 changes: 21 additions & 0 deletions lib/rag/ruby.rb
@@ -0,0 +1,21 @@
class Rag
chainable = Module.new do
def initialize(*)
super

# generate Rc.o
o = Rc.o = Optimism.new
if gemspec_file=Dir["*.gemspec"][0]
gemspec = Gem::Specification.load(gemspec_file)
o.project = gemspec.name
o.version = gemspec.version
end
end
end

include chainable
end

require "rag/ruby/gem"
require "rag/ruby/test"
require "rag/ruby/doc"
2 changes: 1 addition & 1 deletion lib/rag/ruby/gem.rb
@@ -1,4 +1,4 @@
class Rag < Thor
class Rag
desc "release", "release this gem to RubyGems.org"
def release
sudo = Process.uid == 0 ? "" : "sudo"
Expand Down
6 changes: 3 additions & 3 deletions lib/rag/ruby/test.rb
Expand Up @@ -2,14 +2,14 @@ module Ragen
class Test < Thor
default_task :default

desc "default", "default", hide: true
desc "default", "begin auto test", hide: true
def default
system "watchr #{Rag::Rc.o.project}.watchr", :verbose => true
end

desc "test [all]", "auto test with watchr"
desc "all", "test all"
def all
system "rspec --color spec", :verbose => true
system "bundle exec rspec --color spec", :verbose => true
end

end
Expand Down
2 changes: 2 additions & 0 deletions rag.gemspec
Expand Up @@ -16,8 +16,10 @@ A project helper, include create, develop, test, release helper.
s.files = `git ls-files`.split("\n")
s.executables = %w(rag)

s.add_dependency "pd"
s.add_dependency "tagen", "~>1.1.0"
s.add_dependency "pa", "~>1.2.0"
s.add_dependency "optimism", "~>3.1.0"
s.add_dependency "thor"
s.add_dependency "tilt"
end
2 changes: 1 addition & 1 deletion rag.watchr
Expand Up @@ -15,7 +15,7 @@ Signal.trap('QUIT') do
end

def test(path)
cmd = "rspec #{path}"
cmd = "bundle exec rspec #{path}"
puts cmd
system cmd
end
Expand Down
1 change: 0 additions & 1 deletion spec/data/_rag/rb.app/hello.erb

This file was deleted.

8 changes: 0 additions & 8 deletions spec/data/_ragrc

This file was deleted.

3 changes: 3 additions & 0 deletions spec/data/home/ruby.app/Buildfile
@@ -0,0 +1,3 @@
directory2 "template", "hello"

# vim: ft=ruby
1 change: 1 addition & 0 deletions spec/data/home/ruby.app/template/hello.tt
@@ -0,0 +1 @@
<%=project%>

0 comments on commit b550443

Please sign in to comment.