forked from recca0120/vscode-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathReplacer.ts
More file actions
158 lines (130 loc) · 4.54 KB
/
Copy pathPathReplacer.ts
File metadata and controls
158 lines (130 loc) · 4.54 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type { SpawnOptions } from 'node:child_process';
import os from 'node:os';
import path from 'node:path';
import {
VAR_PATH_SEPARATOR,
VAR_PATH_SEPARATOR_SHORT,
VAR_PWD,
VAR_USER_HOME,
VAR_WORKSPACE_FOLDER,
VAR_WORKSPACE_FOLDER_BASENAME,
} from '../constants';
function escapeRegExp(str: string) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
export type Path = { [p: string]: string };
interface PathMapping {
local: string;
remote: string;
}
export class PathReplacer {
private readonly cwd: string;
private readonly pathVariables: Map<string, string>;
private readonly pathMappings: PathMapping[] = [];
constructor(
private options: SpawnOptions = {},
paths?: Path,
workspaceFolder?: string,
) {
this.cwd = this.fixWindowsDriveLetter(
(this.options?.cwd as string) ?? (process.env.cwd as string),
);
const workspaceFolderPath = workspaceFolder
? this.fixWindowsDriveLetter(workspaceFolder)
: this.cwd;
this.pathVariables = new Map<string, string>();
this.pathVariables.set(VAR_PWD, this.cwd);
this.pathVariables.set(VAR_WORKSPACE_FOLDER, workspaceFolderPath);
this.pathVariables.set(
VAR_WORKSPACE_FOLDER_BASENAME,
workspaceFolderPath ? path.basename(workspaceFolderPath) : '',
);
this.pathVariables.set(VAR_USER_HOME, os.homedir());
this.pathVariables.set(VAR_PATH_SEPARATOR, path.sep);
this.pathVariables.set(VAR_PATH_SEPARATOR_SHORT, path.sep);
for (const [key, value] of Object.entries(paths ?? {})) {
const remote = this.replacePathVariables(value);
if (!this.pathVariables.has(key)) {
this.pathMappings.push({ local: key, remote });
continue;
}
const local = this.pathVariables.get(key);
if (local) {
this.pathMappings.push({ local, remote });
}
}
}
replacePathVariables(path: string) {
for (const [key, value] of this.pathVariables) {
path = path.replace(new RegExp(escapeRegExp(key), 'g'), value);
}
return path;
}
toLocal(path: string) {
path = this.removePhpVfsComposer(path);
const { prefix, pathPart, suffix } = this.splitQualifiedName(path);
let result = pathPart;
for (const { local, remote } of this.pathMappings) {
if (!this.isSafePath(local) || remote === '.') {
continue;
}
result = result.replace(new RegExp(escapeRegExp(remote), 'g'), local);
}
result = this.expandRelative(result);
result = this.normalizeToWindows(result);
return `${prefix}${result}${suffix}`;
}
toRemote(path: string) {
path = this.replacePathVariables(path);
path = this.expandRelative(path);
for (const { local, remote } of this.pathMappings) {
if (!this.isSafePath(local)) {
continue;
}
path = path.replace(new RegExp(escapeRegExp(local), 'g'), remote);
}
path = this.normalizeToPosix(path);
path = this.normalizeToWindows(path);
return path;
}
private splitQualifiedName(path: string): {
prefix: string;
pathPart: string;
suffix: string;
} {
const match = path.match(/^(php_qn:\/\/)?(.*?)(?=::|$)(::.*)?$/);
if (!match) {
return { prefix: '', pathPart: path, suffix: '' };
}
return {
prefix: match[1] ?? '',
pathPart: match[2],
suffix: match[3] ?? '',
};
}
private normalizeToPosix(path: string) {
return !/^[a-zA-Z]:/.test(path) && path.indexOf('\\') !== -1
? path.replace(/\\/g, '/')
: path;
}
private normalizeToWindows(path: string) {
return /[a-zA-Z]:/.test(path)
? path.replace(/[a-zA-Z]:[^:]+/g, (path) => path.replace(/\//g, '\\'))
: path;
}
private expandRelative(path: string) {
if (!path.startsWith('./')) {
return path;
}
return this.cwd ? path.replace(/^\./, this.cwd) : path;
}
private removePhpVfsComposer(path: string) {
return path.replace(/phpvfscomposer:\/\//gi, '');
}
private fixWindowsDriveLetter(path: string) {
return /^\\/.test(path) ? `c:${path}` : path;
}
private isSafePath(path: string) {
return !['/', '', ' '].includes(path);
}
}