From b32a2c9b87b77cda1bdf1bd5d58f08a9dead97b4 Mon Sep 17 00:00:00 2001 From: Philip Ross Date: Fri, 9 Jun 2023 18:39:21 -0700 Subject: [PATCH 1/7] added a compiler command to clear the compiler cache --- src/compiler/crystal/command.cr | 4 ++++ src/compiler/crystal/command/clear_cache.cr | 22 +++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/compiler/crystal/command/clear_cache.cr diff --git a/src/compiler/crystal/command.cr b/src/compiler/crystal/command.cr index 273554728f66..2836cbdda88f 100644 --- a/src/compiler/crystal/command.cr +++ b/src/compiler/crystal/command.cr @@ -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 @@ -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 diff --git a/src/compiler/crystal/command/clear_cache.cr b/src/compiler/crystal/command/clear_cache.cr new file mode 100644 index 000000000000..fca3f272a934 --- /dev/null +++ b/src/compiler/crystal/command/clear_cache.cr @@ -0,0 +1,22 @@ +# Implementation of the `crystal clear_cache` command + +class Crystal::Command + private def clear_cache + 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 + end + puts "clearing compiler cache at \"#{CacheDir.instance.dir}\"" + FileUtils.rm_rf(CacheDir.instance.dir) + end +end From 929ab9d04e6b86cccc5a5b9232241a80306f943c Mon Sep 17 00:00:00 2001 From: Philip Ross Date: Wed, 14 Jun 2023 14:49:38 -0700 Subject: [PATCH 2/7] added verbose flag --- src/compiler/crystal/command/clear_cache.cr | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/compiler/crystal/command/clear_cache.cr b/src/compiler/crystal/command/clear_cache.cr index fca3f272a934..6e929bff83f9 100644 --- a/src/compiler/crystal/command/clear_cache.cr +++ b/src/compiler/crystal/command/clear_cache.cr @@ -2,6 +2,7 @@ class Crystal::Command private def clear_cache + verbose = false OptionParser.parse(@options) do |opts| opts.banner = <<-'BANNER' Usage: crystal clear_cache @@ -15,8 +16,13 @@ class Crystal::Command puts opts exit end + + opts.on("-v", "--verbose", "Display detailed information") do + verbose = true + end + end - puts "clearing compiler cache at \"#{CacheDir.instance.dir}\"" + puts "clearing compiler cache at \"#{CacheDir.instance.dir}\"" if verbose FileUtils.rm_rf(CacheDir.instance.dir) end end From 442e1c89f36cf95d7603e3e5e104c163bcc47496 Mon Sep 17 00:00:00 2001 From: Philip Ross Date: Wed, 14 Jun 2023 16:22:24 -0700 Subject: [PATCH 3/7] added a basic spec for clearing the compiler cache --- .../crystal/commands/clear_cache_spec.cr | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 spec/compiler/crystal/commands/clear_cache_spec.cr diff --git a/spec/compiler/crystal/commands/clear_cache_spec.cr b/spec/compiler/crystal/commands/clear_cache_spec.cr new file mode 100644 index 000000000000..95a78408e784 --- /dev/null +++ b/spec/compiler/crystal/commands/clear_cache_spec.cr @@ -0,0 +1,16 @@ +require "../../../spec_helper" + +describe Crystal::Command do + describe "clear_cache" do + it "clears any cached compiler files" do + file_path = File.join(CacheDir.instance.dir, "test_cache_file") + 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 + end + end +end From 38cde89ca78be8d444455093b54ca54fe8ae0794 Mon Sep 17 00:00:00 2001 From: Philip Ross Date: Wed, 14 Jun 2023 16:56:00 -0700 Subject: [PATCH 4/7] Update src/compiler/crystal/command/clear_cache.cr Co-authored-by: Sijawusz Pur Rahnama --- src/compiler/crystal/command/clear_cache.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/crystal/command/clear_cache.cr b/src/compiler/crystal/command/clear_cache.cr index 6e929bff83f9..b9409ce565ae 100644 --- a/src/compiler/crystal/command/clear_cache.cr +++ b/src/compiler/crystal/command/clear_cache.cr @@ -22,7 +22,7 @@ class Crystal::Command end end - puts "clearing compiler cache at \"#{CacheDir.instance.dir}\"" if verbose + puts "Clearing compiler cache at #{CacheDir.instance.dir.inspect}" if verbose FileUtils.rm_rf(CacheDir.instance.dir) end end From 4c3e2728827ffd5e824e90d05f4a6c486ad0ee8e Mon Sep 17 00:00:00 2001 From: Philip Ross Date: Wed, 14 Jun 2023 22:42:03 -0700 Subject: [PATCH 5/7] formatting --- src/compiler/crystal/command/clear_cache.cr | 1 - 1 file changed, 1 deletion(-) diff --git a/src/compiler/crystal/command/clear_cache.cr b/src/compiler/crystal/command/clear_cache.cr index b9409ce565ae..c2eadb7e77e8 100644 --- a/src/compiler/crystal/command/clear_cache.cr +++ b/src/compiler/crystal/command/clear_cache.cr @@ -20,7 +20,6 @@ class Crystal::Command 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) From 5f992db32a942cd0283f74f9be2ad48f2cec2dcd Mon Sep 17 00:00:00 2001 From: Philip Ross Date: Wed, 14 Jun 2023 23:13:11 -0700 Subject: [PATCH 6/7] use tempname and assert that the CacheDir doesn't exist --- spec/compiler/crystal/commands/clear_cache_spec.cr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/compiler/crystal/commands/clear_cache_spec.cr b/spec/compiler/crystal/commands/clear_cache_spec.cr index 95a78408e784..9bab6050a154 100644 --- a/spec/compiler/crystal/commands/clear_cache_spec.cr +++ b/spec/compiler/crystal/commands/clear_cache_spec.cr @@ -3,7 +3,7 @@ require "../../../spec_helper" describe Crystal::Command do describe "clear_cache" do it "clears any cached compiler files" do - file_path = File.join(CacheDir.instance.dir, "test_cache_file") + 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 @@ -11,6 +11,7 @@ describe Crystal::Command do Crystal::Command.run(["clear_cache"]) File.exists?(file_path).should be_false + File.exists?(CacheDir.instance.dir).should be_false end end end From 4023afb470352a6bc6e4bf6484be80a8969b2e44 Mon Sep 17 00:00:00 2001 From: Philip Ross Date: Wed, 14 Jun 2023 23:13:58 -0700 Subject: [PATCH 7/7] use a seperate CacheDir for testing clear_cache --- .../crystal/commands/clear_cache_spec.cr | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/spec/compiler/crystal/commands/clear_cache_spec.cr b/spec/compiler/crystal/commands/clear_cache_spec.cr index 9bab6050a154..f6f9180719c4 100644 --- a/spec/compiler/crystal/commands/clear_cache_spec.cr +++ b/spec/compiler/crystal/commands/clear_cache_spec.cr @@ -1,7 +1,27 @@ 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))