Class: Sprout::Library

Inherits:
Object
  • Object
show all
Includes:
RubyFeature
Defined in:
lib/sprout/library.rb

Overview

Sprout Libraries provide support for sharing and versioning raw or pre-compiled source code across projects.

Sprout Libraries give us the ability to include raw (or pre-compiled) source code directly within a Ruby Gem, or to refer to an archive somewhere on the internet.

A Sprout Library is made up of two components:

  • Specification: The description of the library

  • Source Code: Raw or pre-compiled (swc, jar, abc)

Specification

Libraries can be added to local or remote file targets in a Sprout::Specification. When calling add_library, one must provide the library name (symbol that will be used from Rake) and a relative path (or Array of paths) from the Specification to the library file or directory.

Following is an example of a Sprout::Specification that registers a SWC that will be distributed directly within a Ruby Gem:

Sprout::Specification.new do |s|
  s.name = "asunit4"
  s.version = "4.4.2"
  s.add_file_target do |f|
    f.add_library :swc, File.join('bin', "AsUnit-4.4.2.swc")
  end
end

Following is an example of a Sprout::Specification that registers a ZIP archive that will be distributed separately from the Ruby Gem:

Sprout::Specification.new do |s|
  s.name = "asunit3"
  s.version = "3.0.0"
  s.add_remote_file_target do |f|
    f.url = "https://github.com/patternpark/asunit/tree/3.0.0"
    f.md5 = "abcdefghijklmnopqrstuvwxyz"
    f.add_library :swc, "bin/AsUnit-3.0.1.zip"
  end
end

Libraries can be consumed from any Rakefile without concern for how the source code is distributed. Following is an example Rake task that uses the AsUnit 4.0 Library:

# Define the library Rake::Task:
library :asunit4

# Add the library as a dependency from another task
# that should know how to properly associate the files:
mxmlc 'bin/SomeProject.swf' => :asunit4 do |t|
  t.input = 'src/SomeProject.as'
end

When the library task is executed, the library should be resolved and expanded into the project. When the mxmlc task is executed, the installed library should be associated with the compilation command.


Previous Topic: Generator

Next Topic: Executable


See Also:

Constant Summary

TASK_NAME =
:resolve_sprout_libraries

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Concern

#append_features, extended, #included

Constructor Details

- (Library) initialize(params = nil)

A new instance of Library



135
136
137
138
# File 'lib/sprout/library.rb', line 135

def initialize params=nil
  params.each {|key,value| self.send("#{key}=", value)} unless params.nil?
  super()
end

Instance Attribute Details

- (Object) file_target

Returns the value of attribute file_target



83
84
85
# File 'lib/sprout/library.rb', line 83

def file_target
  @file_target
end

- (Object) installed_project_path

Returns the value of attribute installed_project_path



84
85
86
# File 'lib/sprout/library.rb', line 84

def installed_project_path
  @installed_project_path
end

- (Object) name

Returns the value of attribute name



85
86
87
# File 'lib/sprout/library.rb', line 85

def name
  @name
end

- (Object) path

Returns the value of attribute path



86
87
88
# File 'lib/sprout/library.rb', line 86

def path
  @path
end

- (Object) pkg_name

Returns the value of attribute pkg_name



87
88
89
# File 'lib/sprout/library.rb', line 87

def pkg_name
  @pkg_name
end

- (Object) pkg_version

Returns the value of attribute pkg_version



88
89
90
# File 'lib/sprout/library.rb', line 88

def pkg_version
  @pkg_version
end

- (Object) platform

Returns the value of attribute platform



89
90
91
# File 'lib/sprout/library.rb', line 89

def platform
  @platform
end

Class Method Details

+ (Object) define_task(name = nil, pkg_name = nil, pkg_version = nil)

Create Rake tasks that will load and install a particular library into the current project.

This method is usually accessed from the global library helper.

library :asunit4


129
130
131
132
# File 'lib/sprout/library.rb', line 129

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

+ (Object) project_path

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}"


116
117
118
# File 'lib/sprout/library.rb', line 116

def project_path
  @project_path ||= 'lib'
end

+ (Object) project_path=(path)

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'


101
102
103
# File 'lib/sprout/library.rb', line 101

def project_path=(path)
  @project_path = path
end

Instance Method Details

- (Object) create_installation_tasks

Returns the outer Rake::Task which is invokable.



142
143
144
145
146
# File 'lib/sprout/library.rb', line 142

def create_installation_tasks
  define_lib_dir_task_if_necessary project_path
  create_project_tasks
  create_outer_task
end

- (Object) create_outer_task



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/sprout/library.rb', line 148

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