-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemonMavenExecutor.ts
126 lines (106 loc) · 3.66 KB
/
daemonMavenExecutor.ts
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import MavenExecutor, {
MavenExecutionArgs,
MavenExecutionResult,
} from './mavenExecutor';
import * as childProcess from 'child_process';
import Logger from '../logger';
import ConfigurationProvider from '../configurationProvider';
import * as fs from 'fs';
import * as fsPromises from 'fs/promises';
import { randomBytes } from 'crypto';
const TMP_DIR_NAME = '/tmp/vscode-spotless-maven';
class DaemonMavenExecutor implements MavenExecutor {
private activeProcess: childProcess.ChildProcess | null = null;
constructor(
private configurationProvider: ConfigurationProvider,
private logger: Logger
) {}
public async runSpotlessApply({
pomUri,
documentUri,
documentText,
cancellationToken,
}: MavenExecutionArgs): Promise<MavenExecutionResult> {
const ac = new AbortController();
cancellationToken.onCancellationRequested(() =>
ac.abort('cancellationToken')
);
// mvnd doesn't support reading the stdin (yet) so we need to write to a
// temporary file to pass in our document.
//
// TODO: if we absolutely *must* do this, we should write somewhere less obvious. Spotless does
// not seem to want to format files in /tmp/ so maybe we could identify a suitable output dir like
// target/
const tempFilePath = documentUri.fsPath + '.tmp.java';
await fsPromises.writeFile(tempFilePath, documentText, {
signal: ac.signal,
encoding: 'utf-8',
});
const args = [
'spotless:apply',
'-f',
pomUri.fsPath,
`-DspotlessIdeHook=${tempFilePath}`,
// stdout also seems problematic for reasons that are less clear - NPE trying to create a logger
// '-DspotlessIdeHookUseStdOut',
'--quiet',
];
try {
const { stderr } = await this.runMaven(args, {
signal: ac.signal,
});
const formattedDocumentText = await fsPromises.readFile(tempFilePath, {
encoding: 'utf8',
});
return { formattedDocumentText, spotlessStatus: stderr };
} finally {
await fsPromises.unlink(tempFilePath);
}
}
public dispose(): void {
if (this.activeProcess && this.activeProcess.exitCode === null) {
this.activeProcess.disconnect();
}
for (const file of fs.readdirSync(TMP_DIR_NAME)) {
fs.unlinkSync(file);
}
}
private async writeTempFile(documentText: string): Promise<string> {
// TODO: Windows support
const folder = TMP_DIR_NAME;
fsPromises.mkdir(folder, { recursive: true });
const filename = randomBytes(16).toString('hex');
const path = `${folder}/${filename}`;
await fsPromises.writeFile(path, documentText);
return path;
}
private async runMaven(
args: string[],
options: childProcess.ExecFileOptions
): Promise<{ stdout: string; stderr: string }> {
return new Promise((resolve, reject) => {
const mvndCommand = this.configurationProvider.getMvndPath();
this.logger.trace(`Executing maven daemon: ${mvndCommand}`, ...args);
this.activeProcess = childProcess.execFile(
mvndCommand,
args,
options,
(error, stdout, stderr) => {
if (error) {
// mvnd always fails with an NPE creating a logger for some reason
// this is absolutely evil but I wanted to get something working
// TODO: STOP DOING THIS!!
resolve({ stdout: 'unused', stderr: 'IS DIRTY' });
} else {
this.logger.trace('Maven execution completed');
resolve({ stdout, stderr });
}
}
);
this.activeProcess.stdout?.on('data', (data) => {
this.logger.trace('stdout:', data);
});
});
}
}
export default DaemonMavenExecutor;