public void RunStarted(object automationObject, Dictionary <string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) { replacementsDictionary.TryGetValue("$solutiondirectory$", out string solutionDirectory); var settings = new NgItemWizardViewModel.NgItemWizardSettings(); // Test if @angular/cli is installed globally. this.isNgFound = NgWizardHelper.IsNgFound(solutionDirectory); settings.IsNgFound = this.isNgFound; var dte = (DTE)automationObject; var activeProjects = (Array)dte.ActiveSolutionProjects; if (activeProjects.Length > 0) { var project = (Project)activeProjects.GetValue(0); // The NuGet package needs netstandard2.0. We don't support ASP.NET Core 1.x projects. settings.IsAspNetCore2 = NgWizardHelper.IsAspNetCore2(project); // Look for an existing .angular-cli.json indicating there has been already an Angular CLI app created. settings.IsAngularCliJsonFound = NgWizardHelper.FindFileInRootDir(project, NgWizardHelper.AngularCliJsonFileName); // Test if a package.json exists. settings.IsOldPackageJsonFound = NgWizardHelper.FindFileInRootDir(project, NgWizardHelper.PackageJsonFileName, out string packageJsonFilePath); // Test if an entry for "@angular/core" exists in package.json. settings.IsNpmAngularFound = NgWizardHelper.IsNpmAngularFound(packageJsonFilePath); // Automatic installation is disabled if .gitignore or package.json or tsconfig.json or Startup.cs is opened in an editor window. settings.IsGitignoreOpened = NgWizardHelper.IsFileOpened(project, NgWizardHelper.GitignoreFileName); settings.IsPackageJsonOpened = NgWizardHelper.IsFileOpened(project, NgWizardHelper.PackageJsonFileName); settings.IsStartupCsOpened = NgWizardHelper.IsFileOpened(project, NgWizardHelper.StartupCsFileName); settings.IsTsconfigJsonOpened = NgWizardHelper.IsFileOpened(project, NgWizardHelper.TsconfigJsonFileName); } // Display the wizard to the user. var viewModel = new NgItemWizardViewModel(settings); var mainWindow = new NgItemWizardWindow(viewModel); var accepted = mainWindow.ShowDialog().GetValueOrDefault(); this.installAutomatically = viewModel.InstallAutomatically; if (!accepted) { throw new WizardCancelledException("The wizard has been cancelled by the user."); } }
// This method is only called for item templates, not for project templates. public void ProjectItemFinishedGenerating(ProjectItem projectItem) { var project = projectItem.ContainingProject; if (this.installAutomatically && (project != null)) { string ngNewOutput = String.Empty; bool? ngNewSucceeded = null; bool? mergedPackageJsonFiles = null; bool? modifiedAngularCliJson = null; bool? modifiedStartupSc = null; bool? mergedGitignoreFiles = null; bool renamedTsconfigJson = false; var projectDirectory = Path.GetDirectoryName(project.FullName); if (Directory.Exists(projectDirectory)) { // Starting from ver.1.4, the CLI creates a ".gitignore" file even if the "--ignore-git" option is specified. So "ng new --ignore-git" fails if there is an existing .gitignore in the directory. // +https://github.com/angular/angular-cli/issues/7686 RenameFileIfExists(projectDirectory, NgWizardHelper.GitignoreFileName, NgWizardHelper.GitignoreTempFileName); RenameFileIfExists(projectDirectory, NgWizardHelper.PackageJsonFileName, NgWizardHelper.PackageJsonOldFileName); renamedTsconfigJson = RenameFileIfExists(projectDirectory, NgWizardHelper.TsconfigJsonFileName, NgWizardHelper.TsconfigJsonOldFileName); // Run "ng new" ngNewOutput = NgWizardHelper.RunNgNew(projectDirectory, project.Name, true, this.isNgFound); // Find the .angular-cli.json created by "ng new". ngNewSucceeded = NgWizardHelper.FindFileInRootDir(project, NgWizardHelper.AngularCliJsonFileName); if (ngNewSucceeded.Value) { mergedPackageJsonFiles = MergePackageJsonFiles(projectDirectory); modifiedAngularCliJson = ModifyAngularCliJsonFile(projectDirectory); modifiedStartupSc = ModifyStartupCsFile(projectDirectory); mergedGitignoreFiles = MergeGitignoreFile(projectDirectory); } else { // Rallback renamed files. RenameFileIfExists(projectDirectory, NgWizardHelper.GitignoreTempFileName, NgWizardHelper.GitignoreFileName); RenameFileIfExists(projectDirectory, NgWizardHelper.PackageJsonOldFileName, NgWizardHelper.PackageJsonFileName); renamedTsconfigJson = renamedTsconfigJson && !RenameFileIfExists(projectDirectory, NgWizardHelper.TsconfigJsonOldFileName, NgWizardHelper.TsconfigJsonFileName); } } // Report success/failure of the steps. var ngNewReport = (ngNewSucceeded.GetValueOrDefault() ? "An Angular CLI application was added to the project using the item template version " + NgWizardHelper.GetVersion().ToString() : "Something went wrong with the creation of an Angular CLI application." + (this.isNgFound ? LineBreak + " Error message: " + ngNewOutput : "") ) + LineBreak; var packageJsonReport = mergedPackageJsonFiles.HasValue ? "Merging the package.json files: " + GetResultText(mergedPackageJsonFiles) + LineBreak : String.Empty; var angularCliJsonReport = modifiedAngularCliJson.HasValue ? "Modifying file .angular-cli.json: " + GetResultText(modifiedAngularCliJson) + LineBreak : String.Empty; var startupCsReport = modifiedStartupSc.HasValue ? "Modifying file Startup.cs: " + GetResultText(modifiedStartupSc) + LineBreak : String.Empty; var gitignoreReport = mergedGitignoreFiles.HasValue ? "Merging the .gitignore files: " + GetResultText(mergedGitignoreFiles) + LineBreak : String.Empty; var tsconfigJsonReport = renamedTsconfigJson ? "Renaming file tsconfig.json to tsconfig.json.old: " + GetResultText(renamedTsconfigJson) + LineBreak : String.Empty; var messageText = ngNewReport + packageJsonReport + angularCliJsonReport + startupCsReport + gitignoreReport + tsconfigJsonReport + LineBreak; // Augment the item file with our message. if (projectItem.FileCount != 0) { var fileName = projectItem.FileNames[1]; // 1-based array if (File.Exists(fileName)) { var oldText = File.ReadAllText(fileName); var newText = messageText + oldText; File.WriteAllText(fileName, newText); } } } }