public static IEnumerable <string> GetPatternsCollection(JToken token, string projectDirectory, string projectFilePath, IEnumerable <string> defaultPatterns = null) { defaultPatterns = defaultPatterns ?? Enumerable.Empty <string>(); if (token == null) { return(defaultPatterns); } try { if (token.Type == JTokenType.Null) { return(CreateCollection(projectDirectory)); } if (token.Type == JTokenType.String) { return(CreateCollection(projectDirectory, token.Value <string>())); } if (token.Type == JTokenType.Array) { return(CreateCollection(projectDirectory, token.ValueAsArray <string>())); } } catch (InvalidOperationException ex) { throw FileFormatException.Create(ex, token, projectFilePath); } throw FileFormatException.Create("Value must be either string or array.", token, projectFilePath); }
public static IEnumerable <string> GetPatternsCollection(JsonObject rawProject, string projectDirectory, string projectFilePath, string propertyName, IEnumerable <string> defaultPatterns = null) { defaultPatterns = defaultPatterns ?? Enumerable.Empty <string>(); try { if (!rawProject.Keys.Contains(propertyName)) { return(CreateCollection(projectDirectory, defaultPatterns.ToArray())); } var valueInString = rawProject.ValueAsString(propertyName); if (valueInString != null) { return(CreateCollection(projectDirectory, valueInString)); } var valuesInArray = rawProject.ValueAsStringArray(propertyName); if (valuesInArray != null) { return(CreateCollection(projectDirectory, valuesInArray.Select(s => s.ToString()).ToArray())); } } catch (Exception ex) { throw FileFormatException.Create(ex, rawProject.Value(propertyName), projectFilePath); } throw FileFormatException.Create("Value must be either string or array.", rawProject.Value(propertyName), projectFilePath); }
public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings) { globalSettings = null; string globalJsonPath = null; if (Path.GetFileName(path) == GlobalFileName) { globalJsonPath = path; path = Path.GetDirectoryName(path); } else if (!HasGlobalFile(path)) { return(false); } else { globalJsonPath = Path.Combine(path, GlobalFileName); } globalSettings = new GlobalSettings(); try { using (var fs = File.OpenRead(globalJsonPath)) { var reader = new StreamReader(fs); var jobject = JsonDeserializer.Deserialize(reader) as JsonObject; if (jobject == null) { throw new InvalidOperationException("The JSON file can't be deserialized to a JSON object."); } var projectSearchPaths = jobject.ValueAsStringArray("projects") ?? jobject.ValueAsStringArray("sources") ?? new string[] { }; globalSettings.ProjectSearchPaths = new List <string>(projectSearchPaths); globalSettings.PackagesPath = jobject.ValueAsString("packages"); globalSettings.FilePath = globalJsonPath; } } catch (Exception ex) { throw FileFormatException.Create(ex, globalJsonPath); } return(true); }
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); }
public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings) { globalSettings = null; string globalJsonPath = null; if (Path.GetFileName(path) == GlobalFileName) { globalJsonPath = path; path = Path.GetDirectoryName(path); } else if (!HasGlobalFile(path)) { return(false); } else { globalJsonPath = Path.Combine(path, GlobalFileName); } globalSettings = new GlobalSettings(); string json = File.ReadAllText(globalJsonPath); JObject settings = null; try { settings = JObject.Parse(json); } catch (JsonReaderException ex) { throw FileFormatException.Create(ex, globalJsonPath); } // TODO: Remove sources var projectSearchPaths = settings["projects"] ?? settings["sources"]; globalSettings.ProjectSearchPaths = projectSearchPaths == null ? new string[] { } : projectSearchPaths.ValueAsArray <string>(); globalSettings.PackagesPath = settings.Value <string>("packages"); globalSettings.FilePath = globalJsonPath; return(true); }
public static bool TryGetProject(string path, out Project project, ICollection <ICompilationMessage> diagnostics = null) { project = null; string projectPath = null; if (string.Equals(Path.GetFileName(path), ProjectFileName, StringComparison.OrdinalIgnoreCase)) { projectPath = path; path = Path.GetDirectoryName(path); } else if (!HasProjectFile(path)) { return(false); } else { projectPath = Path.Combine(path, ProjectFileName); } // Assume the directory name is the project name if none was specified var projectName = PathUtility.GetDirectoryName(path); projectPath = Path.GetFullPath(projectPath); try { using (var stream = File.OpenRead(projectPath)) { project = GetProjectFromStream(stream, projectName, projectPath, diagnostics); } } catch (Exception ex) { throw FileFormatException.Create(ex, projectPath); } return(true); }
private void BuildTargetFrameworksAndConfigurations(JsonObject projectJsonObject, ICollection <ICompilationMessage> diagnostics) { // Get the shared compilationOptions _defaultCompilerOptions = GetCompilationOptions(projectJsonObject) ?? new CompilerOptions(); _defaultTargetFrameworkConfiguration = new TargetFrameworkInformation { Dependencies = new List <LibraryDependency>() }; // Add default configurations _configurations["Debug"] = new CompilerOptions { Defines = new[] { "DEBUG", "TRACE" }, Optimize = false }; _configurations["Release"] = new CompilerOptions { Defines = new[] { "RELEASE", "TRACE" }, Optimize = true }; // The configuration node has things like debug/release compiler settings /* * { * "configurations": { * "Debug": { * }, * "Release": { * } * } * } */ var configurations = projectJsonObject.ValueAsJsonObject("configurations"); if (configurations != null) { foreach (var configKey in configurations.Keys) { var compilerOptions = GetCompilationOptions(configurations.ValueAsJsonObject(configKey)); // Only use this as a configuration if it's not a target framework _configurations[configKey] = compilerOptions; } } // The frameworks node is where target frameworks go /* * { * "frameworks": { * "net45": { * }, * "k10": { * } * } * } */ var frameworks = projectJsonObject.ValueAsJsonObject("frameworks"); if (frameworks != null) { foreach (var frameworkKey in frameworks.Keys) { try { var frameworkToken = frameworks.ValueAsJsonObject(frameworkKey); var success = BuildTargetFrameworkNode(frameworkKey, frameworkToken); if (!success) { diagnostics?.Add( new FileFormatMessage( $"\"{frameworkKey}\" is an unsupported framework", ProjectFilePath, CompilationMessageSeverity.Error, frameworkToken)); } } catch (Exception ex) { throw FileFormatException.Create(ex, frameworks.Value(frameworkKey), ProjectFilePath); } } } }
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 }); } } }
internal static Project GetProjectFromStream(Stream stream, string projectName, string projectPath, ICollection <ICompilationMessage> diagnostics = null) { var project = new Project(); var reader = new StreamReader(stream); var rawProject = JsonDeserializer.Deserialize(reader) as JsonObject; if (rawProject == null) { throw FileFormatException.Create( "The JSON file can't be deserialized to a JSON object.", projectPath); } // Meta-data properties project.Name = projectName; project.ProjectFilePath = Path.GetFullPath(projectPath); var version = rawProject.Value("version") as JsonString; if (version == null) { project.Version = new SemanticVersion("1.0.0"); } else { try { var buildVersion = Environment.GetEnvironmentVariable("DNX_BUILD_VERSION"); project.Version = SpecifySnapshot(version, buildVersion); } catch (Exception ex) { throw FileFormatException.Create(ex, version, project.ProjectFilePath); } } var fileVersion = Environment.GetEnvironmentVariable("DNX_ASSEMBLY_FILE_VERSION"); if (string.IsNullOrWhiteSpace(fileVersion)) { project.AssemblyFileVersion = project.Version.Version; } else { try { var simpleVersion = project.Version.Version; project.AssemblyFileVersion = new Version(simpleVersion.Major, simpleVersion.Minor, simpleVersion.Build, int.Parse(fileVersion)); } catch (FormatException ex) { throw new FormatException("The assembly file version is invalid: " + fileVersion, ex); } } project.Description = rawProject.ValueAsString("description"); project.Summary = rawProject.ValueAsString("summary"); project.Copyright = rawProject.ValueAsString("copyright"); project.Title = rawProject.ValueAsString("title"); project.WebRoot = rawProject.ValueAsString("webroot"); project.EntryPoint = rawProject.ValueAsString("entryPoint"); project.ProjectUrl = rawProject.ValueAsString("projectUrl"); project.LicenseUrl = rawProject.ValueAsString("licenseUrl"); project.IconUrl = rawProject.ValueAsString("iconUrl"); project.Authors = rawProject.ValueAsStringArray("authors") ?? new string[] { }; project.Owners = rawProject.ValueAsStringArray("owners") ?? new string[] { }; project.Tags = rawProject.ValueAsStringArray("tags") ?? new string[] { }; project.Language = rawProject.ValueAsString("language"); project.ReleaseNotes = rawProject.ValueAsString("releaseNotes"); project.RequireLicenseAcceptance = rawProject.ValueAsBoolean("requireLicenseAcceptance", defaultValue: false); project.IsLoadable = rawProject.ValueAsBoolean("loadable", defaultValue: true); // TODO: Move this to the dependencies node project.EmbedInteropTypes = rawProject.ValueAsBoolean("embedInteropTypes", defaultValue: false); project.Dependencies = new List <LibraryDependency>(); // Project files project.Files = new ProjectFilesCollection(rawProject, project.ProjectDirectory, project.ProjectFilePath, diagnostics); var compilerInfo = rawProject.ValueAsJsonObject("compiler"); if (compilerInfo != null) { var languageName = compilerInfo.ValueAsString("name") ?? "C#"; var compilerAssembly = compilerInfo.ValueAsString("compilerAssembly"); var compilerType = compilerInfo.ValueAsString("compilerType"); var compiler = new TypeInformation(compilerAssembly, compilerType); project.CompilerServices = new CompilerServices(languageName, compiler); } var commands = rawProject.Value("commands") as JsonObject; if (commands != null) { foreach (var key in commands.Keys) { var value = commands.ValueAsString(key); if (value != null) { project.Commands[key] = value; } } } var scripts = rawProject.Value("scripts") as JsonObject; if (scripts != null) { foreach (var key in scripts.Keys) { var stringValue = scripts.ValueAsString(key); if (stringValue != null) { project.Scripts[key] = new string[] { stringValue }; continue; } var arrayValue = scripts.ValueAsStringArray(key); if (arrayValue != null) { project.Scripts[key] = arrayValue; continue; } throw FileFormatException.Create( string.Format("The value of a script in {0} can only be a string or an array of strings", ProjectFileName), scripts.Value(key), project.ProjectFilePath); } } var repository = rawProject.Value("repository") as JsonObject; if (repository != null) { project.Repository = repository .Keys .ToDictionary( key => key, key => repository.ValueAsString(key).Value); } project.BuildTargetFrameworksAndConfigurations(rawProject, diagnostics); PopulateDependencies( project.ProjectFilePath, project.Dependencies, rawProject, "dependencies", isGacOrFrameworkReference: false); return(project); }
private void BuildTargetFrameworksAndConfigurations(JObject rawProject) { // Get the shared compilationOptions _defaultCompilerOptions = GetCompilationOptions(rawProject) ?? _emptyOptions; _defaultTargetFrameworkConfiguration = new TargetFrameworkInformation { Dependencies = new List <LibraryDependency>() }; // Add default configurations _configurations["Debug"] = new CompilerOptions { Defines = new[] { "DEBUG", "TRACE" }, Optimize = false }; _configurations["Release"] = new CompilerOptions { Defines = new[] { "RELEASE", "TRACE" }, Optimize = true }; // The configuration node has things like debug/release compiler settings /* * { * "configurations": { * "Debug": { * }, * "Release": { * } * } * } */ var configurations = rawProject["configurations"] as JObject; if (configurations != null) { foreach (var configuration in configurations) { var compilerOptions = GetCompilationOptions(configuration.Value); // Only use this as a configuration if it's not a target framework _configurations[configuration.Key] = compilerOptions; } } // The frameworks node is where target frameworks go /* * { * "frameworks": { * "net45": { * }, * "k10": { * } * } * } */ var frameworks = rawProject["frameworks"] as JObject; if (frameworks != null) { foreach (var framework in frameworks) { try { BuildTargetFrameworkNode(framework); } catch (Exception ex) { throw FileFormatException.Create(ex, framework.Value, ProjectFilePath); } } } }
private static void PopulateDependencies( string projectPath, IList <LibraryDependency> results, JObject settings, string propertyName, bool isGacOrFrameworkReference) { var dependencies = settings[propertyName] as JObject; if (dependencies != null) { foreach (var dependency in dependencies.Properties()) { if (string.IsNullOrEmpty(dependency.Name)) { throw FileFormatException.Create( "Unable to resolve dependency ''.", dependency, projectPath); } // Support // "dependencies" : { // "Name" : "1.0" // } var dependencyValue = dependency.Value; var dependencyTypeValue = LibraryDependencyType.Default; string dependencyVersionValue = null; JToken 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); } } SemanticVersionRange dependencyVersionRange = null; if (!string.IsNullOrEmpty(dependencyVersionValue)) { try { dependencyVersionRange = VersionUtility.ParseVersionRange(dependencyVersionValue); } catch (Exception ex) { throw FileFormatException.Create( ex, dependencyVersionToken, projectPath); } } var dependencyLineInfo = (IJsonLineInfo)dependency; results.Add(new LibraryDependency { LibraryRange = new LibraryRange { Name = dependency.Name, VersionRange = dependencyVersionRange, IsGacOrFrameworkReference = isGacOrFrameworkReference, FileName = projectPath, Line = dependencyLineInfo.LineNumber, Column = dependencyLineInfo.LinePosition }, Type = dependencyTypeValue }); } } }
internal static Project GetProjectFromStream(Stream stream, string projectName, string projectPath, ICollection <ICompilationMessage> diagnostics = null) { var project = new Project(); var reader = new JsonTextReader(new StreamReader(stream)); var rawProject = JObject.Load(reader); // Meta-data properties var version = rawProject["version"]; var authors = rawProject["authors"]; var owners = rawProject["owners"]; var tags = rawProject["tags"]; var buildVersion = Environment.GetEnvironmentVariable("DNX_BUILD_VERSION"); project.Name = projectName; project.ProjectFilePath = Path.GetFullPath(projectPath); if (version == null) { project.Version = new SemanticVersion("1.0.0"); } else { try { project.Version = SpecifySnapshot(version.Value <string>(), buildVersion); } catch (Exception ex) { var lineInfo = (IJsonLineInfo)version; throw FileFormatException.Create(ex, version, project.ProjectFilePath); } } var fileVersion = Environment.GetEnvironmentVariable("DNX_ASSEMBLY_FILE_VERSION"); if (string.IsNullOrWhiteSpace(fileVersion)) { project.AssemblyFileVersion = project.Version.Version; } else { try { var simpleVersion = project.Version.Version; project.AssemblyFileVersion = new Version(simpleVersion.Major, simpleVersion.Minor, simpleVersion.Build, int.Parse(fileVersion)); } catch (FormatException ex) { throw new FormatException("The assembly file version is invalid: " + fileVersion, ex); } } project.Description = rawProject.GetValue <string>("description"); project.Summary = rawProject.GetValue <string>("summary"); project.Copyright = rawProject.GetValue <string>("copyright"); project.Title = rawProject.GetValue <string>("title"); project.Authors = authors == null ? new string[] { } : authors.ValueAsArray <string>(); project.Owners = owners == null ? new string[] { } : owners.ValueAsArray <string>(); project.Dependencies = new List <LibraryDependency>(); project.WebRoot = rawProject.GetValue <string>("webroot"); project.EntryPoint = rawProject.GetValue <string>("entryPoint"); project.ProjectUrl = rawProject.GetValue <string>("projectUrl"); project.LicenseUrl = rawProject.GetValue <string>("licenseUrl"); project.IconUrl = rawProject.GetValue <string>("iconUrl"); project.RequireLicenseAcceptance = rawProject.GetValue <bool?>("requireLicenseAcceptance") ?? false; project.Tags = tags == null ? new string[] { } : tags.ValueAsArray <string>(); project.IsLoadable = rawProject.GetValue <bool?>("loadable") ?? true; // TODO: Move this to the dependencies node project.EmbedInteropTypes = rawProject.GetValue <bool>("embedInteropTypes"); // Project files project.Files = new ProjectFilesCollection(rawProject, project.ProjectDirectory, project.ProjectFilePath, diagnostics); var languageInfo = rawProject["language"] as JObject; if (languageInfo != null) { var languageName = languageInfo.GetValue <string>("name") ?? "C#"; var languageServicesAssembly = languageInfo.GetValue <string>("assembly"); var projectReferenceProviderType = languageInfo.GetValue <string>("projectReferenceProviderType"); var libraryExporter = new TypeInformation(languageServicesAssembly, projectReferenceProviderType); project.LanguageServices = new LanguageServices(languageName, libraryExporter); } var commands = rawProject["commands"] as JObject; if (commands != null) { foreach (var command in commands) { project.Commands[command.Key] = command.Value.Value <string>(); } } var scripts = rawProject["scripts"] as JObject; if (scripts != null) { foreach (var script in scripts) { var value = script.Value; if (value.Type == JTokenType.String) { project.Scripts[script.Key] = new string[] { value.Value <string>() }; } else if (value.Type == JTokenType.Array) { project.Scripts[script.Key] = script.Value.ValueAsArray <string>(); } else { throw FileFormatException.Create( string.Format("The value of a script in {0} can only be a string or an array of strings", ProjectFileName), value, project.ProjectFilePath); } } } project.BuildTargetFrameworksAndConfigurations(rawProject); PopulateDependencies( project.ProjectFilePath, project.Dependencies, rawProject, "dependencies", isGacOrFrameworkReference: false); return(project); }
public static Project GetProject(Stream stream, string projectName, string projectPath) { var project = new Project(); var reader = new JsonTextReader(new StreamReader(stream)); var rawProject = JObject.Load(reader); // Metadata properties var version = rawProject["version"]; var authors = rawProject["authors"]; var tags = rawProject["tags"]; var buildVersion = Environment.GetEnvironmentVariable("K_BUILD_VERSION"); project.Name = projectName; project.ProjectFilePath = Path.GetFullPath(projectPath); if (version == null) { project.Version = new SemanticVersion("1.0.0"); } else { try { project.Version = SpecifySnapshot(version.Value <string>(), buildVersion); } catch (Exception ex) { var lineInfo = (IJsonLineInfo)version; throw FileFormatException.Create(ex, version, project.ProjectFilePath); } } project.Description = GetValue <string>(rawProject, "description"); project.Authors = authors == null ? new string[] { } : authors.ToObject <string[]>(); project.Dependencies = new List <LibraryDependency>(); project.WebRoot = GetValue <string>(rawProject, "webroot"); project.EntryPoint = GetValue <string>(rawProject, "entryPoint"); project.ProjectUrl = GetValue <string>(rawProject, "projectUrl"); project.RequireLicenseAcceptance = GetValue <bool?>(rawProject, "requireLicenseAcceptance") ?? false; project.Tags = tags == null ? new string[] { } : tags.ToObject <string[]>(); project.IsLoadable = GetValue <bool?>(rawProject, "loadable") ?? true; // TODO: Move this to the dependencies node project.EmbedInteropTypes = GetValue <bool>(rawProject, "embedInteropTypes"); // Source file patterns project.SourcePatterns = GetSourcePattern(project, rawProject, "code", _defaultSourcePatterns); project.ExcludePatterns = GetSourcePattern(project, rawProject, "exclude", _defaultExcludePatterns); project.BundleExcludePatterns = GetSourcePattern(project, rawProject, "bundleExclude", _defaultBundleExcludePatterns); project.PreprocessPatterns = GetSourcePattern(project, rawProject, "preprocess", _defaultPreprocessPatterns); project.SharedPatterns = GetSourcePattern(project, rawProject, "shared", _defaultSharedPatterns); project.ResourcesPatterns = GetSourcePattern(project, rawProject, "resources", _defaultResourcesPatterns); project.ContentsPatterns = GetSourcePattern(project, rawProject, "files", _defaultContentsPatterns); // Set the default loader information for projects var languageServicesAssembly = DefaultLanguageServicesAssembly; var projectReferenceProviderType = DefaultProjectReferenceProviderType; var languageName = "C#"; var languageInfo = rawProject["language"] as JObject; if (languageInfo != null) { languageName = GetValue <string>(languageInfo, "name"); languageServicesAssembly = GetValue <string>(languageInfo, "assembly"); projectReferenceProviderType = GetValue <string>(languageInfo, "projectReferenceProviderType"); } var libraryExporter = new TypeInformation(languageServicesAssembly, projectReferenceProviderType); project.LanguageServices = new LanguageServices(languageName, libraryExporter); var commands = rawProject["commands"] as JObject; if (commands != null) { foreach (var command in commands) { project.Commands[command.Key] = command.Value.ToObject <string>(); } } var scripts = rawProject["scripts"] as JObject; if (scripts != null) { foreach (var script in scripts) { var value = script.Value; if (value.Type == JTokenType.String) { project.Scripts[script.Key] = new string[] { value.ToObject <string>() }; } else if (value.Type == JTokenType.Array) { project.Scripts[script.Key] = script.Value.ToObject <string[]>(); } else { throw FileFormatException.Create( string.Format("The value of a script in {0} can only be a string or an array of strings", ProjectFileName), value, project.ProjectFilePath); } } } project.BuildTargetFrameworksAndConfigurations(rawProject); PopulateDependencies( project.ProjectFilePath, project.Dependencies, rawProject, "dependencies", isGacOrFrameworkReference: false); return(project); }
public static bool TryGetGlobalSettings(string path, out GlobalSettings globalSettings) { globalSettings = null; string globalJsonPath = null; if (Path.GetFileName(path) == GlobalFileName) { globalJsonPath = path; path = Path.GetDirectoryName(path); } else if (!HasGlobalFile(path)) { return(false); } else { globalJsonPath = Path.Combine(path, GlobalFileName); } globalSettings = new GlobalSettings(); string json = File.ReadAllText(globalJsonPath); JObject settings = null; try { settings = JObject.Parse(json); } catch (JsonReaderException ex) { throw FileFormatException.Create(ex, globalJsonPath); } // TODO: Remove sources var projectSearchPaths = settings["projects"] ?? settings["sources"]; var dependencies = settings["dependencies"] as JObject; globalSettings.ProjectSearchPaths = projectSearchPaths == null ? new string[] { } : projectSearchPaths.ToObject <string[]>(); globalSettings.PackagesPath = settings.Value <string>("packages"); globalSettings.PackageHashes = new Dictionary <Library, string>(); globalSettings.FilePath = globalJsonPath; if (dependencies != null) { foreach (var property in dependencies.Properties()) { var dependencyValue = dependencies[property.Name] as JObject; if (dependencyValue == null) { throw FileFormatException.Create(string.Format( "The value of '{0}' in {1} must be an object", property.Name, GlobalFileName), property, globalJsonPath); } var versionToken = dependencyValue["version"]; var versionValue = versionToken?.ToString(); SemanticVersion version; if (!SemanticVersion.TryParse(versionValue, out version)) { throw FileFormatException.Create(string.Format( "The dependency '{0}' in {1} doesn't have valid version information", property.Name, GlobalFileName), versionToken, globalJsonPath); } var library = new Library() { Name = property.Name, Version = version }; var shaToken = dependencyValue["sha"]; var shaValue = shaToken?.ToString(); if (string.IsNullOrEmpty(shaValue)) { throw FileFormatException.Create(string.Format( "The dependency '{0}' in {1} doesn't have a valid SHA value", property.Name, GlobalFileName), shaToken, globalJsonPath); } globalSettings.PackageHashes[library] = shaValue; } } return(true); }