export async function switchToDotnetProject()

in apps/vs-code-designer/src/app/commands/workflows/switchToDotnetProject.ts [46:161]


export async function switchToDotnetProject(context: IProjectWizardContext, target: vscode.Uri) {
  const isDotNetInstalled = await validateDotNetIsInstalled(context, target.fsPath);
  if (!isDotNetInstalled) {
    return;
  }

  if (target === undefined || Object.keys(target).length === 0) {
    const workspaceFolder = await getWorkspaceFolder(context);
    const projectPath = await tryGetLogicAppProjectRoot(context, workspaceFolder);
    target = vscode.Uri.file(projectPath);
  }

  let version: FuncVersion | undefined = tryParseFuncVersion(getWorkspaceSetting(funcVersionSetting, target.fsPath));

  const projectFiles = await getProjFiles(context, ProjectLanguage.CSharp, target.fsPath);
  if (projectFiles.length > 0) {
    vscode.window.showInformationMessage(
      localize('moveToDotnetCompleted', 'The Logic App project is already a NuGet-based project.'),
      'OK'
    );
    return;
  }

  if (!version) {
    const message: string = localize('initFolder', 'Initialize project for use with VS Code?');
    await context.ui.showWarningMessage(message, { modal: true }, DialogResponses.yes);
    await initProjectForVSCode(context, target.fsPath);

    version = nonNullOrEmptyValue(
      tryParseFuncVersion(getWorkspaceSetting(funcVersionSetting, target.fsPath)),
      funcVersionSetting
    ) as FuncVersion;
  }

  const dotnetTemplateProvider = new DotnetTemplateProvider(version, target.fsPath);

  // We need to get the templates first to ensure that the we can create the dotnet project
  // 1. try to get cached templates
  let templates: ITemplates | undefined = await dotnetTemplateProvider.getCachedTemplates(context);

  // 2. try to download the latest templates
  if (!templates) {
    const templateVersion: string = await dotnetTemplateProvider.getLatestTemplateVersion(context);
    templates = await dotnetTemplateProvider.getLatestTemplates(context, templateVersion);
  }

  // 3. try to get the backup templates
  if (!templates) {
    templates = await dotnetTemplateProvider.getBackupTemplates(context);
  }

  if (!templates) {
    throw new Error(localize('dotnetTemplateError', `Can't find dotnet templates.`));
  }
  const logicAppFolderName = path.basename(target.fsPath);
  const warning: string = localize(
    'confirmMoveToDotnet',
    `This action moves your logic app project, ${logicAppFolderName}, to a NuGet-based project. Confirm that you want to move to a NuGet-based project?`
  );

  const moveButton: vscode.MessageItem = { title: localize('move', 'Move to a NuGet-based project') };
  await context.ui.showWarningMessage(warning, { modal: true }, moveButton, DialogResponses.cancel);

  const projectName: string = path.basename(target.fsPath);
  const templateLanguage = 'CSharp';
  const majorVersion: string = tryGetMajorVersion(version);
  const identity = `Microsoft.AzureFunctions.ProjectTemplate.${templateLanguage}.${Number.parseInt(majorVersion) < 4 ? majorVersion : 3}.x`;
  const functionsVersion: string = `v${majorVersion}`;
  const projectPath: string = target.fsPath;
  const projTemplateKey = await getTemplateKeyFromProjFile(context, projectPath, version, ProjectLanguage.CSharp);
  const dotnetVersion = await getFramework(context, projectPath);
  const useBinaries = useBinariesDependencies();
  const dotnetLocalVersion = useBinaries ? await getLocalDotNetVersionFromBinaries('6') : '';

  await deleteBundleProjectFiles(target);
  await renameBundleProjectFiles(target);

  await executeDotnetTemplateCommand(
    context,
    version,
    projTemplateKey,
    target.fsPath,
    'create',
    '--identity',
    identity,
    '--arg:name',
    wrapArgInQuotes(projectName),
    '--arg:AzureFunctionsVersion',
    functionsVersion
  );

  await copyBundleProjectFiles(target);
  await updateBuildFile(context, target, dotnetVersion);
  if (useBinaries) {
    await createGlobalJsonFile(dotnetLocalVersion, target.fsPath);
  }

  const workspaceFolder: vscode.WorkspaceFolder | undefined = getContainingWorkspace(target.fsPath);

  const wizardOptions = {
    projectPath,
    workspaceFolder,
    workspacePath: (workspaceFolder && workspaceFolder.uri.fsPath) || target.fsPath,
    language: ProjectLanguage.CSharp,
    version,
  };
  const wizardContext: IProjectWizardContext = Object.assign(context, wizardOptions);

  const dotnetInitVSCodeStep = new DotnetInitVSCodeStep();
  dotnetInitVSCodeStep.execute(wizardContext);

  vscode.window.showInformationMessage(
    localize('moveToDotnetCompleted', 'Completed moving your Logic App project to a NuGet-based project.'),
    'OK'
  );
}