Skip to content

Commit cf15fb2

Browse files
authored
Adds initial support for external commands (#14953)
1 parent 025f3e0 commit cf15fb2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{% skip_file if flag?(:interpreted) %}
2+
3+
require "../spec_helper"
4+
5+
describe Crystal::Command do
6+
it "exec external commands", tags: %w[slow] do
7+
with_temp_executable "crystal-external" do |path|
8+
with_tempfile "crystal-external.cr" do |source_file|
9+
File.write source_file, <<-CRYSTAL
10+
puts ENV["CRYSTAL"]?
11+
puts PROGRAM_NAME
12+
puts ARGV
13+
CRYSTAL
14+
15+
Process.run(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal", ["build", source_file, "-o", path])
16+
end
17+
18+
File.exists?(path).should be_true
19+
20+
process = Process.new(ENV["CRYSTAL_SPEC_COMPILER_BIN"]? || "bin/crystal",
21+
["external", "foo", "bar"],
22+
output: :pipe,
23+
env: {"PATH" => {ENV["PATH"], File.dirname(path)}.join(Process::PATH_DELIMITER)}
24+
)
25+
output = process.output.gets_to_end
26+
status = process.wait
27+
status.success?.should be_true
28+
lines = output.lines
29+
lines[0].should match /crystal/
30+
lines[1].should match /crystal-external/
31+
lines[2].should eq %(["foo", "bar"])
32+
end
33+
end
34+
end

src/compiler/crystal/command.cr

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ class Crystal::Command
130130
else
131131
if command.ends_with?(".cr")
132132
error "file '#{command}' does not exist"
133+
elsif external_command = Process.find_executable("crystal-#{command}")
134+
options.shift
135+
Process.exec(external_command, options, env: {"CRYSTAL" => Process.executable_path})
133136
else
134137
error "unknown command: #{command}"
135138
end

0 commit comments

Comments
 (0)