modernizing couchapp

This commit is contained in:
Chris Anderson 2009-01-01 22:45:41 -08:00
parent 293e9abad8
commit cc6039cbd4
4 changed files with 22 additions and 102 deletions

View file

@ -28,17 +28,13 @@ module CouchRest
package_forms(fields["forms"]) package_forms(fields["forms"])
package_views(fields["views"]) package_views(fields["views"])
# views, lang = read_design_views(viewdir)
docid = "_design/#{appname}" docid = "_design/#{appname}"
design = @db.get(docid) rescue {} design = @db.get(docid) rescue {}
design.merge!(fields) design.merge!(fields)
design['_id'] = docid design['_id'] = docid
# design['views'] = views
# design['language'] = lang if lang # design['language'] = lang if lang
@db.save(design) @db.save(design)
push_directory(attachdir, docid) push_directory(attachdir, docid)
# push_fields(appdir, docid)
end end
def push_directory(push_dir, docid=nil) def push_directory(push_dir, docid=nil)
@ -120,7 +116,9 @@ module CouchRest
end end
end end
# deprecated
def push_views(view_dir) def push_views(view_dir)
puts "WARNING this is deprecated, use `couchapp` script"
designs = {} designs = {}
Dir["#{view_dir}/**/*.*"].each do |design_doc| Dir["#{view_dir}/**/*.*"].each do |design_doc|
@ -171,37 +169,6 @@ module CouchRest
designs designs
end end
def push_forms(forms_dir)
designs = {}
Dir["#{forms_dir}/**/*.*"].each do |design_doc|
design_doc_parts = design_doc.split('/')
next if /^lib\..*$/.match design_doc_parts.last
pre_normalized_view_name = design_doc_parts.last.split("-")
form_name = pre_normalized_view_name[0..pre_normalized_view_name.length-2].join("-")
folder = design_doc_parts[-2]
designs[folder] ||= {}
designs[folder]["forms"] ||= {}
design_lang = design_doc_parts.last.split(".").last
designs[folder]["language"] ||= LANGS[design_lang]
libs = ""
Dir["#{forms_dir}/lib.#{design_lang}"].collect do |global_lib|
libs << open(global_lib).read
libs << "\n"
end
designs[folder]["forms"]["#{form_name}"] = read(design_doc, libs)
end
designs.each do |k,v|
create_or_update("_design/#{k}", v)
end
designs
end
def pull_views(view_dir) def pull_views(view_dir)
prefix = "_design" prefix = "_design"
ds = db.documents(:startkey => '#{prefix}/', :endkey => '#{prefix}/ZZZZZZZZZ') ds = db.documents(:startkey => '#{prefix}/', :endkey => '#{prefix}/ZZZZZZZZZ')
@ -233,7 +200,6 @@ module CouchRest
end end
end end
end end
end end
def dir_to_fields(dir) def dir_to_fields(dir)
@ -258,12 +224,6 @@ module CouchRest
return fields return fields
end end
def push_fields(appdir, docid)
fields = dir_to_fields(appdir)
design = @db.get(docid) rescue {}
design.merge!(fields)
@db.save(design)
end
# Generate an application in the given directory. # Generate an application in the given directory.
# This is a class method because it doesn't depend on # This is a class method because it doesn't depend on
@ -279,71 +239,23 @@ module CouchRest
if funcs["lib"] if funcs["lib"]
lib = "var lib = #{funcs["lib"].to_json};" lib = "var lib = #{funcs["lib"].to_json};"
funcs.delete("lib") funcs.delete("lib")
funcs.each do |k,v| apply_lib(funcs, lib)
funcs[k] = v.sub(/(\/\/|#)\ ?include-lib/,lib)
end
end end
end end
def package_views(fileviews) def package_views(views)
view = {} if views["_lib"]
if fileviews["lib"] lib = "var lib = #{views["_lib"].to_json};"
lib = "var lib = #{fileviews["lib"].to_json};" views.delete("_lib")
fileviews.delete("lib")
end end
fileviews.each do |filename, guts| views.each do |view, funcs|
puts filename apply_lib(funcs, lib) if lib
views[k] = v.sub(/(\/\/|#)\ ?include-lib/,lib) if lib
end end
end end
def read_design_views(viewdir) def apply_lib(funcs, lib)
libs = [] funcs.each do |k,v|
language = nil funcs[k] = v.sub(/(\/\/|#)\ ?include-lib/,lib)
views = {}
Dir["#{viewdir}/*.*"].each do |viewfile|
view_parts = viewfile.split('/')
viewfile_name = view_parts.last
# example-map.js
viewfile_name_parts = viewfile_name.split('.')
viewfile_ext = viewfile_name_parts.last
view_name_parts = viewfile_name_parts.first.split('-')
func_type = view_name_parts.pop
view_name = view_name_parts.join('-')
contents = File.open(viewfile).read
if /^lib\..*$/.match viewfile_name
libs.push(contents)
else
views[view_name] ||= {}
language = LANGS[viewfile_ext]
views[view_name][func_type] = contents.sub(/(\/\/|#)include-lib/,libs.join("\n"))
end
end
[views, language]
end
def read_forms(formdir)
lib = {}
language = nil
forms = {}
Dir["#{formdir}/*.*"].each do |viewfile|
view_parts = viewfile.split('/')
viewfile_name = view_parts.last
# example-map.js
viewfile_name_parts = viewfile_name.split('.')
viewfile_ext = viewfile_name_parts.last
view_name_parts = viewfile_name_parts.first.split('-')
func_type = view_name_parts.pop
view_name = view_name_parts.join('-')
contents = File.open(viewfile).read
if /^lib\..*$/.match viewfile_name
libs.push(contents)
else
views[view_name] ||= {}
language = LANGS[viewfile_ext]
views[view_name][func_type] = contents.sub(/(\/\/|#)include-lib/,libs.join("\n"))
end
end end
end end
@ -355,6 +267,7 @@ module CouchRest
Digest::MD5.hexdigest(string) Digest::MD5.hexdigest(string)
end end
# deprecated
def read(file, libs=nil) def read(file, libs=nil)
st = open(file).read st = open(file).read
st.sub!(/(\/\/|#)include-lib/,libs) if libs st.sub!(/(\/\/|#)include-lib/,libs) if libs

View file

@ -0,0 +1,3 @@
function aHelperFunction() {
return "help";
};

View file

@ -2,6 +2,7 @@
// and the list of keys it contains // and the list of keys it contains
function(doc) { function(doc) {
// include-lib
var k, keys = [] var k, keys = []
for (k in doc) keys.push(k); for (k in doc) keys.push(k);
emit(doc._id, keys); emit(doc._id, keys);

View file

@ -54,6 +54,9 @@ describe "couchapp" do
it "should create the views" do it "should create the views" do
@doc['views']['example']['map'].should match(/function/) @doc['views']['example']['map'].should match(/function/)
end end
it "should create the 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`