Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Mar 29, 2024
1 parent af83e2a commit 59fd5ed
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ public ExternalDependenciesService(IExifToolDownload exifToolDownload,
_geoFileDownload = geoFileDownload;
}

public async Task SetupAsync(List<string> args)
public async Task SetupAsync(string[] args)
{
await SetupAsync(ArgsHelper.GetRuntime(args));
}

public async Task SetupAsync(OSPlatform? currentPlatform = null,
Architecture? architecture = null)
public async Task SetupAsync(List<(OSPlatform?, Architecture?)> currentPlatforms)
{
currentPlatform ??= PlatformParser.GetCurrentOsPlatform();

await RunMigrations.Run(_dbContext, _logger, _appSettings);
await _exifToolDownload.DownloadExifTool(currentPlatform == OSPlatform.Windows);


if ( currentPlatforms.Count == 0 )
{
currentPlatforms =
[
( PlatformParser.GetCurrentOsPlatform(),
PlatformParser.GetCurrentArchitecture() )
];
}

foreach ( var (osPlatform, _) in currentPlatforms )
{
await _exifToolDownload.DownloadExifTool(osPlatform == OSPlatform.Windows);
}

await _geoFileDownload.DownloadAsync();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Runtime.InteropServices;

namespace starsky.feature.externaldependencies.Interfaces;

public interface IExternalDependenciesService
{
Task SetupAsync(OSPlatform? currentPlatform = null, Architecture? architecture = null);
Task SetupAsync(string[] args);
}
14 changes: 8 additions & 6 deletions starsky/starsky.foundation.platform/Helpers/ArgsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,23 +642,25 @@ public static ConsoleOutputMode GetConsoleOutputMode(IReadOnlyList<string> args)
/// </summary>
/// <param name="args">arg list</param>
/// <returns>path</returns>
public static OSPlatform? GetRuntime(IReadOnlyList<string?> args)
public static List<(OSPlatform?, Architecture?)> GetRuntime(IReadOnlyList<string?> args)
{
OSPlatform? outputMode = null;
var outputModeList = new List<(OSPlatform?, Architecture?)>();
for ( var arg = 0; arg < args.Count; arg++ )
{
if ( args[arg]?
.Equals("--runtime", StringComparison.CurrentCultureIgnoreCase) != true ||
( arg + 1 ) == args.Count )
.Equals("--runtime",
StringComparison.CurrentCultureIgnoreCase) != true ||
arg + 1 == args.Count )
{
continue;
}

var runtimeItem = args[arg + 1];
outputMode = PlatformParser.RuntimeIdentifier(runtimeItem);
var parsedRuntimeList = PlatformParser.RuntimeIdentifier(runtimeItem);
outputModeList.AddRange(parsedRuntimeList);
}

return outputMode;
return outputModeList;
}

/// <summary>
Expand Down
50 changes: 42 additions & 8 deletions starsky/starsky.foundation.platform/Helpers/PlatformParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,57 @@ public static class PlatformParser
return currentPlatform;
}

public static OSPlatform? RuntimeIdentifier(string? runtimeIdentifier)
public static List<(OSPlatform?, Architecture?)> RuntimeIdentifier(string? runtimeIdentifiers)
{
if ( runtimeIdentifier == null )
var result = new List<(OSPlatform?, Architecture?)>();
if ( runtimeIdentifiers == null )
{
return null;
return [];
}

if ( runtimeIdentifier.StartsWith("win-") )
var runtimes = runtimeIdentifiers.Split(",").Where(x => !string.IsNullOrEmpty(x)).ToList();
foreach ( var runtime in runtimes )
{
var singleRuntimeIdentifier = SingleRuntimeIdentifier(runtime);
if ( singleRuntimeIdentifier is { Item1: not null, Item2: not null } )
{
result.Add(singleRuntimeIdentifier);
}
}

return result;
}

private static (OSPlatform?, Architecture?) SingleRuntimeIdentifier(string? runtimeIdentifier)
{
if ( runtimeIdentifier == null )
{
return OSPlatform.Windows;
return ( null, null );
}

if ( runtimeIdentifier.StartsWith("linux-") )
switch ( runtimeIdentifier )
{
return OSPlatform.Linux;
case "win-x64":
return ( OSPlatform.Windows, Architecture.X64 );
case "win-arm64":
return ( OSPlatform.Windows, Architecture.Arm64 );
case "linux-arm":
return ( OSPlatform.Linux, Architecture.Arm );
case "linux-arm64":
return ( OSPlatform.Linux, Architecture.Arm64 );
case "linux-x64":
return ( OSPlatform.Linux, Architecture.X64 );
case "osx-x64":
return ( OSPlatform.OSX, Architecture.X64 );
case "osx-arm64":
return ( OSPlatform.OSX, Architecture.Arm64 );
default:
return ( null, null );
}
}

return runtimeIdentifier.StartsWith("osx-") ? OSPlatform.OSX : null;
public static Architecture GetCurrentArchitecture()
{
return Architecture.Wasm;
}
}
2 changes: 1 addition & 1 deletion starsky/starskydependenciescli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static async Task Main(string[] args)
var dependenciesService =
serviceProvider.GetRequiredService<IExternalDependenciesService>();

