diff --git a/src/Microsoft.DotNet.Cli.Utils/EmbedAppNameInHost.cs b/src/Microsoft.DotNet.Cli.Utils/EmbedAppNameInHost.cs index 7a91b6f2f..3b5547af8 100644 --- a/src/Microsoft.DotNet.Cli.Utils/EmbedAppNameInHost.cs +++ b/src/Microsoft.DotNet.Cli.Utils/EmbedAppNameInHost.cs @@ -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); + + /// + /// Create an AppHost with embedded configuration of app binary location + /// + /// The path of AppHost template, which has the place holder + /// The destination path for desired location to place, including the file name + /// Full path to app binary or relative path to appHostDestinationFilePath 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); diff --git a/src/dotnet/ShellShim/ShellShimRepository.cs b/src/dotnet/ShellShim/ShellShimRepository.cs index 6fcdf9c11..a7b890525 100644 --- a/src/dotnet/ShellShim/ShellShimRepository.cs +++ b/src/dotnet/ShellShim/ShellShimRepository.cs @@ -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)) diff --git a/test/Microsoft.DotNet.Cli.Utils.Tests/PathUtilityTests.cs b/test/Microsoft.DotNet.Cli.Utils.Tests/PathUtilityTests.cs index 70e7768c7..2951ba9e7 100644 --- a/test/Microsoft.DotNet.Cli.Utils.Tests/PathUtilityTests.cs +++ b/test/Microsoft.DotNet.Cli.Utils.Tests/PathUtilityTests.cs @@ -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")); + } + /// /// Tests that PathUtility.RemoveExtraPathSeparators works correctly with drive references on Windows. ///