Adapt to no config file Apphost shim

Instead of writing the config file. Embed the relative path, instead of only the file name of app binary, to the AppHost itself.
This commit is contained in:
William Li 2018-04-02 18:29:57 -07:00
parent 900e11cbee
commit cbd6434c8b
3 changed files with 45 additions and 25 deletions

View file

@ -13,14 +13,21 @@ namespace Microsoft.DotNet.Cli.Utils
{
private static string _placeHolder = "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"; //hash value embedded in default apphost executable
private static byte[] _bytesToSearch = Encoding.UTF8.GetBytes(_placeHolder);
/// <summary>
/// Create an AppHost with embedded configuration of app binary location
/// </summary>
/// <param name="appHostSourceFilePath">The path of AppHost template, which has the place holder</param>
/// <param name="appHostDestinationFilePath">The destination path for desired location to place, including the file name</param>
/// <param name="appBinaryFilePath">Full path to app binary or relative path to appHostDestinationFilePath</param>
public static void EmbedAndReturnModifiedAppHostPath(
string appHostSourceFilePath,
string appHostDestinationFilePath,
string appBinaryName)
string appBinaryFilePath)
{
var hostExtension = Path.GetExtension(appHostSourceFilePath);
var appbaseName = Path.GetFileNameWithoutExtension(appBinaryName);
var bytesToWrite = Encoding.UTF8.GetBytes(appBinaryName);
var appbaseName = Path.GetFileNameWithoutExtension(appBinaryFilePath);
var bytesToWrite = Encoding.UTF8.GetBytes(appBinaryFilePath);
var destinationDirectory = new FileInfo(appHostDestinationFilePath).Directory.FullName;
if (File.Exists(appHostDestinationFilePath))
@ -31,7 +38,7 @@ namespace Microsoft.DotNet.Cli.Utils
if (bytesToWrite.Length > 1024)
{
throw new EmbedAppNameInHostException(string.Format(LocalizableStrings.EmbedAppNameInHostFileNameIsTooLong, appBinaryName));
throw new EmbedAppNameInHostException(string.Format(LocalizableStrings.EmbedAppNameInHostFileNameIsTooLong, appBinaryFilePath));
}
var array = File.ReadAllBytes(appHostSourceFilePath);

View file

@ -10,6 +10,7 @@ using Microsoft.DotNet.Cli;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.PlatformAbstractions;
using Microsoft.DotNet.Tools;
using Microsoft.DotNet.Tools.Common;
using Microsoft.Extensions.EnvironmentAbstractions;
using Newtonsoft.Json;
@ -58,7 +59,7 @@ namespace Microsoft.DotNet.ShellShim
Directory.CreateDirectory(_shimsDirectory.Value);
}
CreateApphostShimAndConfigFile(
CreateApphostShim(
commandName,
entryPoint: targetExecutablePath);
@ -125,7 +126,7 @@ namespace Microsoft.DotNet.ShellShim
});
}
private void CreateApphostShimAndConfigFile(string commandName, FilePath entryPoint)
private void CreateApphostShim(string commandName, FilePath entryPoint)
{
string appHostSourcePath;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@ -137,21 +138,13 @@ namespace Microsoft.DotNet.ShellShim
appHostSourcePath = Path.Combine(_appHostSourceDirectory, ApphostNameWithoutExtension);
}
var appHostDestinationFilePath = GetShimPath(commandName).Value;
var appBinaryFilePath = PathUtility.GetRelativePath(appHostDestinationFilePath, entryPoint.Value);
EmbedAppNameInHost.EmbedAndReturnModifiedAppHostPath(
appHostSourceFilePath: appHostSourcePath,
appHostDestinationFilePath: GetShimPath(commandName).Value,
appBinaryName: Path.GetFileName(entryPoint.Value));
var config = JsonConvert.SerializeObject(
new RootObject
{
startupOptions = new StartupOptions
{
appRoot = entryPoint.GetDirectoryPath().Value
}
});
File.WriteAllText(GetConfigPath(commandName).Value, config);
appHostDestinationFilePath: appHostDestinationFilePath,
appBinaryFilePath: appBinaryFilePath);
}
private class StartupOptions
@ -177,7 +170,6 @@ namespace Microsoft.DotNet.ShellShim
}
yield return GetShimPath(commandName);
yield return GetConfigPath(commandName);
}
private FilePath GetShimPath(string commandName)
@ -192,11 +184,6 @@ namespace Microsoft.DotNet.ShellShim
}
}
private FilePath GetConfigPath(string commandName)
{
return _shimsDirectory.WithFile(commandName + ".startupconfig.json");
}
private static void SetUserExecutionPermission(FilePath path)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))

View file

@ -21,6 +21,32 @@ namespace Microsoft.DotNet.Cli.Utils
Assert.Equal(@"d:\foo\", PathUtility.GetRelativePath(@"C:\foo\", @"d:\foo\"));
}
[WindowsOnlyFact]
public void GetRelativePathForFilePath()
{
Assert.Equal(
@"mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll",
PathUtility.GetRelativePath(
@"C:\Users\myuser\.dotnet\tools\mytool.exe",
@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll"));
}
[WindowsOnlyFact]
public void GetRelativePathRequireTrailingSlashForDirectoryPath()
{
Assert.NotEqual(
@"mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll",
PathUtility.GetRelativePath(
@"C:\Users\myuser\.dotnet\tools",
@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll"));
Assert.Equal(
@"mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll",
PathUtility.GetRelativePath(
@"C:\Users\myuser\.dotnet\tools\",
@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll"));
}
/// <summary>
/// Tests that PathUtility.RemoveExtraPathSeparators works correctly with drive references on Windows.
/// </summary>