Skip to content

Commit be9d972

Browse files
committed
refactor: paths to installed extensions into a separate method
- Added new `ExtensionData::setExtensionDiscoveryPaths` method to set the paths to installed extensions, and added it's method call to the constructor. - Moved the setting of the extension paths from `setExtensionData` into the new `setExtensionDiscoveryPaths` method. - Added new `ExtensionPaths` interface and moved all the related properties from the `ExtensionMetaData` interface into the new interface. - Added new `ExtensionData::extensionDiscoveryPaths` property to hold the Map of all the paths, and uses the `ExtensionPaths` interface. - Changed references to the `extensionData` property in `setExtensionDiscoveryPaths` method to use the new `extensionDiscoveryPaths` property. - Added `getExtensionDiscoveryPath` and `getAllExtensionDiscoveryPaths` methods to get a specific extension path by key or get all paths, respectively. - Changed all method calls to `get` extension paths in `Configuration` class to use the `getExtensionDiscoveryPath` method; and added the `getAllExtensionDiscoveryPaths` method call in the constructor.
1 parent 07def7e commit be9d972

3 files changed

Lines changed: 60 additions & 18 deletions

File tree

src/configuration.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class Configuration {
7676
public constructor() {
7777
// Always output extension information to channel on activate.
7878
logger.debug(`Extension details:`, this.extensionData.getAll());
79+
logger.debug(`Extension Discovery Paths:`, this.extensionData.getAllExtensionDiscoveryPaths());
7980

8081
this.findAllLanguageConfigFilePaths();
8182
this.setLanguageConfigDefinitions();
@@ -309,8 +310,8 @@ export class Configuration {
309310
// If running in WSL...
310311
if (isWsl) {
311312
// Get the Windows user and built-in extensions paths.
312-
const windowsUserExtensionsPath = this.extensionData.get("WindowsUserExtensionsPathFromWsl");
313-
const windowsBuiltInExtensionsPath = this.extensionData.get("WindowsBuiltInExtensionsPathFromWsl");
313+
const windowsUserExtensionsPath = this.extensionData.getExtensionDiscoveryPath("WindowsUserExtensionsPathFromWsl");
314+
const windowsBuiltInExtensionsPath = this.extensionData.getExtensionDiscoveryPath("WindowsBuiltInExtensionsPathFromWsl");
314315

315316
// Read the paths and create arrays of the extensions.
316317
const windowsBuiltInExtensions = this.readExtensionsFromDirectory(windowsBuiltInExtensionsPath);
@@ -320,8 +321,8 @@ export class Configuration {
320321
extensions.push(...windowsBuiltInExtensions, ...windowsUserExtensions);
321322
}
322323

323-
const userExtensionsPath = this.extensionData.get("userExtensionsPath");
324-
const builtInExtensionsPath = this.extensionData.get("builtInExtensionsPath");
324+
const userExtensionsPath = this.extensionData.getExtensionDiscoveryPath("userExtensionsPath");
325+
const builtInExtensionsPath = this.extensionData.getExtensionDiscoveryPath("builtInExtensionsPath");
325326

326327
// Read the paths and create arrays of the extensions.
327328
const userExtensions = this.readExtensionsFromDirectory(userExtensionsPath);
@@ -971,25 +972,25 @@ export class Configuration {
971972
private logDebugInfo() {
972973
// The path to the built-in extensions. The env variable changes when on WSL.
973974
// So we can use it for both Windows and WSL.
974-
const builtInExtensionsPath = this.extensionData.get("builtInExtensionsPath");
975+
const builtInExtensionsPath = this.extensionData.getExtensionDiscoveryPath("builtInExtensionsPath");
975976

976977
let extensionsPaths = {};
977978

978979
if (isWsl) {
979980
// Get the Windows user and built-in extensions paths.
980-
const windowsUserExtensionsPath = this.extensionData.get("WindowsUserExtensionsPathFromWsl");
981-
const windowsBuiltInExtensionsPath = this.extensionData.get("WindowsBuiltInExtensionsPathFromWsl");
981+
const windowsUserExtensionsPath = this.extensionData.getExtensionDiscoveryPath("WindowsUserExtensionsPathFromWsl");
982+
const windowsBuiltInExtensionsPath = this.extensionData.getExtensionDiscoveryPath("WindowsBuiltInExtensionsPathFromWsl");
982983

983984
extensionsPaths = {
984985
"Windows-installed Built-in Extensions Path": windowsBuiltInExtensionsPath,
985986
"Windows-installed User Extensions Path": windowsUserExtensionsPath,
986987
"WSL-installed Built-in Extensions Path": builtInExtensionsPath,
987-
"WSL-installed User Extensions Path": this.extensionData.get("userExtensionsPath"),
988+
"WSL-installed User Extensions Path": this.extensionData.getExtensionDiscoveryPath("userExtensionsPath"),
988989
};
989990
} else {
990991
extensionsPaths = {
991992
"Built-in Extensions Path": builtInExtensionsPath,
992-
"User Extensions Path": this.extensionData.get("userExtensionsPath"),
993+
"User Extensions Path": this.extensionData.getExtensionDiscoveryPath("userExtensionsPath"),
993994
};
994995
}
995996

src/extensionData.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import isWsl from "is-wsl";
44
import {IPackageJson} from "package-json-type";
55

66
import {readJsonFile} from "./utils";
7-
import {ExtensionMetaData} from "./interfaces/extensionMetaData";
7+
import {ExtensionMetaData, ExtensionPaths} from "./interfaces/extensionMetaData";
88

99
export class ExtensionData {
1010
/**
@@ -14,6 +14,14 @@ export class ExtensionData {
1414
*/
1515
private extensionData = new Map<keyof ExtensionMetaData, string>();
1616

17+
/**
18+
* Extension discovery paths in the form of a key:value Map object.
19+
*
20+
* @type {Map<keyof ExtensionPaths, string>}
21+
*/
22+
private extensionDiscoveryPaths = new Map<keyof ExtensionPaths, string>();
23+
24+
1725
/**
1826
* The package.json data for this extension.
1927
*
@@ -24,6 +32,7 @@ export class ExtensionData {
2432
public constructor() {
2533
this.packageJsonData = this.getExtensionPackageJsonData();
2634
this.setExtensionData();
35+
this.setExtensionDiscoveryPaths();
2736
}
2837

2938
/**
@@ -53,26 +62,31 @@ export class ExtensionData {
5362
* Set the extension data into the extensionData Map.
5463
*/
5564
private setExtensionData() {
56-
// The path to the user extensions.
57-
const userExtensionsPath = isWsl
58-
? path.join(vscode.env.appRoot, "../../", "extensions")
59-
: path.join(this.packageJsonData.extensionPath, "../");
6065

6166
// Set each key-value pair directly into the Map
6267
this.extensionData.set("id", this.packageJsonData.id);
6368
this.extensionData.set("name", this.packageJsonData.name);
6469
this.extensionData.set("namespace", this.packageJsonData.contributes.configuration.namespace);
6570
this.extensionData.set("displayName", this.packageJsonData.displayName);
6671
this.extensionData.set("version", this.packageJsonData.version);
67-
this.extensionData.set("userExtensionsPath", userExtensionsPath);
72+
}
73+
74+
private setExtensionDiscoveryPaths() {
75+
// The path to the user extensions.
76+
// const userExtensionsPath = isWsl ? path.join(vscode.env.appRoot, "../../", "extensions") : path.join(this.extensionPath, "../");
77+
78+
//TODO: REMOVE THIS BEFORE RELEASE. FOR TESTING ONLY.
79+
const userExtensionsPath = isWsl ? path.join(vscode.env.appRoot, "../../", "extensions") : "C:\\Users\\Stuart\\.vscode\\extensions";
80+
81+
this.extensionDiscoveryPaths.set("userExtensionsPath", userExtensionsPath);
6882
// The path to the built-in extensions.
6983
// This env variable changes when on WSL to it's WSL-built-in extensions path.
70-
this.extensionData.set("builtInExtensionsPath", path.join(vscode.env.appRoot, "extensions"));
84+
this.extensionDiscoveryPaths.set("builtInExtensionsPath", path.join(vscode.env.appRoot, "extensions"));
7185

7286
// Only set these if running in WSL
7387
if (isWsl) {
74-
this.extensionData.set("WindowsUserExtensionsPathFromWsl", path.dirname(process.env.VSCODE_WSL_EXT_LOCATION!));
75-
this.extensionData.set("WindowsBuiltInExtensionsPathFromWsl", path.join(process.env.VSCODE_CWD!, "resources/app/extensions"));
88+
this.extensionDiscoveryPaths.set("WindowsUserExtensionsPathFromWsl", path.dirname(process.env.VSCODE_WSL_EXT_LOCATION!));
89+
this.extensionDiscoveryPaths.set("WindowsBuiltInExtensionsPathFromWsl", path.join(process.env.VSCODE_CWD!, "resources/app/extensions"));
7690
}
7791
}
7892

@@ -95,4 +109,24 @@ export class ExtensionData {
95109
public getAll(): ReadonlyMap<keyof ExtensionMetaData, string> {
96110
return this.extensionData;
97111
}
112+
113+
/**
114+
* Get the extension discovery paths by a specified key.
115+
*
116+
* @param {K} key The key of the specific path to get.
117+
*
118+
* @returns {ExtensionPaths[K] | undefined} The value of the extension detail, or undefined if the key does not exist.
119+
*/
120+
public getExtensionDiscoveryPath<K extends keyof ExtensionPaths>(key: K): ExtensionPaths[K] | undefined {
121+
return this.extensionDiscoveryPaths.get(key) as ExtensionPaths[K] | undefined;
122+
}
123+
124+
/**
125+
* Get all extension discovery paths.
126+
*
127+
* @returns {ReadonlyMap<keyof ExtensionPaths, string>} A read-only Map containing all extension discovery paths.
128+
*/
129+
public getAllExtensionDiscoveryPaths(): ReadonlyMap<keyof ExtensionPaths, string> {
130+
return this.extensionDiscoveryPaths;
131+
}
98132
}

src/interfaces/extensionMetaData.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ export interface ExtensionMetaData {
3030
* Directly from package.json "version" key.
3131
*/
3232
version: string;
33+
34+
}
35+
36+
/**
37+
* Extension discovery paths configuration for this extension
38+
*/
39+
export interface ExtensionPaths {
3340
/**
3441
* The path to the user extensions.
3542
*/

0 commit comments

Comments
 (0)