From 34acce8bdacaeef82878df1f130a693b6b52b4bd Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 10 Oct 2018 16:13:20 +0100 Subject: [PATCH 1/3] use TikzManager to create main file for pstool package --- services/clsi/app/coffee/CompileManager.coffee | 4 ++-- services/clsi/app/coffee/TikzManager.coffee | 12 +++++++----- .../clsi/test/unit/coffee/TikzManager.coffee | 16 ++++++++++++++++ 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/services/clsi/app/coffee/CompileManager.coffee b/services/clsi/app/coffee/CompileManager.coffee index eff20ebd2c..0a98e87db4 100644 --- a/services/clsi/app/coffee/CompileManager.coffee +++ b/services/clsi/app/coffee/CompileManager.coffee @@ -58,9 +58,9 @@ module.exports = CompileManager = callback() createTikzFileIfRequired = (callback) -> - TikzManager.checkMainFile compileDir, request.rootResourcePath, resourceList, (error, usesTikzExternalize) -> + TikzManager.checkMainFile compileDir, request.rootResourcePath, resourceList, (error, needsMainFile) -> return callback(error) if error? - if usesTikzExternalize + if needsMainFile TikzManager.injectOutputFile compileDir, request.rootResourcePath, callback else callback() diff --git a/services/clsi/app/coffee/TikzManager.coffee b/services/clsi/app/coffee/TikzManager.coffee index 7605b0d277..60cb1e3496 100644 --- a/services/clsi/app/coffee/TikzManager.coffee +++ b/services/clsi/app/coffee/TikzManager.coffee @@ -4,26 +4,28 @@ ResourceWriter = require "./ResourceWriter" SafeReader = require "./SafeReader" logger = require "logger-sharelatex" -# for \tikzexternalize to work the main file needs to match the +# for \tikzexternalize or pstool to work the main file needs to match the # jobname. Since we set the -jobname to output, we have to create a # copy of the main file as 'output.tex'. module.exports = TikzManager = - checkMainFile: (compileDir, mainFile, resources, callback = (error, usesTikzExternalize) ->) -> + checkMainFile: (compileDir, mainFile, resources, callback = (error, needsMainFile) ->) -> # if there's already an output.tex file, we don't want to touch it for resource in resources if resource.path is "output.tex" logger.log compileDir: compileDir, mainFile: mainFile, "output.tex already in resources" return callback(null, false) - # if there's no output.tex, see if we are using tikz/pgf in the main file + # if there's no output.tex, see if we are using tikz/pgf or pstool in the main file ResourceWriter.checkPath compileDir, mainFile, (error, path) -> return callback(error) if error? SafeReader.readFile path, 65536, "utf8", (error, content) -> return callback(error) if error? usesTikzExternalize = content?.indexOf("\\tikzexternalize") >= 0 - logger.log compileDir: compileDir, mainFile: mainFile, usesTikzExternalize:usesTikzExternalize, "checked for tikzexternalize" - callback null, usesTikzExternalize + usesPsTool = content.indexOf("{pstool}") >= 0 + logger.log compileDir: compileDir, mainFile: mainFile, usesTikzExternalize:usesTikzExternalize, usesPsTool: usesPsTool, "checked for packages needing main file as output.tex" + needsMainFile = (usesTikzExternalize || usesPsTool) + callback null, needsMainFile injectOutputFile: (compileDir, mainFile, callback = (error) ->) -> ResourceWriter.checkPath compileDir, mainFile, (error, path) -> diff --git a/services/clsi/test/unit/coffee/TikzManager.coffee b/services/clsi/test/unit/coffee/TikzManager.coffee index 5a3ec5ce5a..69968aa5ea 100644 --- a/services/clsi/test/unit/coffee/TikzManager.coffee +++ b/services/clsi/test/unit/coffee/TikzManager.coffee @@ -65,6 +65,22 @@ describe 'TikzManager', -> @callback.calledWithExactly(null, false) .should.equal true + describe "and the main file contains \\usepackage{pstool}", -> + beforeEach -> + @SafeReader.readFile = sinon.stub() + .withArgs("#{@compileDir}/#{@mainFile}") + .callsArgWith(3, null, "hello \\usepackage[random-options]{pstool}") + @TikzManager.checkMainFile @compileDir, @mainFile, @resources, @callback + + it "should look at the file on disk", -> + @SafeReader.readFile + .calledWith("#{@compileDir}/#{@mainFile}") + .should.equal true + + it "should call the callback with true ", -> + @callback.calledWithExactly(null, true) + .should.equal true + describe "injectOutputFile", -> beforeEach -> @rootDir = "/mock" From 187786b4e4cfa8c8015184459fba2e7e7984e26a Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Fri, 12 Oct 2018 10:49:54 +0100 Subject: [PATCH 2/3] improve log message --- services/clsi/app/coffee/TikzManager.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/clsi/app/coffee/TikzManager.coffee b/services/clsi/app/coffee/TikzManager.coffee index 60cb1e3496..5b80e6c7c4 100644 --- a/services/clsi/app/coffee/TikzManager.coffee +++ b/services/clsi/app/coffee/TikzManager.coffee @@ -32,6 +32,6 @@ module.exports = TikzManager = return callback(error) if error? fs.readFile path, "utf8", (error, content) -> return callback(error) if error? - logger.log compileDir: compileDir, mainFile: mainFile, "copied file to output.tex for tikz" + logger.log compileDir: compileDir, mainFile: mainFile, "copied file to output.tex as project uses packages which require it" # use wx flag to ensure that output file does not already exist fs.writeFile Path.join(compileDir, "output.tex"), content, {flag:'wx'}, callback From 55fa22caa9fd17a94604c95634774a79da883cca Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 15 Oct 2018 10:01:52 +0100 Subject: [PATCH 3/3] fix exception when content undefined in TikzManager --- services/clsi/app/coffee/TikzManager.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/clsi/app/coffee/TikzManager.coffee b/services/clsi/app/coffee/TikzManager.coffee index 5b80e6c7c4..22def278b1 100644 --- a/services/clsi/app/coffee/TikzManager.coffee +++ b/services/clsi/app/coffee/TikzManager.coffee @@ -22,7 +22,7 @@ module.exports = TikzManager = SafeReader.readFile path, 65536, "utf8", (error, content) -> return callback(error) if error? usesTikzExternalize = content?.indexOf("\\tikzexternalize") >= 0 - usesPsTool = content.indexOf("{pstool}") >= 0 + usesPsTool = content?.indexOf("{pstool}") >= 0 logger.log compileDir: compileDir, mainFile: mainFile, usesTikzExternalize:usesTikzExternalize, usesPsTool: usesPsTool, "checked for packages needing main file as output.tex" needsMainFile = (usesTikzExternalize || usesPsTool) callback null, needsMainFile