lib/radiant/extension/script.rb in radiant-0.8.2 vs lib/radiant/extension/script.rb in radiant-0.9.0.rc2

- old
+ new

@@ -1,8 +1,9 @@ require 'active_resource' require 'tmpdir' require 'fileutils' +require 'rake' module Registry class Extension < ActiveResource::Base self.site = ENV['REGISTRY_URL'] || "http://ext.radiantcms.org/" @@ -17,22 +18,43 @@ def inspect %{ Name: #{name} Description: #{description} -Author: #{author.first_name} #{author.last_name} <#{author.email}> +Author: #{author.name} <#{author.email}> Source code: #{repository_url} Download: #{download_url} Install type: #{install_type} }.strip end end class Action def rake(command) - `rake #{command} RAILS_ENV=#{RAILS_ENV}` + `rake #{command} RAILS_ENV=#{RAILS_ENV}` if tasks_include? command end + + def tasks_include?(command) + extension = command.split('radiant:extensions:') + if extension.length > 1 + extension = extension.reject{|e| e.blank? }[0] + else + extension = extension.to_s + end + rake_file = File.join(RAILS_ROOT, 'vendor', 'extensions', extension) + '/lib/tasks/' + extension + '_extension_tasks.rake' + if File.exist? rake_file + load rake_file + end + tasks = Rake.application.tasks.map(&:name) + tasks.include? "#{command}" + end + + def file_utils + FileUtils + end + + delegate :cd, :cp_r, :rm_r, :to => :file_utils end class Installer < Action attr_accessor :url, :path, :name def initialize(url, name) @@ -44,12 +66,12 @@ migrate update end def copy_to_vendor_extensions - FileUtils.cp_r(self.path, File.expand_path(File.join(RAILS_ROOT, 'vendor', 'extensions', name))) - FileUtils.rm_r(self.path) + cp_r(self.path, File.expand_path(File.join(RAILS_ROOT, 'vendor', 'extensions', name))) + rm_r(self.path) end def migrate rake "radiant:extensions:#{name}:migrate" end @@ -73,11 +95,11 @@ def migrate_down rake "radiant:extensions:#{name}:migrate VERSION=0" end def remove_extension_directory - FileUtils.rm_r(File.join(RAILS_ROOT, 'vendor', 'extensions', name)) + rm_r(File.join(RAILS_ROOT, 'vendor', 'extensions', name)) end end class Checkout < Installer def initialize(extension) @@ -93,11 +115,11 @@ super end def checkout self.path = File.join(Dir.tmpdir, name) - system "cd #{Dir.tmpdir}; #{checkout_command}" + cd(Dir.tmpdir) { system "#{checkout_command}" } end end class Download < Installer def initialize(extension) @@ -134,14 +156,18 @@ end def checkout if project_in_git? system "git submodule add #{url} vendor/extensions/#{name}" - system "cd vendor/extensions/#{name}; git submodule init && git submodule update" + cd(File.join('vendor', 'extensions', name)) do + system "git submodule init && git submodule update" + end else super - system "cd #{path}; git submodule init && git submodule update" + cd(path) do + system "git submodule init && git submodule update" + end end end def copy_to_vendor_extensions super unless project_in_git? @@ -164,33 +190,37 @@ `gem install #{filename}` end end def unpack - output = `cd #{Dir.tmpdir}; gem unpack #{filename.split('-').first}` + output = nil + cd(Dir.tmpdir) do + output = `gem unpack #{filename.split('-').first}` + end self.path = output.match(/'(.*)'/)[1] end end class Tarball < Download def filename "#{self.name}.tar" end def unpack - output = `cd #{Dir.tmpdir}; tar xvf #{filename}` + output = nil + cd(Dir.tmpdir) { output = `tar xvf #{filename}` } self.path = File.join(Dir.tmpdir, output.split(/\n/).first.split('/').first) end end class Gzip < Tarball def filename @unpacked ? super : "#{self.name}.tar.gz" end def unpack - system "cd #{Dir.tmpdir}; gunzip #{self.filename}" + cd(Dir.tmpdir) { system "gunzip #{self.filename}" } @unpacked = true super end end @@ -198,18 +228,19 @@ def filename @unpacked ? super : "#{self.name}.tar.bz2" end def unpack - system "cd #{Dir.tmpdir}; bunzip2 #{self.filename}" + cd(Dir.tmpdir) { system "bunzip2 #{self.filename}" } @unpacked = true super end end class Zip < Download def unpack - output = `cd #{Dir.tmpdir}; unzip #{filename} -d #{name}` + output = nil + cd(Dir.tmpdir) { output = `unzip #{filename} -d #{name}` } self.path = File.join(Dir.tmpdir, name) end end end