Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compiler command crystal clear_cache #13553

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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