Skip to content

Commit 3553f8c

Browse files
authored
Can now run crystal scripts as uncompiled lucky tasks (#871)
1 parent c46ac34 commit 3553f8c

File tree

4 files changed

+45
-17
lines changed

4 files changed

+45
-17
lines changed

Earthfile

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ integration-specs:
5858
FROM +base-image
5959
COPY +build-lucky/lucky /usr/bin/lucky
6060
COPY fixtures/hello_world.cr fixtures/
61+
COPY fixtures/hello_crystal.cr bin/lucky.hello_crystal.cr
6162
COPY fixtures/tasks.cr fixtures/
6263
RUN shards build lucky.hello_world --without-development
6364
RUN crystal spec --tag integration

fixtures/hello_crystal.cr

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
puts "Hello, Crystal!"

spec/integration/lucky_cli_spec.cr

+20-9
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,21 @@ describe "Lucky CLI", tags: "integration" do
99
shell: true,
1010
output: io
1111
)
12-
status.exit_status.should eq(0)
12+
status.exit_code.should eq(0)
1313
io.to_s.should eq("Hello World!\n")
1414
end
1515

16+
it "runs non-compiled tasks" do
17+
io = IO::Memory.new
18+
status = run_lucky(
19+
args: %w[hello_crystal],
20+
shell: true,
21+
output: io
22+
)
23+
status.exit_code.should eq(0)
24+
io.to_s.should eq("Hello, Crystal!\n")
25+
end
26+
1627
it "allows tasks to accept input from STDIN" do
1728
io = IO::Memory.new
1829
run_lucky(
@@ -32,7 +43,7 @@ describe "Lucky CLI", tags: "integration" do
3243
it "returns the lucky CLI help message when passing the -h flag" do
3344
io = IO::Memory.new
3445
status = run_lucky(args: %w[-h], shell: true, output: io)
35-
status.exit_status.should eq(0)
46+
status.exit_code.should eq(0)
3647
io.to_s.should contain("Usage: lucky [command]")
3748
end
3849

@@ -47,7 +58,7 @@ describe "Lucky CLI", tags: "integration" do
4758
"LUCKY_TASKS_FILE" => fixtures_tasks_path.to_s,
4859
}
4960
)
50-
status.exit_status.should eq(0)
61+
status.exit_code.should eq(0)
5162
io.to_s.should contain("Usage: lucky tasks")
5263
end
5364

@@ -61,7 +72,7 @@ describe "Lucky CLI", tags: "integration" do
6172
"LUCKY_TASKS_FILE" => fixtures_tasks_path.to_s,
6273
}
6374
)
64-
status.exit_status.should eq(0)
75+
status.exit_code.should eq(0)
6576
io.to_s.should contain("Usage: lucky dev")
6677
end
6778
end
@@ -78,7 +89,7 @@ describe "Lucky CLI", tags: "integration" do
7889
"LUCKY_TASKS_FILE" => fixtures_tasks_path.to_s,
7990
}
8091
)
81-
status.exit_status.should eq(0)
92+
status.exit_code.should eq(0)
8293
io.to_s.should contain("Custom help message")
8394
end
8495
end
@@ -99,7 +110,7 @@ describe "Lucky CLI", tags: "integration" do
99110
args: %w[init.custom test-project --dir my-project],
100111
shell: true,
101112
)
102-
status.exit_status.should eq(0)
113+
status.exit_code.should eq(0)
103114
Dir.cd("my-project/test-project") do
104115
File.read("src/shards.cr").should contain("lucky")
105116
end
@@ -113,7 +124,7 @@ describe "Lucky CLI", tags: "integration" do
113124
shell: true,
114125
output: io
115126
)
116-
status.exit_status.should eq(0)
127+
status.exit_code.should eq(0)
117128
io.to_s.should contain("Folder named test-project already exists, please use a different name")
118129
end
119130

@@ -124,7 +135,7 @@ describe "Lucky CLI", tags: "integration" do
124135
shell: true,
125136
output: io
126137
)
127-
status.exit_status.should_not eq(0)
138+
status.exit_code.should_not eq(0)
128139
io.to_s.should contain("Project name should only contain lowercase letters, numbers, underscores, and dashes.")
129140
end
130141

@@ -135,7 +146,7 @@ describe "Lucky CLI", tags: "integration" do
135146
shell: true,
136147
output: io
137148
)
138-
status.exit_status.should_not eq(0)
149+
status.exit_code.should_not eq(0)
139150
io.to_s.should contain("Projects cannot be named app, app_database, app_server, shards, start_server.")
140151
end
141152
end

src/lucky.cr

+23-8
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,29 @@ require "./build_and_run_task"
1010
include LuckyTask::TextHelpers
1111

1212
args = ARGV.join(" ")
13-
tasks_file = ENV.fetch("LUCKY_TASKS_FILE", "./tasks.cr")
13+
tasks_file = ENV.fetch("LUCKY_TASKS_FILE", Path["./tasks.cr"].to_s)
1414
inside_app_directory = File.file?(tasks_file)
1515

1616
private def task_name : String?
1717
ARGV.first?
1818
end
1919

20-
private def task_precompiled? : Bool
21-
path = precompiled_task_path
22-
!path.nil? && File.file?(path)
20+
private def task_compiled? : Bool
21+
path = task_path
22+
!path.nil? && {% if compare_versions(Crystal::VERSION, "1.13.0") >= 0 %}File::Info.executable?(path){% else %}File.executable?(path){% end %}
2323
end
2424

25-
private def precompiled_task_path : String?
25+
private def task_path(ext : String = "") : Path?
2626
task_name.try do |name|
27-
"bin/lucky.#{name}"
27+
Path.new("bin", "lucky.#{name}#{ext}")
2828
end
2929
end
3030

31+
private def task_not_compiled? : Bool
32+
path = task_path(".cr")
33+
!path.nil? && File.file?(path)
34+
end
35+
3136
private def built_in_commands : Array(String)
3237
["dev", "tasks", "init", "init.custom"]
3338
end
@@ -148,15 +153,25 @@ elsif start_wizard
148153
LuckyCli::Init.run
149154
elsif generate_custom_app
150155
# already running
151-
elsif task_precompiled?
156+
elsif task_compiled?
152157
exit Process.run(
153-
precompiled_task_path.to_s,
158+
task_path.to_s,
154159
ARGV.skip(1),
155160
shell: true,
156161
input: STDIN,
157162
output: STDOUT,
158163
error: STDERR
159164
).exit_code
165+
elsif task_not_compiled?
166+
crystal_command = {% if flag?(:windows) %}"crystal.exe"{% else %}"crystal"{% end %}
167+
exit Process.run(
168+
crystal_command,
169+
[task_path(".cr").to_s] + ARGV.skip(1),
170+
shell: true,
171+
input: STDIN,
172+
output: STDOUT,
173+
error: STDERR
174+
).exit_code
160175
elsif inside_app_directory
161176
# Run task from tasks.cr file since this task is not precompiled
162177
LuckyCli::BuildAndRunTask.call(tasks_file, args)

0 commit comments

Comments
 (0)