public void IncludeFlag_SuppressParentFlags() { // Arrange var json = @"{ ""dependencies"": { ""packageA"": { ""version"": ""1.0.0"", ""suppressParent"": ""build,contentFiles,runtime,native"" } }, ""frameworks"": { ""net46"": {} } }"; // Act var spec = JsonPackageSpecReader.GetPackageSpec(json, "TestProject", "project.json"); var dependency = spec.Dependencies.Single(); // Assert var expected = LibraryIncludeFlagUtils.GetFlags( new string[] { "build", "runtime", "contentFiles", "native" }); Assert.Equal(expected, dependency.SuppressParent); }
/// <summary> /// Convert a nuspec dependency to a library dependency. /// </summary> public static LibraryDependency GetLibraryDependencyFromNuspec(Packaging.Core.PackageDependency dependency) { // Start with the default. For package to package this is everything but content v2. var includeType = LibraryIncludeFlagUtils.NoContent; // Add includes if (dependency.Include.Count > 0) { includeType = LibraryIncludeFlagUtils.GetFlags(dependency.Include); } // Remove excludes if (dependency.Exclude.Count > 0) { var excludeType = LibraryIncludeFlagUtils.GetFlags(dependency.Exclude); includeType = includeType & ~excludeType; } // Create the library // Nuspec references cannot contain suppress parent flags var libraryDependency = new LibraryDependency { LibraryRange = new LibraryRange { Name = dependency.Id, VersionRange = dependency.VersionRange, TypeConstraint = LibraryDependencyTarget.PackageProjectExternal }, IncludeType = includeType, SuppressParent = LibraryIncludeFlags.None }; return(libraryDependency); }
public void IncludeFlag_UnknownFlagsParse() { // Arrange var json = @"{ ""dependencies"": { ""packageA"": { ""version"": ""1.0.0"", ""include"": ""futureFlag"", ""exclude"": ""futureFlag2"", ""suppressParent"": ""futureFlag"" } }, ""frameworks"": { ""net46"": {} } }"; // Act var spec = JsonPackageSpecReader.GetPackageSpec(json, "TestProject", "project.json"); var dependency = spec.Dependencies.Single(); var futureFlag = LibraryIncludeFlagUtils.GetFlags(new string[] { "futureFlag" }); // Assert Assert.Equal(LibraryIncludeFlags.None, futureFlag); }
public void GetFlags_WhenFlagsIsNull_Throws() { ArgumentNullException exception = Assert.Throws <ArgumentNullException>( () => LibraryIncludeFlagUtils.GetFlags(flags: null)); Assert.Equal("flags", exception.ParamName); }
public void GetFlags_WhenFlagsIsSingleValue_ReturnsFlag( LibraryIncludeFlags input, LibraryIncludeFlags expectedResult) { LibraryIncludeFlags actualResult = LibraryIncludeFlagUtils.GetFlags(new[] { input.ToString() }); Assert.Equal(expectedResult, actualResult); }
public void IncludeFlag_RoundTrip(string expected, string flags) { // Arrange & Act var parsed = LibraryIncludeFlagUtils.GetFlags(flags.Split('|')); var actual = LibraryIncludeFlagUtils.GetFlagString(parsed); // Assert Assert.Equal(expected, actual); }
/// <summary> /// Gets the <see cref="LibraryIncludeFlags" /> for the specified value. /// </summary> /// <param name="value">A semicolon delimited list of include flags.</param> /// <param name="defaultValue">The default value ot return if the value contains no flags.</param> /// <returns>The <see cref="LibraryIncludeFlags" /> for the specified value, otherwise the <paramref name="defaultValue" />.</returns> private static LibraryIncludeFlags GetLibraryIncludeFlags(string value, LibraryIncludeFlags defaultValue) { if (string.IsNullOrWhiteSpace(value)) { return(defaultValue); } string[] parts = MSBuildStringUtility.Split(value); return(parts.Length > 0 ? LibraryIncludeFlagUtils.GetFlags(parts) : defaultValue); }
private static LibraryIncludeFlags GetIncludeFlags(string value, LibraryIncludeFlags defaultValue) { var parts = MSBuildStringUtility.Split(value); if (parts.Length > 0) { return(LibraryIncludeFlagUtils.GetFlags(parts)); } else { return(defaultValue); } }
public void GetFlags_WhenFlagsIsMultipleValues_ReturnsCombinationOfValues() { LibraryIncludeFlags[] expectedFlags = new[] { LibraryIncludeFlags.Runtime, LibraryIncludeFlags.Compile, LibraryIncludeFlags.Build }; LibraryIncludeFlags flags = LibraryIncludeFlagUtils.GetFlags(expectedFlags.Select(flag => flag.ToString())); Assert.Equal("Runtime, Compile, Build", flags.ToString()); }
public void IncludeFlag_IncludeOnlyContent() { // Arrange var json = @"{ ""dependencies"": { ""packageA"": { ""version"": ""1.0.0"", ""include"": ""contentFiles"" } }, ""frameworks"": { ""net46"": {} } }"; // Act var spec = JsonPackageSpecReader.GetPackageSpec(json, "TestProject", "project.json"); var dependency = spec.Dependencies.Single(); // Assert Assert.True(dependency.IncludeType == LibraryIncludeFlagUtils.GetFlags(new string[] { "contentFiles" })); }
private static void PopulateDependencies( string packageSpecPath, IList <LibraryDependency> results, JObject settings, string propertyName, bool isGacOrFrameworkReference) { var dependencies = settings[propertyName] as JObject; if (dependencies != null) { foreach (var dependency in dependencies) { if (string.IsNullOrEmpty(dependency.Key)) { throw FileFormatException.Create( "Unable to resolve dependency ''.", dependency.Value, packageSpecPath); } // Support // "dependencies" : { // "Name" : "1.0" // } var dependencyValue = dependency.Value; var dependencyTypeValue = LibraryDependencyType.Default; var dependencyIncludeFlagsValue = LibraryIncludeFlags.All; var dependencyExcludeFlagsValue = LibraryIncludeFlags.None; var suppressParentFlagsValue = LibraryIncludeFlagUtils.DefaultSuppressParent; var noWarn = new List <NuGetLogCode>(); // This method handles both the dependencies and framework assembly sections. // Framework references should be limited to references. // Dependencies should allow everything but framework references. var targetFlagsValue = isGacOrFrameworkReference ? LibraryDependencyTarget.Reference : LibraryDependencyTarget.All & ~LibraryDependencyTarget.Reference; var autoReferenced = false; string dependencyVersionValue = null; var dependencyVersionToken = dependencyValue; if (dependencyValue.Type == JTokenType.String) { dependencyVersionValue = dependencyValue.Value <string>(); } else { if (dependencyValue.Type == JTokenType.Object) { dependencyVersionToken = dependencyValue["version"]; if (dependencyVersionToken != null && dependencyVersionToken.Type == JTokenType.String) { dependencyVersionValue = dependencyVersionToken.Value <string>(); } } IEnumerable <string> strings; if (TryGetStringEnumerable(dependencyValue["type"], out strings)) { dependencyTypeValue = LibraryDependencyType.Parse(strings); // Types are used at pack time, they should be translated to suppressParent to // provide a matching effect for project to project references. // This should be set before suppressParent is checked. if (!dependencyTypeValue.Contains(LibraryDependencyTypeFlag.BecomesNupkgDependency)) { suppressParentFlagsValue = LibraryIncludeFlags.All; } else if (dependencyTypeValue.Contains(LibraryDependencyTypeFlag.SharedFramework)) { dependencyIncludeFlagsValue = LibraryIncludeFlags.Build | LibraryIncludeFlags.Compile | LibraryIncludeFlags.Analyzers; } } if (TryGetStringEnumerable(dependencyValue["include"], out strings)) { dependencyIncludeFlagsValue = LibraryIncludeFlagUtils.GetFlags(strings); } if (TryGetStringEnumerable(dependencyValue["exclude"], out strings)) { dependencyExcludeFlagsValue = LibraryIncludeFlagUtils.GetFlags(strings); } if (TryGetStringEnumerable(dependencyValue["suppressParent"], out strings)) { // This overrides any settings that came from the type property. suppressParentFlagsValue = LibraryIncludeFlagUtils.GetFlags(strings); } noWarn = GetNuGetLogCodeEnumerableFromJArray(dependencyValue["noWarn"]) .ToList(); var targetToken = dependencyValue["target"]; if (targetToken != null) { var targetString = targetToken.Value <string>(); targetFlagsValue = LibraryDependencyTargetUtils.Parse(targetString); // Verify that the value specified is package, project, or external project if (!ValidateDependencyTarget(targetFlagsValue)) { var message = string.Format( CultureInfo.CurrentCulture, Strings.InvalidDependencyTarget, targetString); throw FileFormatException.Create(message, targetToken, packageSpecPath); } } autoReferenced = GetBoolOrFalse(dependencyValue, "autoReferenced", packageSpecPath); } VersionRange dependencyVersionRange = null; if (!string.IsNullOrEmpty(dependencyVersionValue)) { try { dependencyVersionRange = VersionRange.Parse(dependencyVersionValue); } catch (Exception ex) { throw FileFormatException.Create( ex, dependencyVersionToken, packageSpecPath); } } // Projects and References may have empty version ranges, Packages may not if (dependencyVersionRange == null) { if ((targetFlagsValue & LibraryDependencyTarget.Package) == LibraryDependencyTarget.Package) { throw FileFormatException.Create( new ArgumentException(Strings.MissingVersionOnDependency), dependency.Value, packageSpecPath); } else { // Projects and references with no version property allow all versions dependencyVersionRange = VersionRange.All; } } // the dependency flags are: Include flags - Exclude flags var includeFlags = dependencyIncludeFlagsValue & ~dependencyExcludeFlagsValue; results.Add(new LibraryDependency() { LibraryRange = new LibraryRange() { Name = dependency.Key, TypeConstraint = targetFlagsValue, VersionRange = dependencyVersionRange, }, Type = dependencyTypeValue, IncludeType = includeFlags, SuppressParent = suppressParentFlagsValue, AutoReferenced = autoReferenced, NoWarn = noWarn.ToList() }); } } }
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); }
private static void PopulateDependencies( string projectPath, IList <ProjectLibraryDependency> results, JObject settings, string propertyName, bool isGacOrFrameworkReference) { var dependencies = settings.Value <JToken>(propertyName) as JObject; if (dependencies != null) { foreach (var dependency in dependencies) { if (string.IsNullOrEmpty(dependency.Key)) { throw FileFormatException.Create( "Unable to resolve dependency ''.", dependency.Key, projectPath); } var dependencyValue = dependency.Value; var dependencyTypeValue = LibraryDependencyType.Default; var dependencyIncludeFlagsValue = LibraryIncludeFlags.All; var dependencyExcludeFlagsValue = LibraryIncludeFlags.None; var suppressParentFlagsValue = LibraryIncludeFlagUtils.DefaultSuppressParent; var target = isGacOrFrameworkReference ? LibraryDependencyTarget.Reference : LibraryDependencyTarget.All; string dependencyVersionAsString = null; if (dependencyValue.Type == JTokenType.Object) { // "dependencies" : { "Name" : { "version": "1.0", "type": "build", "target": "project" } } dependencyVersionAsString = dependencyValue.Value <string>("version"); var type = dependencyValue.Value <string>("type"); if (type != null) { dependencyTypeValue = LibraryDependencyType.Parse(new [] { type }); } // Read the target if specified if (!isGacOrFrameworkReference) { var targetStr = dependencyValue.Value <string>("target"); target = LibraryDependencyTargetUtils.Parse(targetStr); } IEnumerable <string> strings; if (TryGetStringEnumerable(dependencyValue["include"], out strings)) { dependencyIncludeFlagsValue = LibraryIncludeFlagUtils.GetFlags(strings); } if (TryGetStringEnumerable(dependencyValue["exclude"], out strings)) { dependencyExcludeFlagsValue = LibraryIncludeFlagUtils.GetFlags(strings); } if (TryGetStringEnumerable(dependencyValue["suppressParent"], out strings)) { // This overrides any settings that came from the type property. suppressParentFlagsValue = LibraryIncludeFlagUtils.GetFlags(strings); } } else if (dependencyValue.Type == JTokenType.String) { // "dependencies" : { "Name" : "1.0" } dependencyVersionAsString = dependencyValue.Value <string>(); } else { throw FileFormatException.Create( string.Format( "Invalid dependency version: {0}. The format is not recognizable.", dependency.Key), dependencyValue, projectPath); } VersionRange dependencyVersionRange = null; if (!string.IsNullOrEmpty(dependencyVersionAsString)) { try { dependencyVersionRange = VersionRange.Parse(dependencyVersionAsString); } catch (Exception ex) { throw FileFormatException.Create(ex, dependencyValue, projectPath); } } // the dependency flags are: Include flags - Exclude flags var includeFlags = dependencyIncludeFlagsValue & ~dependencyExcludeFlagsValue; var lineInfo = (IJsonLineInfo)dependencyValue; results.Add(new ProjectLibraryDependency { LibraryRange = new LibraryRange( dependency.Key, dependencyVersionRange, target), Type = dependencyTypeValue, IncludeType = includeFlags, SuppressParent = suppressParentFlagsValue, SourceFilePath = projectPath, SourceLine = lineInfo.LineNumber, SourceColumn = lineInfo.LinePosition }); } } }
public void GetFlags_WhenFlagsCasingDiffers_ReturnsFlag(string flag) { LibraryIncludeFlags flags = LibraryIncludeFlagUtils.GetFlags(new[] { flag }); Assert.Equal(LibraryIncludeFlags.Compile, flags); }
public void GetFlags_WhenFlagsIsEmpty_ReturnsNone() { LibraryIncludeFlags flags = LibraryIncludeFlagUtils.GetFlags(Enumerable.Empty <string>()); Assert.Equal(LibraryIncludeFlags.None, flags); }