Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/stdinForSingleFilePackages.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DUB accepts single file packages on STDIN

You can pass single file packages to dub on STDIN using dash as first argument to DUB.
All arguments after dash will be passed as runtime arguments to the application.

Example `cat app.d | dub - --foo=bar`
10 changes: 10 additions & 0 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ int runDubCommandLine(string[] args)
environment["TEMP"] = environment["TEMP"].replace("/", "\\");
}

// special stdin syntax
if (args.length >= 2 && args[1] == "-")
{
import dub.internal.utils: getTempFile;

auto path = getTempFile("app", ".d");
stdin.byChunk(4096).joiner.toFile(path.toNativeString());
args = args[0] ~ [path.toNativeString()] ~ args[2..$];
}

// special single-file package shebang syntax
if (args.length >= 2 && args[1].endsWith(".d")) {
args = args[0] ~ ["run", "-q", "--temp-build", "--single", args[1], "--"] ~ args[2 ..$];
Expand Down
8 changes: 7 additions & 1 deletion source/dub/internal/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@ NativePath getTempDir()
NativePath getTempFile(string prefix, string extension = null)
{
import std.uuid : randomUUID;
import std.array: replace;

auto path = getTempDir() ~ (prefix ~ "-" ~ randomUUID.toString() ~ extension);
string fileName = prefix ~ "-" ~ randomUUID.toString() ~ extension;

if (extension !is null && extension == ".d")
fileName = fileName.replace("-", "_");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe directly use _ above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

randomUUID also returns a string containing dashes. Therefore my idea was to keep the dash and replaces all dashes in case of a d module file.


auto path = getTempDir() ~ fileName;
temporary_files ~= path;
return path;
}
Expand Down
9 changes: 9 additions & 0 deletions test/issue1158-stdin-for-single-files.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

. $(dirname "${BASH_SOURCE[0]}")/common.sh

cd ${CURR_DIR}/issue1158-stdin-for-single-files

if ! { cat stdin.d | ${DUB} - --value=v 2>&1 || true; } | grep -cF '["--value=v"]'; then
die $LINENO 'Stdin for single files failed.'
fi
Empty file.
7 changes: 7 additions & 0 deletions test/issue1158-stdin-for-single-files/stdin.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/+ dub.sdl:
name "hello"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is optional since 1.6

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By removing name attribute, the file not longer compiles. I tried different formats:

/+ dub.sdl:
+/
/+ dub.sdl
+/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry. Apparently one items is needed then :/

+/
void main(string[] args) {
import std.stdio : writeln;
writeln(args[1..$]);
}