From 8daf4e045e3837b1db35b725a64f4a1c4d7bc67e Mon Sep 17 00:00:00 2001 From: AR-May <67507805+AR-May@users.noreply.github.com> Date: Wed, 9 Sep 2020 14:59:24 +0200 Subject: [PATCH] issue #1663: fix path to the script in run script post action. --- .../ProcessStartPostActionProcessor.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.TemplateEngine.Cli/PostActionProcessors/ProcessStartPostActionProcessor.cs b/src/Microsoft.TemplateEngine.Cli/PostActionProcessors/ProcessStartPostActionProcessor.cs index c2b78d1d534..41e781fd22a 100644 --- a/src/Microsoft.TemplateEngine.Cli/PostActionProcessors/ProcessStartPostActionProcessor.cs +++ b/src/Microsoft.TemplateEngine.Cli/PostActionProcessors/ProcessStartPostActionProcessor.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.IO; using Microsoft.TemplateEngine.Abstractions; using Microsoft.TemplateEngine.Utils; @@ -27,6 +28,9 @@ public bool Process(IEngineEnvironmentSettings settings, IPostAction actionConfi } settings.Host.LogMessage(string.Format(LocalizableStrings.RunningCommand, actionConfig.Args["executable"] + " " + args)); + + string resolvedExecutablePath = ResolveExecutableFilePath(actionConfig.Args["executable"], outputBasePath); + System.Diagnostics.Process commandResult = System.Diagnostics.Process.Start(new ProcessStartInfo { RedirectStandardError = true, @@ -34,7 +38,7 @@ public bool Process(IEngineEnvironmentSettings settings, IPostAction actionConfi UseShellExecute = false, CreateNoWindow = false, WorkingDirectory = outputBasePath, - FileName = actionConfig.Args["executable"], + FileName = resolvedExecutablePath, Arguments = args }); @@ -55,5 +59,21 @@ public bool Process(IEngineEnvironmentSettings settings, IPostAction actionConfi return allSucceeded; } + + private static string ResolveExecutableFilePath(string executableFileName, string outputBasePath) + { + if (!string.IsNullOrEmpty(outputBasePath) && Directory.Exists(outputBasePath)) + { + string executableCombinedFileName = Path.Combine(Path.GetFullPath(outputBasePath), executableFileName); + if (File.Exists(executableCombinedFileName)) + { + return executableCombinedFileName; + } + } + + // The executable has not been found in the template folder, thus do not use the full path to the file. + // The executable will be further searched in the directories from the PATH environment variable. + return executableFileName; + } } }