Add hacky abstraction for quickly parsing RuntimeConfig.json and determining if it's a portable app
This commit is contained in:
parent
d878331a5e
commit
861b7494d3
5 changed files with 107 additions and 5 deletions
|
@ -152,10 +152,17 @@ namespace Microsoft.DotNet.Cli.Utils
|
||||||
{
|
{
|
||||||
var commandDir = Path.GetDirectoryName(commandPath);
|
var commandDir = Path.GetDirectoryName(commandPath);
|
||||||
|
|
||||||
var runtimeConfig = Directory.EnumerateFiles(commandDir)
|
var runtimeConfigPath = Directory.EnumerateFiles(commandDir)
|
||||||
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
|
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
|
||||||
|
|
||||||
return runtimeConfig != null;
|
if (runtimeConfigPath == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
|
||||||
|
|
||||||
|
return runtimeConfig.IsPortable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
// 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;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.ProjectModel
|
||||||
|
{
|
||||||
|
public class RuntimeConfig
|
||||||
|
{
|
||||||
|
public bool IsPortable { get; }
|
||||||
|
public RuntimeConfigFramework Framework { get; }
|
||||||
|
|
||||||
|
public RuntimeConfig(string runtimeConfigPath)
|
||||||
|
{
|
||||||
|
var runtimeConfigJson = OpenRuntimeConfig(runtimeConfigPath);
|
||||||
|
|
||||||
|
Framework = ParseFramework(runtimeConfigJson);
|
||||||
|
|
||||||
|
IsPortable = Framework != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JObject OpenRuntimeConfig(string runtimeConfigPath)
|
||||||
|
{
|
||||||
|
return JObject.Parse(File.ReadAllText(runtimeConfigPath));
|
||||||
|
}
|
||||||
|
|
||||||
|
private RuntimeConfigFramework ParseFramework(JObject runtimeConfigRoot)
|
||||||
|
{
|
||||||
|
var runtimeOptionsRoot = runtimeConfigRoot["runtimeOptions"];
|
||||||
|
if (runtimeOptionsRoot == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var framework = (JObject) runtimeOptionsRoot["framework"];
|
||||||
|
if (framework == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return RuntimeConfigFramework.ParseFromFrameworkRoot(framework);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,36 @@
|
||||||
|
// 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;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Microsoft.DotNet.ProjectModel
|
||||||
|
{
|
||||||
|
public class RuntimeConfigFramework
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string Version { get; set; }
|
||||||
|
|
||||||
|
public static RuntimeConfigFramework ParseFromFrameworkRoot(JObject framework)
|
||||||
|
{
|
||||||
|
var properties = framework.Properties();
|
||||||
|
|
||||||
|
var name = properties.FirstOrDefault(p => p.Name.Equals("name", StringComparison.OrdinalIgnoreCase));
|
||||||
|
var version = properties.FirstOrDefault(p => p.Name.Equals("version", StringComparison.OrdinalIgnoreCase));
|
||||||
|
|
||||||
|
if (name == null || version == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new RuntimeConfigFramework
|
||||||
|
{
|
||||||
|
Name = name.Value.ToString(),
|
||||||
|
Version = version.Value.ToString()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.DotNet.Cli.Utils;
|
using Microsoft.DotNet.Cli.Utils;
|
||||||
using Microsoft.DotNet.TestFramework;
|
using Microsoft.DotNet.TestFramework;
|
||||||
|
using Microsoft.DotNet.ProjectModel;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Tools.Test.Utilities
|
namespace Microsoft.DotNet.Tools.Test.Utilities
|
||||||
{
|
{
|
||||||
|
@ -111,6 +112,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
|
||||||
|
|
||||||
if (IsPortable(executablePath))
|
if (IsPortable(executablePath))
|
||||||
{
|
{
|
||||||
|
args.Add("exec");
|
||||||
args.Add(ArgumentEscaper.EscapeSingleArg(executablePath));
|
args.Add(ArgumentEscaper.EscapeSingleArg(executablePath));
|
||||||
|
|
||||||
var muxer = new Muxer();
|
var muxer = new Muxer();
|
||||||
|
@ -153,12 +155,19 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
|
||||||
|
|
||||||
private bool IsPortable(string executablePath)
|
private bool IsPortable(string executablePath)
|
||||||
{
|
{
|
||||||
var executableDir = Path.GetDirectoryName(executablePath);
|
var commandDir = Path.GetDirectoryName(executablePath);
|
||||||
|
|
||||||
var runtimeConfig = Directory.EnumerateFiles(executableDir)
|
var runtimeConfigPath = Directory.EnumerateFiles(commandDir)
|
||||||
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
|
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
|
||||||
|
|
||||||
return runtimeConfig != null;
|
if (runtimeConfigPath == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
|
||||||
|
Console.WriteLine(runtimeConfig.Framework);
|
||||||
|
return runtimeConfig.IsPortable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
"dotnet-test-xunit": "1.0.0-dev-91790-12",
|
"dotnet-test-xunit": "1.0.0-dev-91790-12",
|
||||||
"Microsoft.DotNet.TestFramework": "1.0.0-*",
|
"Microsoft.DotNet.TestFramework": "1.0.0-*",
|
||||||
"Microsoft.DotNet.Cli.Utils": "1.0.0-*",
|
"Microsoft.DotNet.Cli.Utils": "1.0.0-*",
|
||||||
|
"Microsoft.DotNet.ProjectModel": "1.0.0-*",
|
||||||
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc2-16537",
|
"Microsoft.Extensions.PlatformAbstractions": "1.0.0-rc2-16537",
|
||||||
"Microsoft.DotNet.InternalAbstractions": {
|
"Microsoft.DotNet.InternalAbstractions": {
|
||||||
"target": "project",
|
"target": "project",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue