Class: JadePug::SystemCompiler
- Defined in:
- lib/jade-pug/system-compiler.rb
Overview
Abstraction layer for system engine compiler.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Compiler
Instance Method Summary collapse
-
#check_node_runtime! ⇒ nil
protected
Checks if Node.js runtime exists in $PATH and is accessible.
-
#check_npm_package! ⇒ nil
protected
Checks if engine NPM package is installed.
-
#compile(source, options = {}) ⇒ String
Compiles the template.
-
#initialize(engine) ⇒ SystemCompiler
constructor
A new instance of SystemCompiler.
-
#npm_package_name ⇒ String
protected
Return the name of engine NPM package.
-
#npm_package_path ⇒ String
protected
Returns the path of globally installed engine NPM package.
-
#npm_package_require_snippet ⇒ String
protected
Returns the JavaScript code used to require engine NPM package.
-
#npm_packages_root ⇒ String
protected
Returns the root directory of globally installed NPM packages.
-
#version ⇒ String
Returns version of engine installed system-wide.
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.
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.
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.
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.
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, = {}) source = prepare_source(source) = () command = ["node", "--eval"] command.push compilation_snippet \ method: "compile#{ "Client" if [:client] }", arguments: [source, ], locals: .fetch(:locals, {}), options: stdout, stderr, exit_status = Open3.capture3(*command) raise engine::CompilationError, stderr unless exit_status.success? process_result(source, stdout, ) end |
#npm_package_name ⇒ String (protected)
Return the name of engine NPM package.
66 67 68 |
# File 'lib/jade-pug/system-compiler.rb', line 66 def npm_package_name engine.name.downcase end |
#npm_package_path ⇒ String (protected)
Returns the path of globally installed engine NPM package.
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_snippet ⇒ String (protected)
Returns the JavaScript code used to require engine NPM package.
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_root ⇒ String (protected)
Returns the root directory of globally installed NPM packages.
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 |
#version ⇒ String
Returns version of engine installed system-wide.
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 |