Skip to content

Commit d06585d

Browse files
committed
fix: replace a hardcoded platform with a dynamic one
1 parent 0ddb260 commit d06585d

File tree

2 files changed

+102
-95
lines changed

2 files changed

+102
-95
lines changed

src/debug-adapter/nativeScriptDebugAdapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ export class NativeScriptDebugAdapter extends ChromeDebugAdapter {
143143
const appDirPath = this.getAppDirPath(transformedArgs.webRoot);
144144

145145
(this.pathTransformer as any).setTransformOptions(args.platform, appDirPath, transformedArgs.webRoot);
146-
(this.sourceMapTransformer as NativeScriptSourceMapTransformer).setDebugAdapter(this);
146+
const sourceMapTransformer = this.sourceMapTransformer as NativeScriptSourceMapTransformer;
147+
148+
sourceMapTransformer.setDebugAdapter(this);
149+
sourceMapTransformer.platform = args.platform.toLowerCase();
147150
(ChromeDebugAdapter as any).SET_BREAKPOINTS_TIMEOUT = 20000;
148151

149152
this.isLiveSync = args.watch;

src/debug-adapter/nativeScriptSourceMapTransformer.ts

Lines changed: 98 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,14 @@ import { BaseSourceMapTransformer, logger as vsCodeChromeDebugLogger, utils as v
33
import * as sourceMapUtils from 'vscode-chrome-debug-core/out/src/sourceMaps/sourceMapUtils'; // tslint:disable-line
44
import { NativeScriptDebugAdapter } from './nativeScriptDebugAdapter';
55

6-
// a workaround for https://github.com/NativeScript/nativescript-vscode-extension/issues/252
7-
// tslint:disable
8-
//--- begin part copied from vscode-chrome-debug-core (https://github.com/microsoft/vscode-chrome-debug-core/blob/d1fe8ca062e277a3480879e1fb63a8ef3b48015a/src/sourceMaps/sourceMapUtils.ts#L78)
9-
//
10-
// VS Code - Debugger for Chrome
11-
//
12-
// Copyright (c) Microsoft Corporation
13-
//
14-
// All rights reserved.
15-
//
16-
// MIT License
17-
//
18-
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
19-
//
20-
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
21-
//
22-
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23-
24-
sourceMapUtils.applySourceMapPathOverrides = function applySourceMapPathOverrides(sourcePath, sourceMapPathOverrides, isVSClient = false) {
25-
const forwardSlashSourcePath = sourcePath.replace(/\\/g, '/');
26-
// Sort the overrides by length, large to small
27-
const sortedOverrideKeys = Object.keys(sourceMapPathOverrides)
28-
.sort((a, b) => b.length - a.length);
29-
30-
// Iterate the key/vals, only apply the first one that matches.
31-
for (const leftPattern of sortedOverrideKeys) {
32-
const rightPattern = sourceMapPathOverrides[leftPattern];
33-
const entryStr = `"${leftPattern}": "${rightPattern}"`;
34-
const asterisks = leftPattern.match(/\*/g) || [];
35-
36-
if (asterisks.length > 1) {
37-
vsCodeChromeDebugLogger.log(`Warning: only one asterisk allowed in a sourceMapPathOverrides entry - ${entryStr}`);
38-
continue;
39-
}
40-
const replacePatternAsterisks = rightPattern.match(/\*/g) || [];
41-
42-
if (replacePatternAsterisks.length > asterisks.length) {
43-
vsCodeChromeDebugLogger.log(`Warning: the right side of a sourceMapPathOverrides entry must have 0 or 1 asterisks - ${entryStr}}`);
44-
continue;
45-
}
46-
// Does it match?
47-
const escapedLeftPattern = vsCodeChromeDebugUtils.escapeRegexSpecialChars(leftPattern, '/*');
48-
const leftRegexSegment = escapedLeftPattern
49-
.replace(/\*/g, '(.*)')
50-
.replace(/\\\\/g, '/');
51-
const leftRegex = new RegExp(`^${leftRegexSegment}$`, 'i');
52-
const overridePatternMatches = forwardSlashSourcePath.match(leftRegex);
53-
54-
if (!overridePatternMatches) {
55-
continue;
56-
}
57-
// Grab the value of the wildcard from the match above, replace the wildcard in the
58-
// replacement pattern, and return the result.
59-
const wildcardValue = overridePatternMatches[1];
60-
61-
// ***************** CUSTOM CODE START *****************
62-
// handle linked files
63-
let mappedPath = path.isAbsolute(wildcardValue) ? wildcardValue : rightPattern.replace(/\*/g, wildcardValue);
64-
// ***************** CUSTOM CODE END *****************
65-
66-
mappedPath = path.join(mappedPath); // Fix any ..
67-
68-
// ***************** CUSTOM CODE START *****************
69-
// handle platform-specific files
70-
const platform = 'android';
71-
const { dir, name, ext } = path.parse(mappedPath);
72-
73-
const platformFileName = `${name}.${platform}${ext}`;
74-
const platformPath = path.join(dir, platformFileName);
75-
76-
if (vsCodeChromeDebugUtils.existsSync(platformPath)) {
77-
mappedPath = platformPath;
78-
}
79-
// ***************** CUSTOM CODE END *****************
80-
81-
if (isVSClient && leftPattern === 'webpack:///./*' && !vsCodeChromeDebugUtils.existsSync(mappedPath)) {
82-
// This is a workaround for a bug in ASP.NET debugging in VisualStudio because the wwwroot is not properly configured
83-
const pathFixingASPNETBug = path.join(rightPattern.replace(/\*/g, path.join('../ClientApp', wildcardValue)));
84-
85-
if (vsCodeChromeDebugUtils.existsSync(pathFixingASPNETBug)) {
86-
mappedPath = pathFixingASPNETBug;
87-
}
88-
}
89-
vsCodeChromeDebugLogger.log(`SourceMap: mapping ${sourcePath} => ${mappedPath}, via sourceMapPathOverrides entry - ${entryStr}`);
90-
91-
return mappedPath;
92-
}
93-
94-
return sourcePath;
95-
};
96-
97-
//--- end part copied from vscode-chrome-debug-core
98-
// tslint:enable
99-
1006
export class NativeScriptSourceMapTransformer extends BaseSourceMapTransformer {
7+
public platform: string;
8+
1019
private debugAdapter: NativeScriptDebugAdapter;
10210

10311
constructor(sourceHandles: any) {
10412
super(sourceHandles);
13+
this.setupSourceMapPathOverrides();
10514
}
10615

10716
public setDebugAdapter(debugAdapter: NativeScriptDebugAdapter): void {
@@ -119,4 +28,99 @@ export class NativeScriptSourceMapTransformer extends BaseSourceMapTransformer {
11928

12029
return scriptParsedResult;
12130
}
31+
32+
private setupSourceMapPathOverrides() {
33+
// a workaround for https://github.com/NativeScript/nativescript-vscode-extension/issues/252
34+
// tslint:disable
35+
//--- begin part copied from vscode-chrome-debug-core (https://github.com/microsoft/vscode-chrome-debug-core/blob/d1fe8ca062e277a3480879e1fb63a8ef3b48015a/src/sourceMaps/sourceMapUtils.ts#L78)
36+
//
37+
// VS Code - Debugger for Chrome
38+
//
39+
// Copyright (c) Microsoft Corporation
40+
//
41+
// All rights reserved.
42+
//
43+
// MIT License
44+
//
45+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
46+
//
47+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
48+
//
49+
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50+
51+
sourceMapUtils.applySourceMapPathOverrides = (sourcePath, sourceMapPathOverrides, isVSClient = false) => {
52+
const forwardSlashSourcePath = sourcePath.replace(/\\/g, '/');
53+
// Sort the overrides by length, large to small
54+
const sortedOverrideKeys = Object.keys(sourceMapPathOverrides)
55+
.sort((a, b) => b.length - a.length);
56+
57+
// Iterate the key/vals, only apply the first one that matches.
58+
for (const leftPattern of sortedOverrideKeys) {
59+
const rightPattern = sourceMapPathOverrides[leftPattern];
60+
const entryStr = `"${leftPattern}": "${rightPattern}"`;
61+
const asterisks = leftPattern.match(/\*/g) || [];
62+
63+
if (asterisks.length > 1) {
64+
vsCodeChromeDebugLogger.log(`Warning: only one asterisk allowed in a sourceMapPathOverrides entry - ${entryStr}`);
65+
continue;
66+
}
67+
const replacePatternAsterisks = rightPattern.match(/\*/g) || [];
68+
69+
if (replacePatternAsterisks.length > asterisks.length) {
70+
vsCodeChromeDebugLogger.log(`Warning: the right side of a sourceMapPathOverrides entry must have 0 or 1 asterisks - ${entryStr}}`);
71+
continue;
72+
}
73+
// Does it match?
74+
const escapedLeftPattern = vsCodeChromeDebugUtils.escapeRegexSpecialChars(leftPattern, '/*');
75+
const leftRegexSegment = escapedLeftPattern
76+
.replace(/\*/g, '(.*)')
77+
.replace(/\\\\/g, '/');
78+
const leftRegex = new RegExp(`^${leftRegexSegment}$`, 'i');
79+
const overridePatternMatches = forwardSlashSourcePath.match(leftRegex);
80+
81+
if (!overridePatternMatches) {
82+
continue;
83+
}
84+
// Grab the value of the wildcard from the match above, replace the wildcard in the
85+
// replacement pattern, and return the result.
86+
const wildcardValue = overridePatternMatches[1];
87+
88+
// ***************** CUSTOM CODE START *****************
89+
// handle linked files
90+
let mappedPath = path.isAbsolute(wildcardValue) ? wildcardValue : rightPattern.replace(/\*/g, wildcardValue);
91+
// ***************** CUSTOM CODE END *****************
92+
93+
mappedPath = path.join(mappedPath); // Fix any ..
94+
95+
// ***************** CUSTOM CODE START *****************
96+
// handle platform-specific files
97+
const { dir, name, ext } = path.parse(mappedPath);
98+
99+
const platformFileName = `${name}.${this.platform}${ext}`;
100+
const platformPath = path.join(dir, platformFileName);
101+
102+
if (vsCodeChromeDebugUtils.existsSync(platformPath)) {
103+
mappedPath = platformPath;
104+
}
105+
// ***************** CUSTOM CODE END *****************
106+
107+
if (isVSClient && leftPattern === 'webpack:///./*' && !vsCodeChromeDebugUtils.existsSync(mappedPath)) {
108+
// This is a workaround for a bug in ASP.NET debugging in VisualStudio because the wwwroot is not properly configured
109+
const pathFixingASPNETBug = path.join(rightPattern.replace(/\*/g, path.join('../ClientApp', wildcardValue)));
110+
111+
if (vsCodeChromeDebugUtils.existsSync(pathFixingASPNETBug)) {
112+
mappedPath = pathFixingASPNETBug;
113+
}
114+
}
115+
vsCodeChromeDebugLogger.log(`SourceMap: mapping ${sourcePath} => ${mappedPath}, via sourceMapPathOverrides entry - ${entryStr}`);
116+
117+
return mappedPath;
118+
}
119+
120+
return sourcePath;
121+
};
122+
123+
//--- end part copied from vscode-chrome-debug-core
124+
// tslint:enable
125+
}
122126
}

0 commit comments

Comments
 (0)