Skip to content

Commit

Permalink
Support directory name in --output CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Feb 13, 2025
1 parent 8e02a42 commit 592d771
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion man/crystal.1
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Disable colored output.
.It Fl -no-codegen
Don't do code generation, just parse the file.
.It Fl o
Specify filename of output.
Specify output path. If a directory, the filename is derived from the first source file (default: ./)
.It Fl -prelude
Specify prelude to use. The default one initializes the garbage collector. You can also use --prelude=empty to use no preludes. This can be useful for checking code generation for a specific source code file.
.It Fl O Ar LEVEL
Expand Down
19 changes: 11 additions & 8 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ class Crystal::Command
opts.on("--no-codegen", "Don't do code generation") do
compiler.no_codegen = true
end
opts.on("-o ", "Output filename") do |an_output_filename|
opts.on("-o ", "Output path. If a directory, the filename is derived from the first source file (default: ./)") do |an_output_filename|
opt_output_filename = an_output_filename
specified_output = true
end
Expand Down Expand Up @@ -583,7 +583,6 @@ class Crystal::Command

compiler.link_flags = link_flags.join(' ') unless link_flags.empty?

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

Expand All @@ -604,18 +603,22 @@ class Crystal::Command
sources.concat gather_sources(filenames)

output_extension = compiler.cross_compile? ? compiler.codegen_target.object_extension : compiler.codegen_target.executable_extension
if output_filename
if File.extname(output_filename).empty?
output_filename += output_extension
end
else

output_path = ::Path[opt_output_filename || "./"]

Check failure on line 607 in src/compiler/crystal/command.cr

View workflow job for this annotation

GitHub Actions / x86_64-windows-release / build

expected argument #1 to 'Path.[]' to be Enumerable(T), not (String | Nil)

if output_path.ends_with_separator? || File.directory?(output_path)
first_filename = sources.first.filename
output_filename = "#{::Path[first_filename].stem}#{output_extension}"
output_filename = (output_path / "#{::Path[first_filename].stem}#{output_extension}").normalize.to_s

# Check if we'll overwrite the main source file
if !compiler.no_codegen? && !run && first_filename == File.expand_path(output_filename)
error "compilation will overwrite source file '#{Crystal.relative_filename(first_filename)}', either change its extension to '.cr' or specify an output file with '-o'"
end
else
output_filename = output_path.to_s
if output_path.extension.empty?
output_filename += output_extension
end
end

output_format ||= allowed_formats[0]
Expand Down

0 comments on commit 592d771

Please sign in to comment.