Skip to content

Commit

Permalink
Add compiler command crystal clear_cache (#13553)
Browse files Browse the repository at this point in the history
Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
  • Loading branch information
baseballlover723 and Sija committed Jun 19, 2023
1 parent 33a74d1 commit a12dcf8
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
37 changes: 37 additions & 0 deletions spec/compiler/crystal/commands/clear_cache_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "../../../spec_helper"

class Crystal::CacheDir
class_setter instance

def initialize(@dir)
Dir.mkdir_p(dir)
end
end

describe Crystal::Command do
describe "clear_cache" do
around_each do |example|
old_cache_dir = CacheDir.instance
temp_dir_name = File.tempname
begin
CacheDir.instance = CacheDir.new(temp_dir_name)
example.run
ensure
FileUtils.rm_rf(temp_dir_name)
CacheDir.instance = old_cache_dir
end
end

it "clears any cached compiler files" do
file_path = File.tempname(dir: CacheDir.instance.dir)
Dir.mkdir_p(File.dirname(file_path))
File.touch(file_path)
File.exists?(file_path).should be_true

Crystal::Command.run(["clear_cache"])

File.exists?(file_path).should be_false
File.exists?(CacheDir.instance.dir).should be_false
end
end
end
4 changes: 4 additions & 0 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Crystal::Command
Command:
init generate a new project
build build an executable
clear_cache clear the compiler cache
docs generate documentation
env print Crystal environment information
eval eval code from args or standard input
Expand Down Expand Up @@ -112,6 +113,9 @@ class Crystal::Command
when "tool".starts_with?(command)
options.shift
tool
when command == "clear_cache"
options.shift
clear_cache
when "help".starts_with?(command), "--help" == command, "-h" == command
puts USAGE
exit
Expand Down
27 changes: 27 additions & 0 deletions src/compiler/crystal/command/clear_cache.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Implementation of the `crystal clear_cache` command

class Crystal::Command
private def clear_cache
verbose = false
OptionParser.parse(@options) do |opts|
opts.banner = <<-'BANNER'
Usage: crystal clear_cache

Clears the compiler cache

Options:
BANNER

opts.on("-h", "--help", "Show this message") do
puts opts
exit
end

opts.on("-v", "--verbose", "Display detailed information") do
verbose = true
end
end
puts "Clearing compiler cache at #{CacheDir.instance.dir.inspect}" if verbose
FileUtils.rm_rf(CacheDir.instance.dir)
end
end

0 comments on commit a12dcf8

Please sign in to comment.