Skip to content

Commit

Permalink
Refactor and support preselected profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
bwateratmsft committed Dec 18, 2024
1 parent 90468cc commit 3562a4f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
20 changes: 9 additions & 11 deletions src/commands/compose/compose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { quickPickWorkspaceFolder } from '../../utils/quickPickWorkspaceFolder';
import { selectComposeCommand } from '../selectCommandTemplate';
import { getComposeProfileList, getComposeProfilesOrServices, getComposeServiceList } from './getComposeSubsetList';

async function compose(context: IActionContext, commands: ('up' | 'down' | 'upSubset')[], message: string, dockerComposeFileUri?: vscode.Uri | string, selectedComposeFileUris?: vscode.Uri[], preselectedServices?: string[]): Promise<void> {
async function compose(context: IActionContext, commands: ('up' | 'down' | 'upSubset')[], message: string, dockerComposeFileUri?: vscode.Uri | string, selectedComposeFileUris?: vscode.Uri[], preselectedServices?: string[], preselectedProfiles?: string[]): Promise<void> {
if (!vscode.workspace.isTrusted) {
throw new UserCancelledError('enforceTrust');
}
Expand Down Expand Up @@ -63,7 +63,7 @@ async function compose(context: IActionContext, commands: ('up' | 'down' | 'upSu
);

// Add the service list if needed
terminalCommand.command = await addServicesOrProfilesIfNeeded(context, folder, terminalCommand.command, preselectedServices);
terminalCommand.command = await addServicesOrProfilesIfNeeded(context, folder, terminalCommand.command, preselectedServices, preselectedProfiles);

const client = await ext.orchestratorManager.getClient();
const taskCRF = new TaskCommandRunnerFactory({
Expand All @@ -82,8 +82,8 @@ export async function composeUp(context: IActionContext, dockerComposeFileUri?:
}

// The parameters of this function should not be changed without updating the compose language service which uses this command
export async function composeUpSubset(context: IActionContext, dockerComposeFileUri?: vscode.Uri, selectedComposeFileUris?: vscode.Uri[], preselectedServices?: string[]): Promise<void> {
return await compose(context, ['upSubset'], vscode.l10n.t('Choose Docker Compose file to bring up'), dockerComposeFileUri, selectedComposeFileUris, preselectedServices);
export async function composeUpSubset(context: IActionContext, dockerComposeFileUri?: vscode.Uri, selectedComposeFileUris?: vscode.Uri[], preselectedServices?: string[], preselectedProfiles?: string[]): Promise<void> {
return await compose(context, ['upSubset'], vscode.l10n.t('Choose Docker Compose file to bring up'), dockerComposeFileUri, selectedComposeFileUris, preselectedServices, preselectedProfiles);
}

export async function composeDown(context: IActionContext, dockerComposeFileUri?: vscode.Uri, selectedComposeFileUris?: vscode.Uri[]): Promise<void> {
Expand All @@ -96,21 +96,19 @@ export async function composeRestart(context: IActionContext, dockerComposeFileU

const serviceListPlaceholder = /\${serviceList}/i;
const profileListPlaceholder = /\${profileList}/i;
async function addServicesOrProfilesIfNeeded(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, command: string, preselectedServices: string[]): Promise<string> {
async function addServicesOrProfilesIfNeeded(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, command: string, preselectedServices: string[], preselectedProfiles: string[]): Promise<string> {
const commandWithoutPlaceholders = command.replace(serviceListPlaceholder, '').replace(profileListPlaceholder, '');

if (preselectedServices?.length && serviceListPlaceholder.test(command)) {
return command.replace(serviceListPlaceholder, preselectedServices.join(' '));
} else if (serviceListPlaceholder.test(command) && profileListPlaceholder.test(command)) {
if (serviceListPlaceholder.test(command) && profileListPlaceholder.test(command)) {
// If both are present, need to ask
const { services, profiles } = await getComposeProfilesOrServices(context, workspaceFolder, commandWithoutPlaceholders);
const { services, profiles } = await getComposeProfilesOrServices(context, workspaceFolder, commandWithoutPlaceholders, preselectedServices, preselectedProfiles);
return command
.replace(serviceListPlaceholder, services)
.replace(profileListPlaceholder, profiles);
} else if (serviceListPlaceholder.test(command)) {
return command.replace(serviceListPlaceholder, await getComposeServiceList(context, workspaceFolder, commandWithoutPlaceholders));
return command.replace(serviceListPlaceholder, await getComposeServiceList(context, workspaceFolder, commandWithoutPlaceholders, preselectedServices));
} else if (profileListPlaceholder.test(command)) {
return command.replace(profileListPlaceholder, await getComposeProfileList(context, workspaceFolder, commandWithoutPlaceholders));
return command.replace(profileListPlaceholder, await getComposeProfileList(context, workspaceFolder, commandWithoutPlaceholders, preselectedProfiles));
} else {
return command;
}
Expand Down
24 changes: 16 additions & 8 deletions src/commands/compose/getComposeSubsetList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ const composeCommandReplaceRegex = /(\b(up|down)\b).*$/i;

type SubsetType = 'services' | 'profiles';

export async function getComposeProfilesOrServices(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, composeCommand: string): Promise<{ services: string | undefined, profiles: string | undefined }> {
export async function getComposeProfilesOrServices(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, composeCommand: string, preselectedServices?: string[], preselectedProfiles?: string[]): Promise<{ services: string | undefined, profiles: string | undefined }> {
const profiles = await getServiceSubsets(workspaceFolder, composeCommand, 'profiles');

if (preselectedServices?.length && preselectedProfiles?.length) {
throw new Error(vscode.l10n.t('Cannot specify both services and profiles to start. Please choose one or the other.'));
}

// If there any profiles, we need to ask the user whether they want profiles or services, since they are mutually exclusive to use
// Otherwise, if there are no profiles, we'll automatically assume services
let useProfiles = false;
if (profiles?.length) {
if (preselectedProfiles?.length) {
useProfiles = true;
} else if (preselectedServices?.length) {
useProfiles = false;
} else if (profiles?.length) {
const profilesOrServices: IAzureQuickPickItem<SubsetType>[] = [
{
label: vscode.l10n.t('Services'),
Expand All @@ -35,12 +43,12 @@ export async function getComposeProfilesOrServices(context: IActionContext, work
}

return {
profiles: useProfiles ? await getComposeProfileList(context, workspaceFolder, composeCommand, profiles) : '',
services: !useProfiles ? await getComposeServiceList(context, workspaceFolder, composeCommand) : '',
profiles: useProfiles ? await getComposeProfileList(context, workspaceFolder, composeCommand, profiles, preselectedProfiles) : '',
services: !useProfiles ? await getComposeServiceList(context, workspaceFolder, composeCommand, preselectedServices) : '',
};
}

export async function getComposeProfileList(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, composeCommand: string, prefetchedProfiles?: string[]): Promise<string> {
export async function getComposeProfileList(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, composeCommand: string, prefetchedProfiles?: string[], preselectedProfiles?: string[]): Promise<string> {
const profiles = prefetchedProfiles ?? await getServiceSubsets(workspaceFolder, composeCommand, 'profiles');

if (!profiles?.length) {
Expand All @@ -51,15 +59,15 @@ export async function getComposeProfileList(context: IActionContext, workspaceFo
// Fetch the previously chosen profiles list. By default, all will be selected.
const workspaceProfileListKey = `vscode-docker.composeProfiles.${workspaceFolder.name}`;
const previousChoices = ext.context.workspaceState.get<string[]>(workspaceProfileListKey, profiles);
const result = await pickSubsets(context, 'profiles', profiles, previousChoices);
const result = preselectedProfiles?.length ? preselectedProfiles : await pickSubsets(context, 'profiles', profiles, previousChoices);

// Update the cache
await ext.context.workspaceState.update(workspaceProfileListKey, result);

return result.map(p => `--profile ${p}`).join(' ');
}

export async function getComposeServiceList(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, composeCommand: string): Promise<string> {
export async function getComposeServiceList(context: IActionContext, workspaceFolder: vscode.WorkspaceFolder, composeCommand: string, preselectedServices?: string[]): Promise<string> {
const services = await getServiceSubsets(workspaceFolder, composeCommand, 'services');

if (!services?.length) {
Expand All @@ -70,7 +78,7 @@ export async function getComposeServiceList(context: IActionContext, workspaceFo
// Fetch the previously chosen services list. By default, all will be selected.
const workspaceServiceListKey = `vscode-docker.composeServices.${workspaceFolder.name}`;
const previousChoices = ext.context.workspaceState.get<string[]>(workspaceServiceListKey, services);
const result = await pickSubsets(context, 'services', services, previousChoices);
const result = preselectedServices?.length ? preselectedServices : await pickSubsets(context, 'services', services, previousChoices);

// Update the cache
await ext.context.workspaceState.update(workspaceServiceListKey, result);
Expand Down

0 comments on commit 3562a4f

Please sign in to comment.