Esempio n. 1
0
        private static ProjectRestoreMetadata GetMSBuildMetadata(PackageSpec packageSpec, JObject rawPackageSpec)
        {
            var rawMSBuildMetadata = rawPackageSpec.Value <JToken>(RestoreOptions) as JObject;

            if (rawMSBuildMetadata == null)
            {
                return(null);
            }

            var projectStyleString = rawMSBuildMetadata.GetValue <string>("projectStyle");

            ProjectStyle?projectStyle = null;

            if (!string.IsNullOrEmpty(projectStyleString) &&
                Enum.TryParse <ProjectStyle>(projectStyleString, ignoreCase: true, result: out var projectStyleValue))
            {
                projectStyle = projectStyleValue;
            }

            var msbuildMetadata = projectStyle == ProjectStyle.PackagesConfig
                ? new PackagesConfigProjectRestoreMetadata()
                : new ProjectRestoreMetadata();

            if (projectStyle.HasValue)
            {
                msbuildMetadata.ProjectStyle = projectStyle.Value;
            }

            msbuildMetadata.ProjectUniqueName = rawMSBuildMetadata.GetValue <string>("projectUniqueName");
            msbuildMetadata.OutputPath        = rawMSBuildMetadata.GetValue <string>("outputPath");

            msbuildMetadata.PackagesPath            = rawMSBuildMetadata.GetValue <string>("packagesPath");
            msbuildMetadata.ProjectJsonPath         = rawMSBuildMetadata.GetValue <string>("projectJsonPath");
            msbuildMetadata.ProjectName             = rawMSBuildMetadata.GetValue <string>("projectName");
            msbuildMetadata.ProjectPath             = rawMSBuildMetadata.GetValue <string>("projectPath");
            msbuildMetadata.CrossTargeting          = GetBoolOrFalse(rawMSBuildMetadata, "crossTargeting", packageSpec.FilePath);
            msbuildMetadata.LegacyPackagesDirectory = GetBoolOrFalse(rawMSBuildMetadata, "legacyPackagesDirectory", packageSpec.FilePath);
            msbuildMetadata.ValidateRuntimeAssets   = GetBoolOrFalse(rawMSBuildMetadata, "validateRuntimeAssets", packageSpec.FilePath);
            msbuildMetadata.SkipContentFileWrite    = GetBoolOrFalse(rawMSBuildMetadata, "skipContentFileWrite", packageSpec.FilePath);

            msbuildMetadata.Sources = new List <PackageSource>();

            var sourcesObj = rawMSBuildMetadata.GetValue <JObject>("sources");

            if (sourcesObj != null)
            {
                foreach (var prop in sourcesObj.Properties())
                {
                    msbuildMetadata.Sources.Add(new PackageSource(prop.Name));
                }
            }

            var filesObj = rawMSBuildMetadata.GetValue <JObject>("files");

            if (filesObj != null)
            {
                foreach (var prop in filesObj.Properties())
                {
                    msbuildMetadata.Files.Add(new ProjectRestoreMetadataFile(prop.Name, prop.Value.ToObject <string>()));
                }
            }

            var frameworksObj = rawMSBuildMetadata.GetValue <JObject>("frameworks");

            if (frameworksObj != null)
            {
                foreach (var frameworkProperty in frameworksObj.Properties())
                {
                    var framework      = NuGetFramework.Parse(frameworkProperty.Name);
                    var frameworkGroup = new ProjectRestoreMetadataFrameworkInfo(framework);

                    var projectsObj = frameworkProperty.Value.GetValue <JObject>("projectReferences");
                    if (projectsObj != null)
                    {
                        foreach (var prop in projectsObj.Properties())
                        {
                            frameworkGroup.ProjectReferences.Add(new ProjectRestoreReference()
                            {
                                ProjectUniqueName = prop.Name,
                                ProjectPath       = prop.Value.GetValue <string>("projectPath"),

                                IncludeAssets = LibraryIncludeFlagUtils.GetFlags(
                                    flags: prop.Value.GetValue <string>("includeAssets"),
                                    defaultFlags: LibraryIncludeFlags.All),

                                ExcludeAssets = LibraryIncludeFlagUtils.GetFlags(
                                    flags: prop.Value.GetValue <string>("excludeAssets"),
                                    defaultFlags: LibraryIncludeFlags.None),

                                PrivateAssets = LibraryIncludeFlagUtils.GetFlags(
                                    flags: prop.Value.GetValue <string>("privateAssets"),
                                    defaultFlags: LibraryIncludeFlagUtils.DefaultSuppressParent),
                            });
                        }
                    }

                    msbuildMetadata.TargetFrameworks.Add(frameworkGroup);
                }
            }

            // Add the config file paths to the equals method
            msbuildMetadata.ConfigFilePaths = new List <string>();

            var configFilePaths = rawMSBuildMetadata.GetValue <JArray>("configFilePaths");

            if (configFilePaths != null)
            {
                foreach (var fallbackFolder in configFilePaths.Select(t => t.Value <string>()))
                {
                    msbuildMetadata.ConfigFilePaths.Add(fallbackFolder);
                }
            }

            msbuildMetadata.FallbackFolders = new List <string>();

            var fallbackObj = rawMSBuildMetadata.GetValue <JArray>("fallbackFolders");

            if (fallbackObj != null)
            {
                foreach (var fallbackFolder in fallbackObj.Select(t => t.Value <string>()))
                {
                    msbuildMetadata.FallbackFolders.Add(fallbackFolder);
                }
            }

            msbuildMetadata.OriginalTargetFrameworks = new List <string>();

            var originalFrameworksObj = rawMSBuildMetadata.GetValue <JArray>("originalTargetFrameworks");

            if (originalFrameworksObj != null)
            {
                foreach (var orignalFramework in originalFrameworksObj.Select(t => t.Value <string>()))
                {
                    msbuildMetadata.OriginalTargetFrameworks.Add(orignalFramework);
                }
            }

            var warningPropertiesObj = rawMSBuildMetadata.GetValue <JObject>("warningProperties");

            if (warningPropertiesObj != null)
            {
                var allWarningsAsErrors = warningPropertiesObj.GetValue <bool>("allWarningsAsErrors");
                var warnAsError         = new HashSet <NuGetLogCode>(GetNuGetLogCodeEnumerableFromJArray(warningPropertiesObj["warnAsError"]));
                var noWarn = new HashSet <NuGetLogCode>(GetNuGetLogCodeEnumerableFromJArray(warningPropertiesObj["noWarn"]));

                msbuildMetadata.ProjectWideWarningProperties = new WarningProperties(warnAsError, noWarn, allWarningsAsErrors);
            }

            // read NuGet lock file msbuild properties
            var restoreLockProperties = rawMSBuildMetadata.GetValue <JObject>("restoreLockProperties");

            if (restoreLockProperties != null)
            {
                msbuildMetadata.RestoreLockProperties = new RestoreLockProperties(
                    restoreLockProperties.GetValue <string>("restorePackagesWithLockFile"),
                    restoreLockProperties.GetValue <string>("nuGetLockFilePath"),
                    GetBoolOrFalse(restoreLockProperties, "restoreLockedMode", packageSpec.FilePath));
            }

            if (msbuildMetadata is PackagesConfigProjectRestoreMetadata pcMsbuildMetadata)
            {
                pcMsbuildMetadata.PackagesConfigPath = rawMSBuildMetadata.GetValue <string>("packagesConfigPath");
            }

            return(msbuildMetadata);
        }