Example #1
0
        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)
            {
                ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>()
                {
                    { "Version", Version },
                    { "DebugType", "portable" }
                });

                if (!IsLegacyPackageReference)
                {
                    ProjectFileUtils.AddProperties(xml, new Dictionary <string, string>()
                    {
                        { "TargetFrameworks", string.Join(";", Frameworks.Select(f => f.Framework.GetShortFolderName())) },
                    });
                }

                var addedToAllProjectReferences = new HashSet <SimpleTestProjectContext>();
                var addedToAllPackageReferences = new HashSet <SimpleTestPackageContext>();

                foreach (var frameworkInfo in Frameworks)
                {
                    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);
                        }

                        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);
        }
Example #2
0
        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", ProjectExtensionsPath));
                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, string.Join(";", Frameworks.Select(f => f.TargetAlias)) },
                    });
                }

                var addedToAllProjectReferences = new HashSet <SimpleTestProjectContext>();
                var addedToAllPackageReferences = new HashSet <SimpleTestPackageContext>();
                var addedToAllPackageDownloads  = new HashSet <SimpleTestPackageContext>();

                foreach (var frameworkInfo in Frameworks)
                {
                    // Add TFM properties
                    var tfmProps = new Dictionary <string, string>(frameworkInfo.Properties);

                    if (!(Type == ProjectStyle.PackageReference && ToolingVersion15))
                    {
                        tfmProps.Add("TargetFrameworkIdentifier", frameworkInfo.Framework.Framework);
                        tfmProps.Add("TargetFrameworkVersion", $"v{NormalizeVersionString(frameworkInfo.Framework.Version)}");
                        tfmProps.Add("_TargetFrameworkVersionWithoutV", NormalizeVersionString(frameworkInfo.Framework.Version));
                        tfmProps.Add("TargetFrameworkMoniker", $"{frameworkInfo.Framework.Framework}, Version={NormalizeVersionString(frameworkInfo.Framework.Version)}");

                        if (frameworkInfo.Framework.HasPlatform)
                        {
                            tfmProps.Add("TargetPlatformIdentifier", frameworkInfo.Framework.Platform);
                            tfmProps.Add("TargetPlatformVersion", NormalizeVersionString(frameworkInfo.Framework.PlatformVersion));
                            tfmProps.Add("TargetPlatformMoniker", $"{frameworkInfo.Framework.Platform}, Version={NormalizeVersionString(frameworkInfo.Framework.PlatformVersion)}");
                        }
                    }

                    // Add properties with a TFM condition
                    ProjectFileUtils.AddProperties(xml, tfmProps, $" '$(TargetFramework)' == '{frameworkInfo.TargetAlias}' ");

                    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.VersionOverride))
                        {
                            props.Add("VersionOverride", package.VersionOverride);
                        }

                        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);
        }
Example #3
0
        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();
            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>();

                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);
        }