Skip to content

Commit 473d88e

Browse files
committed
Abstract the util functions into CoreClrDebugUtil class
1 parent 26de0cd commit 473d88e

File tree

3 files changed

+110
-106
lines changed

3 files changed

+110
-106
lines changed

src/coreclr-debug/main.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ import * as vscode from 'vscode';
88
import * as child_process from 'child_process';
99
import * as fs from 'fs';
1010
import * as path from 'path';
11-
import * as util from './util';
1211
import TelemetryReporter from 'vscode-extension-telemetry';
12+
import CoreClrDebugUtil from './util'
1313

1414
let _channel: vscode.OutputChannel;
1515
let _installLog: NodeJS.WritableStream;
1616
let _reporter: TelemetryReporter; // Telemetry reporter
17+
let _util: CoreClrDebugUtil;
1718

1819
export function activate(context: vscode.ExtensionContext, reporter: TelemetryReporter) {
19-
util.setExtensionDir(context.extensionPath);
20+
_util = new CoreClrDebugUtil(context.extensionPath);
2021

21-
if (util.installCompleteExists()) {
22+
if (CoreClrDebugUtil.existsSync(_util.installCompleteFilePath())) {
2223
console.log('.NET Core Debugger tools already installed');
2324
return;
2425
}
@@ -40,7 +41,7 @@ export function activate(context: vscode.ExtensionContext, reporter: TelemetryRe
4041
_channel = vscode.window.createOutputChannel('coreclr-debug');
4142

4243
// Create our log file and override _channel.append to also output to the log
43-
_installLog = fs.createWriteStream(util.installLogPath());
44+
_installLog = fs.createWriteStream(_util.installLogPath());
4445
(function() {
4546
let proxied = _channel.append;
4647
_channel.append = function(val: string) {
@@ -56,13 +57,13 @@ export function activate(context: vscode.ExtensionContext, reporter: TelemetryRe
5657

5758
writeInstallBeginFile().then(function() {
5859
installStage = 'dotnetRestore'
59-
return spawnChildProcess('dotnet', ['--verbose', 'restore', '--configfile', 'NuGet.config'], _channel, util.coreClrDebugDir())
60+
return spawnChildProcess('dotnet', ['--verbose', 'restore', '--configfile', 'NuGet.config'], _channel, _util.coreClrDebugDir())
6061
}).then(function() {
6162
installStage = "dotnetPublish";
62-
return spawnChildProcess('dotnet', ['--verbose', 'publish', '-o', util.debugAdapterDir()], _channel, util.coreClrDebugDir());
63+
return spawnChildProcess('dotnet', ['--verbose', 'publish', '-o', _util.debugAdapterDir()], _channel, _util.coreClrDebugDir());
6364
}).then(function() {
6465
installStage = "ensureAd7";
65-
return ensureAd7EngineExists(_channel, util.debugAdapterDir());
66+
return ensureAd7EngineExists(_channel, _util.debugAdapterDir());
6667
}).then(function() {
6768
installStage = "additionalTasks";
6869
let promises: Promise<void>[] = [];
@@ -113,27 +114,27 @@ function logTelemetry(eventName: string, properties?: {[prop: string]: string})
113114
}
114115

115116
function rewriteManifest() : void {
116-
const manifestPath = path.join(util.extensionDir(), 'package.json');
117+
const manifestPath = path.join(_util.extensionDir(), 'package.json');
117118
let manifestString = fs.readFileSync(manifestPath, 'utf8');
118119
let manifestObject = JSON.parse(manifestString);
119120
manifestObject.contributes.debuggers[0].runtime = '';
120-
manifestObject.contributes.debuggers[0].program = './coreclr-debug/debugAdapters/OpenDebugAD7' + util.getPlatformExeExtension();
121+
manifestObject.contributes.debuggers[0].program = './coreclr-debug/debugAdapters/OpenDebugAD7' + CoreClrDebugUtil.getPlatformExeExtension();
121122
manifestString = JSON.stringify(manifestObject, null, 2);
122123
fs.writeFileSync(manifestPath, manifestString);
123124
}
124125

125126
function writeInstallBeginFile() : Promise<void> {
126-
return writeEmptyFile(util.installBeginFilePath());
127+
return writeEmptyFile(_util.installBeginFilePath());
127128
}
128129

129130
function deleteInstallBeginFile() {
130-
if (util.existsSync(util.installBeginFilePath())) {
131-
fs.unlinkSync(util.installBeginFilePath());
131+
if (CoreClrDebugUtil.existsSync(_util.installBeginFilePath())) {
132+
fs.unlinkSync(_util.installBeginFilePath());
132133
}
133134
}
134135

135136
function writeCompletionFile() : Promise<void> {
136-
return writeEmptyFile(util.installCompleteFilePath());
137+
return writeEmptyFile(_util.installCompleteFilePath());
137138
}
138139

139140
function writeEmptyFile(path: string) : Promise<void> {
@@ -149,11 +150,11 @@ function writeEmptyFile(path: string) : Promise<void> {
149150
}
150151

151152
function renameDummyEntrypoint() : Promise<void> {
152-
let src = path.join(util.debugAdapterDir(), 'dummy');
153-
let dest = path.join(util.debugAdapterDir(), 'OpenDebugAD7');
153+
let src = path.join(_util.debugAdapterDir(), 'dummy');
154+
let dest = path.join(_util.debugAdapterDir(), 'OpenDebugAD7');
154155

155-
src += util.getPlatformExeExtension();
156-
dest += util.getPlatformExeExtension();
156+
src += CoreClrDebugUtil.getPlatformExeExtension();
157+
dest += CoreClrDebugUtil.getPlatformExeExtension();
157158

158159
const promise = new Promise<void>(function(resolve, reject) {
159160
fs.rename(src, dest, function(err) {
@@ -170,9 +171,9 @@ function renameDummyEntrypoint() : Promise<void> {
170171

171172
function removeLibCoreClrTraceProvider() : Promise<void>
172173
{
173-
const filePath = path.join(util.debugAdapterDir(), 'libcoreclrtraceptprovider' + util.getPlatformLibExtension());
174+
const filePath = path.join(_util.debugAdapterDir(), 'libcoreclrtraceptprovider' + CoreClrDebugUtil.getPlatformLibExtension());
174175

175-
if (!util.existsSync(filePath)) {
176+
if (!CoreClrDebugUtil.existsSync(filePath)) {
176177
return Promise.resolve();
177178
} else {
178179
return new Promise<void>(function(resolve, reject) {
@@ -209,7 +210,7 @@ function isOnPath(command : string) : boolean {
209210
}
210211

211212
const segmentPath = path.join(segment, fileName);
212-
if (util.existsSync(segmentPath)) {
213+
if (CoreClrDebugUtil.existsSync(segmentPath)) {
213214
return true;
214215
}
215216
}

src/coreclr-debug/proxy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import * as path from 'path';
88
import { DebugProtocol } from 'vscode-debugprotocol';
99
import * as child_process from 'child_process';
10-
import * as util from './util';
10+
import CoreClrDebugUtil from './util';
1111

1212
class ProxyErrorResponse implements DebugProtocol.ErrorResponse {
1313
public body: { error?: DebugProtocol.Message };
@@ -39,10 +39,10 @@ function serializeProtocolEvent(message: DebugProtocol.ProtocolMessage): string
3939
// This proxy will still be called and launch OpenDebugAD7 as a child process.
4040
// During subsequent code sessions, the rewritten manifest will be loaded and this proxy will no longer be called.
4141
function proxy() {
42-
util.setExtensionDir(path.resolve(__dirname, '../../'));
42+
let util = new CoreClrDebugUtil(path.resolve(__dirname, '../../'));
4343

44-
if (!util.installCompleteExists()) {
45-
if (util.existsSync(util.installBeginFilePath())) {
44+
if (!CoreClrDebugUtil.existsSync(util.installCompleteFilePath())) {
45+
if (CoreClrDebugUtil.existsSync(util.installBeginFilePath())) {
4646
process.stdout.write(serializeProtocolEvent(new ProxyErrorResponse('The .NET Core Debugger has not finished installing. See Status Bar for details.')));
4747
} else {
4848
process.stdout.write(serializeProtocolEvent(new ProxyErrorResponse('Run \'Debugger: Install .NET Core Debugger\' command or open a .NET project directory to download the .NET Core Debugger')));
@@ -51,7 +51,7 @@ function proxy() {
5151
else
5252
{
5353
new Promise<void>(function(resolve, reject) {
54-
let processPath = path.join(util.debugAdapterDir(), "OpenDebugAD7" + util.getPlatformExeExtension());
54+
let processPath = path.join(util.debugAdapterDir(), "OpenDebugAD7" + CoreClrDebugUtil.getPlatformExeExtension());
5555
let args = process.argv.slice(2);
5656

5757
// do not explicitly set a current working dir

src/coreclr-debug/util.ts

Lines changed: 84 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,103 +15,106 @@ let _installLogPath: string = '';
1515
let _installBeginFilePath: string = '';
1616
let _installCompleteFilePath: string = '';
1717

18-
export function setExtensionDir(extensionDir: string) {
19-
// ensure that this path actually exists and looks like the root of the extension
20-
if (!existsSync(path.join(extensionDir, 'package.json'))) {
21-
throw new Error(`Cannot set extension path to ${extensionDir}`);
18+
export default class CoreClrDebugUtil
19+
{
20+
private _extensionDir: string = '';
21+
private _coreClrDebugDir: string = '';
22+
private _debugAdapterDir: string = '';
23+
private _installLogPath: string = '';
24+
private _installBeginFilePath: string = '';
25+
private _installCompleteFilePath: string = '';
26+
27+
constructor(extensionDir) {
28+
_extensionDir = extensionDir;
29+
_coreClrDebugDir = path.join(_extensionDir, 'coreclr-debug');
30+
_debugAdapterDir = path.join(_coreClrDebugDir, 'debugAdapters');
31+
_installLogPath = path.join(_coreClrDebugDir, 'install.log');
32+
_installBeginFilePath = path.join(_coreClrDebugDir, 'install.begin');
33+
_installCompleteFilePath = path.join(_debugAdapterDir, 'install.complete');
2234
}
23-
_extensionDir = extensionDir;
24-
_coreClrDebugDir = path.join(_extensionDir, 'coreclr-debug');
25-
_debugAdapterDir = path.join(_coreClrDebugDir, 'debugAdapters');
26-
_installLogPath = path.join(_coreClrDebugDir, 'install.log');
27-
_installBeginFilePath = path.join(_coreClrDebugDir, 'install.begin');
28-
_installCompleteFilePath = path.join(_debugAdapterDir, 'install.complete');
29-
}
30-
31-
export function extensionDir(): string {
32-
if (_extensionDir === '')
33-
{
34-
throw new Error('Failed to set extension directory');
35+
36+
extensionDir(): string {
37+
if (_extensionDir === '')
38+
{
39+
throw new Error('Failed to set extension directory');
40+
}
41+
return _extensionDir;
3542
}
36-
return _extensionDir;
37-
}
3843

39-
export function coreClrDebugDir(): string {
40-
if (_coreClrDebugDir === '') {
41-
throw new Error('Failed to set coreclrdebug directory');
44+
coreClrDebugDir(): string {
45+
if (_coreClrDebugDir === '') {
46+
throw new Error('Failed to set coreclrdebug directory');
47+
}
48+
return _coreClrDebugDir;
4249
}
43-
return _coreClrDebugDir;
44-
}
4550

46-
export function debugAdapterDir(): string {
47-
if (_debugAdapterDir === '') {
48-
throw new Error('Failed to set debugadpter directory');
51+
debugAdapterDir(): string {
52+
if (_debugAdapterDir === '') {
53+
throw new Error('Failed to set debugadpter directory');
54+
}
55+
return _debugAdapterDir;
4956
}
50-
return _debugAdapterDir;
51-
}
5257

53-
export function installLogPath(): string {
54-
if (_installLogPath === '') {
55-
throw new Error('Failed to set install log path');
58+
installLogPath(): string {
59+
if (_installLogPath === '') {
60+
throw new Error('Failed to set install log path');
61+
}
62+
return _installLogPath;
5663
}
57-
return _installLogPath;
58-
}
5964

60-
export function installBeginFilePath(): string {
61-
if (_installBeginFilePath === '') {
62-
throw new Error('Failed to set install begin file path');
65+
installBeginFilePath(): string {
66+
if (_installBeginFilePath === '') {
67+
throw new Error('Failed to set install begin file path');
68+
}
69+
return _installBeginFilePath;
6370
}
64-
return _installBeginFilePath;
65-
}
6671

67-
export function installCompleteFilePath(): string {
68-
if (_installCompleteFilePath === '')
69-
{
70-
throw new Error('Failed to set install complete file path');
72+
installCompleteFilePath(): string {
73+
if (_installCompleteFilePath === '')
74+
{
75+
throw new Error('Failed to set install complete file path');
76+
}
77+
return _installCompleteFilePath;
7178
}
72-
return _installCompleteFilePath;
73-
}
74-
75-
export function installCompleteExists() : boolean {
76-
return existsSync(installCompleteFilePath());
77-
}
78-
79-
export function existsSync(path: string) : boolean {
80-
try {
81-
fs.accessSync(path, fs.F_OK);
82-
return true;
83-
} catch (err) {
84-
if (err.code === 'ENOENT') {
85-
return false;
86-
} else {
87-
throw Error(err.code);
79+
80+
static existsSync(path: string) : boolean {
81+
try {
82+
fs.accessSync(path, fs.F_OK);
83+
return true;
84+
} catch (err) {
85+
if (err.code === 'ENOENT') {
86+
return false;
87+
} else {
88+
throw Error(err.code);
89+
}
8890
}
8991
}
90-
}
92+
93+
static getPlatformExeExtension() : string {
94+
if (process.platform === 'win32') {
95+
return '.exe';
96+
}
9197

92-
export function getPlatformExeExtension() : string {
93-
if (process.platform === 'win32') {
94-
return '.exe';
98+
return '';
9599
}
96100

97-
return '';
98-
}
99-
100-
export function getPlatformLibExtension() : string {
101-
switch (process.platform) {
102-
case 'win32':
103-
return '.dll';
104-
case 'darwin':
105-
return '.dylib';
106-
case 'linux':
107-
return '.so';
108-
default:
109-
throw Error('Unsupported platform ' + process.platform);
101+
static getPlatformLibExtension() : string {
102+
switch (process.platform) {
103+
case 'win32':
104+
return '.dll';
105+
case 'darwin':
106+
return '.dylib';
107+
case 'linux':
108+
return '.so';
109+
default:
110+
throw Error('Unsupported platform ' + process.platform);
111+
}
112+
}
113+
114+
115+
/** Used for diagnostics only */
116+
logToFile(message: string): void {
117+
let logFolder = path.resolve(this.coreClrDebugDir(), "extension.log");
118+
fs.writeFileSync(logFolder, `${message}${os.EOL}`, { flag: 'a' });
110119
}
111-
}
112-
113-
/** Used for diagnostics only */
114-
export function logToFile(message: string): void {
115-
let logFolder = path.resolve(coreClrDebugDir(), "extension.log");
116-
fs.writeFileSync(logFolder, `${message}${os.EOL}`, { flag: 'a' });
117120
}

0 commit comments

Comments
 (0)