Skip to content
This repository has been archived by the owner on Mar 1, 2022. It is now read-only.

Commit

Permalink
add getBestInstance and getBestInstanceByDependency
Browse files Browse the repository at this point in the history
analogous to the serve-d workspace methods, just for
usage with workspace-d components
  • Loading branch information
WebFreak001 committed Feb 7, 2019
1 parent ed578e3 commit 6ba1f4a
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions source/workspaced/api.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import core.time;
import dparse.lexer;
import painlessjson;
import standardpaths;

import std.algorithm;
import std.conv;
import std.exception;
import std.file;
import std.json;
import std.meta;
import std.path;
import std.range;
import std.regex;
import std.traits;
import std.typecons;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6ba1f4a

Please sign in to comment.