-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompilationRequest.js
110 lines (98 loc) · 3.57 KB
/
CompilationRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var exec = require('child_process').execFile;
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var util = require('./utilities');
var UserErrors = {
BranchNotFound: (url, branch) => `failed to find branch '${branch}' at ${url}`,
BranchAmbiguity: (url, branch) => `multiple branches at ${url} matched requested ${branch}`,
};
class CompilationRequest {
/**
* @param {string} contentHash
* @param {string} target
* @param {string} command
* @param {string=} workdir
*/
constructor(contentHash, target, command, workdir) {
this.target = target;
this.command = command;
this.workdir = workdir || path.dirname(target);
this.fingerprint = `${command}:${contentHash}:${target}:${workdir}`;
}
/**
* @param {string} text
* @param {string} command
* @return {!Promise<!{request:?CompilationRequest, userError:?string>}
*/
static createForText(text, command) {
var userError = validateCommand(command);
if (userError)
return {request: null, userError};
var hash = crypto.createHash('md5').update(text).digest("hex");
var userError = null;
var request = new CompilationRequest(hash, 'main.tex', command);
return Promise.resolve({request, userError});
}
/**
* @param {string} filePath
* @param {string} target
* @return {!Promise<!{request:?CompilationRequest, userError:?string>}
*/
static async createFileRequest(filePath, target, command) {
var userError = validateCommand(command);
if (userError)
return {request: null, userError};
var hash = await util.computeHash(filePath);
if (!hash)
return null;
var request = new CompilationRequest(hash, target, command);
return {request, userError: null};
}
/**
* @param {string} gitURL
* @param {string} target
* @param {string} branch
* @param {string} command
* @param {string} workdir
* @return {!Promise<!{request:?CompilationRequest, userError:?string>}
*/
static createGitRequest(gitURL, target, branch, command, workdir) {
var userError = validateCommand(command);
if (userError)
return {request: null, userError};
var fulfill;
var promise = new Promise(x => fulfill = x);
var userError = null;
var request = null;
exec('git', ['ls-remote', gitURL, branch], (err, stdout, stderr) => {
if(err !== null) {
userError = err.message;
fulfill({request, userError});
return;
}
var lines = stdout.split('\n');
if (lines.lenght > 1) {
userError = UserErrors.BranchAmbiguity(gitURL, branch);
fulfill({request, userError});
return;
}
if (!lines.length) {
userError = UserErrors.BranchNotFound(gitURL, branch);
fulfill({request, userError});
return;
}
var hash = lines[0].split('\t')[0];
// No need to include branch here: branch is already a part of GIT's SHA.
var request = new CompilationRequest(hash, target, command, workdir);
fulfill({userError, request});
});
return promise;
}
}
function validateCommand(command) {
if (command !== 'python' && command !== 'python2' && command !== 'python3')
return `Unknown command: ${command}`;
return null;
}
module.exports = CompilationRequest;