Skip to content

Commit

Permalink
fix(commands): handle interrupt from Process earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
devnote-dev committed Jul 26, 2024
1 parent 28cc336 commit 8a86531
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/commands/install.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ module Crimson::Commands
end
{% end %}

installed = false
path = uninitialized Path
archive = uninitialized File

Process.on_interrupt do
if archive
archive.close rescue nil
archive.delete rescue nil
end

FileUtils.rm_rf path if path && !installed
STDERR << "\e[?25h"
exit 1
end

path = ENV::LIBRARY_CRYSTAL / version
puts "Installing Crystal version: #{version}"
verbose { "ensuring directory: #{path}" }
Expand All @@ -65,20 +80,12 @@ module Crimson::Commands
archive = File.open path / "crystal-#{version}-#{ENV::TARGET_IDENTIFIER}", mode: "w"
verbose { "location: #{archive.path}" }

installed = false
source = "https://github.com/crystal-lang/crystal/releases/download/" \
"#{version}/crystal-#{version}-#{ENV::TARGET_IDENTIFIER}"

puts "Downloading sources..."
verbose { source }

at_exit do
archive.close unless archive.closed?
archive.delete rescue nil
FileUtils.rm_rf path unless installed
STDERR << "\e[?25h"
end

Crest.get source do |res|
IO.copy res.body_io, archive
archive.close
Expand Down

0 comments on commit 8a86531

Please sign in to comment.