lib/sprout/library.rb in sprout-1.0.26.pre vs lib/sprout/library.rb in sprout-1.0.29.pre

- old
+ new

@@ -1,102 +1,154 @@ module Sprout - module Library + class Library + include RubyFeature + TASK_NAME = :resolve_sprout_libraries - include RubyFeature + attr_accessor :file_target + attr_accessor :installed_project_path + attr_accessor :name + attr_accessor :path + attr_accessor :pkg_name + attr_accessor :pkg_version class << self - ## # Set the path within a project # where libraries should be loaded. + # + # From top of your Rakefile, you can + # set this value with: + # + # Sprout::Library.project_path = 'libs' + # def project_path=(path) @project_path = path end ## # The path within a project where # libraries should be added. # # Defaults to 'lib' + # + # From anywhere in your Rakefile, you can output + # this value with: + # + # puts ">> Library Project Path: #{Sprout::Library.project_path}" + # def project_path @project_path ||= 'lib' end ## # Create Rake tasks that will load and install - # a particular library. - def define_task pkg_name, type=nil, version=nil - library = Sprout::Library.load type, pkg_name.to_s, version - library.installation_task = task pkg_name - library.installation_task.sprout_type = :library - - define_lib_dir_task_if_necessary - path_or_paths = library.path - if path_or_paths.is_a?(Array) - # TODO: Need to add support for merging these directories - # rather than simply clobbering... - path_or_paths.collect { |p| define_path_task pkg_name, library, p } - else - define_path_task pkg_name, library, path_or_paths - end - library + # a particular library into the current project. + # + # This method is usually accessed from the global + # library helper. + # + # library :asunit4 + # + def define_task name=nil, pkg_name=nil, pkg_version=nil + library = Sprout::Library.load name, pkg_name, pkg_version + library.create_installation_tasks end + end - protected + def initialize params=nil + params.each {|key,value| self.send("#{key}=", value)} unless params.nil? + super() + end - def define_lib_dir_task_if_necessary - if !File.exists?(project_path) - define_file_task project_path do - FileUtils.mkdir_p project_path - Sprout::Log.puts ">> Created directory at: #{project_path}" - end + ## + # Returns the outer Rake::Task which is invokable. + def create_installation_tasks + define_lib_dir_task_if_necessary project_path + create_project_tasks + create_outer_task + end + + def create_outer_task + t = task pkg_name + # This helps executable rake tasks decide if they + # want to do something special for library tasks. + t.sprout_entity = self + class << t + def sprout_library? + !sprout_entity.nil? && + sprout_entity.is_a?(Sprout::Library) end end + t + end - def define_path_task pkg_name, library, path - installation_path = "#{project_path}/#{pkg_name}" - if File.directory?(path) - define_directory_copy_task path, installation_path - else - define_file_copy_task path, installation_path - end + private + + def project_path + Sprout::Library.project_path + end + + def create_project_tasks + if path.is_a?(Array) + # TODO: Need to add support for merging these directories + # rather than simply clobbering... + @instaled_project_path = path.collect { |single_path| define_project_path_task pkg_name, single_path } + else + @installed_project_path = define_project_path_task pkg_name, path end + end - def define_directory_copy_task path, installation_path - define_file_task installation_path do - FileUtils.cp_r path, installation_path - Sprout::Log.puts ">> Copied files from: (#{path}) to: (#{installation_path})" + def define_lib_dir_task_if_necessary dir + if !File.exists?(dir) + define_file_task dir do + FileUtils.mkdir_p dir + Sprout::Log.puts ">> Created directory at: #{dir}" end - installation_path end + end - def define_file_copy_task path, installation_path - task_name = "#{installation_path}/#{File.basename(path)}" - define_file_task task_name do - FileUtils.mkdir_p installation_path - FileUtils.cp path, "#{installation_path}/" - Sprout::Log.puts ">> Copied file from: (#{path}) to: (#{task_name})" - end - task_name + def define_project_path_task pkg_name, path + install_path = "#{project_path}/#{pkg_name}" + if File.directory?(path) + define_directory_copy_task path, install_path + else + define_file_copy_task path, install_path end + end - def define_file_task name - file name do |t| - yield if block_given? - end - task Sprout::Library::TASK_NAME => name + def define_directory_copy_task path, install_path + define_file_task install_path do + FileUtils.cp_r path, install_path + Sprout::Log.puts ">> Copied files from: (#{path}) to: (#{install_path})" end + install_path + end + def define_file_copy_task path, install_path + task_name = "#{install_path}/#{File.basename(path)}" + define_file_task task_name do + FileUtils.mkdir_p install_path + FileUtils.cp path, "#{install_path}/" + Sprout::Log.puts ">> Copied file from: (#{path}) to: (#{task_name})" + end + task_name end + def define_file_task name + file name do |t| + yield if block_given? + end + task Sprout::Library::TASK_NAME => name + end end + end # From within a Rakefile, you can load libraries -# by calling this shortcut: +# by calling this method: # # library :asunit4 # # Or, if you would like to specify which registered # library to pull from the identified package (by name): @@ -105,9 +157,22 @@ # # Or, if you'd like to specify version requirements: # # library :asunit4, :swc, '>= 4.2.pre' # -def library pkg_name, type=nil, version=nil - Sprout::Library.define_task pkg_name, type, version +# It's important to note that libraries must also +# be defined in your Gemfile like: +# +# gem "asunit4", ">= 4.2.pre" +# +# Libraries are generally then added to compiler tasks +# as Rake dependencies like: +# +# mxmlc 'bin/SomeRunner.swf' => [:asunit4] do |t| +# t.input = 'src/SomeRunner.as' +# t.source_path << 'test' +# end +# +def library pkg_name, name=nil, version=nil + Sprout::Library.define_task name, pkg_name, version end