Class: HtmlMockup::Release::Processors::Requirejs

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Requirejs

Returns a new instance of Requirejs.



5
6
7
8
9
10
11
# File 'lib/html_mockup/release/processors/requirejs.rb', line 5

def initialize(options = {})
  @options = {
    :build_files => {"javascripts/site.build.js" => {:dir => "javascripts"}},
    :rjs => "r.js",
    :node => "node"
  }.update(options)
end

Instance Method Details

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

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :build_files (Hash)

    An a hash of files to build (as key) and the target as a hash with either => “STRING” or => “STRING” in the release to put it as value, each one will be built in a separate directory. (default is => {:dir => “javascripts”})

  • :node (String)

    The system path for node (defaults to “node” in path)

  • :rjs (String)

    The system path to the requirejs optimizer (r.js) (defaults to “../vendor/requirejs/r.js” (relative to source_path))



17
18
19
20
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/html_mockup/release/processors/requirejs.rb', line 17

def call(release, options={})
  @options.update(options)
  
  begin
    `#{@options[:node]} -v`
  rescue Errno::ENOENT
    raise RuntimeError, "Could not find node in #{@options[:node].inspect}"
  end
  
  rjs_command = rjs_check()
  
  @options[:build_files].each do |build_file, target|
    build_file = release.build_path + build_file
    
    if target.kind_of?(Hash)
      if target[:dir]
        target = target[:dir]
        target_type = :dir
      elsif target[:file]
        target = target[:file]
        target_type = :file
      end
    else
      # Old style
      target_type = :dir
    end
    
    target = release.build_path + target        
    release.log(self, "Optimizing #{build_file}")
            
    # Hack to create tempfile in build
    t = Tempfile.new("requirejs", release.build_path)
    tmp_build_dir = t.path
    t.close
    t.unlink
  
    # Run r.js optimizer
    if target_type == :dir
      output = `#{rjs_command} -o #{build_file} dir=#{tmp_build_dir}`
    else
      output = `#{rjs_command} -o #{build_file} out=#{tmp_build_dir}/out.js`
    end
    
    release.debug(self, output)
    
    # Check if r.js succeeded
    unless $?.success?
      raise RuntimeError, "Asset compilation with node failed.\nr.js output:\n #{output}"
    end
    
    if File.exist?(target)
      release.log(self, "Removing target #{target}")
      FileUtils.rm_rf(target)
    end        
    
    
    # Move the tmp_build_dir to target
    if target_type == :dir
      FileUtils.mv(tmp_build_dir, target)
    else
      FileUtils.mv("#{tmp_build_dir}/out.js", target)
      FileUtils.rm_rf(tmp_build_dir)
    end
  end
end

#rjs_check(path = @options[:rjs]) ⇒ Object

Incase both a file and bin version are availble file version is taken

Returns:

  • rjs_command to invoke r.js optimizer with



88
89
90
91
92
93
94
# File 'lib/html_mockup/release/processors/requirejs.rb', line 88

def rjs_check(path = @options[:rjs])
  rjs_command = rjs_file(path) || rjs_bin(path)
  if !(rjs_command)
    raise RuntimeError, "Could not find r.js optimizer in #{path.inspect} - try updating this by npm install -g requirejs"
  end
  rjs_command
end