export async function selectParameterFile()

in src/documents/parameters/parameterFiles.ts [40:138]


export async function selectParameterFile(actionContext: IActionContext, mapping: DeploymentFileMapping, sourceUri: Uri | undefined): Promise<void> {
  const editor = window.activeTextEditor;
  if (!sourceUri) {
    sourceUri = window.activeTextEditor?.document.uri;
  }

  if (!editor || !sourceUri || editor.document.uri.fsPath !== sourceUri.fsPath) {
    await ext.ui.showWarningMessage(`Please open an Azure Resource Manager template file before trying to associate or create a parameter file.`);
    return;

  }
  if (editor.document.languageId !== armTemplateLanguageId) {
    actionContext.telemetry.properties.languageId = editor.document.languageId;
    await ext.ui.showWarningMessage(`The current file "${sourceUri.fsPath}" does not appear to be an Azure Resource Manager Template. Please open one or make sure the editor Language Mode in the context menu is set to "Azure Resource Manager Template".`);
    return;
  }

  let templateUri: Uri = sourceUri;

  if (templateUri.scheme === documentSchemes.untitled) {
    actionContext.errorHandling.suppressReportIssue = true;
    throw new Error("Please save the template file before associating it with a parameter file.");
  }

  // Get the template file contents so we can find the top-level parameters
  const contents = editor.document.getText(undefined);
  const template: DeploymentTemplateDoc = new DeploymentTemplateDoc(contents, templateUri, editor.document.version);

  let quickPickList: IQuickPickList = await createParameterFileQuickPickList(mapping, templateUri);
  // Show the quick pick
  const result: IAzureQuickPickItem<IPossibleParameterFile | undefined> = await ext.ui.showQuickPick(
    quickPickList.items,
    {
      canPickMany: false,
      placeHolder: `Select a parameter file for "${path.basename(templateUri.fsPath)}"`,
      suppressPersistence: true
    });

  // Interpret result
  if (result === quickPickList.none) {
    // None

    // Remove the mapping for this file, and never ask so the user selection sticks
    await neverAskAgain(templateUri, actionContext);
    await mapping.mapParameterFile(templateUri, undefined);
  } else if (result === quickPickList.browse) {
    // Browse...

    const paramsPaths: Uri[] | undefined = await window.showOpenDialog({
      canSelectMany: false,
      defaultUri: templateUri,
      openLabel: "Select Parameter File"
    });
    if (!paramsPaths || paramsPaths.length !== 1) {
      throw new UserCancelledError();
    }
    const selectedParamsPath: Uri = paramsPaths[0];

    if (!await isParameterFile(selectedParamsPath.fsPath)) {
      const selectAnywayResult = await ext.ui.showWarningMessage(
        `"${selectedParamsPath.fsPath}" does not appear to be a valid parameter file. Select it anyway?`,
        { modal: true },
        DialogResponses.yes,
        DialogResponses.no
      );
      if (selectAnywayResult !== DialogResponses.yes) {
        throw new UserCancelledError();
      }
    }

    // never ask so the user selection sticks
    await neverAskAgain(templateUri, actionContext);

    // Map to the browsed file
    await mapping.mapParameterFile(templateUri, selectedParamsPath);
  } else if (result === quickPickList.newFile) {
    // New parameter file

    let newUri: Uri = await queryCreateParameterFile(actionContext, template.topLevelScope);
    await mapping.mapParameterFile(templateUri, newUri);
    await commands.executeCommand('azurerm-vscode-tools.openParameterFile', templateUri, newUri);
  } else if (result === quickPickList.openCurrent) {
    // Open current

    await commands.executeCommand('azurerm-vscode-tools.openParameterFile', templateUri);
  } else if (result.data === quickPickList.currentParamFile) {
    // Current re-selected

    // Nothing to change
    dontAskAgainThisSession(templateUri, actionContext);
  } else {
    // Item in the list selected

    assert(result.data, "Quick pick item should have had data");
    // never ask so the user selection sticks
    await neverAskAgain(templateUri, actionContext);
    await mapping.mapParameterFile(templateUri, result.data?.uri);
  }
}