Class: Rake::Pipeline::DSL::PipelineDSL

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

Overview

This class is used by ProjectDSL to provide a convenient DSL for configuring a pipeline.

All instance methods of PipelineDSL are available in the context the block passed to Rake::Pipeline.Rake::Pipeline.build.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline) ⇒ void

Create a new Rake::Pipeline::DSL::PipelineDSL to configure a pipeline.

Parameters:

  • pipeline (Pipeline)

    the pipeline that the PipelineDSL should configure.



53
54
55
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 53

def initialize(pipeline)
  @pipeline = pipeline
end

Instance Attribute Details

#pipelinePipeline (readonly)

Returns the pipeline the DSL should configure.

Returns:

  • (Pipeline)

    the pipeline the DSL should configure



11
12
13
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 11

def pipeline
  @pipeline
end

Class Method Details

.evaluate(pipeline, options, &block) ⇒ void

This method returns an undefined value.

Configure a pipeline with a passed in block.

Parameters:

  • pipeline (Pipeline)

    the pipeline that the PipelineDSL should configure.

  • block (Proc)

    the block describing the configuration. This block will be evaluated in the context of a new instance of Rake::Pipeline::DSL::PipelineDSL



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 21

def self.evaluate(pipeline, options, &block)
  dsl = new(pipeline)

  # If any before filters, apply them to the pipeline.
  # They will be run in reverse of insertion order.
  if before_filters = options[:before_filters]
    before_filters.each do |klass, args, block|
      dsl.filter klass, *args, &block
    end
  end

  # Evaluate the block in the context of the DSL.
  dsl.instance_eval(&block)

  # If any after filters, apply them to the pipeline.
  # They will be run in insertion order.
  if after_filters = options[:after_filters]
    after_filters.each do |klass, args, block|
      dsl.filter klass, *args, &block
    end
  end

  # the FinalizingFilter should always come after all
  # user specified after filters
  pipeline.finalize
end

Instance Method Details

#concat(*args, &block) ⇒ Object Also known as: copy

A helper method for adding a concat filter to the pipeline. If the first argument is an Array, it adds a new OrderingConcatFilter, otherwise it adds a new ConcatFilter.



218
219
220
221
222
223
224
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 218

def concat(*args, &block)
  if args.first.kind_of?(Array)
    filter(Rake::Pipeline::OrderingConcatFilter, *args, &block)
  else
    filter(Rake::Pipeline::ConcatFilter, *args, &block)
  end
end

#filter(filter_class, *ctor_args, &block) ⇒ void

This method returns an undefined value.

Add a filter to the pipeline.

In addition to a filter class, #filter takes a block that describes how the filter should map input files to output files.

By default, the block maps an input file into an output file with the same name.

Any additional arguments passed to #filter will be passed on to the filter class’s constructor.

Parameters:

  • filter_class (Class)

    the class of the filter.

  • ctor_args (Array)

    a list of arguments to pass to the filter’s constructor.

  • block (Proc)

    an output file name generator.

See Also:



99
100
101
102
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 99

def filter(filter_class, *ctor_args, &block)
  filter = filter_class.new(*ctor_args, &block)
  pipeline.add_filter(filter)
end

#gsub(*args, &block) ⇒ Object Also known as: replace

A helper method for adding a gsub filter to the pipeline.



230
231
232
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 230

def gsub(*args, &block)
  filter(Rake::Pipeline::GsubFilter, *args, &block)
end

#input(root, glob = "**/*") ⇒ void

This method returns an undefined value.

Add an input location and files to a pipeline.

Examples:

Rake::Pipeline::Project.build do
  input "app" do
    input "assets", "**/*.js"
    # ...
  end
end

Parameters:

  • root (String)

    the root path where the pipeline should find its input files.

  • glob (String) (defaults to: "**/*")

    a file pattern that represents the list of files that the pipeline should process within root. The default is */.



74
75
76
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 74

def input(root, glob="**/*")
  pipeline.add_input root, glob
end

#match(pattern, &block) ⇒ Matcher

Apply a number of filters, but only to files matching a particular pattern.

Inside the block passed to match, you may specify any number of filters that should be applied to files matching the pattern.

Examples:

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

  input "app/assets" do
    # compile coffee files into JS files
    match "*.coffee" do
      coffee_script
    end

    # because the previous step converted coffeee
    # into JS, the coffee files will be included here
    match "*.js" do
      uglify
      concat "application.js"
    end
  end
end

Parameters:

  • pattern (String)

    a glob pattern to match

  • block (Proc)

    a block that supplies filters

Returns:



134
135
136
137
138
139
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 134

def match(pattern, &block)
  matcher = pipeline.copy(Matcher, &block)
  matcher.glob = pattern
  pipeline.add_filter matcher
  matcher
end

#output(root) ⇒ void

This method returns an undefined value.

Specify the output directory for the pipeline.

Parameters:

  • root (String)

    the output directory.



206
207
208
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 206

def output(root)
  pipeline.output_root = root
end

#reject(pattern = '', &block) ⇒ RejectMatcher Also known as: exclude, skip

Reject files matching a pattern or block. You may specify a glob or a glob.

true will skip that file.

Examples:

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

  input "app/assets" do
    # reject everything maching *.min
    reject "*.min"
  end

  input "app/javascripts" do
    reject do |file|
      # process the file here
    end
  end
end

Parameters:

  • pattern (String) (defaults to: '')

    a glob pattern to match

  • a (Proc)

    block used to evaluate each file. Returning

Returns:



165
166
167
168
169
170
171
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 165

def reject(pattern = '', &block)
  matcher = pipeline.copy(RejectMatcher)
  matcher.glob = pattern
  matcher.block = block
  pipeline.add_filter matcher
  matcher
end

#sort(&block) ⇒ SortedPipeline

Apply filters in a sorted fashion. Use this when you need something other than file name ordering.

Examples:

Rake::Pipeline::Project.build do
    match "*.js" do
      # inputs will be sorted according to the block and
      # passed to concat
      sort do |f1, f2|
        # reverse the inputs
        f2.fullpath <=> f1.fullpath
      end
      concat "application.js"
    end
  end
end

Parameters:

  • block (Proc)

    used to sort inputs

Returns:



195
196
197
198
199
200
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 195

def sort(&block)
  sorter = pipeline.copy(SortedPipeline)
  sorter.comparator = block
  pipeline.add_filter sorter
  sorter
end

#strip(matcher) ⇒ Object

A helper method like gsub, but removes everything specified by the matcher. The matcher is the first argument passed to String#gsub

See Also:

  • String#gsub


240
241
242
# File 'lib/rake-pipeline/dsl/pipeline_dsl.rb', line 240

def strip(matcher)
  filter(Rake::Pipeline::GsubFilter, matcher, '')
end