Преглед изворни кода

Use docstore for CLSI request building

James Allen пре 12 година
родитељ
комит
6a2370b5be

+ 45 - 55
services/web/app/coffee/Features/Compile/ClsiManager.coffee

@@ -3,24 +3,23 @@ async = require "async"
 Settings = require "settings-sharelatex"
 Settings = require "settings-sharelatex"
 request = require('request')
 request = require('request')
 Project = require("../../models/Project").Project
 Project = require("../../models/Project").Project
+ProjectEntityHandler = require("../Project/ProjectEntityHandler")
 logger = require "logger-sharelatex"
 logger = require "logger-sharelatex"
 url = require("url")
 url = require("url")
 
 
 module.exports = ClsiManager =
 module.exports = ClsiManager =
 	sendRequest: (project_id, callback = (error, success) ->) ->
 	sendRequest: (project_id, callback = (error, success) ->) ->
-		Project.findById project_id, (error, project) ->
+		ClsiManager._buildRequest project_id, (error, req) ->
 			return callback(error) if error?
 			return callback(error) if error?
-			ClsiManager._buildRequest project, (error, req) ->
+			logger.log project_id: project_id, "sending compile to CLSI"
+			ClsiManager._postToClsi project_id, req, (error, response) ->
 				return callback(error) if error?
 				return callback(error) if error?
-				logger.log project_id: project_id, "sending compile to CLSI"
-				ClsiManager._postToClsi project_id, req, (error, response) ->
-					return callback(error) if error?
-					logger.log project_id: project_id, response: response, "received compile response from CLSI"
-					callback(
-						null
-						(response?.compile?.status == "success")
-						ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
-					)
+				logger.log project_id: project_id, response: response, "received compile response from CLSI"
+				callback(
+					null
+					(response?.compile?.status == "success")
+					ClsiManager._parseOutputFiles(project_id, response?.compile?.outputFiles)
+				)
 
 
 	getLogLines: (project_id, callback = (error, lines) ->) ->
 	getLogLines: (project_id, callback = (error, lines) ->) ->
 		request "#{Settings.apis.clsi.url}/project/#{project_id}/output/output.log", (error, response, body) ->
 		request "#{Settings.apis.clsi.url}/project/#{project_id}/output/output.log", (error, response, body) ->
@@ -44,54 +43,45 @@ module.exports = ClsiManager =
 		return outputFiles
 		return outputFiles
 
 
 	VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
 	VALID_COMPILERS: ["pdflatex", "latex", "xelatex", "lualatex"]
