Skip to content

Commit

Permalink
Introduce crystal --stdin-filename source flag to compile source from…
Browse files Browse the repository at this point in the history
… STDIN, closes #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
```
  • Loading branch information
akzhan committed Sep 18, 2017
1 parent 74050a4 commit 7085f3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions man/crystal.1
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ Maximum number of threads to use for code generation. The default is 8 threads.
Enable target triple; intended to use for cross-compilation. See llvm documentation for more information about target triple.
.It Fl -verbose
Display the commands executed by the system.
.It Fl -stdin-filename Ar FILENAME
Source file name to be read from STDIN.
.El

.Pp
Expand Down
15 changes: 13 additions & 2 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ class Crystal::Command
compiler = Compiler.new
compiler.progress_tracker = @progress_tracker
link_flags = [] of String
filenames = [] of String
has_stdin_filename = false
opt_filenames = nil
opt_arguments = nil
opt_output_filename = nil
Expand Down Expand Up @@ -417,6 +419,11 @@ class Crystal::Command
end
end

opts.on("--stdin-filename ", "Source file name to be read from STDIN") do |stdin_filename|
has_stdin_filename = true
filenames << stdin_filename
end

opts.unknown_args do |before, after|
opt_filenames = before
opt_arguments = after
Expand All @@ -426,7 +433,7 @@ class Crystal::Command
compiler.link_flags = link_flags.join(" ") unless link_flags.empty?

output_filename = opt_output_filename
filenames = opt_filenames.not_nil!
filenames += opt_filenames.not_nil!
arguments = opt_arguments.not_nil!

if single_file && filenames.size > 1
Expand All @@ -439,7 +446,11 @@ class Crystal::Command
exit 1
end

sources = gather_sources(filenames)
sources = [] of Compiler::Source
if has_stdin_filename
sources << Compiler::Source.new(filenames.shift, STDIN.gets_to_end)
end
sources += gather_sources(filenames)
first_filename = sources.first.filename
first_file_ext = File.extname(first_filename)
original_output_filename = File.basename(first_filename, first_file_ext)
Expand Down

0 comments on commit 7085f3c

Please sign in to comment.