private async Task AddProjectItemAsync(string filePath) { var folderPath = Path.GetDirectoryName(filePath); var fullPath = filePath; string projectDirectory = await _vsProjectAdapter.GetProjectDirectoryAsync(); if (filePath.Contains(projectDirectory)) { // folderPath should always be relative to ProjectDirectory so if filePath already contains // ProjectDirectory then get a relative path and construct folderPath to get the appropriate // ProjectItems from dte where you have to add this file. var relativeLockFilePath = FileSystemUtility.GetRelativePath(projectDirectory, filePath); folderPath = Path.GetDirectoryName(relativeLockFilePath); } else { // get the fullPath wrt ProjectDirectory fullPath = FileSystemUtility.GetFullPath(projectDirectory, filePath); } var container = await EnvDTEProjectUtility.GetProjectItemsAsync(_vsProjectAdapter.Project, folderPath, createIfNotExists : true); await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync(); container.AddFromFileCopy(fullPath); }
protected override async Task AddFileToProjectAsync(string path) { Debug.Assert(ThreadHelper.CheckAccess()); if (ExcludeFile(path)) { return; } string folderPath = Path.GetDirectoryName(path); string fullPath = FileSystemUtility.GetFullPath(ProjectFullPath, path); // Add the file to project or folder EnvDTEProjectItems container = await EnvDTEProjectUtility.GetProjectItemsAsync(EnvDTEProject, folderPath, createIfNotExists : true); if (container == null) { throw new ArgumentException( String.Format( CultureInfo.CurrentCulture, Strings.Error_FailedToCreateParentFolder, path, ProjectName)); } AddFileToContainer(fullPath, folderPath, container); NuGetProjectContext.Log(ProjectManagement.MessageLevel.Debug, Strings.Debug_AddedFileToProject, path, ProjectName); }
public override void AddFile(string path, Action <Stream> writeToStream) { // ensure the parent folder is created before adding file to the project if (string.IsNullOrEmpty(path)) { return; } ThreadHelper.JoinableTaskFactory.Run(async delegate { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); await EnvDTEProjectUtility.GetProjectItemsAsync(EnvDTEProject, Path.GetDirectoryName(path), createIfNotExists: true); base.AddFile(path, writeToStream); }); }
/// <summary> /// This method should be on the UI thread. The overrides should ensure that /// </summary> protected virtual async Task AddFileToProjectAsync(string path) { Debug.Assert(ThreadHelper.CheckAccess()); if (ExcludeFile(path)) { return; } // Get the project items for the folder path string folderPath = Path.GetDirectoryName(path); string fullPath = FileSystemUtility.GetFullPath(ProjectFullPath, path); var container = await EnvDTEProjectUtility.GetProjectItemsAsync(EnvDTEProject, folderPath, createIfNotExists : true); // Add the file to project or folder AddFileToContainer(fullPath, folderPath, container); NuGetProjectContext.Log(ProjectManagement.MessageLevel.Debug, Strings.Debug_AddedFileToProject, path, ProjectName); }
protected async Task <EnvDTE.ProjectItems> GetProjectItemsAsync(string folderPath, bool createIfNotExists) { return(await EnvDTEProjectUtility.GetProjectItemsAsync(VsProjectAdapter.Project, folderPath, createIfNotExists)); }