-	_buildRequest: (project, callback = (error, request) ->) ->
-		if project.compiler not in @VALID_COMPILERS
-			project.compiler = "pdflatex"
-
-		resources = []
-		rootResourcePath = null
-
-		addDoc = (basePath, doc, callback = (error) ->) ->
-			path = Path.join(basePath, doc.name)
-			resources.push
-				path:    path
-				content: doc.lines.join("\n")
-			if doc._id.toString() == project.rootDoc_id.toString()
-				rootResourcePath = path
-			callback()
+	_buildRequest: (project_id, callback = (error, request) ->) ->
+		Project.findById project_id, {compiler: 1, rootDoc_id: 1}, (error, project) ->
+			return callback(error) if error?
+			return callback(new Errors.NotFoundError("project does not exist: #{project_id}")) if !project?
 
 
-		addFile = (basePath, file, callback = (error) ->) ->
-			resources.push
-				path:     Path.join(basePath, file.name)
-				url:      "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
-				modified: file.created?.getTime()
-			callback()
+			if project.compiler not in ClsiManager.VALID_COMPILERS
+				project.compiler = "pdflatex"
 
 
-		addFolder = (basePath, folder, callback = (error) ->) ->
-			jobs = []
-			for doc in folder.docs
-				do (doc) ->
-					jobs.push (callback) -> addDoc(basePath, doc, callback)
+			ProjectEntityHandler.getAllDocs project_id, (error, docs = {}) ->
+				return callback(error) if error?
+				ProjectEntityHandler.getAllFiles project_id, (error, files = {}) ->
+					return callback(error) if error?
 
 
-			for file in folder.fileRefs
-				do (file) ->
-					jobs.push (callback) -> addFile(basePath, file, callback)
+					resources = []
+					rootResourcePath = null
 
 
-			for childFolder in folder.folders
-				do (childFolder) ->
-					jobs.push (callback) -> addFolder(Path.join(basePath, childFolder.name), childFolder, callback)
+					for path, doc of docs
+						path = path.replace(/^\//, "") # Remove leading /
+						resources.push
+							path:    path
+							content: doc.lines.join("\n")
+						if project.rootDoc_id? and doc._id.toString() == project.rootDoc_id.toString()
+							rootResourcePath = path
 
 
-			async.series jobs, callback
+					for path, file of files
+						path = path.replace(/^\//, "") # Remove leading /
+						resources.push
+							path:     path
+							url:      "#{Settings.apis.filestore.url}/project/#{project._id}/file/#{file._id}"
+							modified: file.created?.getTime()
 
 
-		addFolder "", project.rootFolder[0], (error) ->
-			if !rootResourcePath?
-				callback new Error("no root document exists")
-			else
-				callback null, {
-					compile:
-						options:
-							compiler: project.compiler
-						rootResourcePath: rootResourcePath
-						resources: resources
-				}
+					if !rootResourcePath?
+						callback new Error("no root document exists")
+					else
+						callback null, {
+							compile:
+								options:
+									compiler: project.compiler
+								rootResourcePath: rootResourcePath
+								resources: resources
+						}
 		
 		

+ 47 - 49
services/web/test/UnitTests/coffee/Compile/ClsiManagerTests.coffee

@@ -16,13 +16,13 @@ describe "ClsiManager", ->
 					clsi:
 					clsi:
 						url: "http://clsi.example.com"
 						url: "http://clsi.example.com"
 			"../../models/Project": Project: @Project = {}
 			"../../models/Project": Project: @Project = {}
+			"../Project/ProjectEntityHandler": @ProjectEntityHandler = {}
 			"logger-sharelatex": @logger = { log: sinon.stub() }
 			"logger-sharelatex": @logger = { log: sinon.stub() }
 		@project_id = "project-id"
 		@project_id = "project-id"
 		@callback = sinon.stub()
 		@callback = sinon.stub()
 
 
 	describe "sendRequest", ->
 	describe "sendRequest", ->
 		beforeEach ->
 		beforeEach ->
-			@Project.findById = sinon.stub().callsArgWith(1, null, @project = "mock-project")
 			@ClsiManager._buildRequest = sinon.stub().callsArgWith(1, null, @request = "mock-request")
 			@ClsiManager._buildRequest = sinon.stub().callsArgWith(1, null, @request = "mock-request")
 
 
 		describe "with a successful compile", ->
 		describe "with a successful compile", ->
@@ -40,14 +40,9 @@ describe "ClsiManager", ->
 				})
 				})
 				@ClsiManager.sendRequest @project_id, @callback
 				@ClsiManager.sendRequest @project_id, @callback
 
 
-			it "should look up the project", ->
-				@Project.findById
-					.calledWith(@project_id)
-					.should.equal true
-
-			it "should convert the project to a request", ->
+			it "should build the request", ->
 				@ClsiManager._buildRequest
 				@ClsiManager._buildRequest
-					.calledWith(@project)
+					.calledWith(@project_id)
 					.should.equal true
 					.should.equal true
 
 
 			it "should send the request to the CLSI", ->
 			it "should send the request to the CLSI", ->
@@ -82,52 +77,55 @@ describe "ClsiManager", ->
 				_id: @project_id
 				_id: @project_id
 				compiler: @compiler = "latex"
 				compiler: @compiler = "latex"
 				rootDoc_id: "mock-doc-id-1"
 				rootDoc_id: "mock-doc-id-1"
