Skip to content

Commit a4f1ed9

Browse files
committed
DeOOPify VendorLibrariesBase
1 parent 94d916b commit a4f1ed9

6 files changed

Lines changed: 111 additions & 119 deletions

File tree

vscode-wpilib/src/dependencyView.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IExternalAPI } from './api';
33
import { localize as i18n } from './locale';
44
import { logger } from './logger';
55
import { IProjectInfo, ProjectInfoGatherer } from './projectinfo';
6-
import { IJsonDependency } from './shared/vendorlibrariesbase';
6+
import { getHomeDirDeps, IJsonDependency, installDependency } from './shared/vendorlibrariesbase';
77
import { VendorLibraries } from './vendorlibraries';
88
import { isNewerVersion } from './versions';
99

@@ -265,7 +265,7 @@ export class DependencyViewProvider implements vscode.WebviewViewProvider {
265265

266266
// If no conflict is found install otherwise show dialog
267267
if (!conflictdep) {
268-
const success = await this.vendorLibraries.installDependency(
268+
const success = await installDependency(
269269
dep,
270270
this.vendorLibraries.getWpVendorFolder(this.wp),
271271
true
@@ -294,7 +294,7 @@ export class DependencyViewProvider implements vscode.WebviewViewProvider {
294294
);
295295
const newDep = await this.listToDependency(reqDep);
296296
if (reqDep && newDep) {
297-
await this.vendorLibraries.installDependency(
297+
await installDependency(
298298
newDep,
299299
this.vendorLibraries.getWpVendorFolder(this.wp),
300300
true
@@ -405,7 +405,7 @@ export class DependencyViewProvider implements vscode.WebviewViewProvider {
405405

406406
// If no conflict is found, proceed with installation
407407
if (!conflictDep) {
408-
const success = await this.vendorLibraries.installDependency(
408+
const success = await installDependency(
409409
file,
410410
this.vendorLibraries.getWpVendorFolder(this.wp),
411411
true
@@ -419,7 +419,7 @@ export class DependencyViewProvider implements vscode.WebviewViewProvider {
419419
for (const required of file.requires) {
420420
try {
421421
const requiredDep = await this.vendorLibraries.getJsonDepURL(required.onlineUrl);
422-
await this.vendorLibraries.installDependency(
422+
await installDependency(
423423
requiredDep,
424424
this.vendorLibraries.getWpVendorFolder(this.wp),
425425
true
@@ -590,7 +590,7 @@ export class DependencyViewProvider implements vscode.WebviewViewProvider {
590590
this.onlineDeps = [];
591591
}
592592
}
593-
this.homeDeps = await this.vendorLibraries.getHomeDirDeps();
593+
this.homeDeps = await getHomeDirDeps();
594594
this.homeDeps.forEach((homedep) => {
595595
const depList: IJsonList = {
596596
path: i18n('ui', homedep.jsonUrl),

vscode-wpilib/src/extension.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,7 @@ async function handleAfterTrusted(
142142
try {
143143
vendorLibs = new VendorLibraries(externalApi);
144144
context.subscriptions.push(vendorLibs);
145-
await addVendorExamples(
146-
extensionResourceLocation,
147-
externalApi.getExampleTemplateAPI(),
148-
vendorLibs
149-
);
145+
await addVendorExamples(extensionResourceLocation, externalApi.getExampleTemplateAPI());
150146
} catch (err) {
151147
logger.error('error creating vendor lib utilities', err);
152148
creationError = true;

vscode-wpilib/src/projectinfo.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ export class ProjectInfoGatherer {
4141
private disposables: vscode.Disposable[] = [];
4242
private statusBar: vscode.StatusBarItem;
4343

44-
public constructor(
45-
vendorLibraries: VendorLibraries,
46-
externalApi: IExternalAPI
47-
) {
44+
public constructor(vendorLibraries: VendorLibraries, externalApi: IExternalAPI) {
4845
this.vendorLibraries = vendorLibraries;
4946
this.externalApi = externalApi;
5047

vscode-wpilib/src/shared/vendorexamples.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { logger } from '../logger';
1010
import { extensionContext } from '../utilities';
1111
import { generateCopyCpp, generateCopyJava } from './generator';
1212
import { getWPILibHomeDir } from './utilitiesapi';
13-
import { VendorLibrariesBase } from './vendorlibrariesbase';
13+
import { findForUUIDs, installDependency } from './vendorlibrariesbase';
1414

1515
interface IJsonExample {
1616
name: string;
@@ -41,8 +41,7 @@ function isJsonExample(arg: unknown): arg is IJsonExample {
4141

4242
export async function addVendorExamples(
4343
resourceRoot: string,
44-
core: IExampleTemplateAPI,
45-
vendorlibs: VendorLibrariesBase
44+
core: IExampleTemplateAPI
4645
): Promise<void> {
4746
const shimmedResourceRoot = path.join(resourceRoot, 'vendordeps');
4847
const storagePath = extensionContext.storagePath;
@@ -144,11 +143,11 @@ export async function addVendorExamples(
144143
}
145144

146145
// Install vendor dependencies
147-
const vendorFiles = await vendorlibs.findForUUIDs(ex.dependencies);
146+
const vendorFiles = await findForUUIDs(ex.dependencies);
148147
const vendorFolder = path.join(folderInto.fsPath, 'vendordeps');
149148

150149
for (const vendorFile of vendorFiles) {
151-
await vendorlibs.installDependency(vendorFile, vendorFolder, true);
150+
await installDependency(vendorFile, vendorFolder, true);
152151
}
153152
} catch (err) {
154153
logger.error('Example generation error: ', err);

vscode-wpilib/src/shared/vendorlibrariesbase.ts

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -39,108 +39,106 @@ export function isJsonDependency(arg: unknown): arg is IJsonDependency {
3939
);
4040
}
4141

42-
export class VendorLibrariesBase {
43-
public async findForUUIDs(uuid: string[]): Promise<IJsonDependency[]> {
44-
const homeDirDeps = await this.getHomeDirDeps();
45-
const foundDeps = homeDirDeps.filter((value) => {
46-
return uuid.indexOf(value.uuid) >= 0;
47-
});
48-
return foundDeps;
49-
}
42+
export async function findForUUIDs(uuid: string[]): Promise<IJsonDependency[]> {
43+
const homeDirDeps = await getHomeDirDeps();
44+
const foundDeps = homeDirDeps.filter((value) => {
45+
return uuid.indexOf(value.uuid) >= 0;
46+
});
47+
return foundDeps;
48+
}
5049

51-
public async installDependency(
52-
dep: IJsonDependency,
53-
url: string,
54-
override: boolean
55-
): Promise<boolean> {
50+
export async function installDependency(
51+
dep: IJsonDependency,
52+
url: string,
53+
override: boolean
54+
): Promise<boolean> {
55+
try {
5656
try {
57-
try {
58-
await access(url);
59-
} catch {
60-
// File doesn't exist, directly write file
61-
await mkdir(url, { recursive: true });
62-
await writeFile(path.join(url, dep.fileName), JSON.stringify(dep, null, 4));
63-
return true;
64-
}
65-
const files = await readdir(url);
66-
67-
for (const file of files) {
68-
const fullPath = path.join(url, file);
69-
const result = await this.readFile(fullPath);
70-
if (result && result.uuid === dep.uuid) {
71-
if (override) {
72-
await unlink(fullPath);
73-
break;
74-
} else {
75-
return false;
76-
}
77-
}
78-
}
79-
57+
await access(url);
58+
} catch {
59+
// File doesn't exist, directly write file
60+
await mkdir(url, { recursive: true });
8061
await writeFile(path.join(url, dep.fileName), JSON.stringify(dep, null, 4));
8162
return true;
82-
} catch (error) {
83-
logger.error(`Failed to install dependency ${dep.name}:`, error);
84-
return false;
8563
}
86-
}
64+
const files = await readdir(url);
65+
66+
for (const file of files) {
67+
const fullPath = path.join(url, file);
68+
const result = await parseVendordepJson(fullPath);
69+
if (result && result.uuid === dep.uuid) {
70+
if (override) {
71+
await unlink(fullPath);
72+
break;
73+
} else {
74+
return false;
75+
}
76+
}
77+
}
8778

88-
public getHomeDirDeps(): Promise<IJsonDependency[]> {
89-
return this.getDependencies(path.join(getWPILibHomeDir(), 'vendordeps'));
79+
await writeFile(path.join(url, dep.fileName), JSON.stringify(dep, null, 4));
80+
return true;
81+
} catch (error) {
82+
logger.error(`Failed to install dependency ${dep.name}:`, error);
83+
return false;
9084
}
85+
}
9186

92-
protected async readFile(file: string): Promise<IJsonDependency | undefined> {
93-
try {
94-
const jsonContents = await readFile(file, 'utf8');
95-
const dep = JSON.parse(jsonContents);
87+
export function getHomeDirDeps(): Promise<IJsonDependency[]> {
88+
return getDependencies(path.join(getWPILibHomeDir(), 'vendordeps'));
89+
}
9690

97-
if (isJsonDependency(dep)) {
98-
return dep;
99-
}
91+
export async function parseVendordepJson(file: string): Promise<IJsonDependency | undefined> {
92+
try {
93+
const jsonContents = await readFile(file, 'utf8');
94+
const dep = JSON.parse(jsonContents);
10095

101-
return undefined;
102-
} catch (err) {
103-
logger.warn('JSON parse error', err);
104-
return undefined;
96+
if (isJsonDependency(dep)) {
97+
return dep;
10598
}
99+
100+
return undefined;
101+
} catch (err) {
102+
logger.warn('JSON parse error', err);
103+
return undefined;
106104
}
105+
}
107106

108-
protected async getDependencies(dir: string): Promise<IJsonDependency[]> {
109-
try {
110-
const files = await readdir(dir);
107+
export async function getDependencies(dir: string): Promise<IJsonDependency[]> {
108+
try {
109+
const files = await readdir(dir);
111110

112-
const promises: Promise<IJsonDependency | undefined>[] = [];
111+
const promises: Promise<IJsonDependency | undefined>[] = [];
113112

114-
for (const file of files) {
115-
promises.push(this.readFile(path.join(dir, file)));
116-
}
113+
for (const file of files) {
114+
promises.push(parseVendordepJson(path.join(dir, file)));
115+
}
117116

118-
const results = await Promise.all(promises);
117+
const results = await Promise.all(promises);
119118

120-
return results.filter((x) => x !== undefined) as IJsonDependency[];
121-
} catch (err) {
122-
return [];
123-
}
119+
return results.filter((x) => x !== undefined) as IJsonDependency[];
120+
} catch (err) {
121+
return [];
124122
}
123+
}
125124

126-
protected async loadFileFromUrl(url: string): Promise<IJsonDependency> {
127-
try {
128-
const response = await fetch(url, {
129-
signal: AbortSignal.timeout(5000),
130-
});
131-
if (response.ok) {
132-
const json = await response.json();
133-
if (isJsonDependency(json)) {
134-
return json;
135-
} else {
136-
throw new Error('Incorrect JSON format');
137-
}
125+
export async function loadFileFromUrl(url: string): Promise<IJsonDependency> {
126+
try {
127+
const response = await fetch(url, {
128+
signal: AbortSignal.timeout(5000),
129+
});
130+
if (response.ok) {
131+
const json = await response.json();
132+
if (isJsonDependency(json)) {
133+
return json;
138134
} else {
139-
throw new Error(`Bad status ${response.status}`);
135+
throw new Error('Incorrect JSON format');
140136
}
141-
} catch (error) {
142-
logger.error(`Failed to load file from URL: ${url}`, error);
143-
throw error;
137+
} else {
138+
throw new Error(`Bad status ${response.status}`);
144139
}
140+
} catch (error) {
141+
logger.error(`Failed to load file from URL: ${url}`, error);
142+
throw error;
145143
}
146144
}

0 commit comments

Comments
 (0)