-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- プラグインのロード - ローカルパッケージのインストール画面 - 依存関係の解決
- Loading branch information
Showing
9 changed files
with
153 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,149 @@ | ||
using NuGet.Packaging.Core; | ||
using System.Reflection; | ||
|
||
using Microsoft.Extensions.DependencyModel; | ||
|
||
using NuGet.Packaging.Core; | ||
using NuGet.Versioning; | ||
|
||
namespace Beutl.Api.Services; | ||
|
||
internal static class CoreLibraries | ||
{ | ||
public static readonly NuGetVersion BeutlVersion = new("0.3.0"); | ||
public static readonly PackageIdentity[] PreferredVersions = | ||
private static Dictionary<string, string>? s_runtimeMap; | ||
private static Dictionary<string, string>? s_pkgMap; | ||
private static List<PackageIdentity>? s_preferredVersions; | ||
|
||
public static IEnumerable<PackageIdentity> GetPreferredVersions() | ||
{ | ||
new PackageIdentity("Beutl.Sdk", BeutlVersion), | ||
new PackageIdentity("Beutl.Configuration", BeutlVersion), | ||
new PackageIdentity("Beutl.Controls", BeutlVersion), | ||
new PackageIdentity("Beutl.Core", BeutlVersion), | ||
new PackageIdentity("Beutl.Extensibility", BeutlVersion), | ||
new PackageIdentity("Beutl.Engine", BeutlVersion), | ||
new PackageIdentity("Beutl.Language", BeutlVersion), | ||
new PackageIdentity("Beutl.Operators", BeutlVersion), | ||
new PackageIdentity("Beutl.ProjectSystem", BeutlVersion), | ||
new PackageIdentity("Beutl.Threading", BeutlVersion), | ||
new PackageIdentity("Beutl.Utilities", BeutlVersion), | ||
}; | ||
|
||
public static bool IsCoreLibraries(string name) | ||
s_preferredVersions ??= CollectPackageDependencies() | ||
.Select(x => new PackageIdentity(x.Name, NuGetVersion.Parse(x.Version))) | ||
.ToList(); | ||
|
||
return s_preferredVersions; | ||
} | ||
|
||
public static IEnumerable<Dependency> CollectPackageDependencies() | ||
{ | ||
return name is "Beutl.Sdk" | ||
or "Beutl.Configuration" | ||
or "Beutl.Controls" | ||
or "Beutl.Core" | ||
or "Beutl.Extensibility" | ||
or "Beutl.Engine" | ||
or "Beutl.Language" | ||
or "Beutl.Operators" | ||
or "Beutl.ProjectSystem" | ||
or "Beutl.Threading" | ||
or "Beutl.Utilities"; | ||
var assembly = Assembly.LoadFile(Path.Combine(AppContext.BaseDirectory, "Beutl.dll")); | ||
DependencyContext? depsContext = DependencyContextLoader.Default.Load(assembly) | ||
?? throw new InvalidOperationException(); | ||
|
||
var library = new HashSet<Dependency>(); | ||
|
||
foreach (RuntimeLibrary lib in depsContext.RuntimeLibraries) | ||
{ | ||
library.Add(new(lib.Name, lib.Version)); | ||
} | ||
|
||
library.Add(new("Beutl.Sdk", "0.3.0")); | ||
|
||
return library; | ||
} | ||
|
||
public static IEnumerable<Dependency> CollectRuntimeDependencies() | ||
{ | ||
var assembly = Assembly.LoadFile(Path.Combine(AppContext.BaseDirectory, "Beutl.dll")); | ||
DependencyContext? depsContext = DependencyContextLoader.Default.Load(assembly) | ||
?? throw new InvalidOperationException(); | ||
|
||
var library = new HashSet<Dependency>(); | ||
|
||
foreach (RuntimeLibrary lib in depsContext.RuntimeLibraries) | ||
{ | ||
foreach (RuntimeAssetGroup group in lib.RuntimeAssemblyGroups) | ||
{ | ||
foreach (RuntimeFile item in group.RuntimeFiles) | ||
{ | ||
string fileName = Path.GetFileNameWithoutExtension(item.Path); | ||
string? version = item.AssemblyVersion; | ||
|
||
if (version == null) | ||
{ | ||
switch (fileName) | ||
{ | ||
case "Beutl": | ||
case "Beutl.Api": | ||
case "Beutl.Configuration": | ||
case "Beutl.Controls": | ||
case "Beutl.Core": | ||
case "Beutl.Embedding.FFmpeg": | ||
case "Beutl.Engine": | ||
case "Beutl.Extensibility": | ||
case "Beutl.Language": | ||
case "Beutl.Operators": | ||
case "Beutl.ProjectSystem": | ||
case "Beutl.Threading": | ||
case "Beutl.Utilities": | ||
case "Beutl.WaitingDialog": | ||
case "bpt": | ||
version = assembly.GetName().Version!.ToString(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
library.Add(new Dependency(fileName, version!)); | ||
} | ||
} | ||
} | ||
|
||
return library; | ||
} | ||
|
||
private static Dictionary<string, string> RuntimeDepsMap => s_runtimeMap ??= CollectRuntimeDependencies().ToDictionary(x => x.Name, x => x.Version); | ||
|
||
private static Dictionary<string, string> PackageDepsMap => s_pkgMap ??= CollectPackageDependencies().ToDictionary(x => x.Name, x => x.Version); | ||
|
||
public static bool IncludedInRuntimeDependencies(string name, Version? version) | ||
{ | ||
if (RuntimeDepsMap.TryGetValue(name, out string? versionStr)) | ||
{ | ||
if (version == null) | ||
return true; | ||
|
||
if (Version.TryParse(versionStr, out Version? installedVersion)) | ||
{ | ||
return installedVersion >= version; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public static bool IncludedInPackageDependencies(string name, NuGetVersion version) | ||
{ | ||
if (PackageDepsMap.TryGetValue(name, out string? installedVersionStr)) | ||
{ | ||
var installedVersion = new NuGetVersion(installedVersionStr); | ||
return installedVersion == version; | ||
|
||
} | ||
|
||
return false; | ||
} | ||
|
||
public static bool IncludedInPackageDependencies(string name, VersionRange versionRange) | ||
{ | ||
if (PackageDepsMap.TryGetValue(name, out string? versionStr)) | ||
{ | ||
var version = new NuGetVersion(versionStr); | ||
|
||
// インストールされているバージョンがMinよりも小さい | ||
if (versionRange.HasLowerBound && versionRange.MinVersion > version) | ||
{ | ||
return false; | ||
} | ||
|
||
// インストールされているバージョンがMaxよりも大きい | ||
if (versionRange.HasUpperBound && versionRange.MaxVersion < version) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters