Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
Update for running generate with config file
Browse files Browse the repository at this point in the history
  • Loading branch information
CloudSmithBrandon committed Feb 6, 2020
1 parent c954961 commit 42b7ff3
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 11 deletions.
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,11 @@
"title": "Generate CDS entity classes",
"category": "UserInterface"
},
{
"command": "cs.cds.controls.explorer.generateEntityCodeUsingConfig",
"title": "Generate CDS entity classes using this configuration",
"category": "UserInterface"
},
{
"command": "cs.cds.controls.explorer.importSolution",
"title": "Import solution to CDS",
Expand Down Expand Up @@ -647,6 +652,10 @@
"command": "cs.cds.controls.explorer.generateEntityCodeToFile",
"when": "false"
},
{
"command": "cs.cds.controls.explorer.generateEntityCodeUsingConfig",
"when": "false"
},
{
"command": "cs.cds.controls.explorer.importSolution",
"when": "false"
Expand Down Expand Up @@ -831,6 +840,11 @@
"when": "explorerResourceIsFolder != true && resourceExtname == .config",
"group": "generateCode@1"
},
{
"command": "cs.cds.controls.explorer.generateEntityCodeUsingConfig",
"when": "explorerResourceIsFolder != true && resourceExtname == .config",
"group": "generateCode@2"
},
{
"command": "cs.cds.controls.explorer.generateEntityCodeToFile",
"when": "explorerResourceIsFolder != true && resourceLangId == csharp || resourceLangId == vb",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<PackageReference Include="Microsoft.CrmSdk.Deployment" Version="9.0.2.22" />
<PackageReference Include="Microsoft.CrmSdk.Workflow" Version="9.0.2.22" />
<PackageReference Include="Microsoft.CrmSdk.XrmTooling.CoreAssembly" Version="9.1.0.26" />
<PackageReference Include="Microsoft.CrmSdk.XrmTooling.WpfControls" Version="9.1.0.26" />
<PackageReference Include="Microsoft.IdentityModel.Clients.ActiveDirectory" Version="3.19.8" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
</ItemGroup>
Expand Down Expand Up @@ -67,6 +68,9 @@
<Reference Include="Microsoft.Xrm.Sdk.Workflow, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.Workflow\9.0.2.22\lib\net462\Microsoft.Xrm.Sdk.Workflow.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xrm.Tooling.UI.Styles, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.XrmTooling.WpfControls\9.1.0.26\lib\net462\Microsoft.Xrm.Tooling.UI.Styles.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Xrm.Tooling.Connector, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CrmSdk.XrmTooling.CoreAssembly\9.1.0.26\lib\net462\Microsoft.Xrm.Tooling.Connector.dll</HintPath>
<Private>True</Private>
Expand Down
1 change: 1 addition & 0 deletions resources/tools/CloudSmith.Cds.CrmSvcUtil/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<package id="Microsoft.CrmSdk.Deployment" version="9.0.2.22" targetFramework="net472" />
<package id="Microsoft.CrmSdk.Workflow" version="9.0.2.22" targetFramework="net472" />
<package id="Microsoft.CrmSdk.XrmTooling.CoreAssembly" version="9.1.0.26" targetFramework="net472" />
<package id="Microsoft.CrmSdk.XrmTooling.WpfControls" version="9.1.0.26" targetFramework="net472" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.8" targetFramework="net472" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net472" />
</packages>
27 changes: 22 additions & 5 deletions src/commands/cs.cds.powerShell.generateEntities.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as path from 'path';
import * as cs from "../cs";
import * as vscode from 'vscode';
import * as FileSystem from '../core/io/FileSystem';
import * as Security from "../core/security/Types";
import ExtensionConfiguration from '../core/ExtensionConfiguration';
import Quickly, { WorkspaceFileItem } from '../core/Quickly';
import ExtensionContext from "../core/ExtensionContext";
import TerminalManager, { TerminalCommand } from '../components/Terminal/SecureTerminal';
import { Utilities } from '../core/Utilities';
import { CdsWebApi } from '../api/cds-webapi/CdsWebApi';
import GlobalStateCredentialStore from '../core/security/GlobalStateCredentialStore';
import * as Security from "../core/security/Types";
import ScriptDownloader from '../components/WebDownloaders/ScriptDownloader';
import logger from '../core/framework/Logger';
import CodeGenerationManager from '../components/CodeGeneration/CodeGenerationManager';
Expand All @@ -23,11 +24,11 @@ import CodeGenerationManager from '../components/CodeGeneration/CodeGenerationMa
* @param {string} [namespace] Optional namespace for generated output
* @returns Promise with output from terminal command running CrmSvcUtil.exe
*/
export default async function run(this: CodeGenerationManager, config?: CdsWebApi.Config | string, folder?: string, outputFileName?: string, namespace?: string) {
export default async function run(this: CodeGenerationManager, config?: CdsWebApi.Config | string, folder?: string, outputFileName?: string, namespace?: string, configFile?: vscode.Uri) {
// setup configurations
const sdkInstallPath = ExtensionConfiguration.getConfigurationValueOrDefault<string>(cs.cds.configuration.tools.sdkInstallPath, path.join(ExtensionContext.Instance.globalStoragePath, "Sdk"));
const coreToolsRoot = !Utilities.$Object.isNullOrEmpty(sdkInstallPath) ? path.join(sdkInstallPath, 'CoreTools') : null;
const workspaceFolder = vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0] : null;
let coreToolsRoot = !Utilities.$Object.isNullOrEmpty(sdkInstallPath) ? path.join(sdkInstallPath, 'CoreTools') : null;

if (!config) {
const connectionChoice = await Quickly.pick("Would you like to connect to CDS using an existing connection or a connection string?", "Existing Connection", "Connection String");
Expand Down Expand Up @@ -82,12 +83,22 @@ export default async function run(this: CodeGenerationManager, config?: CdsWebAp

namespace = namespace || await Quickly.ask("Enter the namespace for the generated code", undefined, path.basename(folder));

let hasConfig: boolean = false;

if (configFile && configFile.fsPath && FileSystem.exists(configFile.fsPath)) {
hasConfig = true;
coreToolsRoot = path.join(ExtensionContext.Instance.globalStoragePath, `\\Tools\\${Utilities.Random.randomStringHex(10)}` );

FileSystem.copyFolder(path.join(ExtensionContext.Instance.globalStoragePath, `\\Tools\\CloudSmith.Cds.CrmSvcUtil\\`), coreToolsRoot);
FileSystem.copyItem(configFile.fsPath, path.join(coreToolsRoot, "CrmSvcUtil.exe.config"));
}

logger.log(`Command: ${cs.cds.powerShell.generateEntities} Checking to see if the CRM SDK is installed`);
await ScriptDownloader.installCdsSdk();

// build a powershell terminal
logger.log(`Command: ${cs.cds.powerShell.generateEntities} Generating entity code`);
return TerminalManager.showTerminal(path.join(ExtensionContext.Instance.globalStoragePath, "\\Scripts\\"))
const result = await TerminalManager.showTerminal(path.join(ExtensionContext.Instance.globalStoragePath, "\\Scripts\\"))
.then(async terminal => {
let returnPromise: Promise<TerminalCommand>;

Expand Down Expand Up @@ -128,7 +139,7 @@ export default async function run(this: CodeGenerationManager, config?: CdsWebAp
if (result && result.output.indexOf("Invalid Login Information : An unsecured or incorrectly secured fault was received from the other party.") !== -1) {
await Quickly.askToRetry('CrmSvcUtil returned a fault attempting to login with the credentials supplied. Would you like to retry with an interactive login?',
async () => {
return terminal.run(new TerminalCommand(`.\\Generate-XrmEntities.ps1 `)
return await terminal.run(new TerminalCommand(`.\\Generate-XrmEntities.ps1 `)
.text(`-ToolsPath ${coreToolsRoot} `)
.text(`-Path "${folder}" `)
.text(`-OutputFile "${outputFileName}" `)
Expand All @@ -141,4 +152,10 @@ export default async function run(this: CodeGenerationManager, config?: CdsWebAp
}
});
});

if (hasConfig) {
FileSystem.deleteFolder(coreToolsRoot);
}

return result;
}
13 changes: 9 additions & 4 deletions src/components/CodeGeneration/CodeGenerationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ import { CdsWebApi } from '../../api/cds-webapi/CdsWebApi';

export default class CodeGenerationManager {
@command(cs.cds.controls.explorer.generateEntityCodeToFile, "Generate entity code to a file")
async generateEntityCodeToFile(file?:vscode.Uri) {
async generateEntityCodeToFile(file?: vscode.Uri) {
return await vscode.commands.executeCommand(cs.cds.powerShell.generateEntities, undefined, path.dirname(file.fsPath), path.basename(file.fsPath), undefined);
}

@command(cs.cds.controls.explorer.generateEntityCodeUsingConfig, "Generate entities using configuration file")
async generateEntityCodeUsingConfig(configFile?: vscode.Uri) {
return await vscode.commands.executeCommand(cs.cds.powerShell.generateEntities, undefined, undefined, undefined, undefined, configFile);
}

@command(cs.cds.controls.explorer.generateEntityCodeToFolder, "Generate entity code to a folder")
async generateEntityCodeToFolder(folder?:vscode.Uri) {
async generateEntityCodeToFolder(folder?: vscode.Uri) {
return await vscode.commands.executeCommand(cs.cds.powerShell.generateEntities, undefined, folder.fsPath, undefined, undefined);
}

Expand All @@ -34,8 +39,8 @@ export default class CodeGenerationManager {
}

@command(cs.cds.powerShell.generateEntities, "Generate entity code using CrmSvcUtil")
async generateEntities(config?: CdsWebApi.Config, folder?: string, outputFileName?: string, namespace?: string) {
generateEntities.apply(this, [config, folder, outputFileName, namespace]);
async generateEntities(config?: CdsWebApi.Config, folder?: string, outputFileName?: string, namespace?: string, configFile?: vscode.Uri) {
generateEntities.apply(this, [ config, folder, outputFileName, namespace, configFile ]);
}

@command(cs.cds.deployment.createCrmSvcUtilConfig, "Create or edit CrmSvcUtil.exe.config")
Expand Down
4 changes: 2 additions & 2 deletions src/core/Quickly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export default class Quickly {
const input = await Quickly.ask(`What is the name of the new ${canPickFiles && !canPickFolders ? "File" : canPickFolders && !canPickFiles ? "Folder" : "Item" }?`);

if (input) {
const isFolder = canPickFolders && !canPickFiles ? true : input.endsWith("/") || input.endsWith("\\") ? true : false;
const isFolder = canPickFolders && !canPickFiles ? true : input.endsWith("/") || input.endsWith("\\");
let newPath = defaultUri.path.endsWith("/") ? defaultUri.path + input : defaultUri.path + "/" + input;

if (isFolder && !newPath.endsWith("/")) { newPath += "/"; }
Expand All @@ -407,7 +407,7 @@ export default class Quickly {

return this.pickWorkspaceFsItem(newUri, placeHolder, ignoreFocusOut, canPickFiles, canPickFolders, canPickLinks, canAddNewItem, allowedFileTypes);
} else {
return new WorkspaceFileItem(newUri.fsPath, itemType);
return new WorkspaceFileItem(newUri.fsPath, vscode.FileType.File);
}
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/core/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as BrowserHelper from "./helpers/Browser";
import * as EncodingHelper from "./helpers/Encoding";
import * as GuidHelper from "./helpers/Guid";
import * as ObjectHelper from "./helpers/Object";
import * as RandomHelper from "./helpers/Random";
import * as StringHelper from "./helpers/String";
import { TextEncoder } from 'util';

Expand Down Expand Up @@ -41,6 +42,12 @@ export interface ObjectUtility {
createInstance<T>(context: Object, name: string, ...args: any[]) : T;
}

export interface RandomUtility {
randomStringHex(length: number): string;
randomStringBase64(length: number): string;
randomString(length: number, chars?: string): string;
}

export interface StringUtility {
parseUtcDate(date: string): Date;
dateAsFilename(): string;
Expand All @@ -56,6 +63,7 @@ export interface UtilityObject {
readonly Encoding: EncodingUtility;
readonly Guid: GuidUtility;
readonly $Object: ObjectUtility;
readonly Random: RandomUtility;
readonly String: StringUtility;
}

Expand All @@ -65,5 +73,6 @@ export class Utilities {
static get Encoding(): EncodingUtility { return EncodingHelper; }
static get Guid(): GuidUtility { return GuidHelper; }
static get $Object(): ObjectUtility { return ObjectHelper; }
static get Random(): RandomUtility { return RandomHelper; }
static get String(): StringUtility { return StringHelper; }
}
31 changes: 31 additions & 0 deletions src/core/helpers/Random.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as crypto from 'crypto';

export function randomStringHex(length: number): string {
return crypto
.randomBytes(Math.ceil(length / 2))
.toString('hex') // convert to hexadecimal format
.slice(0, length); // return required number of characters
}

export function randomStringBase64(length: number): string {
return crypto
.randomBytes(Math.ceil((length * 3) / 4))
.toString('base64') // convert to base64 format
.slice(0, length) // return required number of characters
.replace(/\+/g, '0') // replace '+' with '0'
.replace(/\//g, '0'); // replace '/' with '0'
}

export function randomString(length: number, chars?: string): string {
chars = chars || 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789';

const rnd = crypto.randomBytes(length);
const value = new Array(length);
const d = 256 / Math.min(256, chars.length);

for (var i = 0; i < length; i++) {
value[i] = chars[Math.floor(rnd[i] / d)];
}

return value.join('');
}
1 change: 1 addition & 0 deletions src/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export namespace cds {
static readonly editCrmSvcUtilConfig: string = `${explorer._namespace}.editCrmSvcUtilConfig`;
static readonly generateEntityCodeToFolder: string = `${explorer._namespace}.generateEntityCodeToFolder`;
static readonly generateEntityCodeToFile: string = `${explorer._namespace}.generateEntityCodeToFile`;
static readonly generateEntityCodeUsingConfig: string = `${explorer._namespace}.generateEntityCodeUsingConfig`;
static readonly importSolution: string = `${explorer._namespace}.importSolution`;
static readonly packSolutionFromFolder: string = `${explorer._namespace}.packSolutionFromFolder`;
static readonly packWebResource: string = `${explorer._namespace}.packWebResource`;
Expand Down

0 comments on commit 42b7ff3

Please sign in to comment.