public void WriteProjectInfo_AnalysisFileList_FilesTypes_SpecifiedPlusDefaults() { // Arrange string rootInputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "Inputs"); string rootOutputFolder = TestUtils.CreateTestSpecificFolder(this.TestContext, "Outputs"); ProjectDescriptor descriptor = BuildUtilities.CreateValidProjectDescriptor(rootInputFolder, "fileTypes.proj.txt"); ProjectRootElement projectRoot = CreateInitializedProject(descriptor, new WellKnownProjectProperties(), rootOutputFolder); // Files we don't expect to be included by default string fooType1 = AddFileToProject(projectRoot, "fooType", sonarQubeExclude: null); string xxxType1 = AddFileToProject(projectRoot, "xxxType", sonarQubeExclude: null); AddFileToProject(projectRoot, "barType", sonarQubeExclude: null); // Files we'd normally expect to be included by default string managed1 = AddFileToProject(projectRoot, TargetProperties.ItemType_Compile, sonarQubeExclude: null); string content1 = AddFileToProject(projectRoot, TargetProperties.ItemType_Content, sonarQubeExclude: null); // Update the "item types" property to add some extra item type // NB this has to be done *after* the integration targets have been imported ProjectPropertyGroupElement group = projectRoot.CreatePropertyGroupElement(); projectRoot.AppendChild(group); group.AddProperty("SQAnalysisFileItemTypes", "fooType;$(SQAnalysisFileItemTypes);xxxType"); projectRoot.Save(); // Act ProjectInfo projectInfo = ExecuteWriteProjectInfo(projectRoot, rootOutputFolder); // Assert AssertResultFileExists(projectInfo, AnalysisType.FilesToAnalyze, fooType1, xxxType1, content1, managed1); }
/// <summary> /// Replaces the configurations property with the expected configurations. /// Doesn't attempt to preserve any content since it can all be regenerated. /// Does attempt to preserve the ordering in the project file. /// </summary> /// <param name="project">Project</param> /// <param name="oldPropertyGroups">PropertyGroups to remove</param> /// <param name="newConfigurations"></param> private static void ReplaceConfigurationsProperty(ProjectRootElement project, IEnumerable <ProjectPropertyGroupElement> oldPropertyGroups, IEnumerable <string> newConfigurations) { foreach (var oldPropertyGroup in oldPropertyGroups) { project.RemoveChild(oldPropertyGroup); } string configurationsValue = string.Join(";", newConfigurations); var configurationsProperty = project.Properties.FirstOrDefault(p => p.Name == "Configurations"); if (configurationsProperty == null) { var firstPropertyGroup = project.PropertyGroups.FirstOrDefault(); if (firstPropertyGroup == null) { firstPropertyGroup = project.CreatePropertyGroupElement(); } configurationsProperty = firstPropertyGroup.AddProperty("Configurations", configurationsValue); } else { configurationsProperty.Value = configurationsValue; } }
private ProjectPropertyGroupElement CreatePropertyGroupAtEndOfProject(ProjectRootElement csproj) { var propertyGroup = csproj.CreatePropertyGroupElement(); csproj.InsertBeforeChild(propertyGroup, csproj.LastChild); return(propertyGroup); }
/// <summary> /// Generates choose/when statements to parse values from configuration string /// </summary> /// <param name="propertyName">name of property to parse</param> /// <param name="project">project to update</param> private void ParseProperties(string propertyName, ProjectRootElement project, bool includeAdditionalProperites, Func <PropertyInfo, bool> configurationSelector, string parsedValuePrefix = null) { var parseConfigurationPropertyGroup = project.LastChild as ProjectPropertyGroupElement; if (parseConfigurationPropertyGroup == null || !string.IsNullOrEmpty(project.LastChild.Condition)) { parseConfigurationPropertyGroup = project.CreatePropertyGroupElement(); project.AppendChild(parseConfigurationPropertyGroup); } // delimit property for parsing, this gaurntees that every property value is surrounded in delimiters var parsePropertyName = $"_parse_{propertyName}"; var parseConfigurationValue = $"{ConfigurationFactory.PropertySeperator}$({propertyName}){ConfigurationFactory.PropertySeperator}"; parseConfigurationPropertyGroup.AddProperty(parsePropertyName, parseConfigurationValue); // foreach property, pull it out of Configuration and set derived values. foreach (var property in ConfigurationFactory.GetProperties().Where(configurationSelector)) { var choosePropertiesElement = project.CreateChooseElement(); project.AppendChild(choosePropertiesElement); foreach (var value in ConfigurationFactory.GetValues(property)) { var propertiesCondition = CreateContainsCondition(parsePropertyName, ConfigurationFactory.PropertySeperator + value.Value + ConfigurationFactory.PropertySeperator); var whenPropertiesElement = project.CreateWhenElement(propertiesCondition); choosePropertiesElement.AppendChild(whenPropertiesElement); AddProperties(whenPropertiesElement, value, includeAdditionalProperites, parsedValuePrefix); } var otherwisePropertiesElement = project.CreateOtherwiseElement(); choosePropertiesElement.AppendChild(otherwisePropertiesElement); if (property.DefaultValue != null) { AddProperties(otherwisePropertiesElement, property.DefaultValue, includeAdditionalProperites, parsedValuePrefix); } else { var otherwiseErrorPropertyGroup = project.CreatePropertyGroupElement(); otherwisePropertiesElement.AppendChild(otherwiseErrorPropertyGroup); otherwiseErrorPropertyGroup.AddProperty(ErrorMessageProperty, $"$({ErrorMessageProperty})Could not find a value for {property.Name} from {propertyName} '$({propertyName})'."); } } }
/// <summary> /// Replaces all configuration propertygroups with empty property groups corresponding to the expected configurations. /// Doesn't attempt to preserve any content since it can all be regenerated. /// Does attempt to preserve the ordering in the project file. /// </summary> /// <param name="project">Project</param> /// <param name="oldPropertyGroups">PropertyGroups to remove</param> /// <param name="newConfigurations"></param> private static void ReplaceConfigurationPropertyGroups(ProjectRootElement project, IEnumerable <ProjectPropertyGroupElement> oldPropertyGroups, IEnumerable <string> newConfigurations) { ProjectElement insertAfter = null, insertBefore = null; foreach (var oldPropertyGroup in oldPropertyGroups) { insertBefore = oldPropertyGroup.NextSibling; project.RemoveChild(oldPropertyGroup); } if (insertBefore == null) { // find first itemgroup after imports var insertAt = project.Imports.FirstOrDefault()?.NextSibling; while (insertAt != null) { if (insertAt is ProjectItemGroupElement) { insertBefore = insertAt; break; } insertAt = insertAt.NextSibling; } } if (insertBefore == null) { // find last propertygroup after imports, defaulting to after imports insertAfter = project.Imports.FirstOrDefault(); while (insertAfter?.NextSibling != null && insertAfter.NextSibling is ProjectPropertyGroupElement) { insertAfter = insertAfter.NextSibling; } } foreach (var newConfiguration in newConfigurations) { var newPropertyGroup = project.CreatePropertyGroupElement(); newPropertyGroup.Condition = $"'$(Configuration)|$(Platform)' == '{newConfiguration}'"; if (insertBefore != null) { project.InsertBeforeChild(newPropertyGroup, insertBefore); } else if (insertAfter != null) { project.InsertAfterChild(newPropertyGroup, insertAfter); } else { project.AppendChild(newPropertyGroup); } insertBefore = null; insertAfter = newPropertyGroup; } }
private static void InitializeExtensibilityDebugParameters(ProjectRootElement root) { ProjectElement firstChild = root.FirstChild; ProjectPropertyGroupElement group = root.CreatePropertyGroupElement(); root.InsertAfterChild(group, firstChild); group.AddProperty("StartAction", "Program"); group.AddProperty("StartProgram", "$(DevEnvDir)\\devenv.exe"); group.AddProperty("StartArguments", "/RootSuffix Exp"); }
private bool EnsureProjectGuid(ProjectRootElement project) { ProjectPropertyElement projectGuid = project.Properties.FirstOrDefault(p => p.Name == "ProjectGuid"); string guid = string.Empty; if (projectGuid != null) { guid = projectGuid.Value; string projectName; if (_guidMap.TryGetValue(guid, out projectName)) { Log.LogMessage($"The ProjectGuid='{guid}' is duplicated across projects '{projectName}' and '{project.FullPath}', so creating a new one for project '{project.FullPath}'"); guid = Guid.NewGuid().ToString("B").ToUpper(); _guidMap.Add(guid, project.FullPath); projectGuid.Value = guid; return(true); } else { _guidMap.Add(guid, project.FullPath); } } if (projectGuid == null) { guid = Guid.NewGuid().ToString("B").ToUpper(); var propertyGroup = project.Imports.FirstOrDefault()?.NextSibling as ProjectPropertyGroupElement; if (propertyGroup == null || !string.IsNullOrEmpty(propertyGroup.Condition)) { propertyGroup = project.CreatePropertyGroupElement(); ProjectElement insertAfter = project.Imports.FirstOrDefault(); if (insertAfter == null) { insertAfter = project.Children.FirstOrDefault(); } if (insertAfter != null) { project.InsertAfterChild(propertyGroup, insertAfter); } else { project.AppendChild(propertyGroup); } } propertyGroup.AddProperty("ProjectGuid", guid); return(true); } return(false); }
void AddPropertyWithCondition(ProjectRootElement projectRoot, string name, string value, string condition) { ProjectPropertyGroupElement groupProperty = projectRoot.CreatePropertyGroupElement(); groupProperty.Condition = condition; projectRoot.AppendChild(groupProperty); ProjectPropertyElement property = projectRoot.CreatePropertyElement(name); groupProperty.AppendChild(property); property.Value = value; property.Condition = condition; }
public override bool Execute() { ProjectRootElement project = ProjectRootElement.Create(); var propertyGroup = project.CreatePropertyGroupElement(); project.AppendChild(propertyGroup); propertyGroup.AddProperty("AzureDevOpsCollectionUri", AzureDevOpsCollectionUri ?? "undefined"); propertyGroup.AddProperty("AzureDevOpsProject", AzureDevOpsProject ?? "undefined"); propertyGroup.AddProperty("AzureDevOpsBuildId", AzureDevOpsBuildId.ToString()); var itemGroup = project.CreateItemGroupElement(); project.AppendChild(itemGroup); if (ItemsToSign != null) { foreach (var itemToSign in ItemsToSign) { var filename = itemToSign.ItemSpec.Replace('\\', '/'); { var metadata = itemToSign.CloneCustomMetadata() as Dictionary <string, string>; itemGroup.AddItem("ItemsToSign", Path.GetFileName(itemToSign.ItemSpec), metadata); } } } if (StrongNameSignInfo != null) { foreach (var signInfo in StrongNameSignInfo) { itemGroup.AddItem("StrongNameSignInfo", Path.GetFileName(signInfo.ItemSpec), signInfo.CloneCustomMetadata() as Dictionary <string, string>); } } if (FileSignInfo != null) { foreach (var signInfo in FileSignInfo) { itemGroup.AddItem("FileSignInfo", signInfo.ItemSpec, signInfo.CloneCustomMetadata() as Dictionary <string, string>); } } if (FileExtensionSignInfo != null) { foreach (var signInfo in FileExtensionSignInfo) { itemGroup.AddItem("FileExtensionSignInfo", signInfo.ItemSpec, signInfo.CloneCustomMetadata() as Dictionary <string, string>); } } project.Save(ManifestPath); return(true); }
private static void InitializeLegacyExtensibilityConfigurationProperties(ProjectRootElement root) { ProjectElement firstChild = root.FirstChild; ProjectPropertyGroupElement group = root.CreatePropertyGroupElement(); root.InsertBeforeChild(group, firstChild); group.Label = "Globals"; group.AddProperty("EnableGlobbing", "false"); group.AddProperty("ImportVSSDKTargets", "true"); group.AddProperty("GeneratePkgDefFile", "false"); group.AddProperty("DeployExtension", "true"); group.AddProperty("DeployVSTemplates", "true"); group.AddProperty("UseCodebase", "false"); group.AddProperty("CreateVsixContainer", "true"); group.AddProperty("IncludeAssemblyInVSIXContainer", "false"); group.AddProperty("IncludeDebugSymbolsInVSIXContainer", "true"); group.AddProperty("IncludeDebugSymbolsInLocalVSIXDeployment", "true"); group.AddProperty("CopyBuildOutputToOutputDirectory", "false"); group.AddProperty("CopyOutputSymbolsToOutputDirectory", "false"); group.AddProperty("UseCommonOutputDirectory", "true"); }
private void CreateRuntimeIdentifier(ProjectRootElement project) { var rid = Microsoft.DotNet.PlatformAbstractions.RuntimeEnvironment.GetRuntimeIdentifier(); string[] ridParts = rid.Split('-'); if (ridParts.Length < 1) { throw new System.InvalidOperationException($"Unknown rid format {rid}."); } string osNameAndVersion = ridParts[0]; var propertyGroup = project.CreatePropertyGroupElement(); project.AppendChild(propertyGroup); var runtimeProperty = propertyGroup.AddProperty(RuntimeOSProperty, $"{osNameAndVersion}"); runtimeProperty.Condition = $"'$({RuntimeOSProperty})' == ''"; Log.LogMessage($"Running on OS with RID {rid}, so defaulting RuntimeOS to '{osNameAndVersion}'"); }
/// <inheritdoc /> protected override ProjectElement CreateNewInstance(ProjectRootElement owner) { return(owner.CreatePropertyGroupElement()); }