Class: Rake::Pipeline::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/rake-pipeline/project.rb

Overview

A Project controls the lifecycle of a series of Pipelines, creating them from an Assetfile and recreating them if the Assetfile changes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assetfile_or_pipeline = nil) ⇒ Project

Returns a new instance of Project

Parameters:

  • assetfile_or_pipeline (String|Pipeline) (defaults to: nil)

    if this a String, create a Pipeline from the Assetfile at that path. If it's a Pipeline, just wrap that pipeline.


89
90
91
92
93
94
95
96
97
# File 'lib/rake-pipeline/project.rb', line 89

def initialize(assetfile_or_pipeline=nil)
  reset!
  if assetfile_or_pipeline.kind_of?(String)
    @assetfile_path = File.expand_path(assetfile_or_pipeline)
    rebuild_from_assetfile(@assetfile_path)
  elsif assetfile_or_pipeline
    @pipelines << assetfile_or_pipeline
  end
end

Instance Attribute Details

#after_filters=(value) ⇒ Array (writeonly)

Returns a list of filters to be applied after the specified filters in every pipeline

Returns:

  • (Array)

    a list of filters to be applied after the specified filters in every pipeline


36
37
38
# File 'lib/rake-pipeline/project.rb', line 36

def after_filters=(value)
  @after_filters = value
end

#assetfile_digestString|nil (readonly)

Returns the digest of the Assetfile the project was created with, or nil if the project was created without an Assetfile.

Returns:

  • (String|nil)

    the digest of the Assetfile the project was created with, or nil if the project was created without an Assetfile.


21
22
23
# File 'lib/rake-pipeline/project.rb', line 21

def assetfile_digest
  @assetfile_digest
end

#assetfile_pathString|nil (readonly)

Returns the path to the project's Assetfile or nil if it was created without an Assetfile.

Returns:

  • (String|nil)

    the path to the project's Assetfile or nil if it was created without an Assetfile.


16
17
18
# File 'lib/rake-pipeline/project.rb', line 16

def assetfile_path
  @assetfile_path
end

#before_filters=(value) ⇒ Array (writeonly)

Returns a list of filters to be applied before the specified filters in every pipeline

Returns:

  • (Array)

    a list of filters to be applied before the specified filters in every pipeline


32
33
34
# File 'lib/rake-pipeline/project.rb', line 32

def before_filters=(value)
  @before_filters = value
end

#default_output_rootString

Returns the directory path where pipelines will write their outputs by default

Returns:

  • (String)

    the directory path where pipelines will write their outputs by default


28
29
30
# File 'lib/rake-pipeline/project.rb', line 28

def default_output_root
  @default_output_root
end

#mapsObject (readonly)

Returns the value of attribute maps


12
13
14
# File 'lib/rake-pipeline/project.rb', line 12

def maps
  @maps
end

#pipelinesPipeline (readonly)

Returns the list of pipelines in the project

Returns:

  • (Pipeline)

    the list of pipelines in the project


10
11
12
# File 'lib/rake-pipeline/project.rb', line 10

def pipelines
  @pipelines
end

#tmpdirString

Returns the directory path for temporary files

Returns:

  • (String)

    the directory path for temporary files


24
25
26
# File 'lib/rake-pipeline/project.rb', line 24

def tmpdir
  @tmpdir
end

Class Method Details

.add_to_digest(str) ⇒ Object

Add a value to the list of strings to append to the digest temp directory. Libraries can use this to add (for example) their version numbers so that the pipeline will be rebuilt if the library version changes.

Examples:

Rake::Pipeline::Project.add_to_digest(Rake::Pipeline::Web::Filters::VERSION)

Parameters:


80
81
82
83
# File 'lib/rake-pipeline/project.rb', line 80

def add_to_digest(str)
  self.digest_additions << str.to_s
  self.digest_additions.sort!
end

.build(&block) ⇒ Rake::Pipeline::Project

Configure a new project by evaluating a block with the Rake::Pipeline::DSL::ProjectDSL class.

Examples:

Rake::Pipeline::Project.build do
  tmpdir "tmp"
  output "public"

  input "app/assets" do
    concat "app.js"
  end
end

Returns:

See Also:


55
56
57
58
# File 'lib/rake-pipeline/project.rb', line 55

def build(&block)
  project = new
  project.build(&block)
end

.digest_additionsArray[String]

Returns an array of strings that will be appended to #digested_tmpdir.

Returns:

  • (Array[String])

    an array of strings that will be appended to #digested_tmpdir.


62
63
64
# File 'lib/rake-pipeline/project.rb', line 62

def digest_additions
  @digest_additions ||= []
end

.digest_additions=(additions) ⇒ Object

Set digest_additions to a sorted copy of the given array.


67
68
69
# File 'lib/rake-pipeline/project.rb', line 67

def digest_additions=(additions)
  @digest_additions = additions.sort
end

Instance Method Details

#build(&block) ⇒ Object

