Skip to content

Commit

Permalink
Merge pull request #40 from ZehMatt/refactor/dependencybuild
Browse files Browse the repository at this point in the history
Fix rebuilds not triggering when dependencies changed in plugin.json
  • Loading branch information
ZehMatt authored Nov 6, 2021
2 parents 34222f0 + 838d731 commit f40bdc7
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/Dotx64Managed/Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,20 @@ PluginInfo GetPluginInfo(string jsonFile)
var jsonString = Utils.ReadFileContents(jsonFile);
var pluginInfo = JsonSerializer.Deserialize<PluginInfo>(jsonString);

return JsonSerializer.Deserialize<PluginInfo>(jsonString);
var res = JsonSerializer.Deserialize<PluginInfo>(jsonString);
if(res.Dependencies == null)
{
// Ensure this is never null.
res.Dependencies = Array.Empty<string>();
}

// Ensure no duplicates exist.
res.Dependencies = res.Dependencies.Distinct().ToArray();

// This list has to be sorted to avoid rebuilds when only the order changes.
Array.Sort(res.Dependencies);

return res;
}
catch (System.Exception)
{
Expand Down Expand Up @@ -480,14 +493,25 @@ void OnPluginChange(object sender, FileSystemEventArgs e)
}
}

bool CheckDependeniesChanged(string[] left, string[] right)
{
if (left.Length != right.Length)
return true;

if (Enumerable.SequenceEqual(left, right))
return false;

return true;
}

void OnPluginFileChange(Plugin plugin, PluginFileInfo info)
{
var fullPath = Path.Combine(PluginsPath, info.PluginName, info.FilePath);

if (plugin.ConfigPath == fullPath)
{
Utils.DebugPrintLine("Plugin info modified, reloading meta...");
var requiresRebuild = plugin.Info == null;
var oldInfo = plugin.Info;

var pluginInfo = GetPluginInfo(fullPath);
if (pluginInfo == null)
Expand All @@ -496,7 +520,23 @@ void OnPluginFileChange(Plugin plugin, PluginFileInfo info)
}

plugin.Info = pluginInfo;
if (requiresRebuild && pluginInfo != null)

var requiresRebuild = false;
if (oldInfo == null && pluginInfo != null)
{
// No info previously
requiresRebuild = true;
}
else if (oldInfo != null && pluginInfo != null)
{
if (CheckDependeniesChanged(oldInfo.Dependencies, pluginInfo.Dependencies))
{
Utils.DebugPrintLine("Dependencies changed");
requiresRebuild = true;
}
}

if (requiresRebuild)
{
plugin.RequiresRebuild = true;
TriggerRebuild(50);
Expand Down

0 comments on commit f40bdc7

Please sign in to comment.