Example #1
0
        private static void SetWarningProperties(IObjectWriter writer, ProjectRestoreMetadata msbuildMetadata)
        {
            if (msbuildMetadata.ProjectWideWarningProperties != null &&
                (msbuildMetadata.ProjectWideWarningProperties.AllWarningsAsErrors ||
                 msbuildMetadata.ProjectWideWarningProperties.NoWarn.Count > 0 ||
                 msbuildMetadata.ProjectWideWarningProperties.WarningsAsErrors.Count > 0))
            {
                writer.WriteObjectStart("warningProperties");

                SetValueIfTrue(writer, "allWarningsAsErrors", msbuildMetadata.ProjectWideWarningProperties.AllWarningsAsErrors);

                if (msbuildMetadata.ProjectWideWarningProperties.NoWarn.Count > 0)
                {
                    SetArrayValue(writer, "noWarn", msbuildMetadata
                                  .ProjectWideWarningProperties
                                  .NoWarn
                                  .ToArray()
                                  .OrderBy(c => c)
                                  .Select(c => c.GetName())
                                  .Where(c => !string.IsNullOrEmpty(c)));
                }

                if (msbuildMetadata.ProjectWideWarningProperties.WarningsAsErrors.Count > 0)
                {
                    SetArrayValue(writer, "warnAsError", msbuildMetadata
                                  .ProjectWideWarningProperties
                                  .WarningsAsErrors
                                  .ToArray()
                                  .OrderBy(c => c)
                                  .Select(c => c.GetName())
                                  .Where(c => !string.IsNullOrEmpty(c)));
                }

                writer.WriteObjectEnd();
            }
        }
        private static ProjectRestoreMetadata GetMSBuildMetadata(PackageSpec packageSpec, JObject rawPackageSpec)
        {
            var rawMSBuildMetadata = rawPackageSpec.Value <JToken>(RestoreOptions) as JObject;

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

            var msbuildMetadata = new ProjectRestoreMetadata();

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

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

            ProjectStyle projectStyle;

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

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

            return(msbuildMetadata);
        }