simplyfy couchapp push

This commit is contained in:
Chris Anderson 2009-01-01 22:29:39 -08:00
parent 98ff079093
commit a10d902d71
8 changed files with 96 additions and 29 deletions

View file

@ -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

View file

@ -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 : <xml><node value={doc.title}/></xml>
}
}
})
};

View file

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<title>Generated CouchApp Form Template</title>
</head>
<body>
<div id="header">
<h2><a href="index.html">Back to index</a></h2>
</div>
<div id="content">
<h1><% doc.title %></h1>
</div>
</body>
<script src="/_utils/script/json2.js"></script>
<script src="/_utils/script/jquery.js?1.2.6"></script>
<script src="/_utils/script/jquery.couch.js?0.8.0"></script>
<script src="jquery.couchapp.js"></script>
<script src="blog.js"></script>
<script type="text/javascript" charset="utf-8">
$.CouchApp(function(app) {
var docid = document.location.pathname.split('/').pop();
// hey you could run a query to load more information from views
});
</script>
<script src="showdown.js"></script>
</html>

View file

@ -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