Class: Roger::Release::Processors::Mockup

Inherits:
Base
  • Object
show all
Defined in:
lib/roger/release/processors/mockup.rb

Overview

The Mockup processor that will process all templates

Constant Summary collapse

MIME_TYPES_TO_EXTENSION =
{
  "text/html" => "html",
  "text/css"  => "css",
  "application/javascript" => "js",
  "text/xml" => "xml",
  "application/xml" => "xml",
  "text/csv" => "csv",
  "application/json" => "json"
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#options, #release

Instance Method Summary collapse

Methods inherited from Base

#call, #name

Instance Method Details

#default_optionsObject



18
19
20
21
22
23
24
# File 'lib/roger/release/processors/mockup.rb', line 18

def default_options
  {
    env: {},
    match: ["**/*.{html,md,html.erb}"],
    skip: [/\Astylesheets/, /\Ajavascripts/]
  }
end

#performObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/roger/release/processors/mockup.rb', line 30

def perform
  @options[:env].update("roger.project" => project, "MOCKUP_PROJECT" => project)

  log_call

  release.get_files(@options[:match], @options[:skip]).each do |file_path|
    release.log(self, "    Extract: #{file_path}", true)

    # Avoid rendering partials which can also be included
    # in the roger.base_path
    next if File.basename(file_path).start_with? "_"

    run_on_file!(file_path, @options[:env])
  end
end

#projectObject



26
27
28
# File 'lib/roger/release/processors/mockup.rb', line 26

def project
  release.project
end

#run_on_file(file_path, env = {}) ⇒ Object

Runs the template on a single file and return processed source.



59
60
61
62
63
64
65
66
# File 'lib/roger/release/processors/mockup.rb', line 59

def run_on_file(file_path, env = {})
  renderer = Roger::Renderer.new(
    env.dup,
    partials_path: project.partial_path,
    layouts_path: project.layouts_path
  )
  renderer.render(file_path, project.options[:renderer] || {})
end

#run_on_file!(file_path, env = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/roger/release/processors/mockup.rb', line 46

def run_on_file!(file_path, env = {})
  output = run_on_file(file_path, env)

  # Clean up source file
  FileUtils.rm(file_path)

  # Write out new file
  File.open(target_path(file_path).to_s, "w") do |f|
    f.write(output)
  end
end

#target_path(path) ⇒ Pathname

Determines the output path for a mockup path with a certain template

Returns:

  • (Pathname)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/roger/release/processors/mockup.rb', line 71

def target_path(path)
  parts = File.basename(path.to_s).split(".")
  path = path.to_s

  # Always return .html directly as it will cause too much trouble otherwise
  return Pathname.new(path) if parts.last == "html"

  target_ext = Roger::Renderer.target_extension_for(path)
  source_ext = Roger::Renderer.source_extension_for(path)

  # If there is no target extension
  return Pathname.new(path) if target_ext.empty?

  # If we have at least one extension
  if parts.size > 1
    source_ext_regexp = /#{Regexp.escape(source_ext)}\Z/
    Pathname.new(path.gsub(source_ext_regexp, target_ext))
  else
    Pathname.new(path + "." + target_ext)
  end
end