Class: Roger::Release::Injector

Inherits:
Object
  • Object
show all
Defined in:
lib/roger/release/injector.rb

Overview

The Injector can inject variables and files into other files based on regexps.

Inject VERSION / DATE (i.e. in TOC) r.inject(=> release.version, “DATE” => release.date, :into => %w_doc/toc_doc/toc.html)

Inject CHANGELOG r.inject(=> {file: “”, filter: BlueCloth}, :into => %w_doc/changelog_doc/changelog.html)

Instance Method Summary collapse

Constructor Details

#initialize(variables, options) ⇒ Injector

Processors are based on Tilt (github.com/rtomayko/tilt). Currently supported/tested processors are:

  • ‘md’ for Markdown (bluecloth)

Injection files are relative to the :source_path

Examples:

Simple variable injection (replaces [VARIABLE] into all .css files)

{"[VARIABLE]" => "replacement"}, :into => %w{**/*.css}

Regex variable injection (replaces all matches into test.js files)

{/\/\*\s*\[BANNER\]\s*\*\// => "replacement"}, :into => %w{javacripts/test.js}

Simple variable injection with filtering (replaces [VARIABLE] with :content

run through the markdown processor into all .html files)

  {"[VARIABLE]" => {content: "# header one", processor: "md"}, :into => %w{**/*.html}

Full file injection (replaces all matches of [CHANGELOG] with the contents

of "CHANGELOG.md" into _doc/changelog.html)

  {"CHANGELOG" => {file: "CHANGELOG.md"}}, :into => %w{_doc/changelog.html}

Full file injection with filtering (replaces all matches of [CHANGELOG]

with the contents of "CHANGELOG" which ran through Markdown compresser
into _doc/changelog.html)

  {"CHANGELOG" => {file: "CHANGELOG", processor: "md"}}, :into => %w{_doc/changelog.html}

Parameters:

  • variables (Hash)

    Variables to inject. See example for more info

  • options (Hash)

    a customizable set of options

Options Hash (options):

  • :into (Array)

    An array of file globs relative to the build_path



42
43
44
45
# File 'lib/roger/release/injector.rb', line 42

def initialize(variables, options)
  @variables = variables
  @options = options
end

Instance Method Details

#call(release, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/roger/release/injector.rb', line 47

def call(release, options = {})
  @options.update(options)
  files = release.get_files(@options[:into])

  files.each do |f|
    c = File.read(f)
    injected_vars = []
    @variables.each do |variable, injection|
      if c.gsub!(variable, get_content(injection, release))
        injected_vars << variable
      end
    end
    if injected_vars.size > 0
      release.log(self, "Injected variables #{injected_vars.inspect} into #{f}")
    end
    File.open(f, "w") { |fh| fh.write c }
  end
end

#get_complex_injection(injection, release) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/roger/release/injector.rb', line 81

def get_complex_injection(injection, release)
  content = injection_content(injection, release)

  fail ArgumentError, "No :content or :file specified" unless content

  if injection[:processor]
    if tmpl = Tilt[injection[:processor]]
      (tmpl.new { content }).render
    else
      fail ArgumentError, "Unknown processor #{injection[:processor]}"
    end
  else
    content
  end
end

#get_content(injection, release) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/roger/release/injector.rb', line 66

def get_content(injection, release)
  case injection
  when String
    injection
  when Hash
    get_complex_injection(injection, release)
  else
    if injection.respond_to?(:to_s)
      injection.to_s
    else
      fail ArgumentError, "Woah, what's this? #{injection.inspect}"
    end
  end
end

#injection_content(injection, release) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/roger/release/injector.rb', line 97

def injection_content(injection, release)
  if injection[:file]
    File.read(release.source_path + injection[:file])
  else
    injection[:content]
  end
end