Ejemplo n.º 1
0
        // 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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void ProjectFinishedGenerating(Project project)
        {
            this.project = project;

            string ngNewOutput      = String.Empty;
            string readmeMdFilePath = null;

            // We included empty README.md and package.json to make the files discoverable by the IDE from the very beginning.
            // If the files were marked in the Solution Explorer as missing, opening them would take more time (??? assumption not tested) and cause flicker.
            // Now we are going to replace them with the real ones.
            var projectDirectory = Path.GetDirectoryName(project.FullName);

            if (Directory.Exists(projectDirectory))
            {
                readmeMdFilePath = Path.Combine(projectDirectory, readmeMdFileName);
                if (File.Exists(readmeMdFilePath))
                {
                    File.Delete(readmeMdFilePath);
                }
                var packageJsonFilePath = Path.Combine(projectDirectory, packageJsonFileName);
                if (File.Exists(packageJsonFilePath))
                {
                    File.Delete(packageJsonFilePath);
                }
                // 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
                var gitignoreFilePath     = Path.Combine(projectDirectory, gitignoreFileName);
                var gitignoreTempFilePath = Path.Combine(projectDirectory, gitignoreTempFileName);
                if (File.Exists(gitignoreFilePath))
                {
                    File.Move(gitignoreFilePath, gitignoreTempFilePath);
                }

                var projectName = project.Name.Replace('.', '-').Replace('_', '-');
                // Run "ng new"
                // ngNewOutput = RunNgNew(projectDirectory, project.Name, this.addRouting);
                ngNewOutput = NgWizardHelper.RunNgNew(projectDirectory, projectName, this.addRouting, this.isNgFound);

                if (File.Exists(gitignoreTempFilePath))
                {
                    if (File.Exists(gitignoreFilePath))
                    {
                        File.Delete(gitignoreFilePath);
                    }
                    File.Move(gitignoreTempFilePath, gitignoreFilePath);
                }
            }

            // Find the file created by the "ng new".
            var ngNewSucceeded = File.Exists(readmeMdFilePath);

            var messageText = ngNewSucceeded
                ? String.Format(WizardResources.ReadmeSuccessMessage, project.Name, NgWizardHelper.GetVersion())
                : WizardResources.ReadmeFailureMessage + ngNewOutput;

            ;

            // The Resource returns backslashes escaped. We cannot use regular 'Shift+Enter' line breakes in the editor, besause they produce "\r\n", whereas Ng-generated text has "\n", and Visual Studio Editor displays a dialog asking to normalize line breakes.
            messageText = messageText.Replace("\\n", "\n");

            // Augment README.md with our message.
            if (ngNewSucceeded)
            {
                var oldText = File.ReadAllText(readmeMdFilePath);
                var newText = messageText + oldText;
                File.WriteAllText(readmeMdFilePath, newText);
            }
            else
            {
                // If the "ng new" failed, create a substitute file to display.
                if (readmeMdFilePath != null)
                {
                    File.WriteAllText(readmeMdFilePath, messageText);
                }
            }
        }