Skip to content

Commit

Permalink
Merge pull request dlang#1002 from John-Colvin/sourceGlobbing
Browse files Browse the repository at this point in the history
Source globbing
merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Sep 2, 2019
2 parents 00e06a4 + fc0b709 commit e7c1735
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 12 deletions.
53 changes: 41 additions & 12 deletions source/dub/project.d
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ void processVars(ref BuildSettings dst, in Project project, in Package pack,
dst.addDFlags(processVars(project, pack, gsettings, settings.dflags));
dst.addLFlags(processVars(project, pack, gsettings, settings.lflags));
dst.addLibs(processVars(project, pack, gsettings, settings.libs));
dst.addSourceFiles(processVars(project, pack, gsettings, settings.sourceFiles, true));
dst.addSourceFiles(processVars!true(project, pack, gsettings, settings.sourceFiles, true));
dst.addImportFiles(processVars(project, pack, gsettings, settings.importFiles, true));
dst.addStringImportFiles(processVars(project, pack, gsettings, settings.stringImportFiles, true));
dst.addCopyFiles(processVars(project, pack, gsettings, settings.copyFiles, true));
Expand Down Expand Up @@ -1182,29 +1182,58 @@ void processVars(ref BuildSettings dst, in Project project, in Package pack,
}
}

private string[] processVars(in Project project, in Package pack, in GeneratorSettings gsettings, string[] vars, bool are_paths = false)
private string[] processVars(bool glob = false)(in Project project, in Package pack, in GeneratorSettings gsettings, string[] vars, bool are_paths = false)
{
auto ret = appender!(string[])();
processVars(ret, project, pack, gsettings, vars, are_paths);
processVars!glob(ret, project, pack, gsettings, vars, are_paths);
return ret.data;

}
private void processVars(ref Appender!(string[]) dst, in Project project, in Package pack, in GeneratorSettings gsettings, string[] vars, bool are_paths = false)
private void processVars(bool glob = false)(ref Appender!(string[]) dst, in Project project, in Package pack, in GeneratorSettings gsettings, string[] vars, bool are_paths = false)
{
foreach (var; vars) dst.put(processVars(var, project, pack, gsettings, are_paths));
static if (glob)
alias process = processVarsWithGlob!(Project, Package);
else
alias process = processVars!(Project, Package);
foreach (var; vars)
dst.put(process(var, project, pack, gsettings, are_paths));
}

private string processVars(Project, Package)(string var, in Project project, in Package pack, in GeneratorSettings gsettings, bool is_path)
{
var = var.expandVars!(varName => getVariable(varName, project, pack, gsettings));
if (is_path) {
auto p = NativePath(var);
if (!p.absolute) {
return (pack.path ~ p).toNativeString();
} else return p.toNativeString();
} else return var;
if (!is_path)
return var;
auto p = NativePath(var);
if (!p.absolute)
return (pack.path ~ p).toNativeString();
else
return p.toNativeString();
}

private string[] processVarsWithGlob(Project, Package)(string var, in Project project, in Package pack, in GeneratorSettings gsettings, bool is_path)
{
assert(is_path, "can't glob something that isn't a path");
string res = processVars(var, project, pack, gsettings, is_path);
// Find the unglobbed prefix and iterate from there.
size_t i = 0;
size_t sepIdx = 0;
loop: while (i < res.length) {
switch_: switch (res[i])
{
case '*', '?', '[', '{': break loop;
case '/': sepIdx = i; goto default;
default: ++i; break switch_;
}
}
if (i == res.length) //no globbing found in the path
return [res];
import std.path : globMatch;
import std.file : dirEntries, SpanMode;
return dirEntries(res[0 .. sepIdx], SpanMode.depth)
.map!(de => de.name)
.filter!(name => globMatch(name, res))
.array;
}
/// Expand variables using `$VAR_NAME` or `${VAR_NAME}` syntax.
/// `$$` escapes itself and is expanded to a single `$`.
private string expandVars(alias expandVar)(string s)
Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
custom-unittest/custom-unittest
path-subpackage-ref/test
subpackage-ref/test
subpackage-common-with-sourcefile-globbing/mypackage*

/test_registry
8 changes: 8 additions & 0 deletions test/subpackage-common-with-sourcefile-globbing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh
set -e

cd ${CURR_DIR}/subpackage-common-with-sourcefile-globbing
rm -rf .dub dub.selections.json
${DUB} build --compiler=${DC} :server -v
${DUB} build --compiler=${DC} :client -v
${DUB} build --compiler=${DC} :common -v
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import mypackage.client.extra;
import mypackage.common.blah;
void main() { foo(); blah(); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module mypackage.client.extra;
void foo() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module mypackage.common.blah;
void blah() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import mypackage.server.extra;
import mypackage.common.blah;
void main() { foo(); blah(); }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module mypackage.server.extra;
void foo() {}
19 changes: 19 additions & 0 deletions test/subpackage-common-with-sourcefile-globbing/dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name "mypackage"
targetType "none"
subPackage {
name "server"
sourceFiles "code/mypackage/[sc][oe]*/*.d"
targetType "executable"
}
subPackage {
name "client"
sourceFiles "code/mypackage/client/*.d"
targetType "executable"
dependency "mypackage:common" version="*"
}
subPackage {
name "common"
sourceFiles "code/mypackage/common/*.d"
importPaths "code"
targetType "library"
}

0 comments on commit e7c1735

Please sign in to comment.