class Toys::Templates::Clean

A template for tools that clean build artifacts

Constants

DEFAULT_TOOL_NAME

Default tool name @return [String]

Attributes

context_directory[RW]

Custom context directory for this tool.

@param value [String] @return [String]

name[W]

Name of the tool to create.

@param value [String] @return [String]

paths[W]

An array of glob patterns indicating what to clean.

@param value [Array<String>] @return [Array<String>]

Public Class Methods

new(name: nil, paths: [], context_directory: nil) click to toggle source

Create the template settings for the Clean template.

@param name [String] Name of the tool to create. Defaults to

{DEFAULT_TOOL_NAME}.

@param paths [Array<String>] An array of glob patterns indicating what

to clean. You can also include the symbol `:gitignore` which will
clean all items covered by `.gitignore` files, if contained in a
git working tree.

@param context_directory [String] A custom context directory to use

when executing this tool.
# File lib/toys/templates/clean.rb, line 29
def initialize(name: nil, paths: [], context_directory: nil)
  @name = name
  @paths = paths
  @context_directory = context_directory
end

Public Instance Methods

clean_gitignore() click to toggle source

@private

# File lib/toys/templates/clean.rb, line 100
def clean_gitignore
  result = exec(["git", "rev-parse", "--is-inside-work-tree"], out: :null, err: :null)
  unless result.success?
    logger.error("Skipping :gitignore because we don't seem to be in a git directory")
    return
  end
  clean_gitignore_dir(".")
end
clean_gitignore_dir(dir) click to toggle source

@private

# File lib/toys/templates/clean.rb, line 110
def clean_gitignore_dir(dir)
  children = dir_children(dir)
  result = exec(["git", "check-ignore", "--stdin"],
                in: :controller, out: :capture) do |controller|
    children.each { |child| controller.in.puts(child) }
  end
  result.captured_out.split("\n").each { |path| clean_path(path) }
  children = dir_children(dir) if result.success?
  children.each { |child| clean_gitignore_dir(child) if ::File.directory?(child) }
end
clean_path(path) click to toggle source

@private

# File lib/toys/templates/clean.rb, line 135
def clean_path(path)
  if ::File.exist?(path)
    rm_rf(path)
    puts "Cleaned: #{path}"
  end
end
clean_pattern(pattern) click to toggle source

@private

# File lib/toys/templates/clean.rb, line 130
def clean_pattern(pattern)
  ::Dir.glob(pattern) { |path| clean_path(path) }
end
dir_children(dir) click to toggle source

@private

# File lib/toys/templates/clean.rb, line 122
def dir_children(dir)
  ::Dir.entries(dir)
       .reject { |entry| entry =~ /^\.\.?$/ }
       .sort
       .map { |entry| ::File.join(dir, entry) }
end
name() click to toggle source

@private

# File lib/toys/templates/clean.rb, line 68
def name
  @name || DEFAULT_TOOL_NAME
end
paths() click to toggle source

@private

# File lib/toys/templates/clean.rb, line 63
def paths
  Array(@paths)
end
run() click to toggle source

@private

# File lib/toys/templates/clean.rb, line 84
def run
  cd(context_directory || ::Dir.getwd) do
    template_paths.each do |elem|
      case elem
      when :gitignore
        clean_gitignore
      when ::String
        clean_pattern(elem)
      else
        raise "Unknown path in clean: #{elem.inspect}"
      end
    end
  end
end