Commit ed05a78e by Marcos Augusto

dont add whitespace on routes, and some minor stuff

parent 63467863
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = %q{annotate} s.name = %q{annotate}
s.version = "2.2.3" s.version = "2.2.5"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Marcos Piccinini"] s.authors = ["Marcos Piccinini"]
s.date = %q{2008-12-26} s.date = %q{2008-12-28}
s.default_executable = %q{annotate} s.default_executable = %q{annotate}
s.description = %q{Annotates Rails Models and Routes} s.description = %q{Annotates Rails Models and Routes}
s.email = ["x@nofxx.com"] s.email = ["x@nofxx.com"]
...@@ -26,14 +26,14 @@ Gem::Specification.new do |s| ...@@ -26,14 +26,14 @@ Gem::Specification.new do |s|
s.specification_version = 2 s.specification_version = 2
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<newgem>, [">= 1.2.1"]) s.add_development_dependency(%q<newgem>, [">= 1.2.2"])
s.add_development_dependency(%q<hoe>, [">= 1.8.0"]) s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
else else
s.add_dependency(%q<newgem>, [">= 1.2.1"]) s.add_dependency(%q<newgem>, [">= 1.2.2"])
s.add_dependency(%q<hoe>, [">= 1.8.0"]) s.add_dependency(%q<hoe>, [">= 1.8.0"])
end end
else else
s.add_dependency(%q<newgem>, [">= 1.2.1"]) s.add_dependency(%q<newgem>, [">= 1.2.2"])
s.add_dependency(%q<hoe>, [">= 1.8.0"]) s.add_dependency(%q<hoe>, [">= 1.8.0"])
end end
end end
...@@ -15,6 +15,7 @@ end.parse! ...@@ -15,6 +15,7 @@ end.parse!
begin begin
Rake::Task[task].invoke Rake::Task[task].invoke
#TODO: rescue only rake error
rescue NameError => e rescue NameError => e
puts "Can`t find Rake. Are we in a Rails folder?" puts "Can`t find Rake. Are we in a Rails folder?"
end end
...@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless ...@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
module Annotate module Annotate
VERSION = '2.2.3' VERSION = '2.2.5'
end end
begin begin
......
...@@ -17,21 +17,22 @@ ...@@ -17,21 +17,22 @@
# Released under the same license as Ruby. No Support. No Warranty.module AnnotateRoutes # Released under the same license as Ruby. No Support. No Warranty.module AnnotateRoutes
# #
module AnnotateRoutes module AnnotateRoutes
PREFIX = "#== Route Info" PREFIX = "#== Route Map"
def self.do_annotate def self.do_annotate
routes_rb = File.join("config", "routes.rb") routes_rb = File.join("config", "routes.rb")
header = PREFIX + "\n# Generated on #{Time.now}\n#" header = PREFIX + "\n# Generated on #{Time.now.strftime("%d %b %Y %H:%M")}\n#"
if File.exists? routes_rb if File.exists? routes_rb
routes_map = `rake routes` routes_map = `rake routes`
routes_map = routes_map.split("\n") routes_map = routes_map.split("\n")
routes_map.shift # remove the first line of rake routes which is just a file path routes_map.shift # remove the first line of rake routes which is just a file path
routes_map = routes_map.inject(header){|sum, line| sum<<"\n# "<<line} routes_map = routes_map.inject(header){|sum, line| sum<<"\n# "<<line}
content = File.read(routes_rb) content = File.read(routes_rb)
content = content.split(/^#== Route Info.*?\n/)#, '') content, old = content.split(/^#== Route .*?\n/)
File.open(routes_rb, "wb") do |f| File.open(routes_rb, "wb") do |f|
f.puts content[0] + "\n\n" + routes_map f.puts content.sub!(/\n?\z/, "\n") + routes_map
end end
puts "Route file annotated."
else else
puts "Can`t find routes.rb" puts "Can`t find routes.rb"
end end
......
...@@ -7,24 +7,41 @@ describe AnnotateRoutes do ...@@ -7,24 +7,41 @@ describe AnnotateRoutes do
@mock_file ||= mock(File, stubs) @mock_file ||= mock(File, stubs)
end end
describe "Annotate Job" do
before(:each) do
File.should_receive(:join).with("config", "routes.rb").and_return("config/routes.rb")
end
it "should check if routes.rb exists" do it "should check if routes.rb exists" do
File.should_receive(:join).with("config", "routes.rb").and_return(mock_file) File.should_receive(:exists?).with("config/routes.rb").and_return(false)
File.should_receive(:exists?).with(@mock_file).and_return(false)
AnnotateRoutes.should_receive(:puts).with("Can`t find routes.rb") AnnotateRoutes.should_receive(:puts).with("Can`t find routes.rb")
AnnotateRoutes.do_annotate AnnotateRoutes.do_annotate
end end
it "should annotate!" do describe "When Annotating" do
File.should_receive(:join).with("config", "routes.rb").and_return("config/routes.rb")
before(:each) do
File.should_receive(:exists?).with("config/routes.rb").and_return(true) File.should_receive(:exists?).with("config/routes.rb").and_return(true)
AnnotateRoutes.should_receive(:`).with("rake routes").and_return("bad line\ngood line") AnnotateRoutes.should_receive(:`).with("rake routes").and_return("bad line\ngood line")
File.should_receive(:read).with("config/routes.rb").and_return("bla")
File.should_receive(:open).with("config/routes.rb", "wb").and_yield(mock_file) File.should_receive(:open).with("config/routes.rb", "wb").and_yield(mock_file)
@mock_file.should_receive(:puts).with(/bla\n\n#== Route Info\n# Generated on .*\n#\n# good line/) AnnotateRoutes.should_receive(:puts).with("Route map annotated.")
end
it "should annotate and add a newline!" do
File.should_receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo")
@mock_file.should_receive(:puts).with(/ActionController::Routing...\nfoo\n#== Route Map\n# Generated on .*\n#\n# good line/)
AnnotateRoutes.do_annotate
end
it "should not add a newline if there are empty lines" do
File.should_receive(:read).with("config/routes.rb").and_return("ActionController::Routing...\nfoo\n")
@mock_file.should_receive(:puts).with(/ActionController::Routing...\nfoo\n#== Route Map\n# Generated on .*\n#\n# good line/)
AnnotateRoutes.do_annotate AnnotateRoutes.do_annotate
end end
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment