2010-09-22 22:59:30 +00:00
|
|
|
require 'zip/zipfilesystem'
|
|
|
|
|
|
|
|
module Locomotive
|
|
|
|
module Import
|
|
|
|
class Job
|
|
|
|
|
2010-09-28 08:09:49 +00:00
|
|
|
def initialize(theme_file, site = nil, enabled = {})
|
2010-09-22 22:59:30 +00:00
|
|
|
raise "Theme zipfile not found" unless File.exists?(theme_file)
|
|
|
|
|
|
|
|
@theme_file = theme_file
|
|
|
|
@site = site
|
2010-09-28 22:08:11 +00:00
|
|
|
@enabled = enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def before(worker)
|
|
|
|
@worker = worker
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
2010-09-28 22:08:11 +00:00
|
|
|
puts "theme_file = #{@theme_file} / #{@site.present?} / #{@enabled.inspect}"
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
self.unzip!
|
|
|
|
|
|
|
|
raise "No database.yml found in the theme zipfile" if @database.nil?
|
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
context = {
|
|
|
|
:database => @database,
|
|
|
|
:site => @site,
|
2010-09-28 08:09:49 +00:00
|
|
|
:theme_path => @theme_path,
|
2010-09-28 22:08:11 +00:00
|
|
|
:error => nil,
|
|
|
|
:worker => @worker
|
2010-09-23 23:00:13 +00:00
|
|
|
}
|
|
|
|
|
2010-10-07 00:45:41 +00:00
|
|
|
%w(site content_types assets asset_collections snippets pages).each do |step|
|
2010-09-28 22:08:11 +00:00
|
|
|
if @enabled[step] != false
|
2010-10-07 00:45:41 +00:00
|
|
|
@worker.update_attributes :step => step if @worker
|
|
|
|
# puts "@worker...#{@worker.failed_at.inspect} / #{@worker.failed?.inspect}"
|
2010-09-28 22:08:11 +00:00
|
|
|
"Locomotive::Import::#{step.camelize}".constantize.process(context)
|
|
|
|
else
|
|
|
|
puts "skipping #{step}"
|
2010-09-28 08:09:49 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-23 23:00:13 +00:00
|
|
|
|
2010-09-28 22:08:11 +00:00
|
|
|
|
|
|
|
# rescue Exception => e
|
|
|
|
# context[:error] = e.message
|
|
|
|
# end
|
2010-09-28 08:09:49 +00:00
|
|
|
#
|
2010-09-28 22:08:11 +00:00
|
|
|
# context
|
|
|
|
# # Locomotive::Import::Site.process(context)
|
|
|
|
# #
|
|
|
|
# # Locomotive::Import::ContentTypes.process(context)
|
|
|
|
# #
|
|
|
|
# # Locomotive::Import::Assets.process(context)
|
|
|
|
# #
|
|
|
|
# # Locomotive::Import::Snippets.process(context)
|
|
|
|
# #
|
|
|
|
# # Locomotive::Import::Pages.process(context)
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def unzip!
|
|
|
|
Zip::ZipFile.open(@theme_file) do |zipfile|
|
|
|
|
destination_path = File.join(Rails.root, 'tmp', 'themes', @site.id.to_s)
|
2010-09-28 08:09:49 +00:00
|
|
|
|
|
|
|
FileUtils.rm_r destination_path, :force => true
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
zipfile.each do |entry|
|
|
|
|
next if entry.name =~ /__MACOSX/
|
|
|
|
|
|
|
|
if entry.name =~ /database.yml$/
|
|
|
|
|
|
|
|
@database = YAML.load(zipfile.read(entry.name))
|
2010-09-28 22:08:11 +00:00
|
|
|
@theme_path = File.join(destination_path, entry.name.gsub('database.yml', ''))
|
2010-09-22 22:59:30 +00:00
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2010-09-23 23:00:13 +00:00
|
|
|
FileUtils.mkdir_p(File.dirname(File.join(destination_path, entry.name)))
|
|
|
|
|
|
|
|
zipfile.extract(entry, File.join(destination_path, entry.name))
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|