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.



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

def initialize(argv)
  @dryrun = argv.flag?("dry-run")
  
  @mode = Mode::LOCAL if argv.flag?("local", true)
  @mode = Mode::GLOBAL if argv.flag?("global")
  @mode = Mode::FULL if argv.flag?("full")
  
  super
end

Class Method Details

.optionsObject



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

def self.options
  [
    ['[--local]', '(DEFAULT) Delete caches and temporary files in the current directory'],
    ['[--global]', 'Delete global caches and temporary files'],
    ['[--full]', 'Delete local *and* global temporary files'],
    ['[--dry-run]', 'Simulate deletion (list all files and directories that would be deleted)']
  ].concat(super)
end

Instance Method Details

#collect_global_pathsObject



54
55
56
57
58
59
# File 'lib/commands/command.rb', line 54

def collect_global_paths
  home = File.expand_path("~")
  paths = Dir.glob(home + "/AppData/Microsoft/WebsiteCache")
  paths.push(*Dir.glob(home + "/AppData/Local/Microsoft/**/ComponentModelCache"))
  paths.push(*Dir.glob(home + "/AppData/Local/JetBrains/**/SolutionCaches"))
end

#collect_local_pathsObject



61
62
63
64
65
66
67
# File 'lib/commands/command.rb', line 61

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



73
74
75
76
77
78
# File 'lib/commands/command.rb', line 73

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/commands/command.rb', line 37

def run
  paths = case @mode
  when Mode::LOCAL; collect_local_paths
  when Mode::GLOBAL; collect_global_paths
  when Mode::FULL; collect_global_paths.push(*collect_local_paths)
  end
  
  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



69
70
71
# File 'lib/commands/command.rb', line 69

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