Skip to content

Commit

Permalink
Switch vendor example json to use folders, rather then embedding sour…
Browse files Browse the repository at this point in the history
…ce 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
  • Loading branch information
ThadHouse authored Jan 5, 2021
1 parent 3ac1341 commit 0b57381
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 38 deletions.
58 changes: 20 additions & 38 deletions vscode-wpilib/src/shared/vendorexamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,23 @@ import * as jsonc from 'jsonc-parser';
import * as path from 'path';
import { localize as i18n } from '../locale';
import { logger } from '../logger';
import { existsAsync, extensionContext, mkdirpAsync, readdirAsync, readFileAsync, writeFileAsync } from '../utilities';
import { existsAsync, extensionContext, statAsync, readdirAsync, readFileAsync } from '../utilities';
import * as vscode from '../vscodeshim';
import { IExampleTemplateAPI, IExampleTemplateCreator, IUtilitiesAPI } from '../wpilibapishim';
import { generateCopyCpp, generateCopyJava } from './generator';
import { VendorLibrariesBase } from './vendorlibrariesbase';

export interface IFile {
deployloc: string;
contents: string;
}

interface IJsonExample {
name: string;
description: string;
tags: string[];
gradlebase: string;
language: string;
language: string; // either "java" or "cpp"
commandversion: number;
mainclass?: string | undefined;
packagetoreplace?: string | undefined;
dependencies: string[];
files: IFile[];
foldername: string;
}

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

return jsonDep.name !== undefined && jsonDep.description !== undefined
&& jsonDep.gradlebase !== undefined && jsonDep.dependencies !== undefined
&& jsonDep.files !== undefined && jsonDep.language !== undefined;
&& jsonDep.foldername !== undefined && jsonDep.language !== undefined;
}

export async function addVendorExamples(resourceRoot: string, core: IExampleTemplateAPI, utilities: IUtilitiesAPI,
vendorlibs: VendorLibrariesBase): Promise<void> {
const shimmedResourceRoot = path.join(resourceRoot, 'vendordeps');
const storagePath = extensionContext.storagePath;
if (storagePath === undefined) {
return;
Expand All @@ -46,7 +44,11 @@ export async function addVendorExamples(resourceRoot: string, core: IExampleTemp
if (await existsAsync(exampleDir)) {
const files = await readdirAsync(exampleDir);
for (const file of files) {
const fileContents = await readFileAsync(path.join(exampleDir, file), 'utf8');
const filePath = path.join(exampleDir, file);
if ((await statAsync(filePath)).isDirectory()) {
continue;
}
const fileContents = await readFileAsync(filePath, 'utf8');
const parsed = jsonc.parse(fileContents);
if (Array.isArray(parsed)) {
for (const ex of parsed) {
Expand All @@ -68,43 +70,23 @@ export async function addVendorExamples(resourceRoot: string, core: IExampleTemp
async generate(folderInto: vscode.Uri): Promise<boolean> {
try {
if (ex.language === 'java') {
if (!await generateCopyJava(resourceRoot, async (copyPath, rootDir) => {
for (const copyFile of ex.files) {
const copyFilePath = path.join(copyPath, copyFile.deployloc);
const copyParent = path.dirname(copyFilePath);
await mkdirpAsync(copyParent);
await writeFileAsync(copyFilePath, copyFile.contents);
}
const vendorFiles = await vendorlibs.findForUUIDs(ex.dependencies);
for (const vendorFile of vendorFiles) {
await vendorlibs.installDependency(vendorFile, vendorlibs.getVendorFolder(rootDir), true);
}
return true;
},
if (!await generateCopyJava(shimmedResourceRoot, path.join(exampleDir, ex.foldername),
path.join(gradleBasePath, ex.gradlebase), folderInto.fsPath, 'frc.robot.' + ex.mainclass,
path.join('frc', 'robot'), false, false, ex.packagetoreplace)) {
path.join('frc', 'robot'), false, ex.commandversion !== 2, ex.packagetoreplace)) {
vscode.window.showErrorMessage(i18n('message', 'Cannot create into non empty folder'));
return false;
}
} else {
if (!await generateCopyCpp(resourceRoot, async (copyPath, rootDir) => {
for (const copyFile of ex.files) {
const copyFilePath = path.join(copyPath, copyFile.deployloc);
const copyParent = path.dirname(copyFilePath);
await mkdirpAsync(copyParent);
await writeFileAsync(copyFilePath, copyFile.contents);
}
const vendorFiles = await vendorlibs.findForUUIDs(ex.dependencies);
for (const vendorFile of vendorFiles) {
await vendorlibs.installDependency(vendorFile, vendorlibs.getVendorFolder(rootDir), true);
}
return true;
},
path.join(gradleBasePath, ex.gradlebase), folderInto.fsPath, false, false, false)) {
if (!await generateCopyCpp(shimmedResourceRoot, path.join(exampleDir, ex.foldername),
path.join(gradleBasePath, ex.gradlebase), folderInto.fsPath, false, false, ex.commandversion !== 2)) {
vscode.window.showErrorMessage(i18n('message', 'Cannot create into non empty folder'));
return false;
}
}
const vendorFiles = await vendorlibs.findForUUIDs(ex.dependencies);
for (const vendorFile of vendorFiles) {
await vendorlibs.installDependency(vendorFile, vendorlibs.getVendorFolder(folderInto.fsPath), true);
}
} catch (err) {
logger.error('Example generation error: ', err);
return false;
Expand Down
2 changes: 2 additions & 0 deletions vscode-wpilib/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export async function getPackageName(): Promise<string | undefined> {
return packageName;
}

export const statAsync = util.promisify(fs.stat);

export const readFileAsync = util.promisify(fs.readFile);

export const writeFileAsync = util.promisify(fs.writeFile);
Expand Down
2 changes: 2 additions & 0 deletions wpilib-utility-standalone/src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function getIsWindows(): boolean {
return nodePlatform === 'win32';
}

export const statAsync = util.promisify(fs.stat);

export const existsAsync = util.promisify(fs.exists);

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

0 comments on commit 0b57381

Please sign in to comment.