Class: HtmlMockup::Release

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

Defined Under Namespace

Modules: Finalizers, Processors, Scm Classes: Cleaner, Injector

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, config = {}) ⇒ Release

Returns a new instance of Release.

Parameters:

  • config (Hash) (defaults to: {})

    a customizable set of options

Options Hash (config):

  • :scm (Symbol)

    The SCM to use (default = :git)

  • :target_path (String, Pathname)

    The path/directory to put the release into

  • :build_path (String, Pathname)

    Temporary path used to build the release

  • :cleanup_build (Boolean)

    Wether or not to remove the build_path after we’re done (default = true)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/html_mockup/release.rb', line 60

def initialize(project, config = {})
  defaults = {
    :scm => :git,
    :source_path  => Pathname.new(Dir.pwd) + "html",
    :target_path => Pathname.new(Dir.pwd) + "releases",
    :build_path => Pathname.new(Dir.pwd) + "build",
    :cleanup_build => true
  }
  
  @config = {}.update(defaults).update(config)
  @project = project
  @stack = []
  @finalizers = []
end

Instance Attribute Details

#cleanupsObject (readonly)

Returns the value of attribute cleanups.



8
9
10
# File 'lib/html_mockup/release.rb', line 8

def cleanups
  @cleanups
end

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/html_mockup/release.rb', line 6

def config
  @config
end

#finalizersObject (readonly)

Returns the value of attribute finalizers.



8
9
10
# File 'lib/html_mockup/release.rb', line 8

def finalizers
  @finalizers
end

#injectionsObject (readonly)

Returns the value of attribute injections.



8
9
10
# File 'lib/html_mockup/release.rb', line 8

def injections
  @injections
end

#projectObject (readonly)

Returns the value of attribute project.



6
7
8
# File 'lib/html_mockup/release.rb', line 6

def project
  @project
end

#stackObject (readonly)

Returns the value of attribute stack.



8
9
10
# File 'lib/html_mockup/release.rb', line 8

def stack
  @stack
end

Class Method Details

.camel_case(string) ⇒ Object



49
50
51
52
# File 'lib/html_mockup/release.rb', line 49

def camel_case(string)
  return string if string !~ /_/ && string =~ /[A-Z]+.*/
  string.split('_').map{|e| e.capitalize}.join
end

.default_finalizersObject



16
17
18
# File 'lib/html_mockup/release.rb', line 16

def default_finalizers
  [[self.get_callable(:dir, HtmlMockup::Release::Finalizers), {}]]
end

.default_stackObject



12
13
14
# File 'lib/html_mockup/release.rb', line 12

def default_stack
  []
end

.get_callable(callable, scope) ⇒ Object

Makes callable into a object that responds to call.

