-
-
Notifications
You must be signed in to change notification settings - Fork 322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shared folder strategies #39
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace StabilityMatrix.Models.Packages; | ||
|
||
public interface ISharedFolderStrategy | ||
{ | ||
Task ExecuteAsync(BasePackage package); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace StabilityMatrix.Models.Packages; | ||
|
||
public class LinkedFolderSharedFolderStrategy : ISharedFolderStrategy | ||
{ | ||
private readonly IServiceProvider serviceProvider; | ||
|
||
public LinkedFolderSharedFolderStrategy(IServiceProvider serviceProvider) | ||
{ | ||
this.serviceProvider = serviceProvider; | ||
} | ||
|
||
public Task ExecuteAsync(BasePackage package) | ||
{ | ||
// TODO: Move SharedFolders logic here | ||
// NOTE: We're using this awkward solution because a circular dependency is generated in the graph otherwise | ||
var sharedFolders = serviceProvider.GetRequiredService<ISharedFolders>(); | ||
sharedFolders.SetupLinksForPackage(package, package.InstallLocation); | ||
return Task.CompletedTask; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using StabilityMatrix.Extensions; | ||
using StabilityMatrix.Helper; | ||
using StabilityMatrix.Models.FileInterfaces; | ||
|
||
namespace StabilityMatrix.Models.Packages; | ||
|
||
public class VladAutomaticSharedFolderStrategy : ISharedFolderStrategy | ||
{ | ||
private readonly ISettingsManager settingsManager; | ||
|
||
public VladAutomaticSharedFolderStrategy(ISettingsManager settingsManager) | ||
{ | ||
this.settingsManager = settingsManager; | ||
} | ||
|
||
public async Task ExecuteAsync(BasePackage package) | ||
{ | ||
var installedPackage = settingsManager | ||
.Settings | ||
.InstalledPackages | ||
.Single(p => p.PackageName == package.Name); | ||
Comment on lines
+22
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A few things here -
|
||
var configFilePath = Path.Combine(settingsManager.LibraryDir, installedPackage.LibraryPath!, "config.json"); | ||
|
||
// Load the configuration file | ||
var json = await File.ReadAllTextAsync(configFilePath); | ||
var job = JsonConvert.DeserializeObject<JObject>(json)!; | ||
Comment on lines
+31
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We've been using |
||
|
||
// Update the configuration values | ||
var modelsDirectory = new DirectoryPath(settingsManager.ModelsDirectory); | ||
foreach (var (sharedFolderType, configKey) in map) | ||
{ | ||
var value = Path.Combine(modelsDirectory.FullPath, sharedFolderType.GetStringValue()); | ||
job[configKey] = value; | ||
} | ||
|
||
// Write the configuration file | ||
await File.WriteAllTextAsync(configFilePath, JsonConvert.SerializeObject(job, Formatting.Indented)); | ||
} | ||
|
||
private Dictionary<SharedFolderType, string> map = new() | ||
{ | ||
{ SharedFolderType.StableDiffusion, "ckpt_dir" }, | ||
{ SharedFolderType.Diffusers, "diffusers_dir" }, | ||
{ SharedFolderType.VAE, "vae_dir" }, | ||
{ SharedFolderType.Lora, "lora_dir" }, | ||
{ SharedFolderType.LyCORIS, "lyco_dir" }, | ||
// { SharedFolderType.Styles, "styles_dir"}, | ||
{ SharedFolderType.TextualInversion, "embeddings_dir" }, | ||
{ SharedFolderType.Hypernetwork, "hypernetwork_dir" }, | ||
{ SharedFolderType.Codeformer, "codeformer_models_path" }, | ||
{ SharedFolderType.GFPGAN, "gfpgan_models_path" }, | ||
{ SharedFolderType.ESRGAN, "esrgan_models_path" }, | ||
{ SharedFolderType.BSRGAN , "bsrgan_models_path"}, | ||
{ SharedFolderType.RealESRGAN, "realesrgan_models_path" }, | ||
{ SharedFolderType.ScuNET, "scunet_models_path" }, | ||
{ SharedFolderType.SwinIR, "swinir_models_path" }, | ||
{ SharedFolderType.LDSR, "ldsr_models_path" }, | ||
{ SharedFolderType.CLIP, "clip_models_path" } | ||
}; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,7 +157,10 @@ private void ToastNotificationManagerCompatOnOnActivated( | |
basePackage.StartupComplete += RunningPackageOnStartupComplete; | ||
|
||
// Update shared folder links (in case library paths changed) | ||
sharedFolders.UpdateLinksForPackage(basePackage, packagePath); | ||
if (basePackage.SharedFolderStrategy is LinkedFolderSharedFolderStrategy) | ||
sharedFolders.UpdateLinksForPackage(basePackage, packagePath); | ||
else | ||
await basePackage.SharedFolderStrategy.ExecuteAsync(basePackage); | ||
Comment on lines
+160
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: could you please put curly braces around these statements? According to our style guidelines:
Thank you! |
||
|
||
// Load user launch args from settings and convert to string | ||
var userArgs = settingsManager.GetLaunchArgs(activeInstall.Id); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if it may be better to implement an abstract method, along the lines of
ExecuteSharedFolderStrategy
instead - we could inject theISharedFolders
to the implementations of this, so that the packages without custom strategies can just call the originalSharedFolders
method without theis LinkedFolderSharedFolderStrategy
check.You could still inject the strategies to the classes that need them, we just wouldn't expose the whole strategy object to the callers. You might also need to pass in the directory as a parameter, since the BasePackage doesn't know where it's installed (most of the time).
It might also eliminate the need for
LinkedFolderSharedFolderStrategy
and avoid the circular reference shenanigans.I hope that made sense 😅