/// <summary> /// This method should be on the UI thread. The overrides should ensure that /// Sets NuGetPackageImportStamp to a new random guid. This is a hack to let the project system know it is out /// of date. /// The value does not matter, it just needs to change. /// </summary> protected static void UpdateImportStamp(EnvDTEProject envDTEProject) { Debug.Assert(ThreadHelper.CheckAccess()); IVsBuildPropertyStorage propStore = VsHierarchyUtility.ToVsHierarchy(envDTEProject) as IVsBuildPropertyStorage; if (propStore != null) { // <NuGetPackageImportStamp>af617720</NuGetPackageImportStamp> string stamp = Guid.NewGuid().ToString().Split('-')[0]; try { propStore.SetPropertyValue(NuGetImportStamp, string.Empty, (uint)_PersistStorageType.PST_PROJECT_FILE, stamp); } catch (Exception ex1) { ExceptionHelper.WriteToActivityLog(ex1); } // Remove the NuGetImportStamp so that VC++ project file won't be updated with this stamp on disk, // which causes unnecessary source control pending changes. try { propStore.RemoveProperty(NuGetImportStamp, string.Empty, (uint)_PersistStorageType.PST_PROJECT_FILE); } catch (Exception ex2) { ExceptionHelper.WriteToActivityLog(ex2); } } }
public void GlobalProperties() { using (PackageTestEnvironment testEnv = new PackageTestEnvironment()) { IVsBuildPropertyStorage buildProperty = testEnv.Project as IVsBuildPropertyStorage; Assert.IsNotNull(buildProperty, "Project does not implements IVsBuildPropertyStorage."); // Get string propertyName = "GlobalProperty"; string value = null; int hr = buildProperty.GetPropertyValue(propertyName, null, (uint)_PersistStorageType.PST_PROJECT_FILE, out value); Assert.AreEqual <int>(VSConstants.S_OK, hr, "GetPropertyValue failed"); Assert.AreEqual("Global", value); // Set (with get to confirm) string newValue = "UpdatedGlobal"; hr = buildProperty.SetPropertyValue(propertyName, null, (uint)_PersistStorageType.PST_PROJECT_FILE, newValue); Assert.AreEqual <int>(VSConstants.S_OK, hr, "SetPropertyValue failed"); hr = buildProperty.GetPropertyValue(propertyName, null, (uint)_PersistStorageType.PST_PROJECT_FILE, out value); Assert.AreEqual <int>(VSConstants.S_OK, hr, "GetPropertyValue failed"); Assert.AreEqual(newValue, value); // Remove (with get to confirm) hr = buildProperty.RemoveProperty(propertyName, null, (uint)_PersistStorageType.PST_PROJECT_FILE); Assert.AreEqual <int>(VSConstants.S_OK, hr, "RemoveProperty failed"); hr = buildProperty.GetPropertyValue(propertyName, null, (uint)_PersistStorageType.PST_PROJECT_FILE, out value); Assert.AreEqual <int>(VSConstants.S_OK, hr, "GetPropertyValue failed"); Assert.AreEqual(String.Empty, value); } }
public void ClearProjectProperty(Project dteProject, string propertyName) { if (dteProject == null) { throw new ArgumentNullException(nameof(dteProject)); } if (string.IsNullOrWhiteSpace(propertyName)) { throw new ArgumentNullException(nameof(propertyName)); } IVsHierarchy projectHierarchy = this.GetIVsHierarchy(dteProject); IVsBuildPropertyStorage propertyStorage = projectHierarchy as IVsBuildPropertyStorage; if (propertyStorage != null) { var hr = propertyStorage.RemoveProperty(propertyName, string.Empty, (uint)_PersistStorageType.PST_PROJECT_FILE); Debug.Assert(ErrorHandler.Succeeded(hr), $"Failed to remove property '{propertyName}' for project '{dteProject.Name}'."); } else { Debug.Fail("Could not get IVsBuildPropertyStorage for EnvDTE.Project"); } }
/// <summary> /// Deletes a property from the project's .user file. /// </summary> /// <param name="project">The project to delete the property from.</param> /// <param name="propertyName">The name of the property to delete.</param> public void DeleteUserProperty(Project project, string propertyName) { ThreadHelper.ThrowIfNotOnUIThread(); IVsBuildPropertyStorage propertyStore = GetProjectPropertyStore(project); ErrorHandler.ThrowOnFailure( propertyStore.RemoveProperty(propertyName, null, UserFileFlag)); }
public int RemoveProperty(bool perUser, string configName, string propertyName) { if (buildStorage == null) { return(0); } else { return(buildStorage.RemoveProperty(propertyName, configName, (uint)(perUser ? _PersistStorageType.PST_USER_FILE : _PersistStorageType.PST_PROJECT_FILE))); } }
/// <summary> /// Sets NuGetPackageImportStamp to a new random guid. This is a hack to let the project system know it is out of date. /// The value does not matter, it just needs to change. /// </summary> protected static void UpdateImportStamp(EnvDTEProject envDTEProject, bool isCpsProjectSystem = false) { // There is no reason to call this for pre-Dev12 project systems. if (VSVersionHelper.VsMajorVersion >= 12) { #if VS14 // Switch to UI thread to update Import Stamp for Dev14. if (isCpsProjectSystem && VSVersionHelper.IsVisualStudio2014) { try { var projectServiceAccessor = ServiceLocator.GetInstance <IProjectServiceAccessor>(); ProjectService projectService = projectServiceAccessor.GetProjectService(); IThreadHandling threadHandling = projectService.Services.ThreadingPolicy; threadHandling.SwitchToUIThread(); } catch (Exception ex) { ExceptionHelper.WriteToActivityLog(ex); } } #endif IVsBuildPropertyStorage propStore = VsHierarchyUtility.ToVsHierarchy(envDTEProject) as IVsBuildPropertyStorage; if (propStore != null) { // <NuGetPackageImportStamp>af617720</NuGetPackageImportStamp> string stamp = Guid.NewGuid().ToString().Split('-')[0]; try { int r1 = propStore.SetPropertyValue(NuGetImportStamp, string.Empty, (uint)_PersistStorageType.PST_PROJECT_FILE, stamp); } catch (Exception ex1) { ExceptionHelper.WriteToActivityLog(ex1); } // Remove the NuGetImportStamp so that VC++ project file won't be updated with this stamp on disk, // which causes unnecessary source control pending changes. try { int r2 = propStore.RemoveProperty(NuGetImportStamp, string.Empty, (uint)_PersistStorageType.PST_PROJECT_FILE); } catch (Exception ex2) { ExceptionHelper.WriteToActivityLog(ex2); } } } }
/// <summary> /// Saves a property to the project's .user file. /// </summary> /// <param name="project">The project to save the property to.</param> /// <param name="propertyName">The name of the property to save.</param> /// <param name="value">The value of the property.</param> public void SaveUserProperty(Project project, string propertyName, string value) { ThreadHelper.ThrowIfNotOnUIThread(); IVsBuildPropertyStorage propertyStore = GetProjectPropertyStore(project); int hr; if (value == null) { hr = propertyStore.RemoveProperty(propertyName, null, UserFileFlag); } else { hr = propertyStore.SetPropertyValue(propertyName, null, UserFileFlag, value); } ErrorHandler.ThrowOnFailure(hr); }
private void CommitCommandLineText(string CommandLine) { IVsHierarchy ProjectHierarchy; if (UnrealVSPackage.Instance.SolutionBuildManager.get_StartupProject(out ProjectHierarchy) == VSConstants.S_OK && ProjectHierarchy != null) { Project SelectedStartupProject = Utils.HierarchyObjectToProject(ProjectHierarchy); if (SelectedStartupProject != null) { Configuration SelectedConfiguration = SelectedStartupProject.ConfigurationManager.ActiveConfiguration; if (SelectedConfiguration != null) { IVsBuildPropertyStorage PropertyStorage = ProjectHierarchy as IVsBuildPropertyStorage; if (PropertyStorage != null) { string FullCommandLine = CommandLine; // for "Game" projects automatically remove the game project filename from the start of the command line var ActiveConfiguration = (SolutionConfiguration2)UnrealVSPackage.Instance.DTE.Solution.SolutionBuild.ActiveConfiguration; if (UnrealVSPackage.Instance.IsUE4Loaded && Utils.IsGameProject(SelectedStartupProject) && Utils.HasUProjectCommandLineArg(ActiveConfiguration.Name)) { string AutoPrefix = Utils.GetAutoUProjectCommandLinePrefix(SelectedStartupProject); if (FullCommandLine.IndexOf(Utils.UProjectExtension, StringComparison.OrdinalIgnoreCase) < 0 && string.Compare(FullCommandLine, SelectedStartupProject.Name, StringComparison.OrdinalIgnoreCase) != 0 && !FullCommandLine.StartsWith(SelectedStartupProject.Name + " ", StringComparison.OrdinalIgnoreCase)) { // Committed command line does not specify a .uproject FullCommandLine = AutoPrefix + " " + FullCommandLine; } } // Get the project platform name. string ProjectPlatformName = SelectedConfiguration.PlatformName; if (ProjectPlatformName == "Any CPU") { ProjectPlatformName = "AnyCPU"; } // Get the project kind. C++ projects store the debugger arguments differently to other project types. string ProjectKind = SelectedStartupProject.Kind; // Update the property string ProjectConfigurationName = String.Format("{0}|{1}", SelectedConfiguration.ConfigurationName, ProjectPlatformName); if (String.Equals(ProjectKind, "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}", StringComparison.InvariantCultureIgnoreCase)) { List <string> ExtraFields = Utils.GetExtraDebuggerCommandArguments(ProjectPlatformName, SelectedStartupProject); if (FullCommandLine.Length == 0) { PropertyStorage.RemoveProperty("LocalDebuggerCommandArguments", ProjectConfigurationName, (uint)_PersistStorageType.PST_USER_FILE); PropertyStorage.RemoveProperty("RemoteDebuggerCommandArguments", ProjectConfigurationName, (uint)_PersistStorageType.PST_USER_FILE); foreach (string ExtraField in ExtraFields) { PropertyStorage.RemoveProperty(ExtraField, ProjectConfigurationName, (uint)_PersistStorageType.PST_USER_FILE); } } else { PropertyStorage.SetPropertyValue("LocalDebuggerCommandArguments", ProjectConfigurationName, (uint)_PersistStorageType.PST_USER_FILE, FullCommandLine); PropertyStorage.SetPropertyValue("RemoteDebuggerCommandArguments", ProjectConfigurationName, (uint)_PersistStorageType.PST_USER_FILE, FullCommandLine); foreach (string ExtraField in ExtraFields) { PropertyStorage.SetPropertyValue(ExtraField, ProjectConfigurationName, (uint)_PersistStorageType.PST_USER_FILE, FullCommandLine); } } } else { // For some reason, have to update C# projects this way, otherwise the properties page doesn't update. Conversely, SelectedConfiguration.Properties is always null for C++ projects in VS2017. if (SelectedConfiguration.Properties != null) { foreach (Property Property in SelectedConfiguration.Properties) { if (Property.Name.Equals("StartArguments", StringComparison.InvariantCultureIgnoreCase)) { Property.Value = FullCommandLine; break; } } } } } } } } CommitCommandLineToMRU(CommandLine); }