Parameters:

  • callable (#call, Symbol, Class)

    If callable already responds to #call will just return callable, a Symbol will be searched for in the scope parameter, a class will be instantiated (and checked if it will respond to #call)

  • scope (Module)

    The scope in which to search callable in if it’s a Symbol



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/html_mockup/release.rb', line 24

def get_callable(callable, scope)
  return callable if callable.respond_to?(:call)

  if callable.kind_of?(Symbol)
    callable = camel_case(callable.to_s).to_sym
    if scope.constants.include?(callable)
      c = scope.const_get(callable)
      callable = c if c.is_a?(Class)
    end
  end

  if callable.kind_of?(Class)
    callable = callable.new
  end

  if callable.respond_to?(:call)
    callable
  else
    raise ArgumentError, "Could not resolve #{callable.inspect}. Callable must be an object that responds to #call or a symbol that resolve to such an object or a class with a #call instance method."
  end

end

Instance Method Details

Generates a banner if a block is given, or returns the currently set banner. It automatically takes care of adding comment marks around the banner.

The default banner looks like this:

Version : v1.0.0 =

Date : 2012-06-20 =

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :comment (:css, :js, :html, false)

    Wether or not to comment the output and in what style. (default=js)



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/html_mockup/release.rb', line 163

def banner(options = {}, &block)
  options = {
    :comment => :js
  }.update(options)
  
  if block_given?
    @_banner = yield.to_s
  elsif !@_banner
    banner = []
    banner << "Version : #{self.scm.version}"
    banner << "Date  : #{self.scm.date.strftime("%Y-%m-%d")}"

    size = banner.inject(0){|mem,b| b.size > mem ? b.size : mem }
    banner.map!{|b| "= #{b.ljust(size)} =" }
    div = "=" * banner.first.size
    banner.unshift(div)
    banner << div
    @_banner = banner.join("\n")
  end
  
  if options[:comment]
    self.comment(@_banner, :style => options[:comment])
  else
    @_banner
  end
end

#build_pathObject

Accessor for build_path The build_path is a temporary directory where the release will be built

Returns:

  • Pathname the build_path



87
88
89
# File 'lib/html_mockup/release.rb', line 87

def build_path
  Pathname.new(self.config[:build_path])      
end

#cleanup(pattern) ⇒ Object

Files to clean up in the build directory just before finalization happens

Parameters:

  • Pattern (String)

    to glob within build directory



147
148
149
# File 'lib/html_mockup/release.rb', line 147

def cleanup(pattern)
  @stack << Cleaner.new(pattern)
end

#debug(part, msg, &block) ⇒ Object



237
238
239
# File 'lib/html_mockup/release.rb', line 237

def debug(part, msg, &block)
  self.log(part, msg, true, &block)
end

#extract(options = {}) ⇒ Object

Deprecated.

Don’t use the extractor anymore, use release.use(:mockup, options) processor

Extract the mockup, this will happen anyway, and will always happen first This method gives you a way to pass options to the extractor.

Parameters:

  • Hash

    options Options hash passed to extractor



196
197
198
199
# File 'lib/html_mockup/release.rb', line 196

def extract(options = {})
  self.warn(self, "Don't use the extractor anymore, use release.use(:mockup, options) and release.use(:url_relativizer, options) processors")
  @extractor_options = options
end

#finalize(finalizer, options = {}) ⇒ Object

Write out the whole release into a directory, zip file or anything you can imagine #finalize can be called multiple times, it just will run all of them.

The default finalizer is :dir

Parameters:

  • Finalizer (Symbol, Proc)

    to use



137
138
139
# File 'lib/html_mockup/release.rb', line 137

def finalize(finalizer, options = {})
  @finalizers << [self.class.get_callable(finalizer, HtmlMockup::Release::Finalizers), options]
end

#get_files(globs, excludes = []) ⇒ Object

Parameters:

  • globs (Array)

    an array of file path globs that will be globbed against the build_path

  • excludes (Array) (defaults to: [])

    an array of regexps that will be excluded from the result



249
250
251
252
253
254
255
256
# File 'lib/html_mockup/release.rb', line 249

def get_files(globs, excludes = [])
  files = globs.map{|g| Dir.glob(self.build_path + g) }.flatten
  if excludes.any?
    files.reject{|c| excludes.detect{|e| e.match(c) } }
  else
    files
  end
end

#inject(variables, options) ⇒ Object

Inject variables into files with an optional filter



116
117
118
# File 'lib/html_mockup/release.rb', line 116

def inject(variables, options)
  @stack << Injector.new(variables, options)
end

#log(part, msg, verbose = false, &block) ⇒ Object

Write out a log message



223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/html_mockup/release.rb', line 223

def log(part, msg, verbose = false, &block)
  if !verbose || verbose && self.project.options[:verbose]
    self.project.shell.say "\033[37m#{part.class.to_s}\033[0m" + " : " + msg.to_s, nil, true
  end
  if block_given?
    begin
      self.project.shell.padding = self.project.shell.padding + 1
      yield
    ensure
      self.project.shell.padding = self.project.shell.padding - 1
    end
  end
end

#run!Object

Actually perform the release



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/html_mockup/release.rb', line 202

def run!
  # Validate paths
  validate_paths!
  
  # Extract mockup
  copy_source_path_to_build_path!
  
  validate_stack!
  
  # Run stack
  run_stack!
  
  # Run finalizers
  run_finalizers!
  
  # Cleanup
  cleanup! if self.config[:cleanup_build]
  
end

#scm(force = false) ⇒ Object

Get the current SCM object



100
101
102
103
104
105
106
107
108
109
# File 'lib/html_mockup/release.rb', line 100

def scm(force = false)
  return @_scm if @_scm && !force
  
  case self.config[:scm]
  when :git
    @_scm = Release::Scm::Git.new(:path => self.source_path)
  else
    raise "Unknown SCM #{options[:scm].inspect}"
  end
end

#source_pathObject

Accessor for source_path The source path is the root of the mockup

Returns:

  • Pathanem the source_path



95
96
97
# File 'lib/html_mockup/release.rb', line 95

def source_path
  Pathname.new(self.config[:source_path])
end

#target_pathObject

Accessor for target_path The target_path is the path where the finalizers will put the release

Returns:

  • Pathname the target_path



79
80
81
# File 'lib/html_mockup/release.rb', line 79

def target_path
  Pathname.new(self.config[:target_path])
end

#use(processor, options = {}) ⇒ Object

Use a certain pre-processor



124
125
126
# File 'lib/html_mockup/release.rb', line 124

def use(processor, options = {})
  @stack << [self.class.get_callable(processor, HtmlMockup::Release::Processors), options]
end

#warn(part, msg) ⇒ Object

Write out a warning message



242
243
244
# File 'lib/html_mockup/release.rb', line 242

def warn(part, msg)
  self.project.shell.say "\033[37m#{part.class.to_s}\033[0m" + " : " + "\033[31m#{msg.to_s}\033[0m", nil, true
end