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

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