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.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Microsoft.Build.Framework;
|
||||
|
@ -9,6 +10,7 @@ using Microsoft.Build.Utilities;
|
|||
using Microsoft.DotNet.ProjectModel;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
using NuGet.ProjectModel;
|
||||
|
||||
namespace Microsoft.DotNet.Cli.Tasks
|
||||
|
@ -50,39 +52,38 @@ namespace Microsoft.DotNet.Cli.Tasks
|
|||
|
||||
private void WriteRuntimeConfig()
|
||||
{
|
||||
var json = new JObject();
|
||||
var runtimeOptions = new JObject();
|
||||
json.Add("runtimeOptions", runtimeOptions);
|
||||
RuntimeConfig config = new RuntimeConfig();
|
||||
config.RuntimeOptions = new RuntimeOptions();
|
||||
|
||||
WriteFramework(runtimeOptions);
|
||||
WriteRuntimeOptions(runtimeOptions);
|
||||
AddFramework(config.RuntimeOptions);
|
||||
AddRuntimeOptions(config.RuntimeOptions);
|
||||
|
||||
var runtimeConfigJsonFile =
|
||||
Path.Combine(RuntimeOutputPath, OutputName + FileNameSuffixes.RuntimeConfigJson);
|
||||
|
||||
using (var writer = new JsonTextWriter(new StreamWriter(File.Create(runtimeConfigJsonFile))))
|
||||
{
|
||||
writer.Formatting = Formatting.Indented;
|
||||
json.WriteTo(writer);
|
||||
}
|
||||
WriteToJsonFile(runtimeConfigJsonFile, config);
|
||||
}
|
||||
|
||||
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.
|
||||
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)
|
||||
{
|
||||
var framework = new JObject(
|
||||
new JProperty("name", redistExport.Name),
|
||||
new JProperty("version", redistExport.Version.ToNormalizedString()));
|
||||
runtimeOptions.Add("framework", framework);
|
||||
RuntimeConfigFramework framework = new RuntimeConfigFramework();
|
||||
framework.Name = redistExport.Name;
|
||||
framework.Version = redistExport.Version.ToNormalizedString();
|
||||
|
||||
runtimeOptions.Framework = framework;
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteRuntimeOptions(JObject runtimeOptions)
|
||||
private void AddRuntimeOptions(RuntimeOptions runtimeOptions)
|
||||
{
|
||||
if (string.IsNullOrEmpty(RawRuntimeOptions))
|
||||
{
|
||||
|
@ -92,37 +93,47 @@ namespace Microsoft.DotNet.Cli.Tasks
|
|||
var runtimeOptionsFromProject = JObject.Parse(RawRuntimeOptions);
|
||||
foreach (var runtimeOption in runtimeOptionsFromProject)
|
||||
{
|
||||
runtimeOptions.Add(runtimeOption.Key, runtimeOption.Value);
|
||||
runtimeOptions.RawOptions.Add(runtimeOption.Key, runtimeOption.Value);
|
||||
}
|
||||
}
|
||||
|
||||
private void WriteDevRuntimeConfig()
|
||||
{
|
||||
var json = new JObject();
|
||||
var runtimeOptions = new JObject();
|
||||
json.Add("runtimeOptions", runtimeOptions);
|
||||
RuntimeConfig devConfig = new RuntimeConfig();
|
||||
devConfig.RuntimeOptions = new RuntimeOptions();
|
||||
|
||||
AddAdditionalProbingPaths(runtimeOptions);
|
||||
AddAdditionalProbingPaths(devConfig.RuntimeOptions);
|
||||
|
||||
var runtimeConfigDevJsonFile =
|
||||
Path.Combine(RuntimeOutputPath, OutputName + FileNameSuffixes.RuntimeConfigDevJson);
|
||||
|
||||
using (var writer = new JsonTextWriter(new StreamWriter(File.Create(runtimeConfigDevJsonFile))))
|
||||
{
|
||||
writer.Formatting = Formatting.Indented;
|
||||
json.WriteTo(writer);
|
||||
}
|
||||
WriteToJsonFile(runtimeConfigDevJsonFile, devConfig);
|
||||
}
|
||||
|
||||
private void AddAdditionalProbingPaths(JObject runtimeOptions)
|
||||
private void AddAdditionalProbingPaths(RuntimeOptions runtimeOptions)
|
||||
{
|
||||
var additionalProbingPaths = new JArray();
|
||||
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