Example #1
0
        private static JObject WriteLockFile(LockFile lockFile)
        {
            var json = new JObject
            {
                [VersionProperty]   = new JValue(lockFile.Version),
                [TargetsProperty]   = JsonUtility.WriteObject(lockFile.Targets, WriteTarget),
                [LibrariesProperty] = JsonUtility.WriteObject(lockFile.Libraries, WriteLibrary),
                [ProjectFileDependencyGroupsProperty] = JsonUtility.WriteObject(lockFile.ProjectFileDependencyGroups, WriteProjectFileDependencyGroup)
            };

            if (lockFile.PackageFolders?.Any() == true)
            {
                json[PackageFoldersProperty] = JsonUtility.WriteObject(lockFile.PackageFolders, WriteFileItem);
            }

            if (lockFile.Version >= 2)
            {
                if (lockFile.PackageSpec != null)
                {
                    using (var jsonWriter = new JTokenWriter())
                        using (var writer = new JsonObjectWriter(jsonWriter))
                        {
                            writer.WriteObjectStart();

                            PackageSpecWriter.Write(lockFile.PackageSpec, writer);

                            writer.WriteObjectEnd();

                            json[PackageSpecProperty] = (JObject)jsonWriter.Token;
                        }
                }
            }

            if (lockFile.Version >= 3)
            {
                if (lockFile.LogMessages.Count > 0)
                {
                    var projectPath = lockFile.PackageSpec?.RestoreMetadata?.ProjectPath;
                    json[LogsProperty] = WriteLogMessages(lockFile.LogMessages, projectPath);
                }
            }

            if (lockFile.CentralTransitiveDependencyGroups.Any())
            {
                JToken token = WriteCentralTransitiveDependencyGroup(lockFile.CentralTransitiveDependencyGroups);
                json[CentralTransitiveDependencyGroupsProperty] = (JObject)token;
            }

            return(json);
        }
Example #2
0
        /// <summary>
        /// Clone a PackageSpec and underlying JObject.
        /// </summary>
        public PackageSpec Clone()
        {
            var writer = new JsonObjectWriter();

            PackageSpecWriter.Write(this, writer);
            var json = writer.GetJObject();

            var spec = JsonPackageSpecReader.GetPackageSpec(json);

            spec.Name     = Name;
            spec.FilePath = FilePath;

            return(spec);
        }
Example #3
0
        internal static PackageSpec RoundTrip(this PackageSpec spec, string packageSpecName, string packageSpecPath)
        {
            using (var stringWriter = new StringWriter())
                using (var jsonWriter = new JsonTextWriter(stringWriter))
                    using (var writer = new JsonObjectWriter(jsonWriter))
                    {
                        writer.WriteObjectStart();

                        PackageSpecWriter.Write(spec, writer);

                        writer.WriteObjectEnd();

                        return(JsonPackageSpecReader.GetPackageSpec(stringWriter.ToString(), packageSpecName, packageSpecPath));
                    }
        }
        public static void SaveRuntimeGraph(string filePath, RuntimeGraph runtimeGraph)
        {
            var jsonObjectWriter  = new JsonObjectWriter();
            var runtimeJsonWriter = new RuntimeJsonWriter(jsonObjectWriter);

            JsonRuntimeFormat.WriteRuntimeGraph(runtimeJsonWriter, runtimeGraph);

            using (var file = File.CreateText(filePath))
                using (var jsonWriter = new JsonTextWriter(file))
                {
                    jsonWriter.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii;
                    jsonWriter.Formatting           = Formatting.Indented;
                    jsonObjectWriter.WriteTo(jsonWriter);
                }
        }
Example #5
0
        private static JToken WriteCentralTransitiveDependencyGroup(IList <CentralTransitiveDependencyGroup> centralTransitiveDependencyGroups)
        {
            using (var jsonWriter = new JTokenWriter())
                using (var writer = new JsonObjectWriter(jsonWriter))
                {
                    writer.WriteObjectStart();

                    foreach (var centralTransitiveDepGroup in centralTransitiveDependencyGroups.OrderBy(ptdg => ptdg.FrameworkName))
                    {
                        PackageSpecWriter.SetCentralTransitveDependencyGroup(writer, centralTransitiveDepGroup.FrameworkName, centralTransitiveDepGroup.TransitiveDependencies);
                    }

                    writer.WriteObjectEnd();
                    return(jsonWriter.Token);
                }
        }
        private static void VerifyJsonPackageSpecRoundTrip(string json)
        {
            // Arrange & Act
            var spec = JsonPackageSpecReader.GetPackageSpec(json, "testName", @"C:\fake\path");

            var writer = new JsonObjectWriter();

            PackageSpecWriter.Write(spec, writer);

            var actualResult = writer.GetJson();

            var expected = JObject.Parse(json).ToString();

            // Assert
            Assert.Equal(expected, actualResult);
        }
