From a10d902d71acd0d39a908f2c58c0f523c98f8f59 Mon Sep 17 00:00:00 2001 From: Chris Anderson Date: Thu, 1 Jan 2009 22:29:39 -0800 Subject: [PATCH] simplyfy couchapp push --- lib/couchrest/helper/file_manager.rb | 55 ++++++++++++------- .../_attachments}/index.html | 0 .../{templates => template-app/foo}/bar.txt | 0 .../helper/template-app/forms/example-form.js | 14 +++++ .../forms/lib/example-template.html | 26 +++++++++ .../views}/example-map.js | 0 .../views}/example-reduce.js | 0 spec/couchapp_spec.rb | 30 +++++++--- 8 files changed, 96 insertions(+), 29 deletions(-) rename lib/couchrest/helper/{templates => template-app/_attachments}/index.html (100%) rename lib/couchrest/helper/{templates => template-app/foo}/bar.txt (100%) create mode 100644 lib/couchrest/helper/template-app/forms/example-form.js create mode 100644 lib/couchrest/helper/template-app/forms/lib/example-template.html rename lib/couchrest/helper/{templates => template-app/views}/example-map.js (100%) rename lib/couchrest/helper/{templates => template-app/views}/example-reduce.js (100%) diff --git a/lib/couchrest/helper/file_manager.rb b/lib/couchrest/helper/file_manager.rb index e2a0228..41cba86 100644 --- a/lib/couchrest/helper/file_manager.rb +++ b/lib/couchrest/helper/file_manager.rb @@ -23,16 +23,22 @@ module CouchRest libs = [] viewdir = File.join(appdir,"views") attachdir = File.join(appdir,"_attachments") - views, lang = read_design_views(viewdir) + + fields = dir_to_fields(appdir) + package_forms(fields["forms"]) + package_views(fields["views"]) + + # views, lang = read_design_views(viewdir) docid = "_design/#{appname}" design = @db.get(docid) rescue {} + design.merge!(fields) design['_id'] = docid - design['views'] = views - design['language'] = lang if lang + # design['views'] = views + # design['language'] = lang if lang @db.save(design) push_directory(attachdir, docid) - push_fields(appdir, docid) + # push_fields(appdir, docid) end def push_directory(push_dir, docid=nil) @@ -233,7 +239,6 @@ module CouchRest def dir_to_fields(dir) fields = {} (Dir["#{dir}/**/*.*"] - - Dir["#{dir}/views/**/*.*"] - Dir["#{dir}/_attachments/**/*.*"]).each do |file| farray = file.sub(dir, '').sub(/^\//,'').split('/') myfield = fields @@ -264,25 +269,35 @@ module CouchRest # This is a class method because it doesn't depend on # specifying a database. def self.generate_app(app_dir) - FileUtils.mkdir_p(app_dir) - FileUtils.mkdir_p(File.join(app_dir,"_attachments")) - FileUtils.mkdir_p(File.join(app_dir,"views")) - FileUtils.mkdir_p(File.join(app_dir,"foo")) - - { - "index.html" => "_attachments", - 'example-map.js' => "views", - 'example-reduce.js' => "views", - 'bar.txt' => "foo", - }.each do |filename, targetdir| - template = File.join(File.expand_path(File.dirname(__FILE__)), 'templates',filename) - dest = File.join(app_dir,targetdir,filename) - FileUtils.cp(template, dest) - end + templatedir = File.join(File.expand_path(File.dirname(__FILE__)), 'template-app') + FileUtils.cp_r(templatedir, app_dir) end private + def package_forms(funcs) + if funcs["lib"] + lib = "var lib = #{funcs["lib"].to_json};" + funcs.delete("lib") + funcs.each do |k,v| + funcs[k] = v.sub(/(\/\/|#)\ ?include-lib/,lib) + end + end + end + + def package_views(fileviews) + view = {} + if fileviews["lib"] + lib = "var lib = #{fileviews["lib"].to_json};" + fileviews.delete("lib") + end + fileviews.each do |filename, guts| + puts filename + + views[k] = v.sub(/(\/\/|#)\ ?include-lib/,lib) if lib + end + end + def read_design_views(viewdir) libs = [] language = nil diff --git a/lib/couchrest/helper/templates/index.html b/lib/couchrest/helper/template-app/_attachments/index.html similarity index 100% rename from lib/couchrest/helper/templates/index.html rename to lib/couchrest/helper/template-app/_attachments/index.html diff --git a/lib/couchrest/helper/templates/bar.txt b/lib/couchrest/helper/template-app/foo/bar.txt similarity index 100% rename from lib/couchrest/helper/templates/bar.txt rename to lib/couchrest/helper/template-app/foo/bar.txt diff --git a/lib/couchrest/helper/template-app/forms/example-form.js b/lib/couchrest/helper/template-app/forms/example-form.js new file mode 100644 index 0000000..2ac1ce7 --- /dev/null +++ b/lib/couchrest/helper/template-app/forms/example-form.js @@ -0,0 +1,14 @@ +function(doc, req) { + // include-lib + respondWith(req, { + html : function() { + var html = template(lib["example.html"], doc); + return {body:html} + }, + xml : function() { + return { + body : + } + } + }) +}; \ No newline at end of file diff --git a/lib/couchrest/helper/template-app/forms/lib/example-template.html b/lib/couchrest/helper/template-app/forms/lib/example-template.html new file mode 100644 index 0000000..6e1aa89 --- /dev/null +++ b/lib/couchrest/helper/template-app/forms/lib/example-template.html @@ -0,0 +1,26 @@ + + + + Generated CouchApp Form Template + + + +
+

