Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit cbd6434

Browse files
author
William Li
committed
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.
1 parent 900e11c commit cbd6434

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

src/Microsoft.DotNet.Cli.Utils/EmbedAppNameInHost.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@ public static class EmbedAppNameInHost
1313
{
1414
private static string _placeHolder = "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2"; //hash value embedded in default apphost executable
1515
private static byte[] _bytesToSearch = Encoding.UTF8.GetBytes(_placeHolder);
16+
17+
/// <summary>
18+
/// Create an AppHost with embedded configuration of app binary location
19+
/// </summary>
20+
/// <param name="appHostSourceFilePath">The path of AppHost template, which has the place holder</param>
21+
/// <param name="appHostDestinationFilePath">The destination path for desired location to place, including the file name</param>
22+
/// <param name="appBinaryFilePath">Full path to app binary or relative path to appHostDestinationFilePath</param>
1623
public static void EmbedAndReturnModifiedAppHostPath(
1724
string appHostSourceFilePath,
1825
string appHostDestinationFilePath,
19-
string appBinaryName)
26+
string appBinaryFilePath)
2027
{
2128
var hostExtension = Path.GetExtension(appHostSourceFilePath);
22-
var appbaseName = Path.GetFileNameWithoutExtension(appBinaryName);
23-
var bytesToWrite = Encoding.UTF8.GetBytes(appBinaryName);
29+
var appbaseName = Path.GetFileNameWithoutExtension(appBinaryFilePath);
30+
var bytesToWrite = Encoding.UTF8.GetBytes(appBinaryFilePath);
2431
var destinationDirectory = new FileInfo(appHostDestinationFilePath).Directory.FullName;
2532

2633
if (File.Exists(appHostDestinationFilePath))
@@ -31,7 +38,7 @@ public static void EmbedAndReturnModifiedAppHostPath(
3138

3239
if (bytesToWrite.Length > 1024)
3340
{
34-
throw new EmbedAppNameInHostException(string.Format(LocalizableStrings.EmbedAppNameInHostFileNameIsTooLong, appBinaryName));
41+
throw new EmbedAppNameInHostException(string.Format(LocalizableStrings.EmbedAppNameInHostFileNameIsTooLong, appBinaryFilePath));
3542
}
3643

3744
var array = File.ReadAllBytes(appHostSourceFilePath);

src/dotnet/ShellShim/ShellShimRepository.cs

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Microsoft.DotNet.Cli.Utils;
1111
using Microsoft.DotNet.PlatformAbstractions;
1212
using Microsoft.DotNet.Tools;
13+
using Microsoft.DotNet.Tools.Common;
1314
using Microsoft.Extensions.EnvironmentAbstractions;
1415
using Newtonsoft.Json;
1516

@@ -58,7 +59,7 @@ public void CreateShim(FilePath targetExecutablePath, string commandName)
5859
Directory.CreateDirectory(_shimsDirectory.Value);
5960
}
6061

61-
CreateApphostShimAndConfigFile(
62+
CreateApphostShim(
6263
commandName,
6364
entryPoint: targetExecutablePath);
6465

@@ -125,7 +126,7 @@ public void RemoveShim(string commandName)
125126
});
126127
}
127128

128-
private void CreateApphostShimAndConfigFile(string commandName, FilePath entryPoint)
129+
private void CreateApphostShim(string commandName, FilePath entryPoint)
129130
{
130131
string appHostSourcePath;
131132
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -137,21 +138,13 @@ private void CreateApphostShimAndConfigFile(string commandName, FilePath entryPo
137138
appHostSourcePath = Path.Combine(_appHostSourceDirectory, ApphostNameWithoutExtension);
138139
}
139140

141+
var appHostDestinationFilePath = GetShimPath(commandName).Value;
142+
var appBinaryFilePath = PathUtility.GetRelativePath(appHostDestinationFilePath, entryPoint.Value);
143+
140144
EmbedAppNameInHost.EmbedAndReturnModifiedAppHostPath(
141145
appHostSourceFilePath: appHostSourcePath,
142-
appHostDestinationFilePath: GetShimPath(commandName).Value,
143-
appBinaryName: Path.GetFileName(entryPoint.Value));
144-
145-
var config = JsonConvert.SerializeObject(
146-
new RootObject
147-
{
148-
startupOptions = new StartupOptions
149-
{
150-
appRoot = entryPoint.GetDirectoryPath().Value
151-
}
152-
});
153-
154-
File.WriteAllText(GetConfigPath(commandName).Value, config);
146+
appHostDestinationFilePath: appHostDestinationFilePath,
147+
appBinaryFilePath: appBinaryFilePath);
155148
}
156149

157150
private class StartupOptions
@@ -177,7 +170,6 @@ private IEnumerable<FilePath> GetShimFiles(string commandName)
177170
}
178171

179172
yield return GetShimPath(commandName);
180-
yield return GetConfigPath(commandName);
181173
}
182174

183175
private FilePath GetShimPath(string commandName)
@@ -192,11 +184,6 @@ private FilePath GetShimPath(string commandName)
192184
}
193185
}
194186

195-
private FilePath GetConfigPath(string commandName)
196-
{
197-
return _shimsDirectory.WithFile(commandName + ".startupconfig.json");
198-
}
199-
200187
private static void SetUserExecutionPermission(FilePath path)
201188
{
202189
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))

test/Microsoft.DotNet.Cli.Utils.Tests/PathUtilityTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,32 @@ public void GetRelativePathWithCaseInsensitiveDrives()
2121
Assert.Equal(@"d:\foo\", PathUtility.GetRelativePath(@"C:\foo\", @"d:\foo\"));
2222
}
2323

24+
[WindowsOnlyFact]
25+
public void GetRelativePathForFilePath()
26+
{
27+
Assert.Equal(
28+
@"mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll",
29+
PathUtility.GetRelativePath(
30+
@"C:\Users\myuser\.dotnet\tools\mytool.exe",
31+
@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll"));
32+
}
33+
34+
[WindowsOnlyFact]
35+
public void GetRelativePathRequireTrailingSlashForDirectoryPath()
36+
{
37+
Assert.NotEqual(
38+
@"mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll",
39+
PathUtility.GetRelativePath(
40+
@"C:\Users\myuser\.dotnet\tools",
41+
@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll"));
42+
43+
Assert.Equal(
44+
@"mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll",
45+
PathUtility.GetRelativePath(
46+
@"C:\Users\myuser\.dotnet\tools\",
47+
@"C:\Users\myuser\.dotnet\tools\mytool\1.0.1\mytool\1.0.1\tools\netcoreapp2.1\any\mytool.dll"));
48+
}
49+
2450
/// <summary>
2551
/// Tests that PathUtility.RemoveExtraPathSeparators works correctly with drive references on Windows.
2652
/// </summary>

0 commit comments

Comments
 (0)