await dependenciesService.SetupAsync();
await dependenciesService.SetupAsync(args);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -34,9 +35,13 @@ public ExternalDependenciesServiceTest()
public async Task ExternalDependenciesServiceTest_ExifTool_Default()
{
var fakeExifToolDownload = new FakeExifToolDownload();
var appSettings = new AppSettings
{
DatabaseType = AppSettings.DatabaseTypeList.InMemoryDatabase
};
var externalDependenciesService = new ExternalDependenciesService(fakeExifToolDownload,
_dbContext, new FakeIWebLogger(), new AppSettings(), new FakeIGeoFileDownload());
await externalDependenciesService.SetupAsync([]);
_dbContext, new FakeIWebLogger(), appSettings, new FakeIGeoFileDownload());
await externalDependenciesService.SetupAsync(new List<string>().ToArray());

Assert.AreEqual(new AppSettings().IsWindows, fakeExifToolDownload.Called[0]);
}
Expand All @@ -45,14 +50,40 @@ public async Task ExternalDependenciesServiceTest_ExifTool_Default()
[DataRow("osx-arm64", false)]
[DataRow("linux-x64", false)]
[DataRow("win-x64", true)]
public async Task ExternalDependenciesServiceTest_ExifTool_DataTestMethod(string input, bool expected)
public async Task ExternalDependenciesServiceTest_ExifTool_Single_DataTestMethod(string input,
bool expected)
{
var fakeExifToolDownload = new FakeExifToolDownload();
var appSettings = new AppSettings
{
DatabaseType = AppSettings.DatabaseTypeList.InMemoryDatabase
};
var externalDependenciesService = new ExternalDependenciesService(fakeExifToolDownload,
_dbContext, new FakeIWebLogger(), new AppSettings(), new FakeIGeoFileDownload());
_dbContext, new FakeIWebLogger(), appSettings, new FakeIGeoFileDownload());

await externalDependenciesService.SetupAsync(["--runtime", input]);

Assert.AreEqual(expected, fakeExifToolDownload.Called[0]);
}

[DataTestMethod]
[DataRow("osx-arm64,osx-x64", false, false)]
[DataRow("linux-x64,osx-x64", false, false)]
[DataRow("win-x64,osx-x64", true, false)]
public async Task ExternalDependenciesServiceTest_ExifTool_Multiple_DataTestMethod(string input,
bool expected1, bool expected2)
{
var appSettings = new AppSettings
{
DatabaseType = AppSettings.DatabaseTypeList.InMemoryDatabase
};
var fakeExifToolDownload = new FakeExifToolDownload();
var externalDependenciesService = new ExternalDependenciesService(fakeExifToolDownload,
_dbContext, new FakeIWebLogger(), appSettings, new FakeIGeoFileDownload());

await externalDependenciesService.SetupAsync(["--runtime", input]);

Assert.AreEqual(expected1, fakeExifToolDownload.Called[0]);
Assert.AreEqual(expected2, fakeExifToolDownload.Called[1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ public void Name()
public void GetRuntimeTest(string input, string expected)
{
var args = new List<string> { "--runtime", input }.ToArray();
Assert.AreEqual(expected, ArgsHelper.GetRuntime(args).ToString());
Assert.AreEqual(expected, ArgsHelper.GetRuntime(args)[0].Item1.ToString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ public void GetCurrentOsPlatformTest()
}

[DataTestMethod]
[DataRow("osx-arm64", "OSX")]
[DataRow("osx-x64", "OSX")]
[DataRow("linux-x64", "LINUX")]
[DataRow("linux-arm", "LINUX")]
[DataRow("linux-arm64", "LINUX")]
[DataRow("win-x64", "WINDOWS")]
[DataRow("win-x86", "WINDOWS")]
[DataRow("win-arm64", "WINDOWS")]
[DataRow("test", "")]
[DataRow(null, "")]
public void RuntimeIdentifierTest(string input, string expected)
[DataRow("osx-arm64", "OSX", "Arm64")]
[DataRow("osx-x64", "OSX", "X64")]
[DataRow("linux-x64", "LINUX", "X64")]
[DataRow("linux-arm", "LINUX", "Arm")]
[DataRow("linux-arm64", "LINUX", "Arm64")]
[DataRow("win-x64", "WINDOWS", "X64")]
[DataRow("win-x86", "WINDOWS", "X86")]
[DataRow("win-arm64", "WINDOWS", "Arm64")]
[DataRow("test", "", "")]
[DataRow(null, "", "")]
public void RuntimeIdentifierTest(string input, string expectedOs, string expectedArch)
{
Assert.AreEqual(expected, PlatformParser.RuntimeIdentifier(input).ToString());
var result = PlatformParser.RuntimeIdentifier(input);
Assert.AreEqual(expectedOs, result[0].Item1.ToString());
Assert.AreEqual(expectedArch, result[0].Item2.ToString());
}
}

0 comments on commit 59fd5ed

Please sign in to comment.