Example #7
0
        public static PackageSpec RoundTrip(this PackageSpec spec)
        {
            using (var jsonWriter = new JTokenWriter())
                using (var writer = new JsonObjectWriter(jsonWriter))
                {
                    writer.WriteObjectStart();

                    PackageSpecWriter.Write(spec, writer);

                    writer.WriteObjectEnd();

#pragma warning disable CS0618
                    return(JsonPackageSpecReader.GetPackageSpec((JObject)jsonWriter.Token));

#pragma warning restore CS0618
                }
        }
Example #8
0
        public static void WriteRuntimeGraph(string filePath, RuntimeGraph runtimeGraph)
        {
            using (var fileStream = new FileStream(filePath, FileMode.Create))
                using (var textWriter = new StreamWriter(fileStream))
                    using (var jsonWriter = new JsonTextWriter(textWriter))
                        using (var writer = new JsonObjectWriter(jsonWriter))
                        {
                            jsonWriter.Formatting = Formatting.Indented;

                            // workaround https://github.com/NuGet/Home/issues/9532
                            writer.WriteObjectStart();

                            JsonRuntimeFormat.WriteRuntimeGraph(writer, runtimeGraph);

                            writer.WriteObjectEnd();
                        }
        }
        /// <summary>
        /// Writes a PackageSpec to a file.
        /// </summary>
        /// <param name="packageSpec">A <c>PackageSpec</c> instance.</param>
        /// <param name="filePath">A file path to write to.</param>
        public static void WriteToFile(PackageSpec packageSpec, string filePath)
        {
            if (packageSpec == null)
            {
                throw new ArgumentNullException(nameof(packageSpec));
            }

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException(Strings.ArgumentNullOrEmpty, nameof(filePath));
            }

            using (var fileStream = new FileStream(filePath, FileMode.Create))
                using (var textWriter = new StreamWriter(fileStream))
                    using (var jsonWriter = new JsonTextWriter(textWriter))
                        using (var writer = new JsonObjectWriter(jsonWriter))
                        {
                            jsonWriter.Formatting = Formatting.Indented;

                            Write(packageSpec, writer);
                        }
        }
Example #10
0
        private static JObject WriteLockFile(LockFile lockFile)
        {
            var json = new JObject
            {
                [VersionProperty]   = new JValue(lockFile.Version),
                [TargetsProperty]   = WriteObject(lockFile.Targets, WriteTarget),
                [LibrariesProperty] = WriteObject(lockFile.Libraries, WriteLibrary),
                [ProjectFileDependencyGroupsProperty] = WriteObject(lockFile.ProjectFileDependencyGroups, WriteProjectFileDependencyGroup)
            };

            if (lockFile.PackageFolders?.Any() == true)
            {
                json[PackageFoldersProperty] = WriteObject(lockFile.PackageFolders, WriteFileItem);
            }

            if (lockFile.Version >= 2)
            {
                if (lockFile.PackageSpec != null)
                {
                    var writer = new JsonObjectWriter();
                    PackageSpecWriter.Write(lockFile.PackageSpec, writer);
                    var packageSpec = writer.GetJObject();
                    json[PackageSpecProperty] = packageSpec;
                }
            }

            if (lockFile.Version >= 3)
            {
                if (lockFile.LogMessages.Count > 0)
                {
                    var projectPath = lockFile.PackageSpec?.RestoreMetadata?.ProjectPath;
                    json[LogsProperty] = WriteLogMessages(lockFile.LogMessages, projectPath);
                }
            }

            return(json);
        }
Example #11
0
 public void FromJson(Type type, JObject jObject)
 {
     JsonObjectWriter.WriteListObject(type, jObject, this);
 }
 public JsonObjectWriterTests()
 {
     _stringWriter = new StringWriter();
     _jsonWriter   = new JsonTextWriter(_stringWriter);
     _writer       = new JsonObjectWriter(_jsonWriter);
 }
Example #13
0
 public JsonObjectWriterTests()
 {
     _writer = new JsonObjectWriter();
 }
Example #14
0
        public void Write_ThrowsForNullPackageSpec()
        {
            var writer = new JsonObjectWriter();

            Assert.Throws <ArgumentNullException>(() => PackageSpecWriter.Write(packageSpec: null, writer: writer));
        }