Add hacky abstraction for quickly parsing RuntimeConfig.json and determining if it's a portable app

This commit is contained in:
Bryan Thornbury 2016-03-17 16:39:48 -07:00
parent d878331a5e
commit 861b7494d3
5 changed files with 107 additions and 5 deletions

View file

@ -152,10 +152,17 @@ namespace Microsoft.DotNet.Cli.Utils
{
var commandDir = Path.GetDirectoryName(commandPath);
var runtimeConfig = Directory.EnumerateFiles(commandDir)
var runtimeConfigPath = Directory.EnumerateFiles(commandDir)
.FirstOrDefault(x => x.EndsWith("runtimeconfig.json"));
return runtimeConfig != null;
if (runtimeConfigPath == null)
{
return false;
}
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
return runtimeConfig.IsPortable;
}
}
}

View file

@ -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);
}
}
}

View file

@ -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()
};
}
}
}

View file

@ -8,6 +8,7 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.ProjectModel;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
@ -111,6 +112,7 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
if (IsPortable(executablePath))
{
args.Add("exec");
args.Add(ArgumentEscaper.EscapeSingleArg(executablePath));
var muxer = new Muxer();
@ -153,12 +155,19 @@ namespace Microsoft.DotNet.Tools.Test.Utilities
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"));
return runtimeConfig != null;
if (runtimeConfigPath == null)
{
return false;
}
var runtimeConfig = new RuntimeConfig(runtimeConfigPath);
Console.WriteLine(runtimeConfig.Framework);
return runtimeConfig.IsPortable;
}
}
}

View file

@ -12,6 +12,7 @@
"dotnet-test-xunit": "1.0.0-dev-91790-12",
"Microsoft.DotNet.TestFramework": "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.DotNet.InternalAbstractions": {
"target": "project",