protected override async Task AddFileToProjectAsync(string path) { if (ExcludeFile(path)) { return; } var folderPath = Path.GetDirectoryName(path); var fullPath = FileSystemUtility.GetFullPath(ProjectFullPath, path); await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); // Add the file to project or folder EnvDTEProjectItems container = await GetProjectItemsAsync(folderPath, createIfNotExists : true); if (container == null) { throw new ArgumentException( string.Format( CultureInfo.CurrentCulture, Strings.Error_FailedToCreateParentFolder, path, ProjectName)); } container.AddFromFileCopy(fullPath); NuGetProjectContext.Log(ProjectManagement.MessageLevel.Debug, Strings.Debug_AddedFileToProject, path, ProjectName); }
/// <summary> /// This method should be on the UI thread. The overrides should ensure that /// </summary> protected virtual void AddFileToContainer(string fullPath, string folderPath, EnvDTEProjectItems container) { Debug.Assert(ThreadHelper.CheckAccess()); container.AddFromFileCopy(fullPath); }
protected virtual void AddFileToContainer(string fullPath, string folderPath, EnvDTEProjectItems container) { container.AddFromFileCopy(fullPath); }
static void CopyFilesAndFoldersRecursively(IEnumerable <ProjectItemWrapper> sourceProjectItems, EnvDTE.ProjectItems destinationProjectItems, Context.CopyOrAddAsLink howToDealWithCSharpFiles, int level = 1) { if (!StaticAbortProcessing) { foreach (ProjectItemWrapper sourceItem in sourceProjectItems) { string sourceItemName = sourceItem.GetName(); string sourceItemFullPath = sourceItem.GetFullPath(); //--------------------- // If it is a folder: //--------------------- if (FoldersHelper.IsAFolder(sourceItemFullPath)) { // If it is NOT the "Properties" folder (which exists by default in all projects): if (level != 1 || sourceItemName.ToLower() != "properties") { // Create a new folder with the same name in the destination project: EnvDTE.ProjectItem newFolder = destinationProjectItems.AddFolder(sourceItemName); // Log: AddLogEntryOnUIThread(string.Format(@"Created folder ""{0}"".", sourceItemName)); // Continue the recursion: CopyFilesAndFoldersRecursively(sourceItem.GetChildItems(), newFolder.ProjectItems, howToDealWithCSharpFiles, level + 1); } } //--------------------- // If it is a file: //--------------------- else { EnvDTE.ProjectItem newFile = null; if (sourceItemFullPath.ToLower().EndsWith(".cs")) { //--------------------- // C# file: //--------------------- // Check if the file exists (this can be the case for example if we previously copied a file such as "UserControl.xaml", which automatically copies its child "UserControl.xaml.cs"): EnvDTE.ProjectItem existingItem = FindProjectItemOrNull(sourceItemName, destinationProjectItems); // Copy the file or add it as link: if (howToDealWithCSharpFiles == Context.CopyOrAddAsLink.Copy) { // Copy only if the file is not already there: if (existingItem == null) { newFile = destinationProjectItems.AddFromFileCopy(sourceItemFullPath); } // Log: AddLogEntryOnUIThread(string.Format(@"Copied file ""{0}"".", sourceItemName)); } else if (howToDealWithCSharpFiles == Context.CopyOrAddAsLink.AddAsLink) { // Delete the copied file in order to replace it with a linked file (this can happen for example if we previously copied a file such as "UserControl.xaml", which automatically copies its child "UserControl.xaml.cs", so we need to delete this child): if (existingItem != null) { existingItem.Delete(); } // Add the file "as link": newFile = destinationProjectItems.AddFromFile(sourceItemFullPath); // Log: AddLogEntryOnUIThread(string.Format(@"Added file ""{0}"" as link.", sourceItemName)); } else { throw new NotSupportedException(); } } else if (sourceItemFullPath.ToLower().EndsWith(".xaml") || DoesFileHaveOneOfTheseExtensions(sourceItemFullPath, ExtensionsOfFilesToCopy)) // JPG, PNG, etc. { //--------------------- // XAML file: //--------------------- // Copy the file: newFile = destinationProjectItems.AddFromFileCopy(sourceItemFullPath); // Log: AddLogEntryOnUIThread(string.Format(@"Copied file ""{0}"".", sourceItemName)); } // Continue the recursion: if (newFile != null) { CopyFilesAndFoldersRecursively(sourceItem.GetChildItems(), newFile.ProjectItems, howToDealWithCSharpFiles, level + 1); } } if (StaticAbortProcessing) { break; } } } }