Skip to content

Commit 75f1f05

Browse files
authored
Merge pull request #529 from DanielEgbers/feature/PublishSingleFile-netcoreapp3.1-fix
Fix: "publish to single file"-feature not working on .NET Core 3.1 or higher
2 parents 6454242 + 9fe383e commit 75f1f05

4 files changed

Lines changed: 15 additions & 6 deletions

File tree

src/Dotnet.Script.Core/ScriptPublisher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public void CreateExecutable<TReturn, THost>(ScriptContext context, LogFactory l
8888

8989
var commandRunner = new CommandRunner(logFactory);
9090
// todo: may want to add ability to return dotnet.exe errors
91-
var exitcode = commandRunner.Execute("dotnet", $"publish \"{renamedProjectPath}\" -c Release -r {runtimeIdentifier} -o \"{context.WorkingDirectory}\" {(ScriptEnvironment.Default.TargetFramework == "netcoreapp3.0" ? "/p:PublishSingleFile=true" : "")} /p:DebugType=Embedded");
91+
var exitcode = commandRunner.Execute("dotnet", $"publish \"{renamedProjectPath}\" -c Release -r {runtimeIdentifier} -o \"{context.WorkingDirectory}\" {(ScriptEnvironment.Default.NetCoreVersion.Major >= 3 ? "/p:PublishSingleFile=true" : string.Empty)} /p:DebugType=Embedded");
9292

9393
if (exitcode != 0)
9494
{

src/Dotnet.Script.DependencyModel/Context/ScriptDependencyContextReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public ScriptDependencyContext ReadDependencyContext(string pathToAssetsFile)
6060
}
6161
}
6262

63-
if (ScriptEnvironment.Default.NetCoreVersion.Version.StartsWith("3"))
63+
if (ScriptEnvironment.Default.NetCoreVersion.Major >= 3)
6464
{
6565
var netcoreAppRuntimeAssemblyLocation = Path.GetDirectoryName(typeof(object).Assembly.Location);
6666
var netcoreAppRuntimeAssemblies = Directory.GetFiles(netcoreAppRuntimeAssemblyLocation, "*.dll").Where(IsAssembly).ToArray();

src/Dotnet.Script.DependencyModel/Environment/ScriptEnvironment.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static DotnetVersion GetNetCoreAppVersion()
7676
{
7777
// https://github.com/dotnet/BenchmarkDotNet/blob/94863ab4d024eca04d061423e5aad498feff386b/src/BenchmarkDotNet/Portability/RuntimeInformation.cs#L156
7878
var codeBase = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly.CodeBase;
79-
var pattern = @"^.*Microsoft\.NETCore\.App\/(\d\.\d)(.*?)\/";
79+
var pattern = @"^.*Microsoft\.NETCore\.App\/(\d+\.\d+)(.*?)\/";
8080
var match = Regex.Match(codeBase, pattern, RegexOptions.IgnoreCase);
8181
if (!match.Success)
8282
{
@@ -139,9 +139,17 @@ public DotnetVersion(string version, string tfm)
139139
{
140140
Version = version;
141141
Tfm = tfm;
142+
143+
var versionMatch = Regex.Match(input: Version, pattern: @"^(\d+)(?:\.(\d+))?");
144+
if (versionMatch.Success && versionMatch.Groups[1].Success)
145+
Major = int.Parse(versionMatch.Groups[1].Value);
146+
if (versionMatch.Success && versionMatch.Groups[2].Success)
147+
Minor = int.Parse(versionMatch.Groups[2].Value);
142148
}
143149

144150
public string Version { get; }
145151
public string Tfm { get; }
152+
public int Major { get; }
153+
public int Minor { get; }
146154
}
147155
}

src/Dotnet.Script.Tests/ScriptPublisherTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ public void SimplePublishTest()
4040

4141
Assert.Equal(0, executableRunResult);
4242

43-
#if NETCOREAPP3_0
4443
var publishedFiles = Directory.EnumerateFiles(Path.Combine(workspaceFolder.Path, "publish", _scriptEnvironment.RuntimeIdentifier));
45-
Assert.True(1 == publishedFiles.Count(), "There should be only a single published file on .NET Core 3.0");
46-
#endif
44+
if (_scriptEnvironment.NetCoreVersion.Major >= 3)
45+
Assert.True(publishedFiles.Count() == 1, "There should be only a single published file");
46+
else
47+
Assert.True(publishedFiles.Count() > 1, "There should be multiple published files");
4748
}
4849
}
4950

0 commit comments

Comments
 (0)