Skip to content

Commit

Permalink
Add single policy build and provide build status in a notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Trittin committed Feb 4, 2022
1 parent 7146330 commit 4d956db
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 61 deletions.
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@
},
{
"command": "extension.policy.build",
"title": "B2C Policy build"
"title": "B2C Build all policies"
},
{
"command": "extension.policy.buildSingle",
"title": "B2C Build current policy"
},
{
"command": "extension.policy.smartCopy",
Expand Down Expand Up @@ -124,6 +128,12 @@
"mac": "shift+cmd+5",
"when": "editorTextFocus"
},
{
"command": "extension.policy.buildSingle",
"key": "shift+ctrl+6",
"mac": "shift+cmd+6",
"when": "editorTextFocus"
},
{
"command": "extension.policy.smartCopy",
"key": "shift+ctrl+c",
Expand Down
152 changes: 93 additions & 59 deletions src/PolicyBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,28 @@ import Consts from './Consts';
import RenumberStepsMultiplePolicies from './RenumberStepsMultiplePolicies';

export default class PolicBuild {
static Build() {
static Build({singlePolicy = false} = {}) {

var policyFilter: string = "**/*.{xml}";
if (singlePolicy) {
var editor: vscode.TextEditor = vscode.window.activeTextEditor as vscode.TextEditor;

if (!editor) {
vscode.window.showErrorMessage("No policy is open");
return;
}

if (path.extname(editor.document.fileName) !== ".xml") {
vscode.window.showErrorMessage("The currently open file is not an XML document");
return;
}

policyFilter = path.basename(editor.document.fileName);
}

var rootPath: string;
var hasBuiltAnyPolicies = false;

// Check if a folder is opened
if ((!vscode.workspace.workspaceFolders) || (vscode.workspace.workspaceFolders.length == 0)) {
vscode.window.showWarningMessage("To build a policy you need to open the policy folder in VS code");
Expand Down Expand Up @@ -52,8 +70,15 @@ export default class PolicBuild {
environmentFolder = appSettings.EnvironmentsFolder;
}

vscode.workspace.findFiles(new vscode.RelativePattern(vscode.workspace.rootPath as string, '**/*.{xml}'), `**/${environmentFolder}/**`)
.then((uris) => {
// Give users an indication that we're still doing work
vscode.window.withProgress(
{location: vscode.ProgressLocation.Window, cancellable: false, title: "Policy build status"},
async (progress) => {
progress.report({message: "Building policies"});

// Converted to await because the progress report doesn't work properly otherwise
var uris = await vscode.workspace.findFiles(new vscode.RelativePattern(vscode.workspace.rootPath as string, policyFilter), `**/${environmentFolder}/**`);

let policyFiles: PolicyFile[] = [];
uris.forEach((uri) => {
if (uri.fsPath.indexOf("?") <= 0) {
Expand All @@ -62,89 +87,98 @@ export default class PolicBuild {
}
});

return policyFiles;
}).then((policyFiles) => {
// Automatically renumber orchestration steps if they are out of order
let config = vscode.workspace.getConfiguration('aadb2c');
if (config.autoRenumber) {
progress.report({message: "Renumbering policies"});
RenumberStepsMultiplePolicies.RenumberPolicies(policyFiles);
}

// Get the app settings
vscode.workspace.openTextDocument(filePath).then(doc => {
var appSettings = JSON.parse(doc.getText());
var environmentsRootPath = path.join(rootPath, environmentFolder);
var doc = await vscode.workspace.openTextDocument(filePath);
var appSettings = JSON.parse(doc.getText());
var environmentsRootPath = path.join(rootPath, environmentFolder);

// Ensure environments folder exists
if (!fs.existsSync(environmentsRootPath)) {
fs.mkdirSync(environmentsRootPath);
}
// Ensure environments folder exists
if (!fs.existsSync(environmentsRootPath)) {
fs.mkdirSync(environmentsRootPath);
}

// Iterate through environments
appSettings.Environments.forEach(function (entry) {
// Iterate through environments
appSettings.Environments.forEach(function (entry) {

if (entry.PolicySettings == null) {
vscode.window.showErrorMessage("Can't generate '" + entry.Name + "' environment policies. Error: Accepted PolicySettings element is missing. You may use old version of the appSettings.json file. For more information, see [App Settings](https://github.com/yoelhor/aad-b2c-vs-code-extension/blob/master/README.md#app-settings)");
}
else {
var environmentRootPath = path.join(environmentsRootPath, entry.Name);
if (entry.PolicySettings == null) {
vscode.window.showErrorMessage("Can't generate '" + entry.Name + "' environment policies. Error: Accepted PolicySettings element is missing. You may use old version of the appSettings.json file. For more information, see [App Settings](https://github.com/yoelhor/aad-b2c-vs-code-extension/blob/master/README.md#app-settings)");
}
else {
var environmentRootPath = path.join(environmentsRootPath, entry.Name);

// Ensure environment folder exists
if (!fs.existsSync(environmentRootPath)) {
fs.mkdirSync(environmentRootPath);
}
// Ensure environment folder exists
if (!fs.existsSync(environmentRootPath)) {
fs.mkdirSync(environmentRootPath);
}

// Iterate through the list of settings
policyFiles.forEach(function (file) {
// Iterate through the list of settings
policyFiles.forEach(function (file) {

var policContent = file.Data;
var policContent = file.Data;

// Replace the tenant name
policContent = policContent.replace(new RegExp("\{Settings:Tenant\}", "gi"), entry.Tenant);
// Replace the tenant name
policContent = policContent.replace(new RegExp("\{Settings:Tenant\}", "gi"), entry.Tenant);

// Replace the file name
policContent = policContent.replace(new RegExp("\{Settings:Filename\}", "gi"), file.FileName.replace(/\.[^/.]+$/, ""));
// Replace the file name
policContent = policContent.replace(new RegExp("\{Settings:Filename\}", "gi"), file.FileName.replace(/\.[^/.]+$/, ""));

// Replace the file name and remove the policy prefix
policContent = policContent.replace(new RegExp("\{Settings:PolicyFilename\}", "gi"), file.FileName.replace(/\.[^/.]+$/, "").replace(new RegExp("B2C_1A_", "g"), ""),);
// Replace the file name and remove the policy prefix
policContent = policContent.replace(new RegExp("\{Settings:PolicyFilename\}", "gi"), file.FileName.replace(/\.[^/.]+$/, "").replace(new RegExp("B2C_1A_", "g"), ""),);

// Replace the environment name
policContent = policContent.replace(new RegExp("\{Settings:Environment\}", "gi"), entry.Name);
// Replace the environment name
policContent = policContent.replace(new RegExp("\{Settings:Environment\}", "gi"), entry.Name);

// Replace the rest of the policy settings
Object.keys(entry.PolicySettings).forEach(key => {
policContent = policContent.replace(new RegExp("\{Settings:" + key + "\}", "gi"), entry.PolicySettings[key]);
});
// Replace the rest of the policy settings
Object.keys(entry.PolicySettings).forEach(key => {
policContent = policContent.replace(new RegExp("\{Settings:" + key + "\}", "gi"), entry.PolicySettings[key]);
});

// Check to see if the policy's subdirectory exists.
if (file.SubFolder) {
var policyFolderPath = path.join(environmentRootPath, file.SubFolder);
// Check to see if the policy's subdirectory exists.
if (file.SubFolder) {
var policyFolderPath = path.join(environmentRootPath, file.SubFolder);

if (!fs.existsSync(policyFolderPath)) {
fs.mkdirSync(policyFolderPath, { recursive: true });
}
if (!fs.existsSync(policyFolderPath)) {
fs.mkdirSync(policyFolderPath, { recursive: true });
}
}

var filePath: string;
var filePath: string;

// Save the policy
if (file.SubFolder) {
filePath = path.join(policyFolderPath, file.FileName);
} else {
filePath = path.join(environmentRootPath, file.FileName);
}
// Save the policy
if (file.SubFolder) {
filePath = path.join(policyFolderPath, file.FileName);
} else {
filePath = path.join(environmentRootPath, file.FileName);
}

fs.writeFile(filePath, policContent, 'utf8', (err) => {
if (err) throw err;
});
fs.writeFile(filePath, policContent, 'utf8', (err) => {
if (err) throw err;
});

vscode.window.showInformationMessage("Your policies successfully exported and stored under the Environment folder.");
}
});

hasBuiltAnyPolicies = true;
});
}
});
});


if (hasBuiltAnyPolicies) {
if (singlePolicy) {
vscode.window.showInformationMessage(`Your policy ${policyFilter} successfully exported and stored under the Environment folder.`);
} else {
vscode.window.showInformationMessage("Your policies successfully exported and stored under the Environment folder.");
}
} else {
vscode.window.showWarningMessage("It does not appear there were any policy files to build");
}
}
);
}

});
Expand Down
5 changes: 4 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('ApplicationInsightsExplorer.add', () => InsertCommands.InsertApplicationInsights()));

// Policy build
context.subscriptions.push(vscode.commands.registerCommand('extension.policy.build', () => PolicyBuild.Build()));
context.subscriptions.push(vscode.commands.registerCommand('extension.policy.build', () => PolicyBuild.Build({singlePolicy: false})));

// Policy build
context.subscriptions.push(vscode.commands.registerCommand('extension.policy.buildSingle', () => PolicyBuild.Build({singlePolicy: true})));

// Smart copy
context.subscriptions.push(vscode.commands.registerCommand('extension.policy.smartCopy', () => SmartCopy.Copy()));
Expand Down

0 comments on commit 4d956db

Please sign in to comment.