public void SingleTargetFramework() { LockFile lockFile = GetLockFileWithSingleTargetFramework(); ProjectRootElement project = ValidateProject(lockFile); ProjectItemGroupElement itemGroupElement = project.ItemGroups.ShouldHaveSingleItem(); foreach (LockFileTargetLibrary targetLibrary in lockFile.Targets[0].Libraries) { ProjectItemElement itemElement = itemGroupElement.Items.FirstOrDefault(x => x.Include.Equals(targetLibrary.Name)); itemElement.ShouldNotBeNull(); ProjectMetadataElement versionMetadataElement = itemElement.Metadata.FirstOrDefault(x => x.Name.Equals("Version")); versionMetadataElement.ShouldNotBeNull(); versionMetadataElement.Value.ShouldBe($"[{targetLibrary.Version.ToString()}]"); } itemGroupElement.Items.Count.ShouldBe(lockFile.Targets.Aggregate(0, (count, target) => target.Libraries.Count)); }
private ProjectItemElement MergeWithExistingItemsWithSameCondition( ProjectItemElement item, ProjectItemGroupElement destinationItemGroup) { var existingItemsWithSameCondition = FindExistingItemsWithSameCondition( item, destinationItemGroup.ContainingProject, destinationItemGroup); MigrationTrace.Instance.WriteLine(String.Format( LocalizableStrings.ItemTransformApplicatorMergingItemWithExistingItemsSameChain, nameof(TransformApplicator), existingItemsWithSameCondition.Count())); foreach (var existingItem in existingItemsWithSameCondition) { var mergeResult = MergeItems(item, existingItem); item = mergeResult.InputItem; // Existing Item is null when it's entire set of includes has been merged with the MergeItem if (mergeResult.ExistingItem == null) { existingItem.Parent.RemoveChild(existingItem); } MigrationTrace.Instance.WriteLine(String.Format( LocalizableStrings.ItemTransformApplicatorAddingMergedItem, nameof(TransformApplicator), mergeResult.MergedItem.ItemType, mergeResult.MergedItem.Condition, mergeResult.MergedItem.Include, mergeResult.MergedItem.Exclude)); AddItemToItemGroup(mergeResult.MergedItem, destinationItemGroup); } return(item); }
public ProjectItemElement AddGeneratedFileItem(ProjectItemGroupElement itemGroup, string sourceFileName, string outputFileName) { if (outputFileName == null || outputFileName.Length == 0) { outputFileName = GetOutputFileDefaultName(sourceFileName); } bool compilable = this._compilable; ProjectItemElement itemElement = itemGroup.AddItem(compilable ? "Compile" : "None", outputFileName); itemElement.AddMetadata(ITEMMETADATA_AUTOGEN, "True"); if (compilable) { itemElement.AddMetadata(ITEMMETADATA_DESIGNTIME, "True"); } string customTool = this._customTool; if (!string.IsNullOrEmpty(customTool)) { itemElement.AddMetadata(ITEMMETADATA_GENERATOR, customTool); } itemElement.AddMetadata(ITEMMETADATA_DEPENDENTUPON, sourceFileName); itemElement.AddMetadata(ITEMMETADATA_ORMGENERATOR, this.OfficialName); return(itemElement); }
private void PerformConfigurationPropertyAndItemMappings( CommonCompilerOptions compilerOptions, CommonCompilerOptions configurationCompilerOptions, ProjectPropertyGroupElement propertyGroup, ProjectItemGroupElement itemGroup, ITransformApplicator transformApplicator, string projectDirectory, ProjectType projectType, ProjectRootElement csproj) { foreach (var transform in _propertyTransforms) { var nonConfigurationOutput = transform.Transform(compilerOptions); var configurationOutput = transform.Transform(configurationCompilerOptions); if (!PropertiesAreEqual(nonConfigurationOutput, configurationOutput)) { transformApplicator.Execute(configurationOutput, propertyGroup, mergeExisting: true); } } foreach (var includeContextTransformExecute in _includeContextTransformExecutes) { var nonConfigurationOutput = includeContextTransformExecute(compilerOptions, projectDirectory, projectType); var configurationOutput = includeContextTransformExecute(configurationCompilerOptions, projectDirectory, projectType); configurationOutput = RemoveDefaultCompileAndEmbeddedResourceForWebProjects( configurationOutput, projectType, csproj); transformApplicator.Execute(configurationOutput, itemGroup, mergeExisting: true); } }
public void UpdateMetadataValueAsAttributeWithSpecialCharacters(string projectContents, string updatedProject) { ProjectRootElement projectElement = ProjectRootElement.Create(XmlReader.Create( new StringReader(ObjectModelHelpers.CleanupFileContents(projectContents))), ProjectCollection.GlobalProjectCollection, preserveFormatting: true); ProjectItemGroupElement itemGroup = (ProjectItemGroupElement)projectElement.AllChildren.FirstOrDefault(c => c is ProjectItemGroupElement); var project = new Project(projectElement); var items = Helpers.MakeList(itemGroup.Items); Assert.Equal(1, items.Count); Assert.Equal(1, items[0].Metadata.Count); var metadata = items[0].Metadata.First(); Assert.Equal("m1", metadata.Name); Assert.Equal("v1", metadata.Value); Assert.True(metadata.ExpressedAsAttribute); metadata.Value = @"<&>"""; Assert.True(project.IsDirty); Assert.True(metadata.ExpressedAsAttribute); StringWriter writer = new StringWriter(); project.Save(writer); string expected = @"<?xml version=""1.0"" encoding=""utf-16""?>" + ObjectModelHelpers.CleanupFileContents(updatedProject); string actual = writer.ToString(); VerifyAssertLineByLine(expected, actual); }
/// <summary> /// Creates a temporary MSBuild content project in memory. /// </summary> void CreateBuildProject() { string projectPath = Path.Combine(buildDirectory, "content.contentproj"); string outputPath = Path.Combine(buildDirectory, "bin"); // Create the build project. projectRootElement = ProjectRootElement.Create(projectPath); // Include the standard targets file that defines how to build XNA Framework content. projectRootElement.AddImport("$(MSBuildExtensionsPath)\\Microsoft\\XNA Game Studio\\" + "v4.0\\Microsoft.Xna.GameStudio.ContentPipeline.targets"); buildProject = new Project(projectRootElement); ProjectItemGroupElement teste = projectRootElement.AddItemGroup(); teste.AddItem("ProjectReference", @"C:\Users\Renann\Desktop\StrategyGame\StrategyGameLibrary\StrategyGameLibrary.csproj"); buildProject.SetProperty("XnaPlatform", "Windows"); buildProject.SetProperty("XnaProfile", "Reach"); buildProject.SetProperty("XnaFrameworkVersion", "v4.0"); buildProject.SetProperty("Configuration", "Release"); buildProject.SetProperty("OutputPath", outputPath); // Register any custom importers or processors. foreach (string pipelineAssembly in pipelineAssemblies) { buildProject.AddItem("Reference", pipelineAssembly); } // Hook up our custom error logger. errorLogger = new ErrorLogger(); buildParameters = new BuildParameters(ProjectCollection.GlobalProjectCollection); buildParameters.Loggers = new ILogger[] { errorLogger }; }
void UpdateNuGetPackageReferences(ProjectRootElement projectRoot, ProjectData projectData) { //TODO: only use the top-level pacakages, not all packages var packageConfigFilePath = Path.Combine(projectData.FilePath, Constants.NuGetPackagesConfigFileName); if (File.Exists(packageConfigFilePath)) { XmlDocument document = new XmlDocument(); document.Load(packageConfigFilePath); XmlNodeList packageList = document.GetElementsByTagName("package"); ProjectItemGroupElement packageRefItemGroup = projectRoot.AddItemGroup(); foreach (XmlNode package in packageList) { var id = package.Attributes["id"].InnerText; var version = package.Attributes["version"].InnerText; var item = packageRefItemGroup.AddItem(Constants.PackageReference, id); item.AddMetadata(Constants.Version, version, true); } File.Delete(packageConfigFilePath); } }
private void PerformConfigurationPropertyAndItemMappings( CommonCompilerOptions compilerOptions, CommonCompilerOptions configurationCompilerOptions, ProjectPropertyGroupElement propertyGroup, ProjectItemGroupElement itemGroup, ITransformApplicator transformApplicator, string projectDirectory) { foreach (var transform in _propertyTransforms) { var nonConfigurationOutput = transform.Transform(compilerOptions); var configurationOutput = transform.Transform(configurationCompilerOptions); if (!PropertiesAreEqual(nonConfigurationOutput, configurationOutput)) { transformApplicator.Execute(configurationOutput, propertyGroup); } } foreach (var includeContextTransformExecute in _includeContextTransformExecutes) { var nonConfigurationOutput = includeContextTransformExecute(compilerOptions, projectDirectory); var configurationOutput = includeContextTransformExecute(configurationCompilerOptions, projectDirectory).ToArray(); if (configurationOutput != null && nonConfigurationOutput != null) { // TODO: HACK: this is leaky, see top comments, the throw at least covers the scenario ThrowIfConfigurationHasAdditionalExcludes(configurationOutput, nonConfigurationOutput); RemoveCommonIncludes(configurationOutput, nonConfigurationOutput); configurationOutput = configurationOutput.Where(i => i != null && !string.IsNullOrEmpty(i.Include)).ToArray(); } // Don't merge with existing items when doing a configuration transformApplicator.Execute(configurationOutput, itemGroup, mergeExisting: false); } }
public void Execute( ProjectItemElement item, ProjectItemGroupElement destinationItemGroup, bool mergeExisting) { if (item == null) { return; } MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Item {{ ItemType: {item.ItemType}, Condition: {item.Condition}, Include: {item.Include}, Exclude: {item.Exclude} }}"); MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: ItemGroup {{ Condition: {destinationItemGroup.Condition} }}"); if (mergeExisting) { item = MergeWithExistingItemsWithSameCondition(item, destinationItemGroup); // Item will be null when it's entire set of includes has been merged. if (item == null) { MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Item completely merged"); return; } item = MergeWithExistingItemsWithDifferentCondition(item, destinationItemGroup); // Item will be null when it is equivalent to a conditionless item if (item == null) { MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Item c"); return; } } Execute(item, destinationItemGroup); }
public void SingleTargetFrameworkWithIncludeAssetsAndPrivateAssets() { LockFile lockFile = GetLockFileWithSingleTargetFramework(); ProjectRootElement project = ValidateProject(lockFile); ProjectItemGroupElement itemGroupElement = project.ItemGroups.ShouldHaveSingleItem(); itemGroupElement.Items.ShouldContain(i => i.Include.Equals("Package3"), 1); var itemElement = itemGroupElement.Items.Single(i => i.Include.Equals("Package3")); ProjectMetadataElement includeAssetsMetadataElement = itemElement.Metadata.FirstOrDefault(i => i.Name.Equals("IncludeAssets")); includeAssetsMetadataElement.ShouldNotBeNull(); includeAssetsMetadataElement.Value.ShouldBe("Runtime;ContentFiles"); ProjectMetadataElement privateAssetsMetadataElement = itemElement.Metadata.FirstOrDefault(i => i.Name.Equals("PrivateAssets")); privateAssetsMetadataElement.ShouldNotBeNull(); privateAssetsMetadataElement.Value.ShouldBe("Native"); }
private ProjectItemElement MergeWithExistingItemsWithACondition(ProjectItemElement item, ProjectItemGroupElement destinationItemGroup) { // This logic only applies to conditionless items if (item.ConditionChain().Any() || destinationItemGroup.ConditionChain().Any()) { return(item); } var existingItemsWithACondition = FindExistingItemsWithACondition(item, destinationItemGroup.ContainingProject, destinationItemGroup); MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: Merging Item with {existingItemsWithACondition.Count()} existing items with a different condition chain."); foreach (var existingItem in existingItemsWithACondition) { // If this item is encompassing items in a condition, remove the encompassed includes from the existing item var encompassedIncludes = item.GetEncompassedIncludes(existingItem); if (encompassedIncludes.Any()) { MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: encompassed includes {string.Join(", ", encompassedIncludes)}"); existingItem.RemoveIncludes(encompassedIncludes); } // continue if the existing item is now empty if (!existingItem.Includes().Any()) { MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: Removing Item {{ ItemType: {existingItem.ItemType}, Condition: {existingItem.Condition}, Include: {existingItem.Include}, Exclude: {existingItem.Exclude} }}"); existingItem.Parent.RemoveChild(existingItem); continue; } // If we haven't continued, the existing item may have includes // that need to be removed before being redefined, to avoid duplicate includes // Create or merge with existing remove var remainingIntersectedIncludes = existingItem.IntersectIncludes(item); if (remainingIntersectedIncludes.Any()) { var existingRemoveItem = destinationItemGroup.Items .Where(i => string.IsNullOrEmpty(i.Include) && string.IsNullOrEmpty(i.Exclude) && !string.IsNullOrEmpty(i.Remove)) .FirstOrDefault(); if (existingRemoveItem != null) { var removes = new HashSet <string>(existingRemoveItem.Remove.Split(';')); foreach (var include in remainingIntersectedIncludes) { removes.Add(include); } existingRemoveItem.Remove = string.Join(";", removes); } else { var clearPreviousItem = _projectElementGenerator.CreateItemElement(item.ItemType); clearPreviousItem.Remove = string.Join(";", remainingIntersectedIncludes); AddItemToItemGroup(clearPreviousItem, existingItem.Parent as ProjectItemGroupElement); } } } return(item); }
private ProjectItemElement MergeWithExistingItemsWithSameCondition(ProjectItemElement item, ProjectItemGroupElement destinationItemGroup) { var existingItemsWithSameCondition = FindExistingItemsWithSameCondition(item, destinationItemGroup.ContainingProject, destinationItemGroup); MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Merging Item with {existingItemsWithSameCondition.Count()} existing items with the same condition chain."); foreach (var existingItem in existingItemsWithSameCondition) { var mergeResult = MergeItems(item, existingItem); item = mergeResult.InputItem; // Existing Item is null when it's entire set of includes has been merged with the MergeItem if (mergeResult.ExistingItem == null) { existingItem.Parent.RemoveChild(existingItem); } MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Adding Merged Item {{ ItemType: {mergeResult.MergedItem.ItemType}, Condition: {mergeResult.MergedItem.Condition}, Include: {mergeResult.MergedItem.Include}, Exclude: {mergeResult.MergedItem.Exclude} }}"); AddItemToItemGroup(mergeResult.MergedItem, destinationItemGroup); } return(item); }
private ProjectItemElement MergeWithExistingItemsWithNoCondition(ProjectItemElement item, ProjectItemGroupElement destinationItemGroup) { // This logic only applies to items being placed into a condition if (!item.ConditionChain().Any() && !destinationItemGroup.ConditionChain().Any()) { return(item); } var existingItemsWithNoCondition = FindExistingItemsWithNoCondition(item, destinationItemGroup.ContainingProject, destinationItemGroup); MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: Merging Item with {existingItemsWithNoCondition.Count()} existing items with a different condition chain."); // Handle the item being placed inside of a condition, when it is overlapping with a conditionless item // If it is not definining new metadata or excludes, the conditioned item can be merged with the // conditionless item foreach (var existingItem in existingItemsWithNoCondition) { var encompassedIncludes = existingItem.GetEncompassedIncludes(item); if (encompassedIncludes.Any()) { MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: encompassed includes {string.Join(", ", encompassedIncludes)}"); item.RemoveIncludes(encompassedIncludes); if (!item.Includes().Any()) { MigrationTrace.Instance.WriteLine($"{nameof(ItemTransformApplicator)}: Ignoring Item {{ ItemType: {existingItem.ItemType}, Condition: {existingItem.Condition}, Include: {existingItem.Include}, Exclude: {existingItem.Exclude} }}"); return(null); } } } // If we haven't returned, and there are existing items with a separate condition, we need to // overwrite with those items inside the destinationItemGroup by using a Remove if (existingItemsWithNoCondition.Any()) { // Merge with the first remove if possible var existingRemoveItem = destinationItemGroup.Items .Where(i => string.IsNullOrEmpty(i.Include) && string.IsNullOrEmpty(i.Exclude) && !string.IsNullOrEmpty(i.Remove)) .FirstOrDefault(); if (existingRemoveItem != null) { existingRemoveItem.Remove += ";" + item.Include; } else { var clearPreviousItem = _projectElementGenerator.CreateItemElement(item.ItemType); clearPreviousItem.Remove = item.Include; AddItemToItemGroup(clearPreviousItem, destinationItemGroup); } } return(item); }
private bool ValidateConfigurations() { // The expected format: //<ItemGroup Label="ProjectConfigurations"> // <ProjectConfiguration Include="Debug|Win32"> // <Configuration>Debug</Configuration> // <Platform>Win32</Platform> // </ProjectConfiguration> // <ProjectConfiguration Include="Release|Win32"> // <Configuration>Release</Configuration> // <Platform>Win32</Platform> // </ProjectConfiguration> //</ItemGroup> ProjectItemGroupElement itemGroup = null; foreach (ProjectElement element in this.Project.Children) { if (element.ElementType == ProjectElementType.ItemGroup && String.Equals(element.Label, "ProjectConfigurations", StringComparison.OrdinalIgnoreCase)) { itemGroup = (ProjectItemGroupElement)element; break; } } if (itemGroup != null) { ProjectItemElement itemElement = null; foreach (ProjectItemElement item in itemGroup.Items) { string include = item.Include; if (!String.IsNullOrEmpty(include) && include.IndexOf(this.Configuration, StringComparison.OrdinalIgnoreCase) >= 0 && include.IndexOf(this.Platform, StringComparison.OrdinalIgnoreCase) >= 0) { itemElement = item; break; } } if (itemElement != null && itemElement.HasMetadata) { bool configMatched = false; bool platformMatched = String.Equals(this.Platform, "MixedPlatforms", StringComparison.OrdinalIgnoreCase); foreach (ProjectMetadataElement metadata in itemElement.Metadata) { switch (metadata.Name) { case "Configuration": if (String.Equals(metadata.Value, this.Configuration, StringComparison.OrdinalIgnoreCase)) { configMatched = true; } break; case "Platform": if (String.Equals(metadata.Value, this.Platform, StringComparison.OrdinalIgnoreCase)) { platformMatched = true; } break; } } return(configMatched && platformMatched); } } return(false); }
private static void AddItem(ProjectItemGroupElement itemGroup, string groupName, string item, IEnumerable <KeyValuePair <string, string> > metaData) { itemGroup.AddItem(groupName, item, metaData); }
public static bool IsUniformItemElementType(this ProjectItemGroupElement group, string projectItemElementType) { return(group.Items.All((it) => it.ItemType == projectItemElementType)); }
public override void ConvertItemRefs() { // first identify any items to be removed var toBeRemoved = new List <ProjectItem>(); var parents = new HashSet <ProjectElementContainer>(); foreach (ProjectItem item in this.GetItems("Compile")) { // If the item is outside of the project directory, then don't remove it. // Perhaps say a shared file that a lot of project files include if (!Utils.IsPathInDirectory(this.DirectoryPath, Path.Combine(this.DirectoryPath, item.EvaluatedInclude))) { continue; } // Need to remove it toBeRemoved.Add(item); } // keep track of childless parents. var childless = new List <ProjectElementContainer>(); // second remove them foreach (var item in toBeRemoved) { var p = item.Xml.Parent; if (p != null) { p.RemoveChild(item.Xml); if (parents.Contains(p) == false) { parents.Add(p); } if (p.Count == 0) { childless.Add(p); } } } if (parents.Count == 0) { return; // Nothing was removed } // Re-Add all the items again var firstParent = parents.First(); ProjectItemGroupElement compile_group = firstParent as ProjectItemGroupElement; ProjectItemElement compile = compile_group.AddItem("Compile", "**\\*.cs"); var sb = new StringBuilder(); foreach (var orphan in GetOrphans()) { sb.Append(String.Format("{0};", Utils.PathRelativeTo(this.DirectoryPath, orphan))); compile.Exclude = sb.ToString(); } foreach (var p in childless) { p.Parent.RemoveChild(p); } this.Save(); }
protected override void OnClosed(EventArgs e) { if (_savedChanges) { // Make sure the current document has the necessary // extensions loaded. // UNDONE: We should be able to do this with the document // closed or open as text as well via a registered service // on the ORMDesignerPackage, but this is sufficient for now. Dictionary <string, string> requiredExtensions = null; string[] loadedExtensions = null; foreach (IORMGenerator selectedGenerator in _mainBranch.SelectedGenerators) { foreach (string requiredExtension in selectedGenerator.GetRequiredExtensionsForInputFormat("ORM")) { if (loadedExtensions == null) { loadedExtensions = (new ORMExtensionManager(_projectItem)).GetLoadedExtensions(_serviceProvider); } if (Array.BinarySearch <string>(loadedExtensions, requiredExtension) < 0) { if (requiredExtensions == null) { requiredExtensions = new Dictionary <string, string>(); } else if (requiredExtensions.ContainsKey(requiredExtension)) { continue; } requiredExtensions.Add(requiredExtension, requiredExtension); } } } if (requiredExtensions != null) { _savedChanges = ORMExtensionManager.EnsureExtensions(_projectItem, _serviceProvider, requiredExtensions.Values); } } if (_savedChanges) { #if VISUALSTUDIO_10_0 ProjectItemGroupElement itemGroup = _originalItemGroup; ProjectRootElement project = _project; #else // VISUALSTUDIO_10_0 BuildItemGroup itemGroup = _originalItemGroup; Microsoft.Build.BuildEngine.Project project = _project; #endif // VISUALSTUDIO_10_0 EnvDTE.ProjectItem projectItem = _projectItem; string sourceFileName = _sourceFileName; Dictionary <string, PseudoBuildItem> pseudoItems = _pseudoItemsByOutputFormat; IDictionary <string, IORMGenerator> generators = #if VISUALSTUDIO_15_0 ORMCustomTool.GetORMGenerators(_serviceProvider); #else ORMCustomTool.ORMGenerators; #endif PseudoBuildItem pseudoItem; string generatorNameData; // The first string is the primary generator, others are the format modifiers, space delimited IVsShell shell; Dictionary <string, IORMGenerator> generatorsWithTargetsByOutputFormat = null; IDictionary <string, ORMCustomToolUtility.GeneratorTargetSet> targetSetsByFormatName = null; foreach (PseudoBuildItem testPseudoItem in pseudoItems.Values) { string primaryGeneratorName; IList <string> generatorTargets; IORMGenerator generator; if (!string.IsNullOrEmpty(generatorNameData = testPseudoItem.CurrentGeneratorNames) && null != (primaryGeneratorName = ORMCustomToolUtility.GetPrimaryGeneratorName(generatorNameData)) && generators.TryGetValue(primaryGeneratorName, out generator) && null != (generatorTargets = generator.GeneratorTargetTypes) && 0 != generatorTargets.Count) { (generatorsWithTargetsByOutputFormat ?? (generatorsWithTargetsByOutputFormat = new Dictionary <string, IORMGenerator>(StringComparer.OrdinalIgnoreCase)))[generator.ProvidesOutputFormat] = generator; } } if (generatorsWithTargetsByOutputFormat != null) { IDictionary <string, GeneratorTarget[]> docTargets = null; EnvDTE.Document projectItemDocument = projectItem.Document; string itemPath; if (projectItemDocument != null) { using (Stream targetsStream = ORMCustomToolUtility.GetDocumentExtension <Stream>(projectItemDocument, "ORMGeneratorTargets", itemPath = projectItem.get_FileNames(0), _serviceProvider)) { if (targetsStream != null) { targetsStream.Seek(0, SeekOrigin.Begin); docTargets = new BinaryFormatter().Deserialize(targetsStream) as IDictionary <string, GeneratorTarget[]>; } } } else if (null != (shell = _serviceProvider.GetService(typeof(SVsShell)) as IVsShell)) { Guid pkgId = typeof(ORMDesignerPackage).GUID; IVsPackage package; if (0 != shell.IsPackageLoaded(ref pkgId, out package) || package == null) { shell.LoadPackage(ref pkgId, out package); } // Temporarily load the document so that the generator targets can be resolved. using (Store store = new ModelLoader(ORMDesignerPackage.ExtensionLoader, true).Load(projectItem.get_FileNames(0))) { docTargets = GeneratorTarget.ConsolidateGeneratorTargets(store as IFrameworkServices); } } // We have generators that care about targets, which means that ExpandGeneratorTargets will // product placeholder targets for these generators even if docTargets is currently null. // This allows the dialog to turn on a generator before the data (or even extension) to feed // it is available in the model and provides a smooth transition in and out of this placeholder // state. It is up to the individual generators to proceed without explicit target data or // to produce a message for the user with instructions on how to add the data to the model. Dictionary <string, string> generatorNamesByOutputFormat = new Dictionary <string, string>(); foreach (KeyValuePair <string, PseudoBuildItem> pair in pseudoItems) { generatorNameData = pair.Value.CurrentGeneratorNames; if (!string.IsNullOrEmpty(generatorNameData)) { generatorNamesByOutputFormat[pair.Key] = ORMCustomToolUtility.GetPrimaryGeneratorName(generatorNameData); } } targetSetsByFormatName = ORMCustomToolUtility.ExpandGeneratorTargets(generatorNamesByOutputFormat, docTargets #if VISUALSTUDIO_15_0 , _serviceProvider #endif // VISUALSTUDIO_15_0 ); } Dictionary <string, BitTracker> processedGeneratorTargets = null; if (targetSetsByFormatName != null) { processedGeneratorTargets = new Dictionary <string, BitTracker>(); foreach (KeyValuePair <string, ORMCustomToolUtility.GeneratorTargetSet> kvp in targetSetsByFormatName) { processedGeneratorTargets[kvp.Key] = new BitTracker(kvp.Value.Instances.Length); } } if (null != itemGroup) { #if VISUALSTUDIO_10_0 Dictionary <string, ProjectItemElement> removedItems = null; foreach (ProjectItemElement item in itemGroup.Items) #else // VISUALSTUDIO_10_0 Dictionary <string, BuildItem> removedItems = null; foreach (BuildItem item in itemGroup) #endif // VISUALSTUDIO_10_0 { string primaryGeneratorName; string outputFormat; IORMGenerator generator; if (null != (primaryGeneratorName = ORMCustomToolUtility.GetPrimaryGeneratorName(item.GetEvaluatedMetadata(ITEMMETADATA_ORMGENERATOR))) && string.Equals(item.GetEvaluatedMetadata(ITEMMETADATA_DEPENDENTUPON), sourceFileName, StringComparison.OrdinalIgnoreCase) && generators.TryGetValue(primaryGeneratorName, out generator) && pseudoItems.TryGetValue(outputFormat = generator.ProvidesOutputFormat, out pseudoItem)) { generatorNameData = pseudoItem.CurrentGeneratorNames; ORMCustomToolUtility.GeneratorTargetSet targetSet = null; BitTracker processedForFormat = default(BitTracker); if (targetSetsByFormatName != null) { if (targetSetsByFormatName.TryGetValue(outputFormat, out targetSet)) { processedForFormat = processedGeneratorTargets[outputFormat]; } } List <PseudoBuildInstance> originalInstances; bool removeInstance = false; if (string.IsNullOrEmpty(generatorNameData)) { // The item is deleted, mark for removal removeInstance = true; } else if (null != (originalInstances = pseudoItem.OriginalInstances)) { for (int i = 0, count = originalInstances.Count; i < count && !removeInstance; ++i) { PseudoBuildInstance instance = originalInstances[i]; if (instance.IsRemoved) { continue; } GeneratorTarget[] targets = instance.OriginalGeneratorTargets; if (targetSet != null) { if (targets == null) { // Remove, if a target set is available then it must be used removeInstance = true; } else { int instanceIndex = targetSet.IndexOfInstance(targets, delegate(int ignoreInstance) { return(processedForFormat[ignoreInstance]); }); if (instanceIndex == -1) { removeInstance = true; } else if (!processedForFormat[instanceIndex]) { if (instance.OriginalGeneratorNames != generatorNameData) { // This is a preexisting item, update its meta information ORMCustomToolUtility.SetItemMetaData(item, ITEMMETADATA_ORMGENERATOR, generatorNameData); } processedForFormat[instanceIndex] = true; processedGeneratorTargets[outputFormat] = processedForFormat; break; } } } else if (targets != null) { // Remove, formatter changed to one that does not use a generator target removeInstance = true; } else if (instance.OriginalGeneratorNames != generatorNameData) { // This is a preexisting item, update its meta information ORMCustomToolUtility.SetItemMetaData(item, ITEMMETADATA_ORMGENERATOR, generatorNameData); } if (removeInstance) { instance.IsRemoved = true; } } } if (removeInstance) { if (removedItems == null) { #if VISUALSTUDIO_10_0 removedItems = new Dictionary <string, ProjectItemElement>(); #else // VISUALSTUDIO_10_0 removedItems = new Dictionary <string, BuildItem>(); #endif // VISUALSTUDIO_10_0 } removedItems[ORMCustomToolUtility.GetItemInclude(item)] = item; } } } if (removedItems != null) { EnvDTE.ProjectItems subItems = projectItem.ProjectItems; #if VISUALSTUDIO_10_0 foreach (KeyValuePair <string, ProjectItemElement> removePair in removedItems) { ProjectItemElement removeItem = removePair.Value; ProjectElementContainer removeFrom; if (null != (removeFrom = removeItem.Parent)) { removeFrom.RemoveChild(removeItem); } #else // VISUALSTUDIO_10_0 foreach (KeyValuePair <string, BuildItem> removePair in removedItems) { project.RemoveItem(removePair.Value); #endif // VISUALSTUDIO_10_0 try { EnvDTE.ProjectItem subItem = subItems.Item(removePair.Key); if (subItem != null) { subItem.Delete(); } } catch (ArgumentException) { // Swallow } } } #if !VISUALSTUDIO_10_0 // Empty item groups remove themselves from the project, we'll need // to recreate below if the group is empty after the remove phase. if (itemGroup.Count == 0) { itemGroup = null; } #endif } // Removes and changes are complete, proceed with adds for any new items string newItemDirectory = null; string projectPath = null; EnvDTE.ProjectItems projectItems = null; string tmpFile = null; // Adding a file to our special item group adds it to the build system. However, // it does not add it to the parallel project system, which is what displays in // the solution explorer. Therefore, we also explicitly add the item to the // project system as well. Unfortunately, this extra add automatically creates // a redundant item (usually in a new item group) for our adding item. Track anything // we add through the project system so that we can remove these redundant items from the // build system when we're done. Dictionary <string, string> sideEffectItemNames = null; try { Action <IORMGenerator, string, ORMCustomToolUtility.GeneratorTargetSet, GeneratorTarget[]> addProjectItem = delegate(IORMGenerator generator, string allGenerators, ORMCustomToolUtility.GeneratorTargetSet targetSet, GeneratorTarget[] targetInstance) { if (itemGroup == null) { #if VISUALSTUDIO_10_0 itemGroup = project.AddItemGroup(); #else itemGroup = project.AddNewItemGroup(); #endif itemGroup.Condition = string.Concat(ITEMGROUP_CONDITIONSTART, _projectItemRelativePath, ITEMGROUP_CONDITIONEND); } if (newItemDirectory == null) { // Initialize general information #if VISUALSTUDIO_10_0 projectPath = project.FullPath; #else projectPath = project.FullFileName; #endif newItemDirectory = Path.GetDirectoryName(new Uri(projectPath).MakeRelativeUri(new Uri((string)projectItem.Properties.Item("LocalPath").Value)).ToString()); projectItems = projectItem.ProjectItems; } string defaultFileName = generator.GetOutputFileDefaultName(sourceFileName); string fileName = targetInstance == null ? defaultFileName : ORMCustomToolUtility.GeneratorTargetSet.DecorateFileName(defaultFileName, targetInstance); string fileRelativePath = Path.Combine(newItemDirectory, fileName); string fileAbsolutePath = string.Concat(new FileInfo(projectPath).DirectoryName, Path.DirectorySeparatorChar, fileRelativePath); #if VISUALSTUDIO_10_0 ProjectItemElement newBuildItem; #else BuildItem newBuildItem; #endif newBuildItem = generator.AddGeneratedFileItem(itemGroup, sourceFileName, fileRelativePath); if (allGenerators != null) { ORMCustomToolUtility.SetItemMetaData(newBuildItem, ITEMMETADATA_ORMGENERATOR, allGenerators); } if (targetInstance != null) { ORMCustomToolUtility.SetGeneratorTargetMetadata(newBuildItem, targetInstance); } (sideEffectItemNames ?? (sideEffectItemNames = new Dictionary <string, string>()))[fileRelativePath] = null; if (File.Exists(fileAbsolutePath)) { try { projectItems.AddFromFile(fileAbsolutePath); } catch (ArgumentException) { // Swallow } } else { if (tmpFile == null) { tmpFile = Path.GetTempFileName(); } EnvDTE.ProjectItem newProjectItem = projectItems.AddFromTemplate(tmpFile, fileName); string customTool; if (!string.IsNullOrEmpty(customTool = newBuildItem.GetMetadata(ITEMMETADATA_GENERATOR))) { newProjectItem.Properties.Item("CustomTool").Value = customTool; } } }; foreach (KeyValuePair <string, PseudoBuildItem> keyedPseudoItem in pseudoItems) { pseudoItem = keyedPseudoItem.Value; string allGenerators = pseudoItem.CurrentGeneratorNames; string primaryGenerator = ORMCustomToolUtility.GetPrimaryGeneratorName(allGenerators); if (allGenerators == primaryGenerator) { allGenerators = null; } IORMGenerator generator = generators[primaryGenerator]; string outputFormat = generator.ProvidesOutputFormat; ORMCustomToolUtility.GeneratorTargetSet targetSet = null; if (targetSetsByFormatName != null) { targetSetsByFormatName.TryGetValue(outputFormat, out targetSet); } if (targetSet != null) { // OriginalInstances were already updated in the remove loop and processed // instances were flagged. Find additional instances from the target set (created // just now from the current model), not from the pseudoItem (created from the project // files that possibly reflect a previous version of the model). GeneratorTarget[][] instances = targetSet.Instances; BitTracker processed = processedGeneratorTargets[outputFormat]; for (int i = 0, count = instances.Length; i < count; ++i) { if (!processed[i]) { addProjectItem(generator, allGenerators, targetSet, instances[i]); } } } else if (pseudoItem.OriginalInstances == null) { addProjectItem(generator, allGenerators, null, null); } else { // Make sure there was an original instance that did not have a target set. List <PseudoBuildInstance> originals = pseudoItem.OriginalInstances; int i = 0, count = originals.Count; for (; i < count; ++i) { if (originals[i].OriginalGeneratorTargets == null) { break; } } if (i == count) { addProjectItem(generator, allGenerators, null, null); } } } } finally { if (tmpFile != null) { File.Delete(tmpFile); } } if (sideEffectItemNames != null) { ORMCustomToolUtility.RemoveSideEffectItems(sideEffectItemNames, project, itemGroup); } #if VISUALSTUDIO_10_0 // Old group remove themselves when empty, but this is // not true in the new build system. Clean up as needed. if (itemGroup != null && itemGroup.Items.Count == 0) { project.RemoveChild(itemGroup); } #endif VSLangProj.VSProjectItem vsProjectItem = projectItem.Object as VSLangProj.VSProjectItem; if (vsProjectItem != null) { vsProjectItem.RunCustomTool(); } } base.OnClosed(e); }
public static ProjectItemGroupElement GetUniformItemGroupOrNew(this ProjectRootElement project, string itemName) { ProjectItemGroupElement group = project.ItemGroupsReversed.FirstOrDefault(g => g.Items.All(i => i.ItemType == itemName)); return(group ?? project.AddItemGroup()); }
protected override void ProcessRecord() { try { if (Path != null) { WriteObject((Collection == null) ? ProjectRootElement.Create(Path) : ProjectRootElement.Create(Path, Collection), false); } else if (Xml != null) { WriteObject((Collection == null) ? ProjectRootElement.Create(ValidateXmlSourceAttribute.AsXmlReader(Xml)) : ProjectRootElement.Create(ValidateXmlSourceAttribute.AsXmlReader(Xml), Collection), false); } else { ProjectRootElement project = (Collection == null) ? ProjectRootElement.Create() : ProjectRootElement.Create(Collection); project.DefaultTargets = DefaultTargets; project.AddImport("$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props").Condition = "Exists('$(MSBuildExtensionsPath)\\$(MSBuildToolsVersion)\\Microsoft.Common.props')"; ProjectPropertyGroupElement propertyGroup = project.AddPropertyGroup(); propertyGroup.Condition = ""; propertyGroup.AddProperty("Configuration", Configuration).Condition = " $(Configuration)' == '' "; propertyGroup.AddProperty("Platform", Platform).Condition = " $(Configuration)' == '' "; propertyGroup.AddProperty("ProjectGuid", ProjectGuid.ToString("B").ToUpper()); propertyGroup.AddProperty("OutputType", OutputType); propertyGroup.AddProperty("RootNamespace", RootNamespace); propertyGroup.AddProperty("AssemblyName", (String.IsNullOrWhiteSpace(AssemblyName)) ? RootNamespace : AssemblyName); propertyGroup.AddProperty("TargetFrameworkVersion", ToolLocationHelper.GetDotNetFrameworkVersionFolderPrefix(TargetFrameworkVersion)); project.AddImport("$(MSBuildToolsPath)\\Microsoft.CSharp.targets"); WriteObject(project); propertyGroup = project.AddPropertyGroup(); if (Configuration == "Debug" || Configuration == "Release") { propertyGroup.Condition = " '$(Configuration)|$(Platform)' == 'Debug|" + Platform + "' "; } else { propertyGroup.Condition = " '$(Configuration)|$(Platform)' == '" + Configuration + "|" + Platform + "' "; } propertyGroup.AddProperty("DebugSymbols", "true"); propertyGroup.AddProperty("DebugType", "full"); propertyGroup.AddProperty("Optimize", "false"); if (Configuration == "Debug" || Configuration == "Release") { propertyGroup.AddProperty("OutputPath", "bin\\" + Platform); } else { propertyGroup.AddProperty("OutputPath", "bin\\Debug"); } propertyGroup.AddProperty("DefineConstants", "DEBUG;TRACE"); propertyGroup.AddProperty("ErrorReport", "prompt"); propertyGroup.AddProperty("WarningLevel", "4"); propertyGroup = project.AddPropertyGroup(); propertyGroup.Condition = " '$(Configuration)|$(Platform)' == 'Release|" + Platform + "' "; propertyGroup.AddProperty("DebugType", "pdbonly"); propertyGroup.AddProperty("Optimize", "true"); propertyGroup.AddProperty("OutputPath", "bin\\Release"); propertyGroup.AddProperty("DefineConstants", "TRACE"); propertyGroup.AddProperty("ErrorReport", "prompt"); propertyGroup.AddProperty("WarningLevel", "4"); ProjectItemGroupElement itemGroup = project.AddItemGroup(); itemGroup.AddItem("Reference", "System"); itemGroup.AddItem("Reference", "System.Core"); itemGroup.AddItem("Reference", "Microsoft.CSharp"); itemGroup.AddItem("Reference", "System.Xml"); Assembly assembly = (typeof(PSObject)).Assembly; itemGroup.AddItem("Reference", assembly.FullName + ", processorArchitecture=MSIL", new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("SpecificVersion", "false"), new KeyValuePair <string, string>("HintPath", assembly.Location) }); assembly = (typeof(Microsoft.PowerShell.Commands.OutStringCommand)).Assembly; itemGroup.AddItem("Reference", assembly.FullName + ", processorArchitecture=MSIL", new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("SpecificVersion", "false"), new KeyValuePair <string, string>("HintPath", assembly.Location) }); assembly = (typeof(Microsoft.PowerShell.Commands.GetItemCommand)).Assembly; itemGroup.AddItem("Reference", assembly.FullName + ", processorArchitecture=MSIL", new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("SpecificVersion", "false"), new KeyValuePair <string, string>("HintPath", assembly.Location) }); WriteObject(project); } } catch (Exception exception) { WriteError(new ErrorRecord(exception, "New_MSBuildProject", ErrorCategory.OpenError, null)); } }
private void MergeOnUpdatesWithExistingItemsWithACondition( ProjectItemElement item, ProjectItemElement existingItem, ProjectItemGroupElement destinationItemGroup) { // If this item is encompassing items in a condition, remove the encompassed updates from the existing item var encompassedUpdates = item.GetEncompassedUpdates(existingItem, MigrationTrace.Instance); if (encompassedUpdates.Any()) { MigrationTrace.Instance.WriteLine(String.Format( LocalizableStrings.ItemTransformApplicatorEncompassedUpdates, nameof(ItemTransformApplicator), string.Join(", ", encompassedUpdates))); existingItem.RemoveUpdates(encompassedUpdates); } // continue if the existing item is now empty if (!existingItem.Updates().Any()) { MigrationTrace.Instance.WriteLine(String.Format( LocalizableStrings.ItemTransformApplicatorRemovingItem, nameof(ItemTransformApplicator), existingItem.ItemType, existingItem.Condition, existingItem.Update, existingItem.Exclude)); existingItem.Parent.RemoveChild(existingItem); return; } // If we haven't continued, the existing item may have updates // that need to be removed before being redefined, to avoid duplicate updates // Create or merge with existing remove var remainingIntersectedUpdates = existingItem.IntersectUpdates(item); if (remainingIntersectedUpdates.Any()) { var existingRemoveItem = destinationItemGroup.Items .Where(i => string.IsNullOrEmpty(i.Update) && string.IsNullOrEmpty(i.Exclude) && !string.IsNullOrEmpty(i.Remove)) .FirstOrDefault(); if (existingRemoveItem != null) { var removes = new HashSet <string>(existingRemoveItem.Remove.Split(';')); foreach (var update in remainingIntersectedUpdates) { removes.Add(update); } existingRemoveItem.Remove = string.Join(";", removes); } else { var clearPreviousItem = _projectElementGenerator.CreateItemElement(item.ItemType); clearPreviousItem.Remove = string.Join(";", remainingIntersectedUpdates); AddItemToItemGroup(clearPreviousItem, existingItem.Parent as ProjectItemGroupElement); } } }
/// <summary> /// Creates an MSBuild properties file that specifies the full closure of packages with locked versions. /// </summary> /// <returns>A <see cref="ProjectRootElement"/> object that can be saved.</returns> internal bool TryCreateProject(out ProjectRootElement project) { project = null; if (!File.Exists(ProjectAssetsFile)) { Log.LogError($"NuGet assets file '{ProjectAssetsFile}' does not exist."); return(false); } // should item group be conditioned or items or metadata? Perhaps item condition should be considered and compared as well as an item could be conditioned. Consider the below scenarios. Since we are only parsing the assets file we need to consider the project file entries. // <PackageReference Include="foo" Version="1.2.3" Condition="bar"/> // <PackageReference Include="foo"> // <version>1.2.3</version> // <version Condition="bar">1.2.3</version> // </PackageReference> // What about dependencies of packages that are conditioned? they should be conditioned as well. HashSet <string> packagesToExclude = new HashSet <string>(PackagesToExclude?.Select(i => i.ItemSpec).Distinct() ?? Enumerable.Empty <string>(), StringComparer.OrdinalIgnoreCase); project = ProjectRootElement.Create(); project.ToolsVersion = String.Empty; ProjectPropertyElement wasImportedPropertyElement = project.AddProperty("NuGetDeterministicPropsWasImported", "true"); LockFile lockFile = LockFileUtilities.GetLockFile(ProjectAssetsFile, NullLogger.Instance); bool crossTargeting = lockFile.PackageSpec.TargetFrameworks.Count > 1; foreach (TargetFrameworkInformation targetFramework in lockFile.PackageSpec.TargetFrameworks) { HashSet <LockFileLibrary> addedLibraries = new HashSet <LockFileLibrary>(); ProjectItemGroupElement itemGroupElement = project.AddItemGroup(); if (crossTargeting) { itemGroupElement.Condition = $" '$(TargetFramework)' == '{targetFramework.FrameworkName.GetShortFolderName()}' "; } LockFileTarget target = lockFile.GetTarget(targetFramework.FrameworkName, runtimeIdentifier: null); bool addedImplicitReference = false; foreach (LibraryDependency libraryDependency in targetFramework.Dependencies.Where(i => !packagesToExclude.Contains(i.Name))) { if (libraryDependency.AutoReferenced) { if (ExcludeImplicitReferences) { continue; } addedImplicitReference = true; } LockFileLibrary library = lockFile.GetLibrary(libraryDependency); if (library.Type.Equals("project", StringComparison.OrdinalIgnoreCase)) { // if a csproj name matches a package id then nuget swaps in the csproj output instead of the package. Because of this we should skip adding the package as a locked package because it provides no value. continue; } if (addedLibraries.Contains(library)) { continue; } addedLibraries.Add(library); LockFileTargetLibrary targetLibrary = target.GetTargetLibrary(libraryDependency.Name); itemGroupElement.AddItem("PackageReference", targetLibrary.Name, GetPackageReferenceItemMetadata(library, libraryDependency)); foreach (LockFileLibrary dependency in targetLibrary.ResolveDependencies(lockFile, target).Where(i => !addedLibraries.Contains(i) && !packagesToExclude.Contains(i.Name))) { addedLibraries.Add(dependency); itemGroupElement.AddItem("PackageReference", dependency.Name, GetPackageReferenceItemMetadata(dependency)); } } if (addedImplicitReference) { ProjectPropertyElement disableImplicitFrameworkReferencesPropertyElement = project.AddProperty("DisableImplicitFrameworkReferences", "true"); if (crossTargeting) { disableImplicitFrameworkReferencesPropertyElement.Condition = $" '$(TargetFramework)' == '{targetFramework.FrameworkName.GetShortFolderName()}' "; } } } ProjectImportElement beforeImportElement = project.CreateImportElement("Before.$(MSBuildThisFile)"); project.InsertAfterChild(beforeImportElement, wasImportedPropertyElement.Parent); beforeImportElement.Condition = $"Exists('{beforeImportElement.Project}')"; ProjectImportElement afterImportElement = project.AddImport("After.$(MSBuildThisFile)"); afterImportElement.Condition = $"Exists('{afterImportElement.Project}')"; return(true); }
private ProjectItemElement MergeWithExistingItemsWithDifferentCondition(ProjectItemElement item, ProjectItemGroupElement destinationItemGroup) { var existingItemsWithDifferentCondition = FindExistingItemsWithDifferentCondition(item, destinationItemGroup.ContainingProject, destinationItemGroup); MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Merging Item with {existingItemsWithDifferentCondition.Count()} existing items with a different condition chain."); foreach (var existingItem in existingItemsWithDifferentCondition) { var encompassedIncludes = existingItem.GetEncompassedIncludes(item); if (encompassedIncludes.Any()) { MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: encompassed includes {string.Join(", ", encompassedIncludes)}"); item.RemoveIncludes(encompassedIncludes); if (!item.Includes().Any()) { MigrationTrace.Instance.WriteLine($"{nameof(TransformApplicator)}: Ignoring Item {{ ItemType: {existingItem.ItemType}, Condition: {existingItem.Condition}, Include: {existingItem.Include}, Exclude: {existingItem.Exclude} }}"); return(null); } } } // If we haven't returned, and there are existing items with a separate condition, we need to // overwrite with those items inside the destinationItemGroup by using a Remove // Unless this is a conditionless item, in which case this the conditioned items should be doing the // overwriting. if (existingItemsWithDifferentCondition.Any() && (item.ConditionChain().Count() > 0 || destinationItemGroup.ConditionChain().Count() > 0)) { // Merge with the first remove if possible var existingRemoveItem = destinationItemGroup.Items .Where(i => string.IsNullOrEmpty(i.Include) && string.IsNullOrEmpty(i.Exclude) && !string.IsNullOrEmpty(i.Remove)) .FirstOrDefault(); if (existingRemoveItem != null) { existingRemoveItem.Remove += ";" + item.Include; } else { var clearPreviousItem = _projectElementGenerator.CreateItemElement(item.ItemType); clearPreviousItem.Remove = item.Include; Execute(clearPreviousItem, destinationItemGroup); } } return(item); }
/// <summary> /// Gets all Reference items from a given item group. /// </summary> private static IEnumerable <ProjectItemElement> GetReferences(ProjectItemGroupElement itemGroup) => itemGroup.Items.Where(item => item.ElementName.Equals(MSBuildFacts.MSBuildReferenceName, StringComparison.OrdinalIgnoreCase));
/// <summary> /// Finds the packages.config item in its containing item group. /// </summary> public static ProjectItemElement GetPackagesConfigItem(ProjectItemGroupElement packagesConfigItemGroup) => packagesConfigItemGroup.Items.Single(pe => pe.Include.Equals(PackageFacts.PackagesConfigIncludeName, StringComparison.OrdinalIgnoreCase));
private ProjectItemElement AddPackageReference(ProjectItemGroupElement itemGroupElement, PackageReference package) { LibraryIncludeFlags includeAssets = LibraryIncludeFlags.All; LibraryIncludeFlags excludeAssets = LibraryIncludeFlags.None; LibraryIncludeFlags privateAssets = LibraryIncludeFlags.None; if (package.HasFolder("build") && package.Imports.Count == 0) { excludeAssets |= LibraryIncludeFlags.Build; } if (package.HasFolder("lib") && package.AssemblyReferences.Count == 0) { excludeAssets |= LibraryIncludeFlags.Compile; excludeAssets |= LibraryIncludeFlags.Runtime; } if (package.HasFolder("analyzers") && package.AnalyzerItems.Count == 0) { excludeAssets |= LibraryIncludeFlags.Analyzers; } if (package.IsDevelopmentDependency) { privateAssets |= LibraryIncludeFlags.All; } if (package.IsMissingTransitiveDependency) { includeAssets = LibraryIncludeFlags.None; excludeAssets = LibraryIncludeFlags.None; privateAssets = LibraryIncludeFlags.All; } ProjectItemElement itemElement = itemGroupElement.AppendItem("PackageReference", package.PackageIdentity.Id); itemElement.AddMetadataAsAttribute("Version", package.PackageVersion.ToNormalizedString()); if (includeAssets != LibraryIncludeFlags.All) { itemElement.AddMetadataAsAttribute("IncludeAssets", includeAssets.ToString()); } if (excludeAssets != LibraryIncludeFlags.None) { itemElement.AddMetadataAsAttribute("ExcludeAssets", excludeAssets.ToString()); } if (privateAssets != LibraryIncludeFlags.None) { itemElement.AddMetadataAsAttribute("PrivateAssets", privateAssets.ToString()); } if (package.GeneratePathProperty) { itemElement.AddMetadataAsAttribute("GeneratePathProperty", bool.TrueString); } return(itemElement); }
private void AddProjectTypeSpecificDependencies( MigrationRuleInputs migrationRuleInputs, MigrationSettings migrationSettings, ProjectItemGroupElement noFrameworkPackageReferenceItemGroup) { var project = migrationRuleInputs.DefaultProjectContext.ProjectFile; var type = project.GetProjectType(); switch (type) { case ProjectType.Test: _transformApplicator.Execute( PackageDependencyInfoTransform().Transform( new PackageDependencyInfo { Name = PackageConstants.TestSdkPackageName, Version = ConstantPackageVersions.TestSdkPackageVersion }), noFrameworkPackageReferenceItemGroup, mergeExisting: false); if (project.TestRunner.Equals("xunit", StringComparison.OrdinalIgnoreCase)) { _transformApplicator.Execute( PackageDependencyInfoTransform().Transform( new PackageDependencyInfo { Name = PackageConstants.XUnitPackageName, Version = ConstantPackageVersions.XUnitPackageVersion }), noFrameworkPackageReferenceItemGroup, mergeExisting: false); _transformApplicator.Execute( PackageDependencyInfoTransform().Transform( new PackageDependencyInfo { Name = PackageConstants.XUnitRunnerPackageName, Version = ConstantPackageVersions.XUnitRunnerPackageVersion }), noFrameworkPackageReferenceItemGroup, mergeExisting: false); } else if (project.TestRunner.Equals("mstest", StringComparison.OrdinalIgnoreCase)) { _transformApplicator.Execute( PackageDependencyInfoTransform().Transform( new PackageDependencyInfo { Name = PackageConstants.MstestTestAdapterName, Version = ConstantPackageVersions.MstestTestAdapterVersion }), noFrameworkPackageReferenceItemGroup, mergeExisting: false); _transformApplicator.Execute( PackageDependencyInfoTransform().Transform( new PackageDependencyInfo { Name = PackageConstants.MstestTestFrameworkName, Version = ConstantPackageVersions.MstestTestFrameworkVersion }), noFrameworkPackageReferenceItemGroup, mergeExisting: false); } break; case ProjectType.Library: if (!project.HasDependency( (dep) => dep.Name.Trim().ToLower() == PackageConstants.NetStandardPackageName.ToLower())) { _transformApplicator.Execute( PackageDependencyInfoTransform().Transform( new PackageDependencyInfo { Name = PackageConstants.NetStandardPackageName, Version = PackageConstants.NetStandardPackageVersion }), noFrameworkPackageReferenceItemGroup, mergeExisting: true); } break; default: break; } }
private Task RemoveItems(ProjectItemGroupElement parent, Dictionary <string, ProjectItemElement> items, string directoryName, ProjectWriteLockReleaser access) { return(RemoveItems(parent, items, items.Keys.Where(f => f.StartsWithIgnoreCase(directoryName)).ToList(), access)); }
public ORMGeneratorSelectionControl(EnvDTE.ProjectItem projectItem, IServiceProvider serviceProvider) : this() { _projectItem = projectItem; _serviceProvider = serviceProvider; #if VISUALSTUDIO_10_0 ProjectRootElement project = ProjectRootElement.TryOpen(projectItem.ContainingProject.FullName); string projectFullPath = project.FullPath; #else // VISUALSTUDIO_10_0 Project project = Engine.GlobalEngine.GetLoadedProject(projectItem.ContainingProject.FullName); string projectFullPath = project.FullFileName; #endif // VISUALSTUDIO_10_0 _project = project; string projectItemRelativePath = (string)projectItem.Properties.Item("LocalPath").Value; projectItemRelativePath = (new Uri(projectFullPath)).MakeRelativeUri(new Uri(projectItemRelativePath)).ToString(); _projectItemRelativePath = projectItemRelativePath; #if VISUALSTUDIO_10_0 ProjectItemGroupElement originalItemGroup = ORMCustomTool.GetItemGroup(project, projectItemRelativePath); ProjectItemGroupElement itemGroup; if (originalItemGroup == null) { itemGroup = project.AddItemGroup(); itemGroup.Condition = string.Concat(ITEMGROUP_CONDITIONSTART, projectItemRelativePath, ITEMGROUP_CONDITIONEND); } else { itemGroup = project.AddItemGroup(); itemGroup.Condition = originalItemGroup.Condition; foreach (ProjectItemElement item in originalItemGroup.Items) { ProjectItemElement newItem = itemGroup.AddItem(item.ItemType, item.Include); newItem.Condition = item.Condition; foreach (ProjectMetadataElement metadataElement in item.Metadata) { newItem.AddMetadata(metadataElement.Name, metadataElement.Value); } } } #else // VISUALSTUDIO_10_0 BuildItemGroup originalItemGroup = ORMCustomTool.GetItemGroup(project, projectItemRelativePath); BuildItemGroup itemGroup; if (originalItemGroup == null) { itemGroup = project.AddNewItemGroup(); itemGroup.Condition = string.Concat(ITEMGROUP_CONDITIONSTART, projectItemRelativePath, ITEMGROUP_CONDITIONEND); } else { itemGroup = project.AddNewItemGroup(); itemGroup.Condition = originalItemGroup.Condition; foreach (BuildItem item in originalItemGroup) { BuildItem newItem = itemGroup.AddNewItem(item.Name, item.Include, false); newItem.Condition = item.Condition; item.CopyCustomMetadataTo(newItem); } } #endif // VISUALSTUDIO_10_0 _originalItemGroup = originalItemGroup; _itemGroup = itemGroup; string condition = itemGroup.Condition.Trim(); string sourceFileName = this._sourceFileName = projectItem.Name; this.textBox_ORMFileName.Text = sourceFileName; this.button_SaveChanges.Click += new EventHandler(this.SaveChanges); this.button_Cancel.Click += new EventHandler(this.Cancel); ITree tree = (ITree)(this.virtualTreeControl.MultiColumnTree = new MultiColumnTree(2)); this.virtualTreeControl.SetColumnHeaders(new VirtualTreeColumnHeader[] { // TODO: Localize these. new VirtualTreeColumnHeader("Generated File Format", 0.30f, VirtualTreeColumnHeaderStyles.ColumnPositionLocked | VirtualTreeColumnHeaderStyles.DragDisabled), new VirtualTreeColumnHeader("Generated File Name", 1f, VirtualTreeColumnHeaderStyles.ColumnPositionLocked | VirtualTreeColumnHeaderStyles.DragDisabled) }, true); MainBranch mainBranch = this._mainBranch = new MainBranch(this); int totalCount = mainBranch.VisibleItemCount; int[] primaryIndices = new int[totalCount]; for (int i = 0; i < totalCount; ++i) { if (mainBranch.IsPrimaryDisplayItem(i)) { primaryIndices[i] = i - totalCount; } else { primaryIndices[i] = i + 1; } } Array.Sort <int>(primaryIndices); int lastPrimary = -1; for (int i = 0; i < totalCount; ++i) { int modifiedIndex = primaryIndices[i]; if (modifiedIndex < 0) { primaryIndices[i] = modifiedIndex + totalCount; } else { if (lastPrimary == -1) { lastPrimary = i - 1; } primaryIndices[i] = modifiedIndex - 1; } } int modifierCount = totalCount - mainBranch.Branches.Count; tree.Root = (lastPrimary == -1) ? (IBranch)mainBranch : new BranchPartition( mainBranch, primaryIndices, new BranchPartitionSection(0, lastPrimary + 1, null), new BranchPartitionSection(totalCount - modifierCount, modifierCount, "Generated File Modifiers"), new BranchPartitionSection(lastPrimary + 1, totalCount - lastPrimary - modifierCount - 1, "Intermediate and Secondary Files")); // UNDONE: Localize Header this.virtualTreeControl.ShowToolTips = true; this.virtualTreeControl.FullCellSelect = true; #if VISUALSTUDIO_10_0 Dictionary <string, ProjectItemElement> buildItemsByGenerator = this._itemsByGenerator = new Dictionary <string, ProjectItemElement>(itemGroup.Count, StringComparer.OrdinalIgnoreCase); foreach (ProjectItemElement buildItem in itemGroup.Items) #else // VISUALSTUDIO_10_0 Dictionary <string, BuildItem> buildItemsByGenerator = this._itemsByGenerator = new Dictionary <string, BuildItem>(itemGroup.Count, StringComparer.OrdinalIgnoreCase); foreach (BuildItem buildItem in itemGroup) #endif // VISUALSTUDIO_10_0 { // Do this very defensively so that the dialog can still be opened if a project is out // of step with the generators registered on a specific machine. string generatorNameData = buildItem.GetEvaluatedMetadata(ITEMMETADATA_ORMGENERATOR); string[] generatorNames; // The first string is the primary generator, others are the format modifiers int generatorNameCount; IORMGenerator primaryGenerator; MainBranch.OutputFormatBranch primaryFormatBranch; if (!String.IsNullOrEmpty(generatorNameData) && String.Equals(buildItem.GetEvaluatedMetadata(ITEMMETADATA_DEPENDENTUPON), sourceFileName, StringComparison.OrdinalIgnoreCase) && null != (generatorNames = generatorNameData.Split((char[])null, StringSplitOptions.RemoveEmptyEntries)) && 0 != (generatorNameCount = generatorNames.Length) && ORMCustomTool.ORMGenerators.TryGetValue(generatorNames[0], out primaryGenerator) && mainBranch.Branches.TryGetValue(primaryGenerator.ProvidesOutputFormat, out primaryFormatBranch)) { System.Diagnostics.Debug.Assert(primaryFormatBranch.SelectedORMGenerator == null); primaryFormatBranch.SelectedORMGenerator = primaryGenerator; buildItemsByGenerator.Add(generatorNames[0], buildItem); // Format modifiers are attached to the end of the list for (int i = 1; i < generatorNameCount; ++i) { MainBranch.OutputFormatBranch modifierBranch = primaryFormatBranch.NextModifier; string findName = generatorNames[i]; while (modifierBranch != null) { IORMGenerator testGenerator = modifierBranch.ORMGenerators[0]; if (testGenerator.OfficialName == findName) { modifierBranch.SelectedORMGenerator = testGenerator; break; } modifierBranch = modifierBranch.NextModifier; } } } } }
private bool ConvertProject(string projectPath, string packagesConfigPath) { try { Log.Info($" Converting project \"{projectPath}\""); PackagesConfigReader packagesConfigReader = new PackagesConfigReader(XDocument.Load(packagesConfigPath)); List <PackageReference> packages = packagesConfigReader.GetPackages(allowDuplicatePackageIds: true).Select(i => new PackageReference(i.PackageIdentity, i.TargetFramework, i.IsUserInstalled, i.IsDevelopmentDependency, i.RequireReinstallation, i.AllowedVersions, PackagePathResolver, VersionFolderPathResolver)).ToList(); DetectMissingTransitiveDependencies(packages, projectPath); ProjectRootElement project = ProjectRootElement.Open(projectPath, _projectCollection, preserveFormatting: true); ProjectItemGroupElement packageReferenceItemGroupElement = null; foreach (ProjectElement element in project.AllChildren) { ProcessElement(element, packages); if (packageReferenceItemGroupElement == null && element is ProjectItemElement itemElement && itemElement.ItemType.Equals("Reference")) { // Find the first Reference item and use it to add PackageReference items to, otherwise a new ItemGroup is added packageReferenceItemGroupElement = element.Parent as ProjectItemGroupElement; } } if (packageReferenceItemGroupElement == null) { packageReferenceItemGroupElement = project.AddItemGroup(); } Log.Info(" Current package references:"); foreach (PackageReference package in packages.Where(i => !i.IsMissingTransitiveDependency)) { Log.Info($" {package.PackageIdentity}"); } foreach (ProjectElement element in packages.Where(i => !i.IsMissingTransitiveDependency).SelectMany(i => i.AllElements)) { Log.Debug($" {element.Location}: Removing element {element.ToXmlString()}"); element.Remove(); } if (this._converterSettings.TrimPackages) { List <NuGetFramework> targetFrameworks = new List <NuGetFramework> { FrameworkConstants.CommonFrameworks.Net45 }; using (SourceCacheContext sourceCacheContext = new SourceCacheContext { IgnoreFailedSources = true, }) { var packageRestoreGraph = GetRestoreTargetGraph(packages, projectPath, targetFrameworks, sourceCacheContext); TrimPackages(packages, projectPath, packageRestoreGraph.Flattened); } } Log.Info(" Converted package references:"); foreach (PackageReference package in packages) { ProjectItemElement packageReferenceItemElement = AddPackageReference(packageReferenceItemGroupElement, package); Log.Info($" {packageReferenceItemElement.ToXmlString()}"); } if (project.HasUnsavedChanges) { Log.Debug($" Saving project \"{project.FullPath}\""); project.Save(); } Log.Debug($" Deleting file \"{packagesConfigPath}\""); File.Delete(packagesConfigPath); Log.Info($" Successfully converted \"{project.FullPath}\""); } catch (Exception e) { Log.Error($"Failed to convert '{projectPath}' : {e}"); return(false); } return(true); }