diff --git a/lib/pipelines/compileSoy.js b/lib/pipelines/compileSoy.js index 439ae70f..50d2a521 100644 --- a/lib/pipelines/compileSoy.js +++ b/lib/pipelines/compileSoy.js @@ -46,6 +46,33 @@ function addTemplateParam(soyJsPath, templateName, param) { templateData[soyJsPath][templateName].params.push(param); } +function buildSoyCompilerArgs(files, outputDir, soyDeps) { + var args = [ + '-jar', + PATH_TO_JAR, + '--outputPathFormat', + path.join(outputDir, '{INPUT_DIRECTORY}{INPUT_FILE_NAME}.js') + ].concat(files.map(function(singleFile) { + return path.relative(process.cwd(), singleFile.path); + })); + + if (soyDeps) { + var expanded = expand(soyDeps); + for (var i = 0; i < expanded.length; i++) { + args.push('--deps', expanded[i]); + } + } + return args; +} + +function buildSoyCompilerError(errorMsg) { + var msg = 'Compile error:\n'; + if (errorMsg.indexOf('UnsupportedClassVersionError') !== -1) { + msg += 'Make sure that you have Java version 8 or higher installed.\n'; + } + return new gutil.PluginError('metal-tools-soy', msg + errorMsg); +} + function compileToIncDom(options) { var files = []; return through.obj( @@ -59,59 +86,21 @@ function compileToIncDom(options) { callback(); return; } - - var stream = this; - var outputDir = '/tmp/metal-tools-soy'; - var args = [ - '-jar', - PATH_TO_JAR, - '--outputPathFormat', - path.join(outputDir, '{INPUT_DIRECTORY}{INPUT_FILE_NAME}.js') - ].concat(files.map(function(singleFile) { - return path.relative(process.cwd(), singleFile.path); - })); - - if (options.soyDeps) { - var expanded = expand(options.soyDeps); - for (var i = 0; i < expanded.length; i++) { - args.push('--deps', expanded[i]); - } - } - - var cp = childProcess.spawn('java', args, {cwd: process.cwd()}); - var stderr = ''; - cp.stderr.on('data', function(data) { - stderr += data; - }); - cp.on('exit', function(code) { - if (code === 0) { - for (var i = 0; i < files.length; i++) { - var realPath = path.join(outputDir, path.relative(process.cwd(), files[i].path) + '.js'); - var compiled = new gutil.File({ - base: files[i].base, - contents: fs.readFileSync(realPath), - cwd: files[i].cwd, - path: files[i].path + '.js' - }); - stream.emit('data', compiled); - } - } else { - var msg = 'Compile error:\n'; - if (stderr.indexOf('UnsupportedClassVersionError') !== -1) { - msg += 'Make sure that you have Java version 8 or higher installed.\n'; - } - stream.emit( - 'error', - new gutil.PluginError('metal-tools-soy', msg + stderr) - ); - } - stream.emit('end'); - callback(); - }); + runSoyCompiler(files, options, this, callback); } ); } +function createCompiledGulpFile(file, outputDir) { + var realPath = path.join(outputDir, path.relative(process.cwd(), file.path) + '.js'); + return new gutil.File({ + base: file.base, + contents: fs.readFileSync(realPath), + cwd: file.cwd, + path: file.path + '.js' + }); +} + function extractParams() { return through.obj(function(file, encoding, callback) { try { @@ -208,3 +197,25 @@ function replaceTemplateRequires() { callback(); }); } + +function runSoyCompiler(files, options, stream, callback) { + var outputDir = '/tmp/metal-tools-soy'; + var args = buildSoyCompilerArgs(files, outputDir, options.soyDeps); + + var cp = childProcess.spawn('java', args, {cwd: process.cwd()}); + var stderr = ''; + cp.stderr.on('data', function(data) { + stderr += data; + }); + cp.on('exit', function(code) { + if (code === 0) { + for (var i = 0; i < files.length; i++) { + stream.emit('data', createCompiledGulpFile(files[i], outputDir)); + } + } else { + stream.emit('error', buildSoyCompilerError(stderr)); + } + stream.emit('end'); + callback(); + }); +}