diff --git a/changelog/stdinForSingleFilePackages.dd b/changelog/stdinForSingleFilePackages.dd new file mode 100644 index 0000000..a9f4b57 --- /dev/null +++ b/changelog/stdinForSingleFilePackages.dd @@ -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` \ No newline at end of file diff --git a/source/dub/commandline.d b/source/dub/commandline.d index 4120e0c..ec1aeff 100644 --- a/source/dub/commandline.d +++ b/source/dub/commandline.d @@ -97,6 +97,16 @@ 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 ..$]; diff --git a/source/dub/internal/utils.d b/source/dub/internal/utils.d index 4eb455a..3e36af7 100644 --- a/source/dub/internal/utils.d +++ b/source/dub/internal/utils.d @@ -40,8 +40,14 @@ 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("-", "_"); + + auto path = getTempDir() ~ fileName; temporary_files ~= path; return path; } diff --git a/test/issue1158-stdin-for-single-files.sh b/test/issue1158-stdin-for-single-files.sh new file mode 100755 index 0000000..1d2baea --- /dev/null +++ b/test/issue1158-stdin-for-single-files.sh @@ -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 \ No newline at end of file diff --git a/test/issue1158-stdin-for-single-files/.no_build b/test/issue1158-stdin-for-single-files/.no_build new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/issue1158-stdin-for-single-files/.no_build diff --git a/test/issue1158-stdin-for-single-files/stdin.d b/test/issue1158-stdin-for-single-files/stdin.d new file mode 100644 index 0000000..120a14a --- /dev/null +++ b/test/issue1158-stdin-for-single-files/stdin.d @@ -0,0 +1,7 @@ +/+ dub.sdl: + name "hello" ++/ +void main(string[] args) { + import std.stdio : writeln; + writeln(args[1..$]); +} \ No newline at end of file