public static ExportFile ReadExportFile(string fragmentLockFilePath) { using (var stream = ResilientFileStreamOpener.OpenFile(fragmentLockFilePath)) { try { var rootJObject = JsonDeserializer.Deserialize(new StreamReader(stream)) as JsonObject; if (rootJObject == null) { throw new InvalidDataException(); } var version = ReadInt(rootJObject, "version", defaultValue: int.MinValue); var exports = ReadObject(rootJObject.ValueAsJsonObject("exports"), ReadTargetLibrary); return(new ExportFile(fragmentLockFilePath, version, exports)); } catch (FileFormatException ex) { throw ex.WithFilePath(fragmentLockFilePath); } catch (Exception ex) { throw FileFormatException.Create(ex, fragmentLockFilePath); } } }
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); }
private LockFileTargetLibrary ReadTargetLibrary(string property, JsonValue json) { var jobject = json as JsonObject; if (jobject == null) { throw FileFormatException.Create("The value type is not an object.", json); } var library = new LockFileTargetLibrary(); var parts = property.Split(new[] { '/' }, 2); library.Name = parts[0]; if (parts.Length == 2) { library.Version = SemanticVersion.Parse(parts[1]); } library.Dependencies = ReadObject(jobject.ValueAsJsonObject("dependencies"), ReadPackageDependency); library.FrameworkAssemblies = new HashSet <string>(ReadArray(jobject.Value("frameworkAssemblies"), ReadFrameworkAssemblyReference), StringComparer.OrdinalIgnoreCase); library.RuntimeAssemblies = ReadObject(jobject.ValueAsJsonObject("runtime"), ReadFileItem); library.CompileTimeAssemblies = ReadObject(jobject.ValueAsJsonObject("compile"), ReadFileItem); library.ResourceAssemblies = ReadObject(jobject.ValueAsJsonObject("resource"), ReadFileItem); library.NativeLibraries = ReadObject(jobject.ValueAsJsonObject("native"), ReadFileItem); return(library); }
public static IEnumerable <string> GetPatternsCollection(JsonObject rawProject, string projectDirectory, string projectFilePath, string propertyName, IEnumerable <string> defaultPatterns = null, bool literalPath = false) { defaultPatterns = defaultPatterns ?? Enumerable.Empty <string>(); try { if (!rawProject.Keys.Contains(propertyName)) { return(CreateCollection(projectDirectory, propertyName, defaultPatterns, literalPath)); } var valueInString = rawProject.ValueAsString(propertyName); if (valueInString != null) { return(CreateCollection(projectDirectory, propertyName, new string[] { valueInString }, literalPath)); } var valuesInArray = rawProject.ValueAsStringArray(propertyName); if (valuesInArray != null) { return(CreateCollection(projectDirectory, propertyName, valuesInArray.Select(s => s.ToString()), literalPath)); } } 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 void Create_WithMessageLineColumnAndPathParameters_SetsProperties() { FileFormatException exception = FileFormatException.Create(Message, Line, Column, Path); Assert.Equal(Message, exception.Message); Assert.Equal(Path, exception.Path); Assert.Equal(Line, exception.Line); Assert.Equal(Column, exception.Column); Assert.Null(exception.InnerException); }
public void Create_WithExceptionLineColumnAndPathParameters_SetsProperties() { FileFormatException exception = FileFormatException.Create(InnerException, Line, Column, Path); Assert.Equal($"Error reading '{Path}' at line {Line} column {Column} : {InnerException.Message}", exception.Message); Assert.Equal(Path, exception.Path); Assert.Equal(Line, exception.Line); Assert.Equal(Column, exception.Column); Assert.Same(InnerException, exception.InnerException); }
private void ReadLibrary(JObject json, LockFile lockFile) { if (json == null) { return; } foreach (var child in json) { var key = child.Key; var value = json.Value <JObject>(key); if (value == null) { throw FileFormatException.Create("The value type is not object.", json[key]); } var parts = key.Split(new[] { '/' }, 2); var name = parts[0]; var version = parts.Length == 2 ? _symbols.GetVersion(parts[1]) : null; var type = _symbols.GetString(value.Value <string>("type")); var pathValue = value["path"]; var path = pathValue == null ? null : ReadString(pathValue); if (type == null || string.Equals(type, "package", StringComparison.OrdinalIgnoreCase)) { lockFile.PackageLibraries.Add(new LockFilePackageLibrary { Name = name, Version = version, IsServiceable = ReadBool(value, "serviceable", defaultValue: false), Sha512 = ReadString(value["sha512"]), Files = ReadPathArray(value["files"], ReadString), Path = path }); } else if (type == "project") { var projectLibrary = new LockFileProjectLibrary { Name = name, Version = version }; projectLibrary.Path = path; var buildTimeDependencyValue = value["msbuildProject"]; projectLibrary.MSBuildProject = buildTimeDependencyValue == null ? null : ReadString(buildTimeDependencyValue); lockFile.ProjectLibraries.Add(projectLibrary); } } }
private static void ReadLibrary(JsonObject json, LockFile lockFile) { if (json == null) { return; } foreach (var key in json.Keys) { var value = json.ValueAsJsonObject(key); if (value == null) { throw FileFormatException.Create("The value type is not object.", json.Value(key)); } var parts = key.Split(new[] { '/' }, 2); var name = parts[0]; var version = parts.Length == 2 ? NuGetVersion.Parse(parts[1]) : null; var type = value.ValueAsString("type")?.Value; if (type == null || string.Equals(type, "package", StringComparison.OrdinalIgnoreCase)) { lockFile.PackageLibraries.Add(new LockFilePackageLibrary { Name = name, Version = version, IsServiceable = ReadBool(value, "serviceable", defaultValue: false), Sha512 = ReadString(value.Value("sha512")), Files = ReadPathArray(value.Value("files"), ReadString) }); } else if (type == "project") { var projectLibrary = new LockFileProjectLibrary { Name = name, Version = version }; var pathValue = value.Value("path"); projectLibrary.Path = pathValue == null ? null : ReadString(pathValue); var buildTimeDependencyValue = value.Value("msbuildProject"); projectLibrary.MSBuildProject = buildTimeDependencyValue == null ? null : ReadString(buildTimeDependencyValue); lockFile.ProjectLibraries.Add(projectLibrary); } } }
private LockFilePackageFolder ReadPackageFolder(string property, JToken json) { var jobject = json as JObject; if (jobject == null) { throw FileFormatException.Create("The value type is not an object.", json); } var packageFolder = new LockFilePackageFolder(); packageFolder.Path = property; return(packageFolder); }
private string ReadString(JToken json) { if (json.Type == JTokenType.String) { return(_symbols.GetString(json.ToString())); } else if (json.Type == JTokenType.Null) { return(null); } else { throw FileFormatException.Create("The value type is not string.", json); } }
private static LockFileRuntimeTarget ReadRuntimeTarget(string property, JsonValue json) { var jsonObject = json as JsonObject; if (jsonObject == null) { throw FileFormatException.Create("The value type is not an object.", json); } return(new LockFileRuntimeTarget( path: property, runtime: jsonObject.ValueAsString("rid"), assetType: jsonObject.ValueAsString("assetType") )); }
private static string ReadString(JsonValue json) { if (json is JsonString) { return((json as JsonString).Value); } else if (json is JsonNull) { return(null); } else { throw FileFormatException.Create("The value type is not string.", json); } }
private LockFileRuntimeTarget ReadRuntimeTarget(string property, JToken json) { var jsonObject = json as JObject; if (jsonObject == null) { throw FileFormatException.Create("The value type is not an object.", json); } return(new LockFileRuntimeTarget( path: _symbols.GetString(property), runtime: _symbols.GetString(jsonObject.Value <string>("rid")), assetType: _symbols.GetString(jsonObject.Value <string>("assetType")) )); }
public static IDictionary <string, string> ReadNamedResources(JObject rawProject, string projectFilePath) { JToken namedResourceToken; if (!rawProject.TryGetValue("namedResource", out namedResourceToken)) { return(new Dictionary <string, string>()); } if (namedResourceToken.Type != JTokenType.Object) { throw FileFormatException.Create( "Value must be object.", rawProject.Value <JToken>("namedResource"), projectFilePath); } var namedResources = new Dictionary <string, string>(); foreach (var namedResource in (JObject)namedResourceToken) { if (namedResource.Value.Type != JTokenType.String) { throw FileFormatException.Create("Value must be string.", namedResource.Key, projectFilePath); } var resourcePath = namedResource.Value.ToString(); if (resourcePath.Contains("*")) { throw FileFormatException.Create("Value cannot contain wildcards.", resourcePath, projectFilePath); } var resourceFileFullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(projectFilePath), resourcePath)); if (namedResources.ContainsKey(namedResource.Key)) { throw FileFormatException.Create( string.Format("The named resource {0} already exists.", namedResource.Key), resourcePath, projectFilePath); } namedResources.Add(namedResource.Key, resourceFileFullPath); } return(namedResources); }
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 LockFile Read(string lockFilePath) { using (var stream = new FileStream(lockFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { try { return(Read(lockFilePath, stream)); } catch (FileFormatException ex) { throw ex.WithFilePath(lockFilePath); } catch (Exception ex) { throw FileFormatException.Create(ex, lockFilePath); } } }
public LockFile Read(string filePath) { using (var stream = OpenFileStream(filePath)) { try { return(Read(stream)); } catch (FileFormatException ex) { throw ex.WithFilePath(filePath); } catch (Exception ex) { throw FileFormatException.Create(ex, filePath); } } }
public static LockFile Read(string lockFilePath, bool designTime) { using (var stream = ResilientFileStreamOpener.OpenFile(lockFilePath)) { try { return(new LockFileReader().ReadLockFile(lockFilePath, stream, designTime)); } catch (FileFormatException ex) { throw ex.WithFilePath(lockFilePath); } catch (Exception ex) { throw FileFormatException.Create(ex, lockFilePath); } } }
public static LockFile Read(string lockFilePath, bool patchWithExportFile = true) { using (var stream = ResilientFileStreamOpener.OpenFile(lockFilePath)) { try { return(Read(lockFilePath, stream, patchWithExportFile)); } catch (FileFormatException ex) { throw ex.WithFilePath(lockFilePath); } catch (Exception ex) { throw FileFormatException.Create(ex, lockFilePath); } } }
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 { var json = File.ReadAllText(globalJsonPath); JObject settings = JObject.Parse(json); var projects = settings["projects"]; var dependencies = settings["dependencies"] as JObject; globalSettings.ProjectPaths = projects == null ? new string[] { } : projects.Select(a => a.Value <string>()).ToArray(); globalSettings.PackagesPath = settings.Value <string>("packages"); globalSettings.FilePath = globalJsonPath; } catch (Exception ex) { throw FileFormatException.Create(ex, globalJsonPath); } return(true); }
private static int ReadInt(JObject cursor, string property, int defaultValue) { var number = cursor[property] as JValue; if (number == null || number.Type != JTokenType.Integer) { return(defaultValue); } try { var resultInInt = Convert.ToInt32(number.Value); return(resultInInt); } catch (Exception ex) { // FormatException or OverflowException throw FileFormatException.Create(ex, cursor); } }
private static int ReadInt(JsonObject cursor, string property, int defaultValue) { var number = cursor.Value(property) as JsonNumber; if (number == null) { return(defaultValue); } try { var resultInInt = Convert.ToInt32(number.Raw); return(resultInInt); } catch (Exception ex) { // FormatException or OverflowException throw FileFormatException.Create(ex, cursor); } }
private LockFileLibrary ReadLibrary(string property, JsonValue json) { var jobject = json as JsonObject; if (jobject == null) { throw FileFormatException.Create("The value type is not object.", json); } var library = new LockFileLibrary(); var parts = property.Split(new[] { '/' }, 2); library.Name = parts[0]; if (parts.Length == 2) { library.Version = SemanticVersion.Parse(parts[1]); } library.IsServiceable = ReadBool(jobject, "serviceable", defaultValue: false); library.Sha512 = ReadString(jobject.Value("sha512")); library.Files = ReadPathArray(jobject.Value("files"), ReadString); return(library); }
private static IList <TItem> ReadArray <TItem>(JsonValue json, Func <JsonValue, TItem> readItem) { if (json == null) { return(new List <TItem>()); } var jarray = json as JsonArray; if (jarray == null) { throw FileFormatException.Create("The value type is not array.", json); } var items = new List <TItem>(); for (int i = 0; i < jarray.Length; ++i) { items.Add(readItem(jarray[i])); } return(items); }
private static LockFileTarget ReadTarget(string property, JsonValue json) { var jobject = json as JsonObject; if (jobject == null) { throw FileFormatException.Create("The value type is not an object.", json); } var target = new LockFileTarget(); var parts = property.Split(new[] { '/' }, 2); target.TargetFramework = NuGetFramework.Parse(parts[0]); if (parts.Length == 2) { target.RuntimeIdentifier = parts[1]; } target.Libraries = ReadObject(jobject, ReadTargetLibrary); return(target); }
public static IEnumerable <string> GetPatternsCollection(JObject rawProject, string projectDirectory, string projectFilePath, string propertyName, IEnumerable <string> defaultPatterns = null, bool literalPath = false) { defaultPatterns = defaultPatterns ?? Enumerable.Empty <string>(); try { JToken propertyNameToken; if (!rawProject.TryGetValue(propertyName, out propertyNameToken)) { return(IncludeContext.CreateCollection( projectDirectory, propertyName, defaultPatterns, literalPath)); } if (propertyNameToken.Type == JTokenType.String) { return(IncludeContext.CreateCollection( projectDirectory, propertyName, new string[] { propertyNameToken.Value <string>() }, literalPath)); } if (propertyNameToken.Type == JTokenType.Array) { var valuesInArray = propertyNameToken.Values <string>(); return(IncludeContext.CreateCollection( projectDirectory, propertyName, valuesInArray.Select(s => s.ToString()), literalPath)); } } catch (Exception ex) { throw FileFormatException.Create(ex, rawProject.Value <JToken>(propertyName), projectFilePath); } throw FileFormatException.Create("Value must be either string or array.", rawProject.Value <JToken>(propertyName), projectFilePath); }
private LockFileTargetLibrary ReadTargetLibrary(string property, JToken json) { var jobject = json as JObject; if (jobject == null) { throw FileFormatException.Create("The value type is not an object.", json); } var library = new LockFileTargetLibrary(); var parts = property.Split(new[] { '/' }, 2); library.Name = _symbols.GetString(parts[0]); if (parts.Length == 2) { library.Version = _symbols.GetVersion(parts[1]); } library.Type = _symbols.GetString(jobject.Value <string>("type")); var framework = jobject.Value <string>("framework"); if (framework != null) { library.TargetFramework = _symbols.GetFramework(framework); } library.Dependencies = ReadObject(jobject.Value <JObject>("dependencies"), ReadPackageDependency); library.FrameworkAssemblies = new HashSet <string>(ReadArray(jobject["frameworkAssemblies"], ReadFrameworkAssemblyReference), StringComparer.OrdinalIgnoreCase); library.RuntimeAssemblies = ReadObject(jobject.Value <JObject>("runtime"), ReadFileItem); library.CompileTimeAssemblies = ReadObject(jobject.Value <JObject>("compile"), ReadFileItem); library.ResourceAssemblies = ReadObject(jobject.Value <JObject>("resource"), ReadFileItem); library.NativeLibraries = ReadObject(jobject.Value <JObject>("native"), ReadFileItem); library.ContentFiles = ReadObject(jobject.Value <JObject>("contentFiles"), ReadContentFile); library.RuntimeTargets = ReadObject(jobject.Value <JObject>("runtimeTargets"), ReadRuntimeTarget); return(library); }