<% doc.title %>

+
+ + + + + + + + + \ No newline at end of file diff --git a/lib/couchrest/helper/templates/example-map.js b/lib/couchrest/helper/template-app/views/example-map.js similarity index 100% rename from lib/couchrest/helper/templates/example-map.js rename to lib/couchrest/helper/template-app/views/example-map.js diff --git a/lib/couchrest/helper/templates/example-reduce.js b/lib/couchrest/helper/template-app/views/example-reduce.js similarity index 100% rename from lib/couchrest/helper/templates/example-reduce.js rename to lib/couchrest/helper/template-app/views/example-reduce.js diff --git a/spec/couchapp_spec.rb b/spec/couchapp_spec.rb index da8af52..cd5a082 100644 --- a/spec/couchapp_spec.rb +++ b/spec/couchapp_spec.rb @@ -16,14 +16,26 @@ describe "couchapp" do end describe "generate my-app" do + before(:all) do + `#{@run} generate my-app`.should match(/generating/i) + end it "should create an app directory" do - `#{@run} generate my-app`.should match(/generating/i) Dir["#{@fixdir}/*"].select{|x|x =~ /my-app/}.length.should == 1 end it "should create a views directory" do - `#{@run} generate my-app`.should match(/generating/i) Dir["#{@fixdir}/my-app/*"].select{|x|x =~ /views/}.length.should == 1 end + it "should create an _attachments directory" do + Dir["#{@fixdir}/my-app/*"].select{|x|x =~ /_attachments/}.length.should == 1 + Dir["#{@fixdir}/my-app/_attachments/*"].select{|x|x =~ /index.html/}.length.should == 1 + end + it "should create a forms directory" do + Dir["#{@fixdir}/my-app/*"].select{|x|x =~ /forms/}.length.should == 1 + end + 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/lib/*"].select{|x|x =~ /example-template.html/}.length.should == 1 + end end describe "push my-app #{TESTDB}" do @@ -33,15 +45,14 @@ describe "couchapp" do @db.delete! rescue nil @db = @cr.create_db(TESTDB) rescue nil `#{@run} generate my-app` + `#{@run} push my-app #{TESTDB}` + @doc = @db.get("_design/my-app") end it "should create the design document with the app name" do - `#{@run} push my-app #{TESTDB}` lambda{@db.get("_design/my-app")}.should_not raise_error end it "should create the views" do - `#{@run} push my-app #{TESTDB}` - doc = @db.get("_design/my-app") - doc['views']['example']['map'].should match(/function/) + @doc['views']['example']['map'].should match(/function/) end it "should create view for all the views" do `echo 'moremap' > #{@fixdir}/my-app/views/more-map.js` @@ -50,9 +61,10 @@ describe "couchapp" do doc['views']['more']['map'].should match(/moremap/) end it "should create the index" do - `#{@run} push my-app #{TESTDB}` - doc = @db.get("_design/my-app") - doc['_attachments']['index.html']["content_type"].should == 'text/html' + @doc['_attachments']['index.html']["content_type"].should == 'text/html' + end + it "should push the forms" do + @doc['forms']['example-form'].should match(/Generated CouchApp Form Template/) end end