internal ProjectFilesCollection(JsonObject rawProject, string projectDirectory, string projectFilePath, ICollection<DiagnosticMessage> warnings = null) { _projectDirectory = projectDirectory; _projectFilePath = projectFilePath; var excludeBuiltIns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, "excludeBuiltIn", DefaultBuiltInExcludePatterns); var excludePatterns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, "exclude") .Concat(excludeBuiltIns); var contentBuiltIns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, "contentBuiltIn", DefaultContentsBuiltInPatterns); var compileBuiltIns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, "compileBuiltIn", DefaultCompileBuiltInPatterns); var resourceBuiltIns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, "resourceBuiltIn", DefaultResourcesBuiltInPatterns); // TODO: The legacy names will be retired in the future. var legacyPublishExcludePatternName = "bundleExclude"; var legacyPublishExcludePatternToken = rawProject.ValueAsJsonObject(legacyPublishExcludePatternName); if (legacyPublishExcludePatternToken != null) { _publishExcludePatterns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, legacyPublishExcludePatternName, DefaultPublishExcludePatterns); if (warnings != null) { warnings.Add(new DiagnosticMessage( string.Format("Property \"{0}\" is deprecated. It is replaced by \"{1}\".", legacyPublishExcludePatternName, "publishExclude"), projectFilePath, DiagnosticMessageSeverity.Warning, legacyPublishExcludePatternToken.Line, legacyPublishExcludePatternToken.Column)); } } else { _publishExcludePatterns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, "publishExclude", DefaultPublishExcludePatterns); } _sharedPatternsGroup = PatternGroup.Build(rawProject, projectDirectory, projectFilePath, "shared", legacyName: null, warnings: warnings, fallbackIncluding: DefaultSharedPatterns, additionalExcluding: excludePatterns); _resourcePatternsGroup = PatternGroup.Build(rawProject, projectDirectory, projectFilePath, "resource", "resources", warnings: warnings, additionalIncluding: resourceBuiltIns, additionalExcluding: excludePatterns); _preprocessPatternsGroup = PatternGroup.Build(rawProject, projectDirectory, projectFilePath, "preprocess", legacyName: null, warnings: warnings, fallbackIncluding: DefaultPreprocessPatterns, additionalExcluding: excludePatterns) .ExcludeGroup(_sharedPatternsGroup) .ExcludeGroup(_resourcePatternsGroup); _compilePatternsGroup = PatternGroup.Build(rawProject, projectDirectory, projectFilePath, "compile", "code", warnings: warnings, additionalIncluding: compileBuiltIns, additionalExcluding: excludePatterns) .ExcludeGroup(_sharedPatternsGroup) .ExcludeGroup(_preprocessPatternsGroup) .ExcludeGroup(_resourcePatternsGroup); _contentPatternsGroup = PatternGroup.Build(rawProject, projectDirectory, projectFilePath, "content", "files", warnings: warnings, additionalIncluding: contentBuiltIns, additionalExcluding: excludePatterns.Concat(_publishExcludePatterns)) .ExcludeGroup(_compilePatternsGroup) .ExcludeGroup(_preprocessPatternsGroup) .ExcludeGroup(_sharedPatternsGroup) .ExcludeGroup(_resourcePatternsGroup); _namedResources = NamedResourceReader.ReadNamedResources(rawProject, projectFilePath); }
public static IDictionary<string, string> ReadNamedResources(JsonObject rawProject, string projectFilePath) { if (!rawProject.Keys.Contains("namedResource")) { return new Dictionary<string, string>(); } var namedResourceToken = rawProject.ValueAsJsonObject("namedResource"); if (namedResourceToken == null) { throw FileFormatException.Create("Value must be object.", rawProject.Value("namedResource"), projectFilePath); } var namedResources = new Dictionary<string, string>(); foreach (var namedResourceKey in namedResourceToken.Keys) { var resourcePath = namedResourceToken.ValueAsString(namedResourceKey); if (resourcePath == null) { throw FileFormatException.Create("Value must be string.", namedResourceToken.Value(namedResourceKey), projectFilePath); } if (resourcePath.Value.Contains("*")) { throw FileFormatException.Create("Value cannot contain wildcards.", resourcePath, projectFilePath); } var resourceFileFullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(projectFilePath), resourcePath)); if (namedResources.ContainsKey(namedResourceKey)) { throw FileFormatException.Create( string.Format("The named resource {0} already exists.", namedResourceKey), resourcePath, projectFilePath); } namedResources.Add( namedResourceKey, resourceFileFullPath); } return namedResources; }
private void BuildTargetFrameworksAndConfigurations(Project project, JsonObject projectJsonObject, ICollection<DiagnosticMessage> diagnostics) { // Get the shared compilationOptions project._defaultCompilerOptions = GetCompilationOptions(projectJsonObject) ?? new CompilerOptions(); project._defaultTargetFrameworkConfiguration = new TargetFrameworkInformation { Dependencies = new List<LibraryDependency>() }; // Add default configurations project._compilerOptionsByConfiguration["Debug"] = new CompilerOptions { Defines = new[] { "DEBUG", "TRACE" }, Optimize = false }; project._compilerOptionsByConfiguration["Release"] = new CompilerOptions { Defines = new[] { "RELEASE", "TRACE" }, Optimize = true }; // The configuration node has things like debug/release compiler settings /* { "configurations": { "Debug": { }, "Release": { } } } */ var configurationsSection = projectJsonObject.ValueAsJsonObject("configurations"); if (configurationsSection != null) { foreach (var configKey in configurationsSection.Keys) { var compilerOptions = GetCompilationOptions(configurationsSection.ValueAsJsonObject(configKey)); // Only use this as a configuration if it's not a target framework project._compilerOptionsByConfiguration[configKey] = compilerOptions; } } // The frameworks node is where target frameworks go /* { "frameworks": { "net45": { }, "dnxcore50": { } } } */ var frameworks = projectJsonObject.ValueAsJsonObject("frameworks"); if (frameworks != null) { foreach (var frameworkKey in frameworks.Keys) { try { var frameworkToken = frameworks.ValueAsJsonObject(frameworkKey); var success = BuildTargetFrameworkNode(project, frameworkKey, frameworkToken); if (!success) { diagnostics?.Add( new DiagnosticMessage( DiagnosticMonikers.NU1008, $"\"{frameworkKey}\" is an unsupported framework.", project.ProjectFilePath, DiagnosticMessageSeverity.Error, frameworkToken.Line, frameworkToken.Column)); } } catch (Exception ex) { throw FileFormatException.Create(ex, frameworks.Value(frameworkKey), project.ProjectFilePath); } } } }
/// <summary> /// Parse a Json object which represents project configuration for a specified framework /// </summary> /// <param name="frameworkKey">The name of the framework</param> /// <param name="frameworkValue">The Json object represent the settings</param> /// <returns>Returns true if it successes.</returns> private bool BuildTargetFrameworkNode(Project project, string frameworkKey, JsonObject frameworkValue) { // If no compilation options are provided then figure them out from the node var compilerOptions = GetCompilationOptions(frameworkValue) ?? new CompilerOptions(); var frameworkName = FrameworkNameHelper.ParseFrameworkName(frameworkKey); // If it's not unsupported then keep it if (frameworkName == VersionUtility.UnsupportedFrameworkName) { // REVIEW: Should we skip unsupported target frameworks return false; } // Add the target framework specific define var defines = new HashSet<string>(compilerOptions.Defines ?? Enumerable.Empty<string>()); var frameworkDefinition = Tuple.Create(frameworkKey, frameworkName); var frameworkDefine = FrameworkNameHelper.MakeDefaultTargetFrameworkDefine(frameworkDefinition); if (!string.IsNullOrEmpty(frameworkDefine)) { defines.Add(frameworkDefine); } compilerOptions.Defines = defines; var targetFrameworkInformation = new TargetFrameworkInformation { FrameworkName = frameworkName, Dependencies = new List<LibraryDependency>() }; var frameworkDependencies = new List<LibraryDependency>(); PopulateDependencies( project.ProjectFilePath, frameworkDependencies, frameworkValue, "dependencies", isGacOrFrameworkReference: false); var frameworkAssemblies = new List<LibraryDependency>(); PopulateDependencies( project.ProjectFilePath, frameworkAssemblies, frameworkValue, "frameworkAssemblies", isGacOrFrameworkReference: true); frameworkDependencies.AddRange(frameworkAssemblies); targetFrameworkInformation.Dependencies = frameworkDependencies; targetFrameworkInformation.WrappedProject = frameworkValue.ValueAsString("wrappedProject"); var binNode = frameworkValue.ValueAsJsonObject("bin"); if (binNode != null) { targetFrameworkInformation.AssemblyPath = binNode.ValueAsString("assembly"); targetFrameworkInformation.PdbPath = binNode.ValueAsString("pdb"); } project._compilerOptionsByFramework[frameworkName] = compilerOptions; project._targetFrameworks[frameworkName] = targetFrameworkInformation; return true; }
private static void PopulateDependencies( string projectPath, IList<LibraryDependency> results, JsonObject settings, string propertyName, bool isGacOrFrameworkReference) { var dependencies = settings.ValueAsJsonObject(propertyName); if (dependencies != null) { foreach (var dependencyKey in dependencies.Keys) { if (string.IsNullOrEmpty(dependencyKey)) { throw FileFormatException.Create( "Unable to resolve dependency ''.", dependencies.Value(dependencyKey), projectPath); } var dependencyValue = dependencies.Value(dependencyKey); var dependencyTypeValue = LibraryDependencyType.Default; JsonString dependencyVersionAsString = null; string target = null; if (dependencyValue is JsonObject) { // "dependencies" : { "Name" : { "version": "1.0", "type": "build", "target": "project" } } var dependencyValueAsObject = (JsonObject)dependencyValue; dependencyVersionAsString = dependencyValueAsObject.ValueAsString("version"); // Remove support for flags (we only support build and nothing right now) var type = dependencyValueAsObject.ValueAsString("type"); if (type != null) { dependencyTypeValue = LibraryDependencyType.Parse(type.Value); } // Read the target if specified target = dependencyValueAsObject.ValueAsString("target")?.Value; } else if (dependencyValue is JsonString) { // "dependencies" : { "Name" : "1.0" } dependencyVersionAsString = (JsonString)dependencyValue; } else { throw FileFormatException.Create( string.Format("Invalid dependency version: {0}. The format is not recognizable.", dependencyKey), dependencyValue, projectPath); } SemanticVersionRange dependencyVersionRange = null; if (!string.IsNullOrEmpty(dependencyVersionAsString?.Value)) { try { dependencyVersionRange = VersionUtility.ParseVersionRange(dependencyVersionAsString.Value); } catch (Exception ex) { throw FileFormatException.Create( ex, dependencyValue, projectPath); } } results.Add(new LibraryDependency { LibraryRange = new LibraryRange(dependencyKey, isGacOrFrameworkReference) { VersionRange = dependencyVersionRange, FileName = projectPath, Line = dependencyValue.Line, Column = dependencyValue.Column, Target = target }, Type = dependencyTypeValue }); } } }
private static CompilerOptions GetCompilationOptions(JsonObject rawObject) { var rawOptions = rawObject.ValueAsJsonObject("compilationOptions"); if (rawOptions == null) { return null; } return new CompilerOptions { Defines = rawOptions.ValueAsStringArray("define"), LanguageVersion = rawOptions.ValueAsString("languageVersion"), AllowUnsafe = rawOptions.ValueAsNullableBoolean("allowUnsafe"), Platform = rawOptions.ValueAsString("platform"), WarningsAsErrors = rawOptions.ValueAsNullableBoolean("warningsAsErrors"), Optimize = rawOptions.ValueAsNullableBoolean("optimize"), KeyFile = rawOptions.ValueAsString("keyFile"), DelaySign = rawOptions.ValueAsNullableBoolean("delaySign"), StrongName = rawOptions.ValueAsNullableBoolean("strongName"), EmitEntryPoint = rawOptions.ValueAsNullableBoolean("emitEntryPoint") }; }
private static void PopulateDependencies( string projectPath, IList<LibraryDependency> results, JsonObject settings, string propertyName, bool isGacOrFrameworkReference) { var dependencies = settings.ValueAsJsonObject(propertyName); if (dependencies != null) { foreach (var dependencyKey in dependencies.Keys) { if (string.IsNullOrEmpty(dependencyKey)) { throw FileFormatException.Create( "Unable to resolve dependency ''.", dependencies.Value(dependencyKey), projectPath); } var dependencyValue = dependencies.Value(dependencyKey); var dependencyTypeValue = LibraryDependencyType.Default; JsonString dependencyVersionAsString = null; if (dependencyValue is JsonObject) { // "dependencies" : { "Name" : { "version": "1.0", "type": "build" } } var dependencyValueAsObject = (JsonObject)dependencyValue; dependencyVersionAsString = dependencyValueAsObject.ValueAsString("version"); IEnumerable<string> strings; if (TryGetStringEnumerable(dependencyValueAsObject, "type", out strings)) { dependencyTypeValue = LibraryDependencyType.Parse(strings); } } else if (dependencyValue is JsonString) { // "dependencies" : { "Name" : "1.0" } dependencyVersionAsString = (JsonString)dependencyValue; } else { throw FileFormatException.Create( string.Format("Invalid dependency version: {0}. The format is not recognizable.", dependencyKey), dependencyValue, projectPath); } SemanticVersionRange dependencyVersionRange = null; if (!string.IsNullOrEmpty(dependencyVersionAsString?.Value)) { try { dependencyVersionRange = VersionUtility.ParseVersionRange(dependencyVersionAsString.Value); } catch (Exception ex) { throw FileFormatException.Create( ex, dependencyValue, projectPath); } } results.Add(new LibraryDependency { LibraryRange = new LibraryRange(dependencyKey, isGacOrFrameworkReference) { VersionRange = dependencyVersionRange, FileName = projectPath, Line = dependencyValue.Line, Column = dependencyValue.Column }, Type = dependencyTypeValue }); } } }