Skip to content

Commit

Permalink
Delete smart contract packages (resolves IBM-Blockchain#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakeeyturner committed Sep 13, 2018
1 parent 8235c7a commit 0e71168
Show file tree
Hide file tree
Showing 22 changed files with 686 additions and 449 deletions.
754 changes: 382 additions & 372 deletions client/package.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import * as vscode from 'vscode';
import { ParsedCertificate } from '../fabric/ParsedCertificate';
import { FabricRuntimeRegistry } from '../fabric/FabricRuntimeRegistry';
import { FabricRuntimeRegistryEntry } from '../fabric/FabricRuntimeRegistryEntry';
import { PackageRegistryManager } from '../explorer/packages/PackageRegistryManager';
import { PackageRegistryEntry } from '../explorer/packages/PackageRegistryEntry';

export class CommandsUtil {
export class UserInputUtil {

static readonly ADD_TO_WORKSPACE = 'Add to workspace';
static readonly OPEN_IN_CURRENT_WINDOW = 'Open in current window';
Expand Down Expand Up @@ -81,6 +83,25 @@ export class CommandsUtil {
return vscode.window.showQuickPick(runtimeNames, quickPickOptions);
}

static async showSmartContractPackagesQuickPickBox(prompt: string): Promise<string | undefined> {
const packageRegistryManager: PackageRegistryManager = new PackageRegistryManager();

const packages: PackageRegistryEntry[] = await packageRegistryManager.getAll();
const quickPickOptions = {
ignoreFocusOut: false,
canPickMany: true,
placeHolder: prompt
};

const packageNames = [];

packages.forEach((_package) => {
packageNames.push(_package.name);
});

return vscode.window.showQuickPick(packageNames, quickPickOptions);
}

static showFolderOptions(prompt: string): Thenable<string | undefined> {
const options: Array<string> = [this.ADD_TO_WORKSPACE, this.OPEN_IN_NEW_WINDOW, this.OPEN_IN_CURRENT_WINDOW];

Expand Down
10 changes: 5 additions & 5 deletions client/src/commands/addConnectionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@
*/
'use strict';
import * as vscode from 'vscode';
import {CommandsUtil} from './commandsUtil';
import {UserInputUtil} from './UserInputUtil';

// TODO: make it save where have got up to
export async function addConnection(): Promise<{} | void> {
console.log('addConnection');
const connectionName: string = await CommandsUtil.showInputBox('Enter a name for the connection');
const connectionName: string = await UserInputUtil.showInputBox('Enter a name for the connection');

if (!connectionName) {
return Promise.resolve();
}

const connectionProfilePath: string = await CommandsUtil.showInputBox('Enter a file path to the connection profile json file');
const connectionProfilePath: string = await UserInputUtil.showInputBox('Enter a file path to the connection profile json file');

if (!connectionProfilePath) {
return Promise.resolve();
}

const certificatePath: string = await CommandsUtil.showInputBox('Enter a file path to the certificate file');
const certificatePath: string = await UserInputUtil.showInputBox('Enter a file path to the certificate file');

if (!certificatePath) {
return Promise.resolve();
}

const privateKeyPath: string = await CommandsUtil.showInputBox('Enter a file path to the private key file');
const privateKeyPath: string = await UserInputUtil.showInputBox('Enter a file path to the private key file');

if (!privateKeyPath) {
return Promise.resolve();
Expand Down
8 changes: 4 additions & 4 deletions client/src/commands/addConnectionIdentityCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
'use strict';
import * as vscode from 'vscode';
import { CommandsUtil } from './commandsUtil';
import { UserInputUtil } from './UserInputUtil';
import { ConnectionTreeItem } from '../explorer/model/ConnectionTreeItem';

export async function addConnectionIdentity(connectionItem: ConnectionTreeItem): Promise<{} | void> {
Expand All @@ -23,20 +23,20 @@ export async function addConnectionIdentity(connectionItem: ConnectionTreeItem):
if (connectionItem) {
connectionName = connectionItem.connection.name;
} else {
connectionName = await CommandsUtil.showConnectionQuickPickBox('Choose a connection to add an identity to');
connectionName = await UserInputUtil.showConnectionQuickPickBox('Choose a connection to add an identity to');
}

if (!connectionName) {
return Promise.resolve();
}

const certificatePath: string = await CommandsUtil.showInputBox('Enter a file path to the certificate file');
const certificatePath: string = await UserInputUtil.showInputBox('Enter a file path to the certificate file');

if (!certificatePath) {
return Promise.resolve();
}

const privateKeyPath: string = await CommandsUtil.showInputBox('Enter a file path to the private key file');
const privateKeyPath: string = await UserInputUtil.showInputBox('Enter a file path to the private key file');

if (!privateKeyPath) {
return Promise.resolve();
Expand Down
6 changes: 3 additions & 3 deletions client/src/commands/connectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
'use strict';
import * as vscode from 'vscode';
import { CommandsUtil } from './commandsUtil';
import { UserInputUtil } from './UserInputUtil';
import { IFabricConnection } from '../fabric/IFabricConnection';
import { ParsedCertificate } from '../fabric/ParsedCertificate';
import { FabricConnectionFactory } from '../fabric/FabricConnectionFactory';
Expand All @@ -27,7 +27,7 @@ export async function connect(connectionName: string, identityName?: string): Pr
console.log('connect', connectionName, identityName);

if (!connectionName) {
connectionName = await CommandsUtil.showConnectionQuickPickBox('Choose a connection to connect with');
connectionName = await UserInputUtil.showConnectionQuickPickBox('Choose a connection to connect with');
if (!connectionName) {
return;
}
Expand Down Expand Up @@ -59,7 +59,7 @@ export async function connect(connectionName: string, identityName?: string): Pr
if (connectionRegistryEntry.identities.length > 1) {

if (!identityName) {
identityName = await CommandsUtil.showIdentityConnectionQuickPickBox('Choose an identity to connect with', connectionRegistryEntry);
identityName = await UserInputUtil.showIdentityConnectionQuickPickBox('Choose an identity to connect with', connectionRegistryEntry);
if (!identityName) {
return;
}
Expand Down
16 changes: 8 additions & 8 deletions client/src/commands/createSmartContractProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { VSCodeOutputAdapter } from '../logging/VSCodeOutputAdapter';
import { CommandUtil } from '../util/CommandUtil';
import * as child_process from 'child_process';
import * as path from 'path';
import { CommandsUtil } from './commandsUtil';
import { UserInputUtil } from './UserInputUtil';

class GeneratorDependencies {
needYo: boolean = false;
Expand All @@ -43,8 +43,8 @@ export async function createSmartContractProject(): Promise<void> {

// if yo/generator fabric are missing, ask if we can install them
if (dependencies.missingDependencies()) {
const installPermission: string = await CommandsUtil.showQuickPickYesNo('Can this extension install missing npm packages before proceeding?');
if (installPermission !== CommandsUtil.YES) {
const installPermission: string = await UserInputUtil.showQuickPickYesNo('Can this extension install missing npm packages before proceeding?');
if (installPermission !== UserInputUtil.YES) {
vscode.window.showErrorMessage('npm modules: yo and generator-fabric are required before creating a smart contract project');
return;
}
Expand Down Expand Up @@ -98,7 +98,7 @@ export async function createSmartContractProject(): Promise<void> {
const folderPath: string = folderUri.fsPath;
const folderName: string = path.basename(folderPath);

const openMethod: string = await CommandsUtil.showFolderOptions('Choose how to open your new project');
const openMethod: string = await UserInputUtil.showFolderOptions('Choose how to open your new project');

if (!openMethod) {
return;
Expand Down Expand Up @@ -247,13 +247,13 @@ async function getSmartContractLanguageOptions(): Promise<string[]> {
}

async function openNewProject(openMethod: string, uri: vscode.Uri): Promise<void> {
if (openMethod === CommandsUtil.ADD_TO_WORKSPACE) {
if (openMethod === UserInputUtil.ADD_TO_WORKSPACE) {
const openFolders: Array<vscode.WorkspaceFolder> = vscode.workspace.workspaceFolders || [];
vscode.workspace.updateWorkspaceFolders(openFolders.length, 0, {uri: uri});
} else {
let openNewWindow = true;

if (openMethod === CommandsUtil.OPEN_IN_CURRENT_WINDOW) {
if (openMethod === UserInputUtil.OPEN_IN_CURRENT_WINDOW) {
openNewWindow = false;
await checkForUnsavedFiles();
}
Expand All @@ -268,8 +268,8 @@ async function checkForUnsavedFiles(): Promise<void> {
});

if (unsavedFiles) {
const answer: string = await CommandsUtil.showQuickPickYesNo('Do you want to save any unsaved changes?');
if (answer === CommandsUtil.YES) {
const answer: string = await UserInputUtil.showQuickPickYesNo('Do you want to save any unsaved changes?');
if (answer === UserInputUtil.YES) {
await vscode.workspace.saveAll(true);
}
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/commands/deleteConnectionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
'use strict';
import * as vscode from 'vscode';
import {CommandsUtil} from './commandsUtil';
import {UserInputUtil} from './UserInputUtil';
import { ConnectionTreeItem } from '../explorer/model/ConnectionTreeItem';

export async function deleteConnection(connectionItem: ConnectionTreeItem): Promise<{} | void> {
Expand All @@ -24,7 +24,7 @@ export async function deleteConnection(connectionItem: ConnectionTreeItem): Prom
if (connectionItem) {
connectionToDelete = connectionItem.connection.name;
} else {
connectionToDelete = await CommandsUtil.showConnectionQuickPickBox('Choose the connection that you want to delete');
connectionToDelete = await UserInputUtil.showConnectionQuickPickBox('Choose the connection that you want to delete');
}

const connections: Array<any> = vscode.workspace.getConfiguration().get('fabric.connections');
Expand Down
54 changes: 54 additions & 0 deletions client/src/commands/deleteSmartContractPackageCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
import * as vscode from 'vscode';
import { UserInputUtil } from './UserInputUtil';
import { PackageTreeItem } from '../explorer/model/PackageTreeItem';
import * as myExtension from '../extension';
import * as fs from 'fs-extra';
import * as homeDir from 'home-dir';
import { CommandUtil } from '../util/CommandUtil';
import { PackageRegistryManager } from '../explorer/packages/PackageRegistryManager';
import { PackageRegistryEntry } from '../explorer/packages/PackageRegistryEntry';

export async function deleteSmartContractPackage(packageTreeItem: PackageTreeItem): Promise<{} | void> {
console.log('deleteSmartContractPackage');
let packageToDelete: string | string[];
if (packageTreeItem) {
packageToDelete = [packageTreeItem.name];
} else {
packageToDelete = await UserInputUtil.showSmartContractPackagesQuickPickBox('Choose the smart contract package(s) that you want to delete');
}

const packageRegistryManager: PackageRegistryManager = new PackageRegistryManager();

const packages: PackageRegistryEntry[] = await packageRegistryManager.getAll();
if (packageToDelete === undefined) {
return;
}
let packageDir: string = vscode.workspace.getConfiguration().get('fabric.package.directory');
if (packageDir.startsWith('~')) {
// Remove tilda and replace with home dir
packageDir = homeDir(packageDir.replace('~', ''));
}

for (const _package of packages) {
if (packageToDelete.includes(_package.name)) {
console.log('Removing', packageDir + '/' + _package.chaincodeLanguage + '/' + _package.name);
const path = _package.chaincodeLanguage === 'go' ? _package.chaincodeLanguage + '/src' : _package.chaincodeLanguage;
await fs.remove(packageDir + '/' + path + '/' + _package.name);
}
}
return vscode.commands.executeCommand('blockchainAPackageExplorer.refreshEntry');
}
4 changes: 2 additions & 2 deletions client/src/commands/restartFabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as vscode from 'vscode';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { CommandsUtil } from './commandsUtil';
import { UserInputUtil } from './UserInputUtil';
import { RuntimeTreeItem } from '../explorer/model/RuntimeTreeItem';
import { VSCodeOutputAdapter } from '../logging/VSCodeOutputAdapter';
import { FabricRuntime } from '../fabric/FabricRuntime';
Expand All @@ -23,7 +23,7 @@ export async function restartFabricRuntime(runtimeTreeItem?: RuntimeTreeItem): P
const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
let runtimeName: string;
if (!runtimeTreeItem) {
runtimeName = await CommandsUtil.showRuntimeQuickPickBox('Select the Fabric runtime to restart');
runtimeName = await UserInputUtil.showRuntimeQuickPickBox('Select the Fabric runtime to restart');
} else {
runtimeName = runtimeTreeItem.getName();
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/commands/startFabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as vscode from 'vscode';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { CommandsUtil } from './commandsUtil';
import { UserInputUtil } from './UserInputUtil';
import { RuntimeTreeItem } from '../explorer/model/RuntimeTreeItem';
import { VSCodeOutputAdapter } from '../logging/VSCodeOutputAdapter';
import { FabricRuntime } from '../fabric/FabricRuntime';
Expand All @@ -23,7 +23,7 @@ export async function startFabricRuntime(runtimeTreeItem?: RuntimeTreeItem): Pro
const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
let runtimeName: string;
if (!runtimeTreeItem) {
runtimeName = await CommandsUtil.showRuntimeQuickPickBox('Select the Fabric runtime to start');
runtimeName = await UserInputUtil.showRuntimeQuickPickBox('Select the Fabric runtime to start');
} else {
runtimeName = runtimeTreeItem.getName();
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/commands/stopFabricRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as vscode from 'vscode';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { CommandsUtil } from './commandsUtil';
import { UserInputUtil } from './UserInputUtil';
import { RuntimeTreeItem } from '../explorer/model/RuntimeTreeItem';
import { VSCodeOutputAdapter } from '../logging/VSCodeOutputAdapter';
import { FabricRuntime } from '../fabric/FabricRuntime';
Expand All @@ -23,7 +23,7 @@ export async function stopFabricRuntime(runtimeTreeItem?: RuntimeTreeItem): Prom
const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
let runtimeName: string;
if (!runtimeTreeItem) {
runtimeName = await CommandsUtil.showRuntimeQuickPickBox('Select the Fabric runtime to stop');
runtimeName = await UserInputUtil.showRuntimeQuickPickBox('Select the Fabric runtime to stop');
} else {
runtimeName = runtimeTreeItem.getName();
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/commands/toggleFabricRuntimeDevMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as vscode from 'vscode';
import { FabricRuntimeManager } from '../fabric/FabricRuntimeManager';
import { CommandsUtil } from './commandsUtil';
import { UserInputUtil } from './UserInputUtil';
import { RuntimeTreeItem } from '../explorer/model/RuntimeTreeItem';
import { VSCodeOutputAdapter } from '../logging/VSCodeOutputAdapter';
import { FabricRuntime } from '../fabric/FabricRuntime';
Expand All @@ -23,7 +23,7 @@ export async function toggleFabricRuntimeDevMode(runtimeTreeItem?: RuntimeTreeIt
const runtimeManager: FabricRuntimeManager = FabricRuntimeManager.instance();
let runtimeName: string;
if (!runtimeTreeItem) {
runtimeName = await CommandsUtil.showRuntimeQuickPickBox('Select the Fabric runtime to toggle development mode');
runtimeName = await UserInputUtil.showRuntimeQuickPickBox('Select the Fabric runtime to toggle development mode');
} else {
runtimeName = runtimeTreeItem.getName();
}
Expand Down
1 change: 1 addition & 0 deletions client/src/explorer/model/PackageTreeItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BlockchainTreeItem } from './BlockchainTreeItem';
import { BlockchainExplorerProvider } from '../BlockchainExplorerProvider';

export class PackageTreeItem extends BlockchainTreeItem {
contextValue = 'blockchain-package-item';

constructor(provider: BlockchainExplorerProvider, public readonly name: string) {
super(provider, name, vscode.TreeItemCollapsibleState.None);
Expand Down
2 changes: 2 additions & 0 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { startFabricRuntime } from './commands/startFabricRuntime';
import { stopFabricRuntime } from './commands/stopFabricRuntime';
import { restartFabricRuntime } from './commands/restartFabricRuntime';
import { toggleFabricRuntimeDevMode } from './commands/toggleFabricRuntimeDevMode';
import { deleteSmartContractPackage } from './commands/deleteSmartContractPackageCommand';

let blockchainNetworkExplorerProvider: BlockchainNetworkExplorerProvider;
let blockchainPackageExplorerProvider: BlockchainPackageExplorerProvider;
Expand Down Expand Up @@ -96,6 +97,7 @@ export function registerCommands(context: vscode.ExtensionContext): void {
context.subscriptions.push(vscode.commands.registerCommand('blockchainExplorer.stopFabricRuntime', (runtimeTreeItem?: RuntimeTreeItem) => stopFabricRuntime(runtimeTreeItem)));
context.subscriptions.push(vscode.commands.registerCommand('blockchainExplorer.restartFabricRuntime', (runtimeTreeItem?: RuntimeTreeItem) => restartFabricRuntime(runtimeTreeItem)));
context.subscriptions.push(vscode.commands.registerCommand('blockchainExplorer.toggleFabricRuntimeDevMode', (runtimeTreeItem?: RuntimeTreeItem) => toggleFabricRuntimeDevMode(runtimeTreeItem)));
context.subscriptions.push(vscode.commands.registerCommand('blockchainAPackageExplorer.deleteSmartContractPackageEntry', (project) => deleteSmartContractPackage(project)));

context.subscriptions.push(vscode.workspace.onDidChangeConfiguration((e) => {

Expand Down
Loading

0 comments on commit 0e71168

Please sign in to comment.