summaryrefslogtreecommitdiff
path: root/tool/lib
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2024-04-22 11:01:32 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2024-04-22 13:00:19 +0900
commit44d2b5949167fe5b7de68de06b103e586d4133f8 (patch)
treeb719fe125759aae576bb88cd6402c7cec1dcaca3 /tool/lib
parentc65bc2e5d9f0bc56b22d6bb765a1f25c53aa2ea7 (diff)
Extract `list_tree` as a method and separate from removals
Diffstat (limited to 'tool/lib')
-rw-r--r--tool/lib/_tmpdir.rb86
1 files changed, 56 insertions, 30 deletions
diff --git a/tool/lib/_tmpdir.rb b/tool/lib/_tmpdir.rb
index f3b8560258..242c4c3fa0 100644
--- a/tool/lib/_tmpdir.rb
+++ b/tool/lib/_tmpdir.rb
@@ -21,39 +21,65 @@ END {
rescue Errno::ENOTEMPTY
require_relative "colorize"
colorize = Colorize.new
- mode_inspect = ->(m, s) {
- [
- (m & 0o4 == 0 ? ?- : ?r),
- (m & 0o2 == 0 ? ?- : ?w),
- (m & 0o1 == 0 ? (s ? s.upcase : ?-) : (s || ?x)),
- ]
- }
- filecolor = ->(st) {
- st.directory? ? "bold;blue" : st.symlink? ? "bold;cyan" : st.executable? ? "bold;green" : nil
- }
- list_tree = ->(parent, indent = " ") {
- Dir.children(parent).each do |child|
- path = File.join(parent, child)
- st = File.lstat(path)
- m = st.mode
- m = [
- (st.file? ? ?- : st.ftype[0]),
- mode_inspect[m >> 6, (?s unless m & 04000 == 0)],
- mode_inspect[m >> 3, (?s unless m & 02000 == 0)],
- mode_inspect[m, (?t unless m & 01000 == 0)],
- ].join("")
- warn [
- indent, m, st.nlink, st.size, st.mtime,
- colorize.decorate(child, filecolor[st]),
- (["->", colorize.cyan(File.readlink(path))] if st.symlink?),
- ].compact.join(" ")
- if st.directory?
- list_tree[File.join(parent, child), indent + " "]
+ ls = Struct.new(:colorize) do
+ def mode_inspect(m, s)
+ [
+ (m & 0o4 == 0 ? ?- : ?r),
+ (m & 0o2 == 0 ? ?- : ?w),
+ (m & 0o1 == 0 ? (s ? s.upcase : ?-) : (s || ?x)),
+ ]
+ end
+ def decorate_path(path, st)
+ case
+ when st.directory?
+ color = "bold;blue"
+ type = "/"
+ when st.symlink?
+ color = "bold;cyan"
+ # type = "@"
+ when st.executable?
+ color = "bold;green"
+ type = "*"
+ when path.end_with?(".gem")
+ color = "green"
+ end
+ colorize.decorate(path, color) + (type || "")
+ end
+ def list_tree(parent, indent = "", &block)
+ children = Dir.children(parent).map do |child|
+ [child, path = File.join(parent, child), File.lstat(path)]
+ end
+ nlink_width = children.map {|child, path, st| st.nlink}.max.to_s.size
+ size_width = children.map {|child, path, st| st.size}.max.to_s.size
+
+ children.each do |child, path, st|
+ m = st.mode
+ m = [
+ (st.file? ? ?- : st.ftype[0]),
+ mode_inspect(m >> 6, (?s unless m & 04000 == 0)),
+ mode_inspect(m >> 3, (?s unless m & 02000 == 0)),
+ mode_inspect(m, (?t unless m & 01000 == 0)),
+ ].join("")
+ warn sprintf("%s* %s %*d %*d %s % s%s",
+ indent, m, nlink_width, st.nlink, size_width, st.size,
+ st.mtime.to_s, decorate_path(child, st),
+ (" -> " + decorate_path(File.readlink(path), File.stat(path)) if
+ st.symlink?))
+ if st.directory?
+ list_tree(File.join(parent, child), indent + " ", &block)
+ end
+ yield path, st if block
end
end
- }
+ end.new(colorize)
warn colorize.notice("Children under ")+colorize.fail(tmpdir)+":"
- list_tree[tmpdir]
+ ls.list_tree(tmpdir) do |path, st|
+ if st.directory?
+ Dir.rmdir(path)
+ else
+ File.unlink(path)
+ end
+ end
FileUtils.rm_rf(tmpdir)
end
end