From 6ba1f4a0fce0375803bf73980e219c6269bdc68a Mon Sep 17 00:00:00 2001 From: WebFreak001 Date: Thu, 7 Feb 2019 16:05:48 +0100 Subject: [PATCH] add getBestInstance and getBestInstanceByDependency analogous to the serve-d workspace methods, just for usage with workspace-d components --- source/workspaced/api.d | 106 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/source/workspaced/api.d b/source/workspaced/api.d index a89876e..fe91fc8 100644 --- a/source/workspaced/api.d +++ b/source/workspaced/api.d @@ -4,6 +4,7 @@ import core.time; import dparse.lexer; import painlessjson; import standardpaths; + import std.algorithm; import std.conv; import std.exception; @@ -11,6 +12,7 @@ import std.file; import std.json; import std.meta; import std.path; +import std.range; import std.regex; import std.traits; import std.typecons; @@ -523,6 +525,92 @@ class WorkspaceD return null; } + Instance getBestInstanceByDependency(WithComponent)(string file) nothrow + { + Instance best; + size_t bestLength; + foreach (instance; instances) + { + foreach (folder; chain(instance.importPaths, instance.importFiles, + instance.stringImportPaths)) + { + if (folder.length > bestLength && file.startsWith(folder) && instance.has!WithComponent) + { + best = instance; + bestLength = folder.length; + } + } + } + return best; + } + + Instance getBestInstanceByDependency(string file) nothrow + { + Instance best; + size_t bestLength; + foreach (instance; instances) + { + foreach (folder; chain(instance.importPaths, instance.importFiles, + instance.stringImportPaths)) + { + if (folder.length > bestLength && file.startsWith(folder)) + { + best = instance; + bestLength = folder.length; + } + } + } + return best; + } + + Instance getBestInstance(WithComponent)(string file, bool fallback = true) nothrow + { + file = buildNormalizedPath(file); + Instance ret = null; + size_t best; + foreach (instance; instances) + { + if (instance.cwd.length > best && file.startsWith(instance.cwd) && instance + .has!WithComponent) + { + ret = instance; + best = instance.cwd.length; + } + } + if (!ret && fallback) + { + ret = getBestInstanceByDependency!WithComponent(file); + if (ret) + return ret; + foreach (instance; instances) + if (instance.has!WithComponent) + return instance; + } + return ret; + } + + Instance getBestInstance(string file, bool fallback = true) nothrow + { + file = buildNormalizedPath(file); + Instance ret = null; + size_t best; + foreach (instance; instances) + { + if (instance.cwd.length > best && file.startsWith(instance.cwd)) + { + ret = instance; + best = instance.cwd.length; + } + } + if (!ret && fallback && instances.length) + { + ret = getBestInstanceByDependency(file); + if (!ret) + ret = instances[0]; + } + return ret; + } + T get(T)() { auto name = getUDAs!(T, ComponentInfo)[0].name; @@ -559,6 +647,24 @@ class WorkspaceD return inst.has!T; } + T best(T)(string file, bool fallback = true) + { + if (!file.length) + return this.get!T; + auto inst = getBestInstance!T(file); + if (inst is null) + throw new Exception("cwd for '" ~ file ~ "' not found"); + return inst.get!T; + } + + bool hasBest(T)(string cwd, bool fallback = true) + { + auto inst = getBestInstance!T(cwd); + if (inst is null) + return false; + return inst.has!T; + } + Future!JSONValue run(string cwd, string component, string method, JSONValue[] args) { auto instance = getInstance(cwd);