2010-09-22 22:59:30 +00:00
|
|
|
require 'zip/zipfilesystem'
|
|
|
|
|
|
|
|
module Locomotive
|
|
|
|
module Import
|
|
|
|
class Job
|
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
def initialize(identifier, site = nil, enabled = {})
|
|
|
|
raise "Theme identifier not found" if identifier.blank?
|
2010-09-22 22:59:30 +00:00
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
@identifier = identifier
|
2010-09-22 22:59:30 +00:00
|
|
|
@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-10-19 10:20:09 +00:00
|
|
|
self.log "theme identifier #{@identifier} / enabled steps = #{@enabled.inspect}"
|
2010-09-28 22:08:11 +00:00
|
|
|
|
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-19 10:20:09 +00:00
|
|
|
self.log "performing '#{step}' step"
|
2010-09-28 22:08:11 +00:00
|
|
|
"Locomotive::Import::#{step.camelize}".constantize.process(context)
|
2010-10-12 00:12:18 +00:00
|
|
|
@worker.update_attributes :step => step if @worker
|
2010-09-28 22:08:11 +00:00
|
|
|
else
|
2010-10-19 10:20:09 +00:00
|
|
|
self.log "skipping #{step}"
|
2010-09-28 08:09:49 +00:00
|
|
|
end
|
|
|
|
end
|
2010-09-22 22:59:30 +00:00
|
|
|
end
|
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
def success(worker)
|
|
|
|
self.log 'deleting original zip file'
|
|
|
|
|
|
|
|
uploader = ThemeUploader.new(@site)
|
|
|
|
|
|
|
|
uploader.retrieve_from_store!(@identifier)
|
|
|
|
|
|
|
|
uploader.remove!
|
|
|
|
|
|
|
|
self.log 'deleting working folder'
|
|
|
|
|
|
|
|
FileUtils.rm_rf(themes_folder) rescue nil
|
|
|
|
end
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
protected
|
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
def log(message)
|
|
|
|
puts "\t[import_theme] #{message}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def themes_folder
|
|
|
|
File.join(Rails.root, 'tmp', 'themes', @site.id.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepare_folder
|
|
|
|
FileUtils.rm_rf self.themes_folder if File.exists?(self.themes_folder)
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(self.themes_folder)
|
|
|
|
end
|
|
|
|
|
|
|
|
def retrieve_zip_file
|
|
|
|
uploader = ThemeUploader.new(@site)
|
|
|
|
|
|
|
|
uploader.retrieve_from_store!(@identifier)
|
|
|
|
|
|
|
|
if uploader.file.respond_to?(:url)
|
|
|
|
self.log 'file from remote storage'
|
|
|
|
|
|
|
|
@theme_file = File.join(self.themes_folder, @identifier)
|
|
|
|
|
|
|
|
File.open(@theme_file, 'w') { |f| f.write(uploader.file.read) }
|
|
|
|
else # local filesystem
|
|
|
|
self.log 'file from local storage'
|
|
|
|
|
|
|
|
@theme_file = uploader.path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-22 22:59:30 +00:00
|
|
|
def unzip!
|
2010-10-19 10:20:09 +00:00
|
|
|
self.prepare_folder
|
2010-09-28 08:09:49 +00:00
|
|
|
|
2010-10-19 10:20:09 +00:00
|
|
|
self.retrieve_zip_file
|
|
|
|
|
|
|
|
self.log "unzip #{@theme_file}"
|
|
|
|
|
|
|
|
Zip::ZipFile.open(@theme_file) do |zipfile|
|
|
|
|
destination_path = self.themes_folder
|
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
|