public void Save() { XDocument cpvm = XDocument.Parse( "<Project>" + "<PropertyGroup>" + "<CentralPackageVersionsFileImported>true</CentralPackageVersionsFileImported>" + "</PropertyGroup>" + "</Project>"); NuGetFramework framework = null; foreach (var pv in _packageVersions) { ProjectFileUtils.AddItem(cpvm, "PackageVersion", pv.Key, framework: framework, properties: new Dictionary <string, string>(), new Dictionary <string, string>() { ["Version"] = pv.Value }); } var filePath = Path.Combine(_path, Name); if (File.Exists(filePath)) { File.Delete(filePath); } File.WriteAllText(filePath, cpvm.ToString()); }
private void ModifyProjectFile() { using (FileStream stream = new FileStream(ProjectFilePath, FileMode.Open, FileAccess.ReadWrite)) { var xml = XDocument.Load(stream); if (PackageIconUrl != null) { ProjectFileUtils.AddProperty(xml, "PackageIconUrl", PackageIconUrl); } if (PackageIcon != null) { ProjectFileUtils.AddProperty(xml, "PackageIcon", PackageIcon); } if (PackageReadmeFile != null) { ProjectFileUtils.AddProperty(xml, "PackageReadmeFile", PackageReadmeFile); } ProjectFileUtils.AddProperties(xml, Properties); foreach (var tup in ItemGroupEntries) { var attributes = new Dictionary <string, string>(); var properties = new Dictionary <string, string>(); if (tup.PackagePath != null) { attributes["PackagePath"] = tup.PackagePath; } if (tup.Pack != null) { attributes["Pack"] = tup.Pack; } if (tup.Version != null) { attributes["Version"] = tup.Version; } ProjectFileUtils.AddItem(xml, tup.ItemType, tup.ItemPath, string.Empty, properties, attributes); } ProjectFileUtils.WriteXmlToFile(xml, stream); } }
public XDocument GetXML() { var sampleCSProjPath = (Type == ProjectStyle.PackageReference && ToolingVersion15) ? "Test.Utility.compiler.resources.project2.csproj" : "Test.Utility.compiler.resources.project1.csproj"; var s = ResourceTestUtility.GetResource(sampleCSProjPath, typeof(SimpleTestProjectContext)); var xml = XDocument.Parse(s); // MSBuildProjectExtensionsPath needs to be set before Microsoft.Common.props is imported, so add a new // PropertyGroup as the first element under the Project var ns = xml.Root.GetDefaultNamespace(); if (SetMSBuildProjectExtensionsPath) { var propertyGroup = new XElement(ns + "PropertyGroup"); propertyGroup.Add(new XElement(ns + "MSBuildProjectExtensionsPath", OutputPath)); xml.Root.AddFirst(propertyGroup); } ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { "ProjectGuid", "{" + ProjectGuid.ToString() + "}" }, { "AssemblyName", ProjectName } }); ProjectFileUtils.AddProperties(xml, Properties); if (Type == ProjectStyle.PackageReference) { if (WarningsAsErrors) { ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { "WarningsAsErrors", "true" } }); } ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { "Version", Version }, { "DebugType", "portable" } }); if (!IsLegacyPackageReference) { var tfPropName = SingleTargetFramework ? "TargetFramework" : "TargetFrameworks"; ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { tfPropName, OriginalFrameworkStrings.Count != 0 ? string.Join(";", OriginalFrameworkStrings): string.Join(";", Frameworks.Select(f => f.Framework.GetShortFolderName())) }, }); } var addedToAllProjectReferences = new HashSet <SimpleTestProjectContext>(); var addedToAllPackageReferences = new HashSet <SimpleTestPackageContext>(); var addedToAllPackageDownloads = new HashSet <SimpleTestPackageContext>(); foreach (var frameworkInfo in Frameworks) { // Add properties with a TFM condition ProjectFileUtils.AddProperties(xml, frameworkInfo.Properties, $" '$(TargetFramework)' == '{frameworkInfo.Framework.GetShortFolderName()}' "); foreach (var package in frameworkInfo.PackageReferences) { var referenceFramework = frameworkInfo.Framework; // Drop the conditional if it is not needed if (Frameworks.All(f => f.PackageReferences.Contains(package))) { referenceFramework = NuGetFramework.AnyFramework; if (!addedToAllPackageReferences.Add(package)) { // Skip since this was already added continue; } } var props = new Dictionary <string, string>(); var attributes = new Dictionary <string, string>(); // To support CPVM scenarios the Version can be null // In these cases do not write any version if (package.Version != null) { if (ToolingVersion15) { attributes.Add("Version", package.Version.ToString()); } else { props.Add("Version", package.Version.ToString()); } } if (!string.IsNullOrEmpty(package.Include)) { props.Add("IncludeAssets", package.Include); } if (!string.IsNullOrEmpty(package.Exclude)) { props.Add("ExcludeAssets", package.Exclude); } if (!string.IsNullOrEmpty(package.PrivateAssets)) { props.Add("PrivateAssets", package.PrivateAssets); } if (!string.IsNullOrEmpty(package.Aliases)) { props.Add("Aliases", package.Aliases); } if (!string.IsNullOrEmpty(package.NoWarn)) { props.Add("NoWarn", package.NoWarn); } ProjectFileUtils.AddItem( xml, "PackageReference", package.Id, referenceFramework, props, attributes); } foreach (var package in frameworkInfo.PackageDownloads) { var referenceFramework = frameworkInfo.Framework; // Drop the conditional if it is not needed if (Frameworks.All(f => f.PackageDownloads.Contains(package))) { referenceFramework = NuGetFramework.AnyFramework; if (!addedToAllPackageDownloads.Add(package)) { // Skip since this was already added continue; } } var props = new Dictionary <string, string>(); var attributes = new Dictionary <string, string>(); props.Add("Version", $"[{package.Version.ToString()}]"); ProjectFileUtils.AddItem( xml, "PackageDownload", package.Id, referenceFramework, props, attributes); } foreach (var project in frameworkInfo.ProjectReferences) { var referenceFramework = frameworkInfo.Framework; // Drop the conditional if it is not needed if (Frameworks.All(f => f.ProjectReferences.Contains(project))) { referenceFramework = NuGetFramework.AnyFramework; if (!addedToAllProjectReferences.Add(project)) { // Skip since this was already added continue; } } var props = new Dictionary <string, string> { { "Name", project.ProjectName }, { "Project", project.ProjectGuid.ToString() } }; if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("IncludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("ExcludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.PrivateAssets)) { props.Add("PrivateAssets", project.PrivateAssets); } ProjectFileUtils.AddItem( xml, "ProjectReference", $"{project.ProjectPath}", referenceFramework, props, new Dictionary <string, string>()); } } // Add tool references foreach (var tool in DotnetCLIToolReferences) { var props = new Dictionary <string, string>(); var attributes = new Dictionary <string, string>(); if (ToolingVersion15) { attributes.Add("Version", tool.Version.ToString()); } else { props.Add("Version", tool.Version.ToString()); } ProjectFileUtils.AddItem( xml, "DotNetCliToolReference", $"{tool.Id}", NuGetFramework.AnyFramework, props, attributes); } } else { // Add all project references directly foreach (var project in Frameworks.SelectMany(f => f.ProjectReferences).Distinct()) { var props = new Dictionary <string, string> { { "Name", project.ProjectName }, { "Project", project.ProjectGuid.ToString() } }; if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("IncludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("ExcludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.PrivateAssets)) { props.Add("PrivateAssets", project.PrivateAssets); } ProjectFileUtils.AddItem( xml, "ProjectReference", $"{project.ProjectPath}", NuGetFramework.AnyFramework, props, new Dictionary <string, string>()); } } return(xml); }
public XDocument GetXML() { var sampleCSProjPath = (Type == ProjectStyle.PackageReference && ToolingVersion15) ? "Test.Utility.compiler.resources.project2.csproj" : "Test.Utility.compiler.resources.project1.csproj"; var s = ResourceTestUtility.GetResource(sampleCSProjPath, typeof(SimpleTestProjectContext)); var xml = XDocument.Parse(s); ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { "ProjectGuid", "{" + ProjectGuid.ToString() + "}" }, { "BaseIntermediateOutputPath", OutputPath }, { "AssemblyName", ProjectName } }); ProjectFileUtils.AddProperties(xml, Properties); if (Type == ProjectStyle.PackageReference) { if (WarningsAsErrors) { ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { "WarningsAsErrors", "true" } }); } ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { "Version", Version }, { "DebugType", "portable" } }); if (!IsLegacyPackageReference) { var tfPropName = SingleTargetFramework ? "TargetFramework" : "TargetFrameworks"; ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>() { { tfPropName, OriginalFrameworkStrings.Count != 0 ? string.Join(";", OriginalFrameworkStrings): string.Join(";", Frameworks.Select(f => f.Framework.GetShortFolderName())) }, }); } var addedToAllProjectReferences = new HashSet <SimpleTestProjectContext>(); var addedToAllPackageReferences = new HashSet <SimpleTestPackageContext>(); foreach (var frameworkInfo in Frameworks) { // Add properties with a TFM condition ProjectFileUtils.AddProperties(xml, frameworkInfo.Properties, $" '$(TargetFramework)' == '{frameworkInfo.Framework.GetShortFolderName()}' "); foreach (var package in frameworkInfo.PackageReferences) { var referenceFramework = frameworkInfo.Framework; // Drop the conditional if it is not needed if (Frameworks.All(f => f.PackageReferences.Contains(package))) { referenceFramework = NuGetFramework.AnyFramework; if (!addedToAllPackageReferences.Add(package)) { // Skip since this was already added continue; } } var props = new Dictionary <string, string>(); var attributes = new Dictionary <string, string>(); if (ToolingVersion15) { attributes.Add("Version", package.Version.ToString()); } else { props.Add("Version", package.Version.ToString()); } if (!string.IsNullOrEmpty(package.Include)) { props.Add("IncludeAssets", package.Include); } if (!string.IsNullOrEmpty(package.Exclude)) { props.Add("ExcludeAssets", package.Exclude); } if (!string.IsNullOrEmpty(package.PrivateAssets)) { props.Add("PrivateAssets", package.PrivateAssets); } if (!string.IsNullOrEmpty(package.NoWarn)) { props.Add("NoWarn", package.NoWarn); } ProjectFileUtils.AddItem( xml, "PackageReference", package.Id, referenceFramework, props, attributes); } foreach (var project in frameworkInfo.ProjectReferences) { var referenceFramework = frameworkInfo.Framework; // Drop the conditional if it is not needed if (Frameworks.All(f => f.ProjectReferences.Contains(project))) { referenceFramework = NuGetFramework.AnyFramework; if (!addedToAllProjectReferences.Add(project)) { // Skip since this was already added continue; } } var props = new Dictionary <string, string> { { "Name", project.ProjectName }, { "Project", project.ProjectGuid.ToString() } }; if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("IncludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("ExcludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.PrivateAssets)) { props.Add("PrivateAssets", project.PrivateAssets); } ProjectFileUtils.AddItem( xml, "ProjectReference", $"{project.ProjectPath}", referenceFramework, props, new Dictionary <string, string>()); } } // Add tool references foreach (var tool in DotnetCLIToolReferences) { var props = new Dictionary <string, string>(); var attributes = new Dictionary <string, string>(); if (ToolingVersion15) { attributes.Add("Version", tool.Version.ToString()); } else { props.Add("Version", tool.Version.ToString()); } ProjectFileUtils.AddItem( xml, "DotNetCliToolReference", $"{tool.Id}", NuGetFramework.AnyFramework, props, attributes); } } else { // Add all project references directly foreach (var project in Frameworks.SelectMany(f => f.ProjectReferences).Distinct()) { var props = new Dictionary <string, string> { { "Name", project.ProjectName }, { "Project", project.ProjectGuid.ToString() } }; if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("IncludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.ExcludeAssets)) { props.Add("ExcludeAssets", project.ExcludeAssets); } if (!string.IsNullOrEmpty(project.PrivateAssets)) { props.Add("PrivateAssets", project.PrivateAssets); } ProjectFileUtils.AddItem( xml, "ProjectReference", $"{project.ProjectPath}", NuGetFramework.AnyFramework, props, new Dictionary <string, string>()); } } return(xml); }