Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions vscode/powershellprotools/src/commands/packageAsExe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,32 @@ function makeDefaultPackageConfig(rootPath: string) {
}
}

function getPackageRoot(packageManifest: string): string | undefined {
const packageConfig = fs.readFileSync(packageManifest, 'utf8');
const rootMatch = packageConfig.match(/^\s*Root\s*=\s*(['\"])(.*?)\1\s*(?:#.*)?$/m);
return rootMatch ? rootMatch[2] : undefined;
}

function updatePackageRoot(packageManifest: string, scriptPath: string): boolean {
const packageConfig = fs.readFileSync(packageManifest, 'utf8');
const updatedPackageConfig = packageConfig.replace(
/^(\s*Root\s*=\s*)(['\"])(.*?)\2(\s*(?:#.*)?)$/m,
`$1'${scriptPath.replace(/'/g, "''")}'$4`);

if (updatedPackageConfig === packageConfig) {
return false;
}

fs.writeFileSync(packageManifest, updatedPackageConfig);
return true;
}

function samePath(firstPath: string, secondPath: string): boolean {
return path.resolve(firstPath).toLowerCase() === path.resolve(secondPath).toLowerCase();
}

export function packageAsExe() {
return vscode.commands.registerCommand('powershell.packageAsExe', (resource) => {
return vscode.commands.registerCommand('powershell.packageAsExe', async (resource) => {
if (!Container.IsInitialized()) return;

var rootPath;
Expand Down Expand Up @@ -97,6 +121,32 @@ export function packageAsExe() {
vscode.window.showInformationMessage(`Created default package manifest at ${packageManifest} `);
}

const activeEditor = vscode.window.activeTextEditor;
const packageRoot = getPackageRoot(packageManifest);
if (activeEditor && packageRoot && activeEditor.document.uri.fsPath.toLowerCase().endsWith('.ps1')) {
const activeScriptPath = activeEditor.document.uri.fsPath;
const packageRootPath = path.isAbsolute(packageRoot)
? packageRoot
: path.join(path.dirname(packageManifest), packageRoot);

if (!samePath(packageRootPath, activeScriptPath)) {
const useManifestRoot = `Use package manifest root (${packageRoot})`;
const useActiveScript = `Use active script (${path.basename(activeScriptPath)})`;
const selection = await vscode.window.showQuickPick([useManifestRoot, useActiveScript], {
placeHolder: 'The package manifest targets a different script. Which script should be packaged?'
});

if (!selection) {
return;
}

if (selection === useActiveScript && !updatePackageRoot(packageManifest, activeScriptPath)) {
vscode.window.showWarningMessage(`Unable to update the Root value in ${packageManifest}.`);
return;
}
}
}

Container.PowerShellService.Package(packageManifest);
});
}
}
Loading