Class: VsClean::Command

Inherits:
CLAide::Command
  • Object
show all
Defined in:
lib/commands/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



19
20
21
22
23
# File 'lib/commands/command.rb', line 19

def initialize(argv)
  @dryrun = argv.flag?("dry-run")
  @global = argv.flag?("global")
  super
end

Class Method Details

.optionsObject



12
13
14
15
16
17
# File 'lib/commands/command.rb', line 12

def self.options
  [
    ['[--global]', 'Delete global caches and temporary files'],
    ['[--dry-run]', 'Simulate deletion (list all files and directories that would be deleted)']
  ].concat(super)
end

Instance Method Details

#collect_global_pathsObject



38
39
40
41
42
43
# File 'lib/commands/command.rb', line 38

def collect_global_paths
  home = File.expand_path("~")
  paths = Dir.glob(home + "/AppData/Local/JetBrains/**/SolutionCaches").select { |f| File.directory?(f) }
  paths.push(home + "/AppData/Microsoft/WebsiteCache")
  paths.push(*Dir.glob(home + "/AppData/Local/Microsoft/**/ComponentModelCache"))
end

#collect_local_pathsObject



45
46
47
48
49
50
51
# File 'lib/commands/command.rb', line 45

def collect_local_paths
  # bin and obj directories

  paths = Dir.glob("**/{bin,obj}").select { |f| File.directory?(f) }
  
  # .suo files (can cause Intellisense errors, solution load issues and more)

  paths.push(*Dir.glob("**/.vs/**/.suo"))
end

#delete(path) ⇒ Object



57
58
59
60
61
62
# File 'lib/commands/command.rb', line 57

def delete(path)
  FileUtils.rm_r(path)
  Console.log_substep("Deleted '#{path}'")
rescue StandardError => e
  Console.log_error("Could not delete '#{path}': #{e.message}")
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/commands/command.rb', line 25

def run
  paths = @global ? collect_global_paths : collect_local_paths
  
  if paths.none?
    Console.log_step("All good... nothing to clean!")
    return
  end
    
  Console.log_step("Cleaning...") 
  paths.each { |d| @dryrun ? simulate_delete(d) : delete(d) }
  Console.log_step("Done!")
end

#simulate_delete(path) ⇒ Object



53
54
55
# File 'lib/commands/command.rb', line 53

def simulate_delete(path)
  Console.log_substep("Would delete '#{path}'")
end