-				rootFolder: [
-					docs: [
-						@doc_1 = {
-							name: "main.tex"
-							_id: "mock-doc-id-1"
-							lines: ["Hello", "world"]
-						}
-					]
-					fileRefs: []
-					folders: [
-						@chapters_folder = {
-							name: "chapters"
-							docs: [
-								@doc_2 = {
-									name: "chapter1.tex"
-									_id: "mock-doc-id-2"
-									lines: [
-										"Chapter 1"
-									]
-								}
-							]
-							fileRefs: []
-							folders: []
-						},
-						@images_folder = {
-							name: "images"
-							docs: []
-							fileRefs: [
-								@file_1 = {
-									name: "image.png"
-									_id:  "mock-file-id-1"
-									created: new Date()
-								}
-							]
-							folders: []
-						}
+
+			@docs = {
+				"/main.tex": @doc_1 = {
+					name: "main.tex"
+					_id: "mock-doc-id-1"
+					lines: ["Hello", "world"]
+				},
+				"/chapters/chapter1.tex": @doc_2 = {
+					name: "chapter1.tex"
+					_id: "mock-doc-id-2"
+					lines: [
+						"Chapter 1"
 					]
 					]
+				}
+			}
 
 
-				]
+			@files = {
+				"/images/image.png": @file_1 = {
+					name: "image.png"
+					_id:  "mock-file-id-1"
+					created: new Date()
+				}
+			}
+
+			@Project.findById = sinon.stub().callsArgWith(2, null, @project)
+			@ProjectEntityHandler.getAllDocs = sinon.stub().callsArgWith(1, null, @docs)
+			@ProjectEntityHandler.getAllFiles = sinon.stub().callsArgWith(1, null, @files)
 
 
 		describe "with a valid project", ->
 		describe "with a valid project", ->
 			beforeEach (done) ->
 			beforeEach (done) ->
-				@ClsiManager._buildRequest @project, (error, request) =>
+				@ClsiManager._buildRequest @project_id, (error, request) =>
 					@request = request
 					@request = request
 					done()
 					done()
 
 
+			it "should get the project with the required fields", ->
+				@Project.findById
+					.calledWith(@project_id, {compiler:1, rootDoc_id: 1})
+					.should.equal true
+
+			it "should get all the docs", ->
+				@ProjectEntityHandler.getAllDocs
+					.calledWith(@project_id)
+					.should.equal true
+
+			it "should get all the files", ->
+				@ProjectEntityHandler.getAllFiles
+					.calledWith(@project_id)
+					.should.equal true
+
 			it "should build up the CLSI request", ->
 			it "should build up the CLSI request", ->
 				expect(@request).to.deep.equal(
 				expect(@request).to.deep.equal(
 					compile:
 					compile:
@@ -135,13 +133,13 @@ describe "ClsiManager", ->
 							compiler: @compiler
 							compiler: @compiler
 						rootResourcePath: "main.tex"
 						rootResourcePath: "main.tex"
 						resources: [{
 						resources: [{
-							path:    @doc_1.name
+							path:    "main.tex"
 							content: @doc_1.lines.join("\n")
 							content: @doc_1.lines.join("\n")
 						}, {
 						}, {
-							path:    "#{@chapters_folder.name}/#{@doc_2.name}"
+							path:    "chapters/chapter1.tex"
 							content: @doc_2.lines.join("\n")
 							content: @doc_2.lines.join("\n")
 						}, {
 						}, {
-							path: "#{@images_folder.name}/#{@file_1.name}"
+							path: "images/image.png"
 							url:  "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
 							url:  "#{@settings.apis.filestore.url}/project/#{@project_id}/file/#{@file_1._id}"
 							modified: @file_1.created.getTime()
 							modified: @file_1.created.getTime()
 						}]
 						}]