Skip to content

Commit

Permalink
プラグイン関係を修正
Browse files Browse the repository at this point in the history
- プラグインのロード
- ローカルパッケージのインストール画面
- 依存関係の解決
  • Loading branch information
yuto-trd committed Aug 17, 2023
1 parent f75817a commit 71133f2
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 39 deletions.
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.6.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.FileSystemGlobbing" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="7.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion packages/Beutl.Sdk/Beutl.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Version>0.3.0-preview1</Version>
<Version>0.3.0</Version>
<AssemblyVersion>0.3.0.0</AssemblyVersion>
</PropertyGroup>

Expand Down
1 change: 1 addition & 0 deletions src/Beutl.Api/Beutl.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyModel" />
<PackageReference Include="Nito.AsyncEx" />
<PackageReference Include="ReactiveProperty" />
<PackageReference Include="System.Reactive" />
Expand Down
167 changes: 139 additions & 28 deletions src/Beutl.Api/Services/CoreLibraries.cs
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;
}
}
8 changes: 4 additions & 4 deletions src/Beutl.Api/Services/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static async Task GetPackageDependencies(PackageIdentity package,
ISet<SourcePackageDependencyInfo> availablePackages,
CancellationToken cancellationToken = default)
{
if (availablePackages.Contains(package) || IsCoreLibraries(package.Id)) return;
if (availablePackages.Contains(package) || IncludedInPackageDependencies(package.Id, package.Version)) return;

foreach (SourceRepository sourceRepository in repositories)
{
Expand All @@ -70,11 +70,11 @@ SourcePackageDependencyInfo dependencyInfo

if (dependencyInfo == null) continue;

if (dependencyInfo.Dependencies.Any(x => IsCoreLibraries(x.Id)))
if (dependencyInfo.Dependencies.Any(x => IncludedInPackageDependencies(x.Id, x.VersionRange)))
{
dependencyInfo = new SourcePackageDependencyInfo(
dependencyInfo,
dependencyInfo.Dependencies.Where(x => !IsCoreLibraries(x.Id)),
dependencyInfo.Dependencies.Where(x => !IncludedInPackageDependencies(x.Id, x.VersionRange)),
dependencyInfo.Listed,
dependencyInfo.Source,
dependencyInfo.DownloadUri,
Expand Down Expand Up @@ -102,7 +102,7 @@ public static void GetPackageDependencies(
NuGetFramework framework,
ISet<PackageDependencyInfo> availablePackages)
{
if (availablePackages.Contains(package) || IsCoreLibraries(package.Id)) return;
if (availablePackages.Contains(package) || IncludedInPackageDependencies(package.Id, package.Version)) return;

availablePackages.Add(package);

Expand Down
4 changes: 2 additions & 2 deletions src/Beutl.Api/Services/PackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ await Helper.GetPackageDependencies(
new[] { packageId },
Enumerable.Empty<string>(),
Enumerable.Empty<PackageReference>(),
CoreLibraries.PreferredVersions,
CoreLibraries.GetPreferredVersions(),
availablePackages,
repositories.Select(s => s.PackageSource),
logger);
Expand All @@ -330,7 +330,7 @@ SourcePackageDependencyInfo[] packagesToInstall
foreach (SourcePackageDependencyInfo packageToInstall in packagesToInstall)
{
// Beutl.Sdkに含まれるライブラリの場合、飛ばす。
if (CoreLibraries.IsCoreLibraries(packageToInstall.Id))
if (CoreLibraries.IncludedInPackageDependencies(packageToInstall.Id, packageToInstall.Version))
{
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Beutl.Api/Services/PluginLoadContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public PluginLoadContext(string mainDirectory, PackageFolderReader? reader = nul
return LoadFromAssemblyPath(assemblyPath);
}

if (!CoreLibraries.IsCoreLibraries(name.Name!))
if (!CoreLibraries.IncludedInRuntimeDependencies(name.Name!, name.Version))
{
assemblyPath = _pluginResolver.ResolveAssemblyToPath(name);
if (assemblyPath != null)
Expand Down
3 changes: 2 additions & 1 deletion src/Beutl.Controls/Styles.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@
</Setter>
</Style>

<Style Selector=":is(Visual).transparent">
<Style Selector=":is(InputElement).transparent">
<Setter Property="Opacity" Value="0" />
<Setter Property="IsHitTestVisible" Value="False" />
</Style>

<Style Selector="ToggleSwitch">
Expand Down
4 changes: 2 additions & 2 deletions src/Beutl.Extensions.FFmpeg/Beutl.Extensions.FFmpeg.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<Description>FFmpeg for beutl</Description>
<PackageTags>ffmpeg;decoder;decoding;encoder;encoding;video;audio</PackageTags>
<PackageLicenseExpression>GPL-2.0-only</PackageLicenseExpression>
<Version>1.0.0</Version>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<Version>0.3.0</Version>
<AssemblyVersion>0.3.0.0</AssemblyVersion>
<Authors>b-editor</Authors>

<FFmpegBuildIn Condition="'$(FFmpegBuildIn)' == ''">True</FFmpegBuildIn>
Expand Down

0 comments on commit 71133f2

Please sign in to comment.