예제 #1
0
        public void AddToProject(ProjectLocation location, CreateFilesOperationResult creationResult)
        {
            string folderPath = location.FolderPath;
            string targetProjectUniqueName = location.ProjectUniqueName;

            var project = FindProject(targetProjectUniqueName);

            if (project == null)
            {
                throw new ArgumentException(Strings.ProjectNotFound.FormatUI(targetProjectUniqueName), nameof(targetProjectUniqueName));
            }

            var parentItems = GetTargetProjectItems(project, folderPath);

            // Remember which folder items we're adding, because we can't query them
            // in F# project system
            var folderItems = new Dictionary <string, EnvDTE.ProjectItems>();

            try {
                foreach (var createdFolderPath in creationResult.FoldersCreated)
                {
                    var absoluteFilePath = Path.Combine(folderPath, createdFolderPath);
                    var folder           = GetOrCreateFolderItem(parentItems, createdFolderPath);
                    folderItems[createdFolderPath] = folder;
                }
            } catch (NotImplementedException) {
                // Some project types such as C++ don't support creating folders
            }

            foreach (var createdFilePath in creationResult.FilesCreated)
            {
                var absoluteFilePath = Path.Combine(folderPath, createdFilePath);
                EnvDTE.ProjectItems itemParent;
                try {
                    itemParent = GetOrCreateFolderItem(parentItems, Path.GetDirectoryName(createdFilePath));
                } catch (NotImplementedException) {
                    // Some project types such as C++ don't support creating folders
                    // so we'll add everything flat
                    itemParent = parentItems;
                } catch (ArgumentException) {
                    // Some project types such as F# don't return folders as ProjectItem
                    // so we can't find the folder we just created above. Attempting to
                    // create it generates a ArgumentException saying the folder already exists.
                    if (!folderItems.TryGetValue(Path.GetDirectoryName(createdFilePath), out itemParent))
                    {
                        itemParent = parentItems;
                    }
                }

                if (FindItemByName(itemParent, Path.GetFileName(createdFilePath)) == null)
                {
                    itemParent.AddFromFile(absoluteFilePath);
                }
            }
        }
예제 #2
0
        public void AddToProject(ProjectLocation location, CreateFilesOperationResult creationResult) {
            string folderPath = location.FolderPath;
            string targetProjectUniqueName = location.ProjectUniqueName;

            var project = FindProject(targetProjectUniqueName);
            if (project == null) {
                throw new ArgumentException(Strings.ProjectNotFound.FormatUI(targetProjectUniqueName), nameof(targetProjectUniqueName));
            }

            var parentItems = GetTargetProjectItems(project, folderPath);

            // Remember which folder items we're adding, because we can't query them
            // in F# project system
            var folderItems = new Dictionary<string, EnvDTE.ProjectItems>();
            try {
                foreach (var createdFolderPath in creationResult.FoldersCreated) {
                    var absoluteFilePath = Path.Combine(folderPath, createdFolderPath);
                    var folder = GetOrCreateFolderItem(parentItems, createdFolderPath);
                    folderItems[createdFolderPath] = folder;
                }
            } catch (NotImplementedException) {
                // Some project types such as C++ don't support creating folders
            }

            foreach (var createdFilePath in creationResult.FilesCreated) {
                var absoluteFilePath = Path.Combine(folderPath, createdFilePath);
                EnvDTE.ProjectItems itemParent;
                try {
                    itemParent = GetOrCreateFolderItem(parentItems, Path.GetDirectoryName(createdFilePath));
                } catch (NotImplementedException) {
                    // Some project types such as C++ don't support creating folders
                    // so we'll add everything flat
                    itemParent = parentItems;
                } catch (ArgumentException) {
                    // Some project types such as F# don't return folders as ProjectItem
                    // so we can't find the folder we just created above. Attempting to
                    // create it generates a ArgumentException saying the folder already exists.
                    if (!folderItems.TryGetValue(Path.GetDirectoryName(createdFilePath), out itemParent)) {
                        itemParent = parentItems;
                    }
                }

                if (FindItemByName(itemParent, Path.GetFileName(createdFilePath)) == null) {
                    itemParent.AddFromFile(absoluteFilePath);
                }
            }
        }
예제 #3
0
        public async Task CreateFilesAsync() {
            var selection = SelectedTemplate;
            Debug.Assert(selection != null);
            if (selection == null) {
                throw new InvalidOperationException("CreateFilesAsync called with null SelectedTemplate");
            }

            ResetStatus();

            CreatingStatus = OperationStatus.InProgress;
            OpenInExplorerFolderPath = null;

            try {
                var contextFilePath = Path.GetTempFileName();
                SaveUserInput(contextFilePath);

                _outputWindow.ShowAndActivate();
                _outputWindow.WriteLine(String.Empty);
                _outputWindow.WriteLine(Strings.RunningTemplateStarted.FormatUI(selection.DisplayName));

                var operationResult = await _cutterClient.CreateFilesAsync(_templateLocalFolderPath, UserConfigFilePath, contextFilePath, OutputFolderPath);

                if (operationResult.FilesReplaced.Length > 0) {
                    _outputWindow.WriteLine(Strings.ReplacedFilesHeader);
                    foreach (var replacedfile in operationResult.FilesReplaced) {
                        _outputWindow.WriteLine(Strings.ReplacedFile.FormatUI(replacedfile.OriginalFilePath, replacedfile.BackupFilePath));
                    }
                }

                try {
                    File.Delete(contextFilePath);
                } catch (UnauthorizedAccessException) {
                } catch (IOException) {
                }

                _outputWindow.WriteLine(Strings.RunningTemplateSuccess.FormatUI(selection.DisplayName, OutputFolderPath));

                ReportTemplateEvent(CookiecutterTelemetry.TelemetryArea.Template, CookiecutterTelemetry.TemplateEvents.Run, selection);

                ContextItems.Clear();
                ResetStatus();
                _templateLocalFolderPath = null;
                OpenInExplorerFolderPath = OutputFolderPath;
                CreatingStatus = OperationStatus.Succeeded;

                if (TargetProjectLocation != null) {
                    try {
                        var location = new ProjectLocation() {
                            FolderPath = OpenInExplorerFolderPath,
                            ProjectUniqueName = TargetProjectLocation.ProjectUniqueName,
                        };
                        _projectSystemClient.AddToProject(location, operationResult);

                        ReportTemplateEvent(CookiecutterTelemetry.TelemetryArea.Template, CookiecutterTelemetry.TemplateEvents.AddToProject, selection);
                    } catch (Exception ex) when (!ex.IsCriticalException()) {
                        _outputWindow.WriteErrorLine(Strings.AddToProjectError.FormatUI(ex.Message));

                        ReportTemplateEvent(CookiecutterTelemetry.TelemetryArea.Template, CookiecutterTelemetry.TemplateEvents.AddToProject, selection, ex);
                    }
                }

                Home();
            } catch (Exception ex) when (!ex.IsCriticalException()) {
                CreatingStatus = OperationStatus.Failed;

                _outputWindow.WriteErrorLine(ex.Message);
                _outputWindow.WriteLine(Strings.RunningTemplateFailed.FormatUI(selection.DisplayName));

                ReportTemplateEvent(CookiecutterTelemetry.TelemetryArea.Template, CookiecutterTelemetry.TemplateEvents.Run, selection, ex);
            }
        }
예제 #4
0
 public void AddToProject(ProjectLocation location, CreateFilesOperationResult creationResult) {
     Added.Add(Tuple.Create(location, creationResult));
 }
예제 #5
0
 internal void NewSession(ProjectLocation location) {
     _cookiecutterPage?.NewSession(location);
 }
예제 #6
0
        internal void NewSession(ProjectLocation location) {
            Home();

            if (location != null) {
                ViewModel.OutputFolderPath = location.FolderPath;
                ViewModel.FixedOutputFolder = true;
                ViewModel.TargetProjectLocation = location;
            }
        }