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

Introduce crystal --stdin-filename source flag #4571

Conversation

akzhan
Copy link
Contributor

@akzhan akzhan commented Jun 14, 2017

to compile source from STDIN, closes #4561.

Synopsys

cat src/math/math.cr | ./bin/crystal build --no-codegen --no-color -o /dev/null -f json --stdin-filename src/math/math.cr
[{"file":"src/math/math.cr","line":6,"column":3,"size":2,"message":"already initialized constant Math::PI"}]

@akzhan akzhan changed the title Introduce crystal --fake_source sourcepath flag Introduce crystal --fake-source sourcepath flag Jun 14, 2017
@akzhan akzhan force-pushed the introduce-crystal-compiler-flag---fake-source-source-path branch from 1bd3250 to f911595 Compare June 14, 2017 22:39
if is_fake_source_filename
sources = [Compiler::Source.new(filenames.first, STDIN.gets_to_end)]
else
sources = gather_sources(filenames.not_nil!)
Copy link
Contributor

@Sija Sija Jun 15, 2017

Choose a reason for hiding this comment

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

.not_nil! ain't needed here, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, @Sija. Just refactored code, and it's ok.

@ysbaddaden
Copy link
Contributor

ysbaddaden commented Jun 15, 2017

Bad naming. Without documentation it's impossible to understand what "fake source" means; it's even confusing (why would my source code be fake?).

I'd prefer having - in place of the input filename; it doesn't mean anything by itself (not confusing), and there is history and convention in UNIX tools.

@RX14
Copy link
Contributor

RX14 commented Jun 15, 2017

How about --stdin-filename?

@akzhan
Copy link
Contributor Author

akzhan commented Jun 15, 2017

--stdin-filepath looks good.

@akzhan akzhan force-pushed the introduce-crystal-compiler-flag---fake-source-source-path branch from f911595 to 03476ea Compare June 15, 2017 18:27
@akzhan akzhan changed the title Introduce crystal --fake-source sourcepath flag Introduce crystal --stdin-filepath sourcepath flag Jun 15, 2017
@akzhan
Copy link
Contributor Author

akzhan commented Jun 15, 2017

Code refactored, --stdin-filepath now implemented seamlessly.

@ysbaddaden
Copy link
Contributor

Urgh. Why not just --stdin ?

@RX14
Copy link
Contributor

RX14 commented Jun 18, 2017

@ysbaddaden because it's not very descriptive. I'd assume that --stdin triggers it to read from stdin, and the option would take no arguments. --stdin-filepath is just a lot more descriptive.

@akzhan
Copy link
Contributor Author

akzhan commented Jun 20, 2017

@ysbaddaden This is for on-the-fly linters to check source code buffer. Every source file has its path, and it's important for relative "require". Nothing in practice can be built without knowledge of its path.

@akzhan
Copy link
Contributor Author

akzhan commented Aug 4, 2017

Any chances to get it merged?)

@akzhan
Copy link
Contributor Author

akzhan commented Sep 2, 2017

@RX14 please review this simple feature

@Sija
Copy link
Contributor

Sija commented Sep 9, 2017

@RX14 @asterite @mverzilli 🏓

Copy link
Member

@bcardiff bcardiff left a comment

Choose a reason for hiding this comment

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

This seems to allow combining both options the stding and additional files. I am not sure there is a use case for that and the order of the scripts might not be clear.

Due to this additional feature (combining stdin and actual files) some additional arrays are created. I think it could be refactored to avoid that.

In case the playground is removed from the compiler this function might come handy.

@asterite
Copy link
Member

@bcardiff same thoughts as me. This is why I can't approve this. I think we just need one flag without arguments.

@akzhan
Copy link
Contributor Author

akzhan commented Sep 18, 2017

@asterite use case - try to compile unsaved code in text editor buffer, which is modified version of on-disk src/tools/something.cr.

There is require "./markup", and compiler should know how to resolve ./markup location.

So we need to provide original/proposed source file name.

@asterite
Copy link
Member

@akzhan Yes, but it should be:

crystal file1.cr --stdin

That is, --stdin shouldn't expect an argument. The filename is deduced from the usual filename given to the compiler. This is because the compiler can actually receive multiple files:

crystal file1.cr file2.cr --stdin

In this case only the first filename will be used for the output, so the same should applied for the name for stdin.

@akzhan
Copy link
Contributor Author

akzhan commented Sep 18, 2017

and which filename is used as proposed one for stdin stream in case of

crystal file1.cr file2.cr --stdin

is stdin file named file1.cr or file2.cr?

--stdin-filepath disallows any ambiguity.

@asterite
Copy link
Member

@akzhan The first one. I explain it in my comment. If you compile many files, the first one is used for the output name.

Note that in your PR this is accepted:

crystal file1.cr --stdin-file file2.cr

I know this is less ambiguous because you specify the name with --stdin-file, but since the compiler already allows passing multiple files and takes the output name from the first one, doing the same for --stdin is OK (at least consistent). Plus, I don't think in normal cases you will pass more than one file to the compiler with --stdin.

@akzhan
Copy link
Contributor Author

akzhan commented Sep 18, 2017

@asterite source file name required not for output (usually editor runs compiler without code generation).

it is required to resolve relative require in source code.

It may be used for output too. But trouble is require relative to file name.

@asterite
Copy link
Member

@akzhan What do you mean?

@akzhan
Copy link
Contributor Author

akzhan commented Sep 18, 2017

I always think about compiling of multiple files and stdin. so I want to inform compiler where is stdin file originated.

Your option - always inform compiler that first file is from stdin.
My option - inform compiler that stdin is that concrete file (not first etc.).

But feel free to apply your option.

@asterite
Copy link
Member

@akzhan I agree, both options are similar. Maybe your option is better because it's more explicit.

@RX14 Suggested --stdin-filename but you used --stdin-filepath. I think filename is more common.

… STDIN, closes crystal-lang#4561.

Synopsys:

```bash
cat src/math/math.cr | ./bin/crystal build --no-codegen --no-color -o /dev/null -f
json --stdin-filename src/math/math.cr
```
@akzhan akzhan force-pushed the introduce-crystal-compiler-flag---fake-source-source-path branch from 03476ea to 7085f3c Compare September 18, 2017 14:46
@akzhan
Copy link
Contributor Author

akzhan commented Sep 18, 2017

Renamed to --stdin-filename. Added to man page.

@akzhan akzhan changed the title Introduce crystal --stdin-filepath sourcepath flag Introduce crystal --stdin-filename source flag Sep 19, 2017
@Sija
Copy link
Contributor

Sija commented Sep 21, 2017

Time to 🎲

@asterite asterite merged commit a214c8d into crystal-lang:master Sep 21, 2017
@akzhan akzhan deleted the introduce-crystal-compiler-flag---fake-source-source-path branch September 22, 2017 01:12
@mverzilli mverzilli added this to the Next milestone Sep 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compile source from STDIN
7 participants