Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue1158-stdin-for-single-files #1516

Merged
merged 1 commit into from
Aug 13, 2018
Merged

issue1158-stdin-for-single-files #1516

merged 1 commit into from
Aug 13, 2018

Conversation

andre2007
Copy link
Contributor

This pull request allows running single source code files from stdin.

Example:
curl -s http://localhost:8080/singledub.d | dub -

The - (dash) is the marker for reading stdin.

@dlang-bot
Copy link
Collaborator

Thanks for your pull request and interest in making D better, @andre2007! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the annotated coverage diff directly on GitHub with CodeCov's browser extension
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

@@ -40,8 +40,14 @@ NativePath getTempDir()
NativePath getTempFile(string prefix, string extension = null)
{
import std.uuid : randomUUID;
import std.array: replace;
Copy link
Member

Choose a reason for hiding this comment

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

I think the existing file uses tabs whereas you use whitespace here. We should probably just convert all files to whitespace at some point...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@@ -0,0 +1,7 @@
/+ dub.sdl:
name "hello"
Copy link
Member

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
Member

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 :/

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

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

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.

@wilzbach
Copy link
Member

Maybe a short changelog entry so that people know about this?

@andre2007
Copy link
Contributor Author

Change log added

import dub.internal.utils: getTempFile;

auto path = getTempFile("app", ".d");
std.file.write(path.toNativeString(), stdin.byLineCopy.join);
Copy link
Member

Choose a reason for hiding this comment

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

FYI if you don't want the the FQSN (std.file), there exists toFile just for this reason:

stdin.byChunk(4096).joiner.toFile(path.toNativeString())

Also use byChunk instead of byLineCopy to avoid unnecessary allocation + copying and as a bonus you don't loose the line endings.
Furthermore with joiner you avoid allocations :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

great! looks much better now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

DMD 2.071.2 and lower does not support toFile ):


auto path = getTempFile("app", ".d");
std.file.write(path.toNativeString(), stdin.byLineCopy.join);
args = args[0]~[path.toNativeString()]~args[2..$];
Copy link
Member

Choose a reason for hiding this comment

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

No need for [] here.
Also mind the style of the existing file (and DStyle): in other words spaces around ~.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Square brackets are needed.
Args without []: ["C:\Users\home\git\dub\bin\dub.exeC:\Users\home\Desktop\abc.d"]
Args with []: ["C:\Users\home\git\dub\bin\dub.exe", "C:\Users\home\Desktop\abc.d"]

@andre2007
Copy link
Contributor Author

@wilzbach Unfortunately I had to switch back to std.file.write as toFile isn't available in the lower DMD/Phobos releases. The write function also had some issue with the joiner result, I had to add .array.

Will we stay on 2.068.2 as lowest version to be supported or is there a plan to remove some of the oldest versions?

@wilzbach
Copy link
Member

The latest GDC release has been the blocking reason for an upgrade.
The newest GDC release is at 2.076 (master is at 2.082).

I can make that upgrade tomorrow.

@andre2007
Copy link
Contributor Author

@wilzbach did you already had the chance to make the upgrade (GDC was blocking)?

@wilzbach
Copy link
Member

Not yet, but done now #1520 (might require a few tries of experimentation to get it right tough, was a bit more complex than I remembered).

So if you don't want to depend/wait on this, feel free to revert here for now ;-)
(I don't mind including the "new" toFile and other changes in my PR - actually a good check that the upgrade worked.)

@andre2007
Copy link
Contributor Author

Yes I really like your proposed syntax (toFile). I will revert to your proposal when the merge was done.

@andre2007
Copy link
Contributor Author

It seems there is a general error in semaphoreci. The gdc build fails with:
-bash: ./semaphore-ci.sh: No such file or directory

Also the dmd build fails, here no clear error description can be found.

https://semaphoreci.com/wilzbach/dub/branches/pull-request-1516/builds/7

@wilzbach
Copy link
Member

wilzbach commented Aug 7, 2018

Yes, we need to merge #1520 first (I changed the scripts while I was setting up the newer gdc compiler.)
I have been waiting on the approval from Martin & Sönke on this one, which I got over the weekend, so we can finally move ahead with the upgrade.

@andre2007
Copy link
Contributor Author

@wilzbach I changed the coding to your proposal (stdin.byChunk(4096).joiner.toFile(path.toNativeString());) and it works fine. Except some dub packages throwing errors. But I assume this is a general issue.

@wilzbach wilzbach merged commit 520d527 into dlang:master Aug 13, 2018
@andre2007 andre2007 deleted the fix_issue_1158 branch November 13, 2018 20:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants