| Class | Sprout::LibraryTask |
| In: |
sprout/lib/sprout/tasks/library_task.rb
|
| Parent: | Rake::Task |
A Library is simply shared code. Some libraries are distributed with only source, others are only pre-compiled binaries (SWC for ActionScript libraries), and still others are made available in both forms.
The Sprout::LibraryTask will download and copy a remote library sprout gem. The remote archive can include (or reference) either source or a pre-compiled file. For ActionScript libraries, this would be a SWC file.
This task is integrated with some of the compiler tasks in such a way that if an Sprout::MXMLCTask has any number of library tasks in it‘s prerequisites list, each of those libraries will be added to the compiler directive appropriately.
Following is a simple example of a library task. Using only this simple task definition, the Adobe corelib library sprout gem will be downloaded, installed and copied to your Sprout::ProjectModel lib_dir.
library :corelib
By adding this named task as a prerequisite to your compilation task, that SWC will also be added to the Sprout::MXMLCTask library_path parameter.
mxmlc 'bin/SomeProject.swf' => :corelib
You can also specify a particular library gem version if the library has changed since your project began.
library :asunit3 do |t|
t.version = '3.0.1'
end
This will ensure that only that particular library version is used for this project.
You may want to refer to a library using a particular task name, but have it use a different library sprout gem. This can be done using the gem_name parameter as follows:
library :asunit do |t|
t.gem_name = 'sprout-asunit3-library'
end
This may be useful because now the AsUnit sources will be installed to:
lib/asunit
instead of:
lib/asunit3
and you can now depend on this library as simply +:asunit+ in your compiler tasks.
You can easily create your own library gems using the Sprout::GemWrapTask and then refer to them by gem name.
In order to share your library tasks, you will need to do one of the following:
If your gem name begins with 'sprout-' and ends with '-library', you (and others) can refer to it by only the string in between that prefix and suffix. Otherwise, you (and others) will always have to set the gem_name parameter to the full name of your custom library.
You can search for all available libraries as follows:
gem search -r sprout-*library
Only results that begin with ‘sprout-’ are known, valid libraries.
| version | [RW] | The RubyGems gem version string for this library (e.g., +version = ‘0.0.1’+) |
Ensure that namespaced rake tasks only use the final part for name-based features
# File sprout/lib/sprout/tasks/library_task.rb, line 50 def clean_name return name.split(':').pop end
Unlike other rake tasks, Library tasks are actually resolved at ‘define’ time. This allows the tool tasks to build their own dependencies (like file deps)
(I'm sure there's a better way to do this, if you know how to fix this,
and would like to contribute to sprouts, this might be a good spot for it)
# File sprout/lib/sprout/tasks/library_task.rb, line 77 def define @file_target = sprout(gem_name, version) @library_path = File.join(@file_target.installed_path, @file_target.archive_path) define_file_task(library_path, project_path) end
The full gem name like ‘sprout-asunit3-library‘
# File sprout/lib/sprout/tasks/library_task.rb, line 44 def gem_name @gem_name ||= "sprout-#{clean_name}-library" end
The path to the library source or swc that will be copied into your project. This can actually be any full or relative path on your system, but should almost always be left alone for automatic resolution.
# File sprout/lib/sprout/tasks/library_task.rb, line 57 def library_path @library_path ||= nil end
Override the the project folder where the library will be installed.
By default, libraries are installed into Sprout::ProjectModel lib_dir.
# File sprout/lib/sprout/tasks/library_task.rb, line 64 def project_lib if(library_path.index('.swc')) @project_lib ||= ProjectModel.instance.swc_dir else @project_lib ||= ProjectModel.instance.lib_dir end end
The path within the project where this library is installed
# File sprout/lib/sprout/tasks/library_task.rb, line 88 def project_path if(File.directory?(@library_path)) # library is source dir File.join(project_lib, clean_name) else # library is a binary (like swc, jar, etc) File.join(project_lib, File.basename(@file_target.archive_path)) end end