more reorg for couchapp

This commit is contained in:
Chris Anderson 2009-01-01 23:11:01 -08:00
parent c435aa0932
commit 8733b631e3
4 changed files with 50 additions and 48 deletions

View file

@ -15,6 +15,7 @@ module CouchRest
"js" => "test/javascript", "js" => "test/javascript",
"txt" => "text/plain" "txt" => "text/plain"
} }
def initialize(dbname, host="http://127.0.0.1:5984") def initialize(dbname, host="http://127.0.0.1:5984")
@db = CouchRest.new(host).database(dbname) @db = CouchRest.new(host).database(dbname)
end end
@ -25,8 +26,9 @@ module CouchRest
attachdir = File.join(appdir,"_attachments") attachdir = File.join(appdir,"_attachments")
fields = dir_to_fields(appdir) fields = dir_to_fields(appdir)
package_forms(fields["forms"]) library = fields["library"]
package_views(fields["views"]) package_forms(fields["forms"], library)
package_views(fields["views"], library)
docid = "_design/#{appname}" docid = "_design/#{appname}"
design = @db.get(docid) rescue {} design = @db.get(docid) rescue {}
@ -37,6 +39,37 @@ module CouchRest
push_directory(attachdir, docid) push_directory(attachdir, docid)
end end
def dir_to_fields(dir)
fields = {}
(Dir["#{dir}/**/*.*"] -
Dir["#{dir}/_attachments/**/*.*"]).each do |file|
farray = file.sub(dir, '').sub(/^\//,'').split('/')
myfield = fields
while farray.length > 1
front = farray.shift
myfield[front] ||= {}
myfield = myfield[front]
end
fname, fext = farray.shift.split('.')
fguts = File.open(file).read
if fext == 'json'
myfield[fname] = JSON.parse(fguts)
else
myfield[fname] = fguts
end
end
return fields
end
# Generate an application in the given directory.
# This is a class method because it doesn't depend on
# specifying a database.
def self.generate_app(app_dir)
templatedir = File.join(File.expand_path(File.dirname(__FILE__)), 'template-app')
FileUtils.cp_r(templatedir, app_dir)
end
def push_directory(push_dir, docid=nil) def push_directory(push_dir, docid=nil)
docid ||= push_dir.split('/').reverse.find{|part|!part.empty?} docid ||= push_dir.split('/').reverse.find{|part|!part.empty?}
@ -116,57 +149,23 @@ module CouchRest
end end
end end
def dir_to_fields(dir)
fields = {}
(Dir["#{dir}/**/*.*"] -
Dir["#{dir}/_attachments/**/*.*"]).each do |file|
farray = file.sub(dir, '').sub(/^\//,'').split('/')
myfield = fields
while farray.length > 1
front = farray.shift
myfield[front] ||= {}
myfield = myfield[front]
end
fname, fext = farray.shift.split('.')
fguts = File.open(file).read
if fext == 'json'
myfield[fname] = JSON.parse(fguts)
else
myfield[fname] = fguts
end
end
return fields
end
# Generate an application in the given directory.
# This is a class method because it doesn't depend on
# specifying a database.
def self.generate_app(app_dir)
templatedir = File.join(File.expand_path(File.dirname(__FILE__)), 'template-app')
FileUtils.cp_r(templatedir, app_dir)
end
private private
def package_forms(funcs) def package_forms(funcs, library)
if funcs["lib"] if library
lib = "var lib = #{funcs["lib"].to_json};" lib = "var library = #{library.to_json};"
funcs.delete("lib")
apply_lib(funcs, lib) apply_lib(funcs, lib)
end end
end end
def package_views(views) def package_views(views, library)
if views["_lib"] if library
lib = "var lib = #{views["_lib"].to_json};" lib = "var library = #{library.to_json};"
views.delete("_lib")
end
views.each do |view, funcs| views.each do |view, funcs|
apply_lib(funcs, lib) if lib apply_lib(funcs, lib) if lib
end end
end end
end
def apply_lib(funcs, lib) def apply_lib(funcs, lib)
funcs.each do |k,v| funcs.each do |k,v|

View file

@ -34,7 +34,7 @@ describe "couchapp" do
end end
it "should create a forms and libs" do it "should create a forms and libs" do
Dir["#{@fixdir}/my-app/forms/*"].select{|x|x =~ /example-form.js/}.length.should == 1 Dir["#{@fixdir}/my-app/forms/*"].select{|x|x =~ /example-form.js/}.length.should == 1
Dir["#{@fixdir}/my-app/forms/lib/*"].select{|x|x =~ /example-template.html/}.length.should == 1 Dir["#{@fixdir}/my-app/library/templates/*"].select{|x|x =~ /example-template.html/}.length.should == 1
end end
end end
@ -57,6 +57,9 @@ describe "couchapp" do
it "should create the view libs" do it "should create the view libs" do
@doc['views']['example']['map'].should match(/aHelperFunction/) @doc['views']['example']['map'].should match(/aHelperFunction/)
end end
it "should create specific view libs" do
@doc['views']['example']['map'].should match(/aHelperFunction/)
end
it "should create view for all the views" do it "should create view for all the views" do
`mkdir -p #{@fixdir}/my-app/views/more` `mkdir -p #{@fixdir}/my-app/views/more`
`echo 'moremap' > #{@fixdir}/my-app/views/more/map.js` `echo 'moremap' > #{@fixdir}/my-app/views/more/map.js`