Skip to content

Commit 0b57381

Browse files
authored
Switch vendor example json to use folders, rather then embedding source in files, (#429)
* Switch vendor example json to use folders, rather then embedding source in files, Its much easier to use, and much more realistic about what should be happening * Fix lint * Skip directories, fix resource root
1 parent 3ac1341 commit 0b57381

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

vscode-wpilib/src/shared/vendorexamples.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,23 @@ import * as jsonc from 'jsonc-parser';
44
import * as path from 'path';
55
import { localize as i18n } from '../locale';
66
import { logger } from '../logger';
7-
import { existsAsync, extensionContext, mkdirpAsync, readdirAsync, readFileAsync, writeFileAsync } from '../utilities';
7+
import { existsAsync, extensionContext, statAsync, readdirAsync, readFileAsync } from '../utilities';
88
import * as vscode from '../vscodeshim';
99
import { IExampleTemplateAPI, IExampleTemplateCreator, IUtilitiesAPI } from '../wpilibapishim';
1010
import { generateCopyCpp, generateCopyJava } from './generator';
1111
import { VendorLibrariesBase } from './vendorlibrariesbase';
1212

13-
export interface IFile {
14-
deployloc: string;
15-
contents: string;
16-
}
17-
1813
interface IJsonExample {
1914
name: string;
2015
description: string;
16+
tags: string[];
2117
gradlebase: string;
22-
language: string;
18+
language: string; // either "java" or "cpp"
19+
commandversion: number;
2320
mainclass?: string | undefined;
2421
packagetoreplace?: string | undefined;
2522
dependencies: string[];
26-
files: IFile[];
23+
foldername: string;
2724
}
2825

2926
// tslint:disable-next-line:no-any
@@ -32,11 +29,12 @@ function isJsonExample(arg: any): arg is IJsonExample {
3229

3330
return jsonDep.name !== undefined && jsonDep.description !== undefined
3431
&& jsonDep.gradlebase !== undefined && jsonDep.dependencies !== undefined
35-
&& jsonDep.files !== undefined && jsonDep.language !== undefined;
32+
&& jsonDep.foldername !== undefined && jsonDep.language !== undefined;
3633
}
3734

3835
export async function addVendorExamples(resourceRoot: string, core: IExampleTemplateAPI, utilities: IUtilitiesAPI,
3936
vendorlibs: VendorLibrariesBase): Promise<void> {
37+
const shimmedResourceRoot = path.join(resourceRoot, 'vendordeps');
4038
const storagePath = extensionContext.storagePath;
4139
if (storagePath === undefined) {
4240
return;
@@ -46,7 +44,11 @@ export async function addVendorExamples(resourceRoot: string, core: IExampleTemp
4644
if (await existsAsync(exampleDir)) {
4745
const files = await readdirAsync(exampleDir);
4846
for (const file of files) {
49-
const fileContents = await readFileAsync(path.join(exampleDir, file), 'utf8');
47+
const filePath = path.join(exampleDir, file);
48+
if ((await statAsync(filePath)).isDirectory()) {
49+
continue;
50+
}
51+
const fileContents = await readFileAsync(filePath, 'utf8');
5052
const parsed = jsonc.parse(fileContents);
5153
if (Array.isArray(parsed)) {
5254
for (const ex of parsed) {
@@ -68,43 +70,23 @@ export async function addVendorExamples(resourceRoot: string, core: IExampleTemp
6870
async generate(folderInto: vscode.Uri): Promise<boolean> {
6971
try {
7072
if (ex.language === 'java') {
71-
if (!await generateCopyJava(resourceRoot, async (copyPath, rootDir) => {
72-
for (const copyFile of ex.files) {
73-
const copyFilePath = path.join(copyPath, copyFile.deployloc);
74-
const copyParent = path.dirname(copyFilePath);
75-
await mkdirpAsync(copyParent);
76-
await writeFileAsync(copyFilePath, copyFile.contents);
77-
}
78-
const vendorFiles = await vendorlibs.findForUUIDs(ex.dependencies);
79-
for (const vendorFile of vendorFiles) {
80-
await vendorlibs.installDependency(vendorFile, vendorlibs.getVendorFolder(rootDir), true);
81-
}
82-
return true;
83-
},
73+
if (!await generateCopyJava(shimmedResourceRoot, path.join(exampleDir, ex.foldername),
8474
path.join(gradleBasePath, ex.gradlebase), folderInto.fsPath, 'frc.robot.' + ex.mainclass,
85-
path.join('frc', 'robot'), false, false, ex.packagetoreplace)) {
75+
path.join('frc', 'robot'), false, ex.commandversion !== 2, ex.packagetoreplace)) {
8676
vscode.window.showErrorMessage(i18n('message', 'Cannot create into non empty folder'));
8777
return false;
8878
}
8979
} else {
90-
if (!await generateCopyCpp(resourceRoot, async (copyPath, rootDir) => {
91-
for (const copyFile of ex.files) {
92-
const copyFilePath = path.join(copyPath, copyFile.deployloc);
93-
const copyParent = path.dirname(copyFilePath);
94-
await mkdirpAsync(copyParent);
95-
await writeFileAsync(copyFilePath, copyFile.contents);
96-
}
97-
const vendorFiles = await vendorlibs.findForUUIDs(ex.dependencies);
98-
for (const vendorFile of vendorFiles) {
99-
await vendorlibs.installDependency(vendorFile, vendorlibs.getVendorFolder(rootDir), true);
100-
}
101-
return true;
102-
},
103-
path.join(gradleBasePath, ex.gradlebase), folderInto.fsPath, false, false, false)) {
80+
if (!await generateCopyCpp(shimmedResourceRoot, path.join(exampleDir, ex.foldername),
81+
path.join(gradleBasePath, ex.gradlebase), folderInto.fsPath, false, false, ex.commandversion !== 2)) {
10482
vscode.window.showErrorMessage(i18n('message', 'Cannot create into non empty folder'));
10583
return false;
10684
}
10785
}
86+
const vendorFiles = await vendorlibs.findForUUIDs(ex.dependencies);
87+
for (const vendorFile of vendorFiles) {
88+
await vendorlibs.installDependency(vendorFile, vendorlibs.getVendorFolder(folderInto.fsPath), true);
89+
}
10890
} catch (err) {
10991
logger.error('Example generation error: ', err);
11092
return false;

vscode-wpilib/src/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ export async function getPackageName(): Promise<string | undefined> {
5353
return packageName;
5454
}
5555

56+
export const statAsync = util.promisify(fs.stat);
57+
5658
export const readFileAsync = util.promisify(fs.readFile);
5759

5860
export const writeFileAsync = util.promisify(fs.writeFile);

wpilib-utility-standalone/src/utilities.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ export function getIsWindows(): boolean {
1616
return nodePlatform === 'win32';
1717
}
1818

19+
export const statAsync = util.promisify(fs.stat);
20+
1921
export const existsAsync = util.promisify(fs.exists);
2022

2123
export const copyFileAsync = util.promisify(fs.copyFile);

0 commit comments

Comments
 (0)