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

Added destroy method (Issue #8) #11

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/lib/file_tree.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,26 @@ module Teeplate
renderer
end

# Destroy the rendered files.
def destroy(out_dir, force : Bool = false, interactive : Bool = false, interact : Bool = false, list : Bool = false, color : Bool = false, per_entry : Bool = false, quit : Bool = true)
renderer = Renderer.new(out_dir, force: force, interact: interactive || interact, list: list, color: color, per_entry: per_entry, quit: quit)
renderer << destroy_file_entries
renderer.destroy
renderer
end

# Returns file entries to be rendered.
#
# This method just returns the `#file_entries` method's result. To filter entries, override this method.
def rendered_file_entries
file_entries
end

# :nodoc:
def destroy_file_entries
file_entries
end

# :nodoc:
def ____collect_files(files)
end
Expand Down
53 changes: 53 additions & 0 deletions src/lib/renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ module Teeplate
# :nodoc:
getter data_entries = [] of AsDataEntry

# :nodoc:
getter? pending_destroy = false

# :nodoc:
getter entries = [] of RenderingEntry

Expand Down Expand Up @@ -97,5 +100,55 @@ module Teeplate
@quitted = true
end
end

# :nodoc:
def destroy
@pending_destroy = true
begin
if @interactive
@entries.each do |entry|
entry.destroy if should_destroy?(entry)
end
else
@entries.each(&.destroy) if should_destroy_all?(@entries)
end
rescue ex : Quit
@quitted = true
end
end

# Confirm whether the user wants to destroy a singe file.
def should_destroy?(entry : RenderingEntry)
STDOUT.puts "Destroy #{entry.out_path}? (y/n)"

loop do
case input = ::STDIN.gets.to_s.strip.downcase
when "y"
return true
when "n"
return false
end
end
end

# Confirm whether or not the user wishes to destroy multiple files.
def should_destroy_all?(entries : Array(RenderingEntry))
return should_destroy?(entries.first) if entries.size == 1

STDOUT.puts "Destroy all the following files? (y/n)"

entries.each do |entry|
puts entry.out_path
end

loop do
case input = ::STDIN.gets.to_s.strip.downcase
when "y"
return true
when "n"
return false
end
end
end
end
end
11 changes: 11 additions & 0 deletions src/lib/rendering_entry.cr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ module Teeplate
end
end

# :nodoc:
def destroy
begin
File.delete out_path
list_if_any "destroyed ", :red
rescue
list_if_any "skipped ", :yellow
end
end

# :nodoc:
def set_perm
if perm = @data.perm? && File.file?(out_path)
Expand Down Expand Up @@ -125,6 +135,7 @@ module Teeplate
return :keep if [email protected]? || @renderer.keeps_all?
return modifies?("#{local_path} is a symlink...", diff: false) if File.symlink?(out_path)
return :modify if appends?
return :destroy if @renderer.pending_destroy?
return :none if identical?
modifies?("#{local_path} already exists...", diff: true)
end
Expand Down