Skip to content

Commit

Permalink
Added --include and --exclude options to format tool (#4635)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota authored and Serdar Dogruyol committed Apr 19, 2018
1 parent 9f589a0 commit 591580e
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/compiler/crystal/command/format.cr
Expand Up @@ -15,6 +15,8 @@ class Crystal::Command

private def format
@format = "text"
excludes = ["lib"] of String
includes = [] of String
check = nil

option_parser =
Expand All @@ -29,6 +31,14 @@ class Crystal::Command
@format = f
end

opts.on("-i <path>", "--include <path>", "Include path") do |f|
includes << f
end

opts.on("-e <path>", "--exclude <path>", "Exclude path (default: lib)") do |f|
excludes << f
end

opts.on("-h", "--help", "Show this message") do
puts opts
exit
Expand All @@ -51,9 +61,17 @@ class Crystal::Command
end
end

files = Dir["./**/*.cr"] if files.empty?
includes = normalize_paths includes
excludes = normalize_paths excludes
excludes = excludes - includes

format_many files, check_files
if files.empty?
files = Dir["./**/*.cr"]
else
files = normalize_paths files
end

format_many files, check_files, excludes

if check_files
if check_files.empty?
Expand All @@ -76,6 +94,14 @@ class Crystal::Command
end
end

private def normalize_paths(paths)
path_start = ".#{File::SEPARATOR}"
paths.map do |path|
path = path_start + path unless path.starts_with?(path_start)
path.rstrip(File::SEPARATOR)
end
end

private def format_stdin(check_files)
source = STDIN.gets_to_end

Expand Down Expand Up @@ -130,19 +156,21 @@ class Crystal::Command
end
end

private def format_many(files, check_files)
private def format_many(files, check_files, excludes)
files.each do |filename|
format_file_or_directory filename, check_files
format_file_or_directory filename, check_files, excludes
end
end

private def format_file_or_directory(filename, check_files)
private def format_file_or_directory(filename, check_files, excludes)
if File.file?(filename)
format_file filename, check_files
unless excludes.any? { |exclude| filename.starts_with?(exclude) }
format_file filename, check_files
end
elsif Dir.exists?(filename)
filename = filename.chomp('/')
filenames = Dir["#{filename}/**/*.cr"]
format_many filenames, check_files
format_many filenames, check_files, excludes
else
error "file or directory does not exist: #{filename}"
end
Expand Down

0 comments on commit 591580e

Please sign in to comment.