file manager pushes apps

This commit is contained in:
Chris Anderson 2008-09-16 11:14:25 -04:00
parent bb8371a718
commit 2356df978e
2 changed files with 105 additions and 0 deletions

View file

@ -185,9 +185,68 @@ module CouchRest
end
def push_app(appdir, appname)
libs = []
viewdir = File.join(appdir,"views")
attachdir = File.join(appdir,"attachments")
views, lang = read_design_views(viewdir)
docid = "_design/#{appname}"
design = @db.get(docid) rescue {}
design['_id'] = docid
design['views'] = views
design['language'] = lang
@db.save(design)
# puts views.inspect
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)
FileUtils.mkdir_p(app_dir)
FileUtils.mkdir_p(File.join(app_dir,"attachments"))
FileUtils.mkdir_p(File.join(app_dir,"views"))
index_template = File.join(File.expand_path(File.dirname(__FILE__)), 'templates','index.html')
index_dest = File.join(app_dir,"attachments","index.html")
FileUtils.cp(index_template, index_dest)
map_template = File.join(File.expand_path(File.dirname(__FILE__)), 'templates','example-map.js')
map_dest = File.join(app_dir,"views","example-map.js")
FileUtils.cp(map_template, map_dest)
rereduce_template = File.join(File.expand_path(File.dirname(__FILE__)), 'templates','example-reduce.js')
rereduce_dest = File.join(app_dir,"views","example-reduce.js")
FileUtils.cp(rereduce_template, rereduce_dest)
end
private
def read_design_views(viewdir)
libs = []
language = nil
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 say words
puts words if @loud
end

View file

@ -24,6 +24,52 @@ describe CouchRest::FileManager do
end
end
describe CouchRest::FileManager, "generating an app" do
before(:all) do
@appdir = File.expand_path(File.dirname(__FILE__)) + '/fixtures/couchapp'
`rm -rf #{@appdir}`
`mkdir -p #{@appdir}`
CouchRest::FileManager.generate_app(@appdir)
end
it "should create an attachments directory" do
Dir["#{@appdir}/*"].select{|x|x =~ /attachments/}.length.should == 1
end
it "should create a views directory" do
Dir["#{@appdir}/*"].select{|x|x =~ /views/}.length.should == 1
end
it "should create index.html" do
html = File.open("#{@appdir}/attachments/index.html").read
html.should match(/DOCTYPE/)
end
it "should create an example view" do
map = File.open("#{@appdir}/views/example-map.js").read
map.should match(/function\(doc\)/)
reduce = File.open("#{@appdir}/views/example-reduce.js").read
reduce.should match(/rereduce/)
end
end
describe CouchRest::FileManager, "pushing an app" do
before(:all) do
@cr = CouchRest.new(COUCHHOST)
@db = @cr.database(TESTDB)
@db.delete! rescue nil
@db = @cr.create_db(TESTDB) rescue nil
@appdir = File.expand_path(File.dirname(__FILE__)) + '/fixtures/couchapp'
`rm -rf #{@appdir}`
`mkdir -p #{@appdir}`
CouchRest::FileManager.generate_app(@appdir)
@fm = CouchRest::FileManager.new(TESTDB, COUCHHOST)
r = @fm.push_app(@appdir, "couchapp")
end
it "should create a design document" do
lambda{@db.get("_design/couchapp")}.should_not raise_error
end
end
describe CouchRest::FileManager, "pushing views" do
before(:all) do
@cr = CouchRest.new(COUCHHOST)