Use object serialization to write the runtime config files.
This commit is contained in:
parent
efc0efe4f9
commit
ba0f71f5a5
4 changed files with 96 additions and 32 deletions
|
@ -2,6 +2,7 @@
|
||||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.Build.Framework;
|
using Microsoft.Build.Framework;
|
||||||
|
@ -9,6 +10,7 @@ using Microsoft.Build.Utilities;
|
||||||
using Microsoft.DotNet.ProjectModel;
|
using Microsoft.DotNet.ProjectModel;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
using NuGet.ProjectModel;
|
using NuGet.ProjectModel;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Cli.Tasks
|
namespace Microsoft.DotNet.Cli.Tasks
|
||||||
|
@ -50,39 +52,38 @@ namespace Microsoft.DotNet.Cli.Tasks
|
||||||
|
|
||||||
private void WriteRuntimeConfig()
|
private void WriteRuntimeConfig()
|
||||||
{
|
{
|
||||||
var json = new JObject();
|
RuntimeConfig config = new RuntimeConfig();
|
||||||
var runtimeOptions = new JObject();
|
config.RuntimeOptions = new RuntimeOptions();
|
||||||
json.Add("runtimeOptions", runtimeOptions);
|
|
||||||
|
|
||||||
WriteFramework(runtimeOptions);
|
AddFramework(config.RuntimeOptions);
|
||||||
WriteRuntimeOptions(runtimeOptions);
|
AddRuntimeOptions(config.RuntimeOptions);
|
||||||
|
|
||||||
var runtimeConfigJsonFile =
|
var runtimeConfigJsonFile =
|
||||||
Path.Combine(RuntimeOutputPath, OutputName + FileNameSuffixes.RuntimeConfigJson);
|
Path.Combine(RuntimeOutputPath, OutputName + FileNameSuffixes.RuntimeConfigJson);
|
||||||
|
|
||||||
using (var writer = new JsonTextWriter(new StreamWriter(File.Create(runtimeConfigJsonFile))))
|
WriteToJsonFile(runtimeConfigJsonFile, config);
|
||||||
{
|
|
||||||
writer.Formatting = Formatting.Indented;
|
|
||||||
json.WriteTo(writer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteFramework(JObject runtimeOptions)
|
private void AddFramework(RuntimeOptions runtimeOptions)
|
||||||
{
|
{
|
||||||
// TODO: get this from the lock file once https://github.com/NuGet/Home/issues/2695 is fixed.
|
// TODO: get this from the lock file once https://github.com/NuGet/Home/issues/2695 is fixed.
|
||||||
var packageName = "Microsoft.NETCore.App";
|
var packageName = "Microsoft.NETCore.App";
|
||||||
|
|
||||||
var redistExport = LockFile.Libraries.FirstOrDefault(e => e.Name.Equals(packageName, StringComparison.OrdinalIgnoreCase));
|
var redistExport = LockFile
|
||||||
|
.Libraries
|
||||||
|
.FirstOrDefault(e => e.Name.Equals(packageName, StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
if (redistExport != null)
|
if (redistExport != null)
|
||||||
{
|
{
|
||||||
var framework = new JObject(
|
RuntimeConfigFramework framework = new RuntimeConfigFramework();
|
||||||
new JProperty("name", redistExport.Name),
|
framework.Name = redistExport.Name;
|
||||||
new JProperty("version", redistExport.Version.ToNormalizedString()));
|
framework.Version = redistExport.Version.ToNormalizedString();
|
||||||
runtimeOptions.Add("framework", framework);
|
|
||||||
|
runtimeOptions.Framework = framework;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteRuntimeOptions(JObject runtimeOptions)
|
private void AddRuntimeOptions(RuntimeOptions runtimeOptions)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(RawRuntimeOptions))
|
if (string.IsNullOrEmpty(RawRuntimeOptions))
|
||||||
{
|
{
|
||||||
|
@ -92,37 +93,47 @@ namespace Microsoft.DotNet.Cli.Tasks
|
||||||
var runtimeOptionsFromProject = JObject.Parse(RawRuntimeOptions);
|
var runtimeOptionsFromProject = JObject.Parse(RawRuntimeOptions);
|
||||||
foreach (var runtimeOption in runtimeOptionsFromProject)
|
foreach (var runtimeOption in runtimeOptionsFromProject)
|
||||||
{
|
{
|
||||||
runtimeOptions.Add(runtimeOption.Key, runtimeOption.Value);
|
runtimeOptions.RawOptions.Add(runtimeOption.Key, runtimeOption.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void WriteDevRuntimeConfig()
|
private void WriteDevRuntimeConfig()
|
||||||
{
|
{
|
||||||
var json = new JObject();
|
RuntimeConfig devConfig = new RuntimeConfig();
|
||||||
var runtimeOptions = new JObject();
|
devConfig.RuntimeOptions = new RuntimeOptions();
|
||||||
json.Add("runtimeOptions", runtimeOptions);
|
|
||||||
|
|
||||||
AddAdditionalProbingPaths(runtimeOptions);
|
AddAdditionalProbingPaths(devConfig.RuntimeOptions);
|
||||||
|
|
||||||
var runtimeConfigDevJsonFile =
|
var runtimeConfigDevJsonFile =
|
||||||
Path.Combine(RuntimeOutputPath, OutputName + FileNameSuffixes.RuntimeConfigDevJson);
|
Path.Combine(RuntimeOutputPath, OutputName + FileNameSuffixes.RuntimeConfigDevJson);
|
||||||
|
|
||||||
using (var writer = new JsonTextWriter(new StreamWriter(File.Create(runtimeConfigDevJsonFile))))
|
WriteToJsonFile(runtimeConfigDevJsonFile, devConfig);
|
||||||
{
|
|
||||||
writer.Formatting = Formatting.Indented;
|
|
||||||
json.WriteTo(writer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddAdditionalProbingPaths(JObject runtimeOptions)
|
private void AddAdditionalProbingPaths(RuntimeOptions runtimeOptions)
|
||||||
{
|
{
|
||||||
var additionalProbingPaths = new JArray();
|
|
||||||
foreach (var packageFolder in LockFile.PackageFolders)
|
foreach (var packageFolder in LockFile.PackageFolders)
|
||||||
{
|
{
|
||||||
additionalProbingPaths.Add(packageFolder.Path);
|
if (runtimeOptions.AdditionalProbingPaths == null)
|
||||||
|
{
|
||||||
|
runtimeOptions.AdditionalProbingPaths = new List<string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
runtimeOptions.Add("additionalProbingPaths", additionalProbingPaths);
|
runtimeOptions.AdditionalProbingPaths.Add(packageFolder.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void WriteToJsonFile(string fileName, object value)
|
||||||
|
{
|
||||||
|
JsonSerializer serializer = new JsonSerializer();
|
||||||
|
serializer.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||||||
|
serializer.Formatting = Formatting.Indented;
|
||||||
|
serializer.DefaultValueHandling = DefaultValueHandling.Ignore;
|
||||||
|
|
||||||
|
using (JsonTextWriter writer = new JsonTextWriter(new StreamWriter(File.Create(fileName))))
|
||||||
|
{
|
||||||
|
serializer.Serialize(writer, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
14
src/Microsoft.DotNet.Core.Build.Tasks/RuntimeConfig.cs
Normal file
14
src/Microsoft.DotNet.Core.Build.Tasks/RuntimeConfig.cs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Cli.Tasks
|
||||||
|
{
|
||||||
|
internal class RuntimeConfig
|
||||||
|
{
|
||||||
|
public RuntimeOptions RuntimeOptions { get; set; }
|
||||||
|
|
||||||
|
public RuntimeConfig()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Cli.Tasks
|
||||||
|
{
|
||||||
|
internal class RuntimeConfigFramework
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string Version { get; set; }
|
||||||
|
|
||||||
|
public RuntimeConfigFramework()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
src/Microsoft.DotNet.Core.Build.Tasks/RuntimeOptions.cs
Normal file
23
src/Microsoft.DotNet.Core.Build.Tasks/RuntimeOptions.cs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
||||||
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.Cli.Tasks
|
||||||
|
{
|
||||||
|
internal class RuntimeOptions
|
||||||
|
{
|
||||||
|
public RuntimeConfigFramework Framework { get; set; }
|
||||||
|
|
||||||
|
public List<string> AdditionalProbingPaths { get; set; }
|
||||||
|
|
||||||
|
[JsonExtensionData]
|
||||||
|
public IDictionary<string, JToken> RawOptions { get; } = new Dictionary<string, JToken>();
|
||||||
|
|
||||||
|
public RuntimeOptions()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue