-
Notifications
You must be signed in to change notification settings - Fork 15
/
lupdate.ts
61 lines (56 loc) · 1.94 KB
/
lupdate.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
import * as path from 'path';
import { EMPTY, iif } from 'rxjs';
import { mergeMap, switchMap } from 'rxjs/operators';
import * as vscode from 'vscode';
import {
createPathIfNotExist,
getActiveWorkspaceFolderPath,
getFsPathOrActiveDocumentPath,
} from '../utils/paths';
import { PredefinedVariableResolver } from '../utils/predefined-variable-resolver';
import { run } from '../utils/run';
import {
createFileWatcher$,
enabled$,
watchFileChangedAndCreated,
} from '../utils/watcher';
import { Tool } from './tool';
const NAME = 'lupdate';
export async function updateTranslation(fileUri?: vscode.Uri) {
const inPath = getFsPathOrActiveDocumentPath(fileUri);
const tool = new Tool(NAME, new PredefinedVariableResolver(inPath));
const outputPath = tool.getOutputPath(['ts']) ?? getTsPathForPylupdate(tool);
if (outputPath) createPathIfNotExist(outputPath);
return run({
command:
`${await tool.getPathWithQuotes()} ` +
`"${inPath}" ` +
/**
* The options need to come AFTER the input files due to the wierd CLI
* requirements from pylupdate5.
*/
`${tool.args.join(' ')}`,
cwd: getActiveWorkspaceFolderPath(),
});
}
/**
* The TS file output path of pylupdate (both 5 and 6) is flagged with `-ts`
* instead of a the regular CLI format, such as `--ts`. Thus, we need to parse
* it manually.
*/
function getTsPathForPylupdate(tool: Tool) {
const matched = tool.args
.join(' ')
.match(/-ts\s+("(?<pathWithSpaces>[\S\s]+)"|(?<path>[\S]+))(\s+|$)/);
if (matched?.groups)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return path.dirname(matched.groups.pathWithSpaces ?? matched.groups.path);
return;
}
const uiFileWatcher$ = createFileWatcher$('**/*.ui');
export const liveExecution$ = enabled$('lupdate').pipe(
switchMap(enabled =>
iif(() => enabled, uiFileWatcher$.pipe(watchFileChangedAndCreated()), EMPTY)
),
mergeMap(uri => updateTranslation(uri))
);