protected override void AddFileToProject(string path) { if (ExcludeFile(path)) { return; } string folderPath = Path.GetDirectoryName(path); string fullPath = FileSystemUtility.GetFullPath(EnvDTEProjectUtility.GetFullPath(EnvDTEProject), path); // Add the file to project or folder EnvDTEProjectItems container = EnvDTEProjectUtility.GetProjectItems(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(MessageLevel.Debug, Strings.Debug_AddedFileToProject, path, ProjectName); }
public override void RemoveFile(string path) { if (string.IsNullOrEmpty(path)) { return; } string folderPath = Path.GetDirectoryName(path); var root = EnvDTEProjectUtility.GetFullPath(EnvDTEProject); string fullPath = FileSystemUtility.GetFullPath(root, path); bool succeeded; succeeded = VCProjectHelper.RemoveFileFromProject(EnvDTEProject.Object, fullPath, folderPath); if (succeeded) { // The RemoveFileFromProject() method only removes file from project. // We want to delete it from disk too. FileSystemUtility.DeleteFileAndParentDirectoriesIfEmpty(root, path, NuGetProjectContext); if (!String.IsNullOrEmpty(folderPath)) { NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_RemovedFileFromFolder, Path.GetFileName(path), folderPath); } else { NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_RemovedFile, Path.GetFileName(path)); } } }
public VSMSBuildNuGetProjectSystem(EnvDTEProject envDTEProject, INuGetProjectContext nuGetProjectContext) { if (envDTEProject == null) { throw new ArgumentNullException("envDTEProject"); } if (nuGetProjectContext == null) { throw new ArgumentNullException("nuGetProjectContext"); } EnvDTEProject = envDTEProject; ProjectFullPath = EnvDTEProjectUtility.GetFullPath(envDTEProject); NuGetProjectContext = nuGetProjectContext; }
public override void RemoveImport(string targetFullPath) { if (String.IsNullOrEmpty(targetFullPath)) { throw new ArgumentNullException(nameof(targetFullPath), CommonResources.Argument_Cannot_Be_Null_Or_Empty); } ThreadHelper.JoinableTaskFactory.Run(async delegate { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var root = EnvDTEProjectUtility.GetFullPath(EnvDTEProject); // For VS 2012 or above, the operation has to be done inside the Writer lock string relativeTargetPath = PathUtility.GetRelativePath(PathUtility.EnsureTrailingSlash(root), targetFullPath); await RemoveImportStatementForVS2013Async(relativeTargetPath); }); }
public override void AddImport(string targetFullPath, ImportLocation location) { // For VS 2012 or above, the operation has to be done inside the Writer lock if (String.IsNullOrEmpty(targetFullPath)) { throw new ArgumentNullException(nameof(targetFullPath)); } ThreadHelper.JoinableTaskFactory.Run(async delegate { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var root = EnvDTEProjectUtility.GetFullPath(EnvDTEProject); string relativeTargetPath = PathUtility.GetRelativePath(PathUtility.EnsureTrailingSlash(root), targetFullPath); await AddImportStatementForVS2013Async(location, relativeTargetPath); }); }
protected override void AddFileToProject(string path) { if (ExcludeFile(path)) { return; } // Get the project items for the folder path string folderPath = Path.GetDirectoryName(path); string fullPath = FileSystemUtility.GetFullPath(EnvDTEProjectUtility.GetFullPath(EnvDTEProject), path);; ThreadHelper.Generic.Invoke(() => { VCProjectHelper.AddFileToProject(EnvDTEProject.Object, fullPath, folderPath); }); NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_AddedFileToProject, path, ProjectName); }
public void AddBindingRedirects() { var settings = ServiceLocator.GetInstanceSafe <Configuration.ISettings>(); var behavior = new BindingRedirectBehavior(settings); if (!behavior.IsSkipped) { NuGetUIThreadHelper.JoinableTaskFactory.Run(async delegate { try { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); InitForBindingRedirects(); if (IsBindingRedirectSupported && VSSolutionManager != null) { await RuntimeHelpers.AddBindingRedirectsAsync(VSSolutionManager, EnvDTEProject, VSFrameworkMultiTargeting, NuGetProjectContext); } } catch (Exception ex) { var fileName = EnvDTEProjectUtility.GetFullPath(EnvDTEProject); var level = behavior.FailOperations ? ProjectManagement.MessageLevel.Error : ProjectManagement.MessageLevel.Warning; NuGetProjectContext.Log(level, Strings.FailedToUpdateBindingRedirects, fileName, ex.Message); if (behavior.FailOperations) { throw; } } }); } }
public static async Task <IEnumerable <AssemblyBinding> > AddBindingRedirectsAsync( ISolutionManager solutionManager, EnvDTEProject envDTEProject, AppDomain domain, IDictionary <string, HashSet <string> > projectAssembliesCache, IVsFrameworkMultiTargeting frameworkMultiTargeting, INuGetProjectContext nuGetProjectContext) { // Run this on the UI thread since it enumerates all references await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var redirects = Enumerable.Empty <AssemblyBinding>(); var msBuildNuGetProjectSystem = GetMSBuildNuGetProjectSystem(solutionManager, envDTEProject); // If no msBuildNuGetProjectSystem, no binding redirects. Bail if (msBuildNuGetProjectSystem == null) { return(redirects); } // Get the full path from envDTEProject var root = EnvDTEProjectUtility.GetFullPath(envDTEProject); IEnumerable <string> assemblies = EnvDTEProjectUtility.GetAssemblyClosure(envDTEProject, projectAssembliesCache); redirects = BindingRedirectResolver.GetBindingRedirects(assemblies, domain); if (frameworkMultiTargeting != null) { // filter out assemblies that already exist in the target framework (CodePlex issue #3072) FrameworkName targetFrameworkName = EnvDTEProjectUtility.GetDotNetFrameworkName(envDTEProject); redirects = redirects.Where(p => !FrameworkAssemblyResolver.IsHigherAssemblyVersionInFramework(p.Name, p.AssemblyNewVersion, targetFrameworkName)); } // Create a binding redirect manager over the configuration var manager = new BindingRedirectManager(EnvDTEProjectUtility.GetConfigurationFile(envDTEProject), msBuildNuGetProjectSystem); // Add the redirects manager.AddBindingRedirects(redirects); return(redirects); }
public override void RemoveImport(string targetPath) { if (String.IsNullOrEmpty(targetPath)) { throw new ArgumentNullException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "targetPath"); } var root = EnvDTEProjectUtility.GetFullPath(EnvDTEProject); // For VS 2012 or above, the operation has to be done inside the Writer lock string relativeTargetPath = PathUtility.GetRelativePath(PathUtility.EnsureTrailingSlash(root), targetPath); if (VSVersionHelper.IsVisualStudio2012) { EnvDTEProjectUtility.DoWorkInWriterLock(EnvDTEProject, buildProject => MicrosoftBuildEvaluationProjectUtility.RemoveImportStatement(buildProject, relativeTargetPath)); EnvDTEProjectUtility.Save(EnvDTEProject); } else { RemoveImportStatementForVS2013(relativeTargetPath); } }
public override void AddReference(string referencePath) { string name = Path.GetFileNameWithoutExtension(referencePath); try { var root = EnvDTEProjectUtility.GetFullPath(EnvDTEProject); EnvDTEProjectUtility.GetAssemblyReferences(EnvDTEProject).AddFromFile(PathUtility.GetAbsolutePath(root, referencePath)); // Always create a refresh file. Vs does this for us in most cases, however for GACed binaries, it resorts to adding a web.config entry instead. // This may result in deployment issues. To work around ths, we'll always attempt to add a file to the bin. RefreshFileUtility.CreateRefreshFile(root, PathUtility.GetAbsolutePath(EnvDTEProjectUtility.GetFullPath(EnvDTEProject), referencePath), this); NuGetProjectContext.Log(MessageLevel.Debug, Strings.Debug_AddReference, name, ProjectName); } catch (Exception e) { throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Strings.FailedToAddReference, name), e); } }
public override void RemoveReference(string name) { // Remove the reference via DTE. RemoveDTEReference(name); // For GACed binaries, VS would not clear the refresh files for us since it assumes the reference exists in web.config. // We'll clean up any remaining .refresh files. var refreshFilePath = Path.Combine("bin", Path.GetFileName(name) + ".refresh"); var root = EnvDTEProjectUtility.GetFullPath(EnvDTEProject); var refreshFileFullPath = FileSystemUtility.GetFullPath(root, refreshFilePath); if (File.Exists(refreshFileFullPath)) { try { FileSystemUtility.DeleteFile(refreshFileFullPath, NuGetProjectContext); } catch (Exception e) { NuGetProjectContext.Log(MessageLevel.Warning, e.Message); } } }
public virtual void RemoveImport(string targetFullPath) { if (String.IsNullOrEmpty(targetFullPath)) { throw new ArgumentNullException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, "targetPath"); } string relativeTargetPath = PathUtility.GetRelativePath(PathUtility.EnsureTrailingSlash(EnvDTEProjectUtility.GetFullPath(EnvDTEProject)), targetFullPath); EnvDTEProjectUtility.RemoveImportStatement(EnvDTEProject, relativeTargetPath); EnvDTEProjectUtility.Save(EnvDTEProject); // notify the project system of the change UpdateImportStamp(EnvDTEProject); }
public bool TryCreateNuGetProject(EnvDTE.Project envDTEProject, ProjectSystemProviderContext context, out NuGetProject result) { if (envDTEProject == null) { throw new ArgumentNullException(nameof(envDTEProject)); } if (context == null) { throw new ArgumentNullException(nameof(context)); } ThreadHelper.ThrowIfNotOnUIThread(); result = null; var msBuildNuGetProjectSystem = MSBuildNuGetProjectSystemFactory.CreateMSBuildNuGetProjectSystem( envDTEProject, context.ProjectContext); var isWebSite = msBuildNuGetProjectSystem is WebSiteProjectSystem; // Web sites cannot have project.json if (!isWebSite) { // Find the project file path var projectFilePath = EnvDTEProjectUtility.GetFullProjectPath(envDTEProject); if (!string.IsNullOrEmpty(projectFilePath)) { var msbuildProjectFile = new FileInfo(projectFilePath); var projectNameFromMSBuildPath = Path.GetFileNameWithoutExtension(msbuildProjectFile.Name); // Treat projects with project.json as build integrated projects // Search for projectName.project.json first, then project.json // If the name cannot be determined, search only for project.json string projectJsonPath = null; if (string.IsNullOrEmpty(projectNameFromMSBuildPath)) { projectJsonPath = Path.Combine(msbuildProjectFile.DirectoryName, ProjectJsonPathUtilities.ProjectConfigFileName); } else { projectJsonPath = ProjectJsonPathUtilities.GetProjectConfigPath(msbuildProjectFile.DirectoryName, projectNameFromMSBuildPath); } if (File.Exists(projectJsonPath)) { result = new ProjectJsonBuildIntegratedProjectSystem( projectJsonPath, msbuildProjectFile.FullName, envDTEProject, EnvDTEProjectUtility.GetCustomUniqueName(envDTEProject)); } } } // Create a normal MSBuild project if no project.json was found if (result == null) { var folderNuGetProjectFullPath = context.PackagesPathFactory(); // Project folder path is the packages config folder path var packagesConfigFolderPath = EnvDTEProjectUtility.GetFullPath(envDTEProject); result = new VSMSBuildNuGetProject( envDTEProject, msBuildNuGetProjectSystem, folderNuGetProjectFullPath, packagesConfigFolderPath); } return(result != null); }