Skip to content

Commit 13c1a23

Browse files
authored
Add support to generate publish WebJob Run command file compatible wi… (dotnet#42242)
2 parents 7061397 + b109bf8 commit 13c1a23

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

src/WebSdk/Publish/Targets/TransformTargets/Microsoft.NET.Sdk.Publish.TransformFiles.targets

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
244244
<_UseAppHost Condition=" '$(_UseAppHost)' == '' ">$(SelfContained)</_UseAppHost>
245245
<_UseAppHost Condition=" '$(_UseAppHost)' == '' Or '$(RuntimeIdentifier)' == '' ">false</_UseAppHost>
246246
<_ExecutableExtension Condition=" '$(_ExecutableExtension)' == '' And $(RuntimeIdentifier.StartsWith('win')) ">.exe</_ExecutableExtension>
247+
<_IsLinux Condition=" '$(IsLinux)' == 'True'">true</_IsLinux>
247248
</PropertyGroup>
248249

249250
<GenerateRunCommandFile
@@ -252,6 +253,7 @@ Copyright (C) Microsoft Corporation. All rights reserved.
252253
TargetPath="$(TargetPath)"
253254
WebJobsDirectory="$(PublishIntermediateOutputPath)\app_data\Jobs\$(WebJobType)\$(WebJobName)\"
254255
UseAppHost="$(_UseAppHost)"
255-
ExecutableExtension="$(_ExecutableExtension)" />
256+
ExecutableExtension="$(_ExecutableExtension)"
257+
IsLinux="$(_IsLinux)" />
256258
</Target>
257259
</Project>

src/WebSdk/Publish/Tasks/Tasks/WebJobs/GenerateRunCommandFile.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Microsoft.NET.Sdk.Publish.Tasks.WebJobs
77
{
88
public class GenerateRunCommandFile : Task
99
{
10-
private const string RunCommandFile = "run.cmd";
10+
private const string RunCommandFile = "run";
1111
[Required]
1212
public string ProjectDirectory { get; set; }
1313
[Required]
@@ -17,14 +17,18 @@ public class GenerateRunCommandFile : Task
1717
[Required]
1818
public bool UseAppHost { get; set; }
1919
public string ExecutableExtension { get; set; }
20+
public bool IsLinux { get; set; }
2021

2122
public override bool Execute()
2223
{
23-
bool isRunCommandFilePresent = File.Exists(Path.Combine(ProjectDirectory, RunCommandFile));
24+
string runCmdFileExtension = IsLinux ? "sh" : "cmd";
25+
string runCmdFileName = $"{RunCommandFile}.{runCmdFileExtension}";
26+
27+
bool isRunCommandFilePresent = File.Exists(Path.Combine(ProjectDirectory, runCmdFileName));
2428
if (!isRunCommandFilePresent)
2529
{
26-
string command = WebJobsCommandGenerator.RunCommand(TargetPath, UseAppHost, ExecutableExtension);
27-
File.WriteAllText(Path.Combine(WebJobsDirectory, RunCommandFile), command);
30+
string command = WebJobsCommandGenerator.RunCommand(TargetPath, UseAppHost, ExecutableExtension, IsLinux);
31+
File.WriteAllText(Path.Combine(WebJobsDirectory, runCmdFileName), command);
2832
}
2933

3034
return true;

src/WebSdk/Publish/Tasks/WebJobsCommandGenerator.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,20 @@ namespace Microsoft.NET.Sdk.Publish.Tasks
55
{
66
public static class WebJobsCommandGenerator
77
{
8-
public static string RunCommand(string targetPath, bool useAppHost, string executableExtension)
8+
public static string RunCommand(string targetPath, bool useAppHost, string executableExtension, bool isLinux)
99
{
1010
string appName = Path.GetFileName(targetPath);
1111

1212
string command = $"dotnet {appName}";
1313
if (useAppHost)
1414
{
1515
command = Path.ChangeExtension(appName, !string.IsNullOrWhiteSpace(executableExtension) ? executableExtension : null);
16+
17+
// dot-space syntax to execute the command
18+
if (isLinux)
19+
{
20+
command = $". {command}";
21+
}
1622
}
1723

1824
// For Apps targeting .NET Framework, the extension is always exe. RID is not set for .NETFramework apps with PlatformType set to AnyCPU.
@@ -21,7 +27,17 @@ public static string RunCommand(string targetPath, bool useAppHost, string execu
2127
command = Path.ChangeExtension(appName, ".exe");
2228
}
2329

24-
return $"{command} %*";
30+
// pass-all-parameters argument
31+
var passParamsArg = isLinux ? "\"$@\"" : "%*";
32+
command = $"{command} {passParamsArg}";
33+
34+
// for Linux add header for bash script
35+
if (isLinux)
36+
{
37+
command = $"#!/bin/bash\n{command}";
38+
}
39+
40+
return $"{command}";
2541
}
2642
}
2743
}

test/Microsoft.NET.Sdk.Publish.Tasks.Tests/WebJobsCommandGeneratorTests.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Microsoft.Net.Sdk.Publish.Tasks.Tests
99
public class WebJobsCommandGeneratorTests
1010
{
1111
[Theory]
12+
// Windows
1213
[InlineData("c:/test/WebApplication1.dll", false, ".exe", "dotnet WebApplication1.dll %*")]
1314

1415
[InlineData("c:/test/WebApplication1.dll", true, ".exe", "WebApplication1.exe %*")]
@@ -19,12 +20,19 @@ public class WebJobsCommandGeneratorTests
1920

2021
[InlineData("/usr/test/WebApplication1.dll", true, ".sh", "WebApplication1.sh %*")]
2122
[InlineData("/usr/test/WebApplication1.dll", false, ".sh", "dotnet WebApplication1.dll %*")]
22-
public void WebJobsCommandGenerator_Generates_Correct_RunCmd(string targetPath, bool useAppHost, string executableExtension, string expected)
23+
24+
//Linux
25+
[InlineData("c:/test/WebApplication1.dll", false, "", "#!/bin/bash\ndotnet WebApplication1.dll \"$@\"", true)]
26+
[InlineData("c:/test/WebApplication1.dll", true, "", "#!/bin/bash\n. WebApplication1 \"$@\"", true)]
27+
28+
[InlineData("/usr/test/WebApplication1.dll", false, ".sh", "#!/bin/bash\ndotnet WebApplication1.dll \"$@\"", true)]
29+
[InlineData("/usr/test/WebApplication1.dll", true, ".sh", "#!/bin/bash\n. WebApplication1.sh \"$@\"", true)]
30+
public void WebJobsCommandGenerator_Generates_Correct_RunCmd(string targetPath, bool useAppHost, string executableExtension, string expected, bool isLinux = false)
2331
{
2432
// Arrange
2533

2634
// Test
27-
string generatedRunCommand = WebJobsCommandGenerator.RunCommand(targetPath, useAppHost, executableExtension);
35+
string generatedRunCommand = WebJobsCommandGenerator.RunCommand(targetPath, useAppHost, executableExtension, isLinux);
2836

2937
// Assert
3038
Assert.Equal(expected, generatedRunCommand);

0 commit comments

Comments
 (0)