Evaluate a block using the Rake::Pipeline::DSL::ProjectDSL DSL against an existing project.


101
102
103
104
# File 'lib/rake-pipeline/project.rb', line 101

def build(&block)
  DSL::ProjectDSL.evaluate(self, &block) if block
  self
end

#build_pipeline(input, glob = nil, &block) ⇒ Object

Build a new pipeline and add it to our list of pipelines.


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/rake-pipeline/project.rb', line 199

def build_pipeline(input, glob=nil, &block)
  pipeline = Rake::Pipeline.build({
    :before_filters => @before_filters,
    :after_filters => @after_filters,
    :output_root => default_output_root,
    :tmpdir => digested_tmpdir,
    :project => self
  }, &block)

  if input.kind_of?(Array)
    input.each { |x| pipeline.add_input(x) }
  elsif input.kind_of?(Hash)
    pipeline.inputs = input
  else
    pipeline.add_input(input, glob)
  end

  @pipelines << pipeline
  pipeline
end

#cleanObject

Remove the project's temporary and output files.


134
135
136
137
# File 'lib/rake-pipeline/project.rb', line 134

def clean
  last_manifest.clear
  files_to_clean.each { |file| FileUtils.rm_rf(file) }
end

#cleanup_tmpdirvoid

This method returns an undefined value.

Clean out old tmp directories from the pipeline's Rake::Pipeline#tmpdir.


143
144
145
# File 'lib/rake-pipeline/project.rb', line 143

def cleanup_tmpdir
  obsolete_tmpdirs.each { |dir| FileUtils.rm_rf(dir) }
end

#digested_tmpdirString

Returns A subdirectory of #tmpdir with the digest of the Assetfile's contents and any digest_additions in its name.

Returns:

  • (String)

    A subdirectory of #tmpdir with the digest of the Assetfile's contents and any digest_additions in its name.


164
165
166
167
168
169
170
# File 'lib/rake-pipeline/project.rb', line 164

def digested_tmpdir
  suffix = assetfile_digest
  unless self.class.digest_additions.empty?
    suffix += "-#{self.class.digest_additions.join('-')}"
  end
  File.join(tmpdir, "rake-pipeline-#{suffix}")
end

#files_to_cleanObject


186
187
188
189
# File 'lib/rake-pipeline/project.rb', line 186

def files_to_clean
  setup_pipelines
  obsolete_tmpdirs + [digested_tmpdir] + output_files.map(&:fullpath)
end

#invokevoid

This method returns an undefined value.

Invoke all of the project's pipelines, detecting any changes to the Assetfile and rebuilding the pipelines if necessary.


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rake-pipeline/project.rb', line 111

def invoke
  @invoke_mutex.synchronize do
    last_manifest.read_manifest

    if dirty?
      rebuild_from_assetfile(assetfile_path) if assetfile_dirty?

      # The temporary files have to be cleaned otherwise
      # there will be a "ghost" input. Here's an example 
      # rake task: application.js => [a.js, b.js]. Deleting a.js
      # will make application.js => [b.js]. The task correctly checks
      # if B has changed (which it hasn't) and says that application.js
      # is correct. Cleaning tmp files ensures that this doesn't happen.
      clean if files_deleted?

      pipelines.each(&:invoke)

      manifest.write_manifest
    end
  end
end

#last_manifestManifest

Returns the manifest to write dependency information to

Returns:

  • (Manifest)

    the manifest to write dependency information to


228
229
230
# File 'lib/rake-pipeline/project.rb', line 228

def last_manifest
  @last_manifest ||= Rake::Pipeline::Manifest.new(manifest_path)
end

#manifestManifest

Returns the manifest to write dependency information to

Returns:

  • (Manifest)

    the manifest to write dependency information to


222
223
224
# File 'lib/rake-pipeline/project.rb', line 222

def manifest
  @manifest ||= Rake::Pipeline::Manifest.new(manifest_path)
end

#manifest_pathString

Returns the path to the dynamic dependency manifest

Returns:

  • (String)

    the path to the dynamic dependency manifest


233
234
235
# File 'lib/rake-pipeline/project.rb', line 233

def manifest_path
  File.join(digested_tmpdir, "manifest.json")
end

#obsolete_tmpdirsObject


174
175
176
177
178
179
180
181
182
# File 'lib/rake-pipeline/project.rb', line 174

def obsolete_tmpdirs
  if File.directory?(tmpdir)
    Dir["#{tmpdir}/rake-pipeline-*"].sort.reject do |dir|
      dir == digested_tmpdir
    end
  else
    []
  end
end

#output_filesArray[FileWrapper]

Returns a list of the files that will be generated when this project is invoked.

Returns:

  • (Array[FileWrapper])

    a list of the files that will be generated when this project is invoked.


193
194
195
196
# File 'lib/rake-pipeline/project.rb', line 193

def output_files
  setup_pipelines
  pipelines.map(&:output_files).flatten
end