Class: JadePug::SystemCompiler

Inherits:
Compiler
  • Object
show all
Defined in:
lib/jade-pug/system-compiler.rb

Overview

Abstraction layer for system engine compiler.

Direct Known Subclasses

Jade::SystemCompiler, Pug::SystemCompiler

Instance Attribute Summary

Attributes inherited from Compiler

#engine

Instance Method Summary collapse

Methods inherited from Compiler

#compilation_snippet, #prepare_options, #prepare_source, #process_result, #shipped?, #system?

Constructor Details

#initialize(engine) ⇒ SystemCompiler

Returns a new instance of SystemCompiler.

Parameters:



14
15
16
17
18
19
20
21
# File 'lib/jade-pug/system-compiler.rb', line 14

def initialize(engine)
  super(engine, nil)

  check_node_runtime!
  check_npm_package!

  engine.echo "Resolved system #{ engine.name } to #{ version }."
end

Instance Method Details

#check_node_runtime!nil (protected)

Checks if Node.js runtime exists in $PATH and is accessible.

Returns:

  • (nil)

Raises:



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/jade-pug/system-compiler.rb', line 108

def check_node_runtime!
  stdout, exit_status = Open3.capture2("node", "--version")

  if exit_status.success?
    @node_version = stdout.strip.gsub(/\Av/, "")
    engine.echo "Using Node.js runtime #{ @node_version }."
  else
    raise engine::CompilerError, %{No Node.js runtime has been found in your system.}
  end
  nil
end

#check_npm_package!nil (protected)

Checks if engine NPM package is installed.

Returns:

  • (nil)

Raises:



127
128
129
130
131
132
133
134
135
136
# File 'lib/jade-pug/system-compiler.rb', line 127

def check_npm_package!
  exit_status = Open3.capture2("node", "--eval", npm_package_require_snippet)[1]

  unless exit_status.success?
    raise engine::CompilerError, \
      %{No #{ engine.name } NPM package has been found in your system. } +
      %{Have you forgotten to "npm install --global #{ npm_package_name }"?}
  end
  nil
end

#compile(source, options = {}) ⇒ String

Compiles the template.

Parameters:

  • source (String, #read)
  • options (hash) (defaults to: {})

Returns:

  • (String)

Raises:

  • (engine::CompilationError)


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jade-pug/system-compiler.rb', line 29

def compile(source, options = {})
  source  = prepare_source(source)
  options = prepare_options(options)
  command = ["node", "--eval"]
  command.push compilation_snippet \
    method:    "compile#{ "Client" if options[:client] }",
    arguments: [source, options],
    locals:    options.fetch(:locals, {}),
    options:   options
  stdout, stderr, exit_status = Open3.capture3(*command)
  raise engine::CompilationError, stderr unless exit_status.success?
  process_result(source, stdout, options)
end

#npm_package_nameString (protected)

Return the name of engine NPM package.

Returns:

  • (String)


66
67
68
# File 'lib/jade-pug/system-compiler.rb', line 66

def npm_package_name
  engine.name.downcase
end

#npm_package_pathString (protected)

Returns the path of globally installed engine NPM package.

Returns:

  • (String)


74
75
76
# File 'lib/jade-pug/system-compiler.rb', line 74

def npm_package_path
  File.join(npm_packages_root, engine.name.downcase)
end

#npm_package_require_snippetString (protected)

Returns the JavaScript code used to require engine NPM package.

Returns:

  • (String)


82
83
84
# File 'lib/jade-pug/system-compiler.rb', line 82

def npm_package_require_snippet
  "require(#{ JSON.dump(npm_package_path) })"
end

#npm_packages_rootString (protected)

Returns the root directory of globally installed NPM packages.

Returns:

  • (String)


90
91
92
93
94
95
96
97
98
99
# File 'lib/jade-pug/system-compiler.rb', line 90

def npm_packages_root
  stdout, exit_status = Open3.capture2("npm", "root", "--global")

  if exit_status.success?
    stdout.strip
  else
    raise engine::CompilerError, \
      %{Unable to get NPM packages root. Perhaps, the problem with Node.js runtime.}
  end
end

#versionString

Returns version of engine installed system-wide.

Returns:

  • (String)


47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jade-pug/system-compiler.rb', line 47

def version
  stdout, exit_status = Open3.capture2 "node", "--eval", \
    "console.log(require(#{ JSON.dump(File.join(npm_package_path, "package.json")) }).version)"

  if exit_status.success?
    stdout.strip
  else
    raise engine::CompilerError, \
      %{Failed to get #{ engine.name } version. Perhaps, the problem with Node.js runtime.}
  end
end