Skip to content

Commit 2e7b6c5

Browse files
committed
Use lazy for all runtime information
1 parent b95b6f6 commit 2e7b6c5

File tree

8 files changed

+31
-34
lines changed

8 files changed

+31
-34
lines changed

src/Dotnet.Script.Core/ScriptCompiler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public virtual ScriptCompilationContext<TReturn> CreateCompilationContext<TRetur
8585
{
8686
if (context == null) throw new ArgumentNullException(nameof(context));
8787

88-
Logger.Verbose($"Current runtime is '{RuntimeHelper.GetPlatformIdentifier()}'.");
88+
Logger.Verbose($"Current runtime is '{RuntimeHelper.PlatformIdentifier}'.");
8989
RuntimeDependency[] runtimeDependencies = GetRuntimeDependencies(context);
9090

9191
var scriptOptions = CreateScriptOptions(context, runtimeDependencies.ToList());
@@ -130,7 +130,7 @@ private static void LoadNativeAssets(RuntimeDependency[] runtimeDependencies)
130130
{
131131
foreach (var nativeAsset in runtimeDependencies.SelectMany(rtd => rtd.NativeAssets).Distinct())
132132
{
133-
if (RuntimeHelper.IsWindows())
133+
if (RuntimeHelper.IsWindows)
134134
{
135135
LoadLibrary(nativeAsset);
136136
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public DotnetRestorer(CommandRunner commandRunner, LogFactory logFactory)
1818

1919
public void Restore(string pathToProjectFile)
2020
{
21-
var runtimeIdentifier = RuntimeHelper.GetRuntimeIdentifier();
21+
var runtimeIdentifier = RuntimeHelper.RuntimeIdentifier;
2222
_logger.Debug($"Restoring {pathToProjectFile} using the dotnet cli. RuntimeIdentifier : {runtimeIdentifier}");
2323
var exitcode = _commandRunner.Execute("dotnet", $"restore \"{pathToProjectFile}\" -r {runtimeIdentifier}");
2424
if (exitcode != 0)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public NuGetRestorer(CommandRunner commandRunner, LogFactory logFactory)
2828
public void Restore(string pathToProjectFile)
2929
{
3030
ExtractNugetExecutable();
31-
if (RuntimeHelper.IsWindows())
31+
if (RuntimeHelper.IsWindows)
3232
{
3333
_commandRunner.Execute(PathToNuget, $"restore {pathToProjectFile}");
3434
}
@@ -42,7 +42,7 @@ public void Restore(string pathToProjectFile)
4242

4343
private bool CheckAvailability()
4444
{
45-
if (RuntimeHelper.IsWindows())
45+
if (RuntimeHelper.IsWindows)
4646
{
4747
return _commandRunner.Execute(PathToNuget) == 0;
4848
}

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@ public static class RuntimeHelper
1515

1616
private static readonly Lazy<string> LazyInstallLocation = new Lazy<string>(GetInstallLocation);
1717

18-
public static string GetPlatformIdentifier()
19-
{
20-
if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Darwin) return "osx";
21-
if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Linux) return "linux";
22-
return "win";
23-
}
18+
private static readonly Lazy<string> LazyPlatformIdentifier = new Lazy<string>(GetPlatformIdentifier);
2419

25-
public static bool IsWindows()
26-
{
27-
return GetPlatformIdentifier() == "win";
28-
}
20+
private static readonly Lazy<string> LazyRuntimeIdentifier = new Lazy<string>(GetRuntimeIdentifier);
21+
22+
private static readonly Lazy<bool> LazyIsWindows = new Lazy<bool>(() => PlatformIdentifier == "win");
23+
24+
public static bool IsWindows => LazyIsWindows.Value;
25+
26+
public static string PlatformIdentifier => LazyPlatformIdentifier.Value;
27+
28+
public static string RuntimeIdentifier => LazyRuntimeIdentifier.Value;
2929

3030
public static string TargetFramework => LazyTargetFramework.Value;
3131

3232
public static string InstallLocation => LazyInstallLocation.Value;
3333

34+
private static string GetPlatformIdentifier()
35+
{
36+
if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Darwin) return "osx";
37+
if (RuntimeEnvironment.OperatingSystemPlatform == Platform.Linux) return "linux";
38+
return "win";
39+
}
40+
3441
private static string GetNetCoreAppVersion()
3542
{
3643
// https://github.com/dotnet/BenchmarkDotNet/blob/94863ab4d024eca04d061423e5aad498feff386b/src/BenchmarkDotNet/Portability/RuntimeInformation.cs#L156
@@ -54,7 +61,7 @@ private static string GetInstallLocation()
5461
private static string GetDotnetBinaryPath()
5562
{
5663
string basePath;
57-
if (IsWindows())
64+
if (IsWindows)
5865
{
5966
basePath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles);
6067
}
@@ -78,7 +85,7 @@ private static string GetProcessArchitecture()
7885
return RuntimeEnvironment.RuntimeArchitecture;
7986
}
8087

81-
public static string GetRuntimeIdentifier()
88+
private static string GetRuntimeIdentifier()
8289
{
8390
var platformIdentifier = GetPlatformIdentifier();
8491
if (platformIdentifier == "osx" || platformIdentifier == "linux")
@@ -99,7 +106,7 @@ public static string CreateTempFolder(string targetDirectory)
99106
var tempDirectory = Path.GetTempPath();
100107
var pathRoot = Path.GetPathRoot(targetDirectory);
101108
var targetDirectoryWithoutRoot = targetDirectory.Substring(pathRoot.Length);
102-
if (pathRoot.Length > 0 && RuntimeHelper.IsWindows())
109+
if (pathRoot.Length > 0 && IsWindows)
103110
{
104111
var driveLetter = pathRoot.Substring(0, 1);
105112
if (driveLetter == "\\")

src/Dotnet.Script.DependencyModel/Runtime/RuntimeDependencyResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private RuntimeAssembly[] ProcessRuntimeAssemblies(RuntimeLibrary runtimeLibrary
113113

114114
var runtimeAssemblyGroup =
115115
runtimeLibrary.RuntimeAssemblyGroups.FirstOrDefault(rag =>
116-
rag.Runtime == RuntimeHelper.GetRuntimeIdentifier());
116+
rag.Runtime == RuntimeHelper.RuntimeIdentifier);
117117

118118
if (runtimeAssemblyGroup == null)
119119
{

src/Dotnet.Script.Tests/ScriptExecutionTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void ShouldIncludeExceptionLineNumberAndFile()
3636
public void ShouldHandlePackageWithNativeLibraries()
3737
{
3838
// We have no story for this on *nix yet
39-
if (RuntimeHelper.IsWindows())
39+
if (RuntimeHelper.IsWindows)
4040
{
4141
var result = Execute(Path.Combine("NativeLibrary", "NativeLibrary.csx"));
4242
Assert.Contains("Connection successful", result.output);
@@ -77,7 +77,7 @@ public static void ShouldHandleIssue166()
7777
{
7878
// System.Data.SqlClient loads native assets
7979
// No story on *nix yet.
80-
if (RuntimeHelper.IsWindows())
80+
if (RuntimeHelper.IsWindows)
8181
{
8282
var result = Execute(Path.Combine("Issue166", "Issue166.csx"));
8383
Assert.Contains("Connection successful", result.output);

src/Dotnet.Script.Tests/ScriptPackagesTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private static void BuildScriptPackages()
136136
foreach (var specFile in specFiles)
137137
{
138138
string command;
139-
if (RuntimeHelper.IsWindows())
139+
if (RuntimeHelper.IsWindows)
140140
{
141141
command = pathtoNuget430;
142142
var result = ProcessHelper.RunAndCaptureOutput(command, new[] { $"pack {specFile}", $"-OutputDirectory {pathToPackagesOutputFolder}" });

src/Dotnet.Script/Program.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,13 @@ private static string GetVersionInfo()
212212
sb.AppendLine($"Version : {versionAttribute?.InformationalVersion}");
213213
sb.AppendLine($"Install location : {RuntimeHelper.InstallLocation}");
214214
sb.AppendLine($"Target framework : {RuntimeHelper.TargetFramework}");
215-
sb.AppendLine($"Platform identifier : {RuntimeHelper.GetPlatformIdentifier()}");
216-
sb.AppendLine($"Runtime identifier : {RuntimeHelper.GetRuntimeIdentifier()}");
215+
sb.AppendLine($"Platform identifier : {RuntimeHelper.PlatformIdentifier}");
216+
sb.AppendLine($"Runtime identifier : {RuntimeHelper.RuntimeIdentifier}");
217217
return sb.ToString();
218218

219219

220220
}
221221

222-
private static string GetInfo()
223-
{
224-
StringBuilder sb = new StringBuilder();
225-
sb.AppendLine($"Install location : {RuntimeHelper.InstallLocation}");
226-
sb.AppendLine($"Target framework : {RuntimeHelper.TargetFramework}");
227-
sb.AppendLine($"Platform identifier : {RuntimeHelper.GetPlatformIdentifier()}");
228-
sb.AppendLine($"Runtime identifier : {RuntimeHelper.GetRuntimeIdentifier()}");
229-
return sb.ToString();
230-
}
231-
232222
private static ScriptCompiler GetScriptCompiler(bool debugMode)
233223
{
234224
var logger = new ScriptLogger(ScriptConsole.Default.Error, debugMode);

0 commit comments

Comments
 (0)