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

Add filter options #13632

Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 14 additions & 2 deletions src/compiler/crystal/command.cr
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ class Crystal::Command
hierarchy_exp : String?,
cursor_location : String?,
output_format : String?,
combine_rpath : Bool do
combine_rpath : Bool,
includes : Array(String),
excludes : Array(String) do
def compile(output_filename = self.output_filename)
compiler.emit_base_filename = emit_base_filename || output_filename.rchop(File.extname(output_filename))
compiler.compile sources, output_filename, combine_rpath: combine_rpath
Expand All @@ -367,6 +369,8 @@ class Crystal::Command
hierarchy_exp = nil
cursor_location = nil
output_format = nil
excludes = [] of String
includes = [] of String

option_parser = parse_with_crystal_opts do |opts|
opts.banner = "Usage: crystal #{command} [options] [programfile] [--] [arguments]\n\nOptions:"
Expand Down Expand Up @@ -414,6 +418,14 @@ class Crystal::Command
opts.on("-f tree|flat", "--format tree|flat", "Output format tree (default) or flat") do |f|
output_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
else
opts.on("-f text|json", "--format text|json", "Output format text (default) or json") do |f|
output_format = f
Expand Down Expand Up @@ -576,7 +588,7 @@ class Crystal::Command
end

combine_rpath = run && !no_codegen
@config = CompilerConfig.new compiler, sources, output_filename, emit_base_filename, arguments, specified_output, hierarchy_exp, cursor_location, output_format, combine_rpath
@config = CompilerConfig.new compiler, sources, output_filename, emit_base_filename, arguments, specified_output, hierarchy_exp, cursor_location, output_format, combine_rpath, includes, excludes
end

private def gather_sources(filenames)
Expand Down
48 changes: 42 additions & 6 deletions src/compiler/crystal/tools/dependencies.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,46 @@ class Crystal::Command
private def dependencies
config = create_compiler "tool dependencies", no_codegen: true, dependencies: true

config.compiler.dependency_printer = DependencyPrinter.new(STDOUT, flat: config.output_format == "flat")
dependency_printer = DependencyPrinter.new(STDOUT, flat: config.output_format == "flat")
dependency_printer.includes.concat config.includes.map { |path| ::Path[path].expand.to_s }
dependency_printer.excludes.concat config.excludes.map { |path| ::Path[path].expand.to_s }
config.compiler.dependency_printer = dependency_printer

config.compiler.top_level_semantic config.sources
end
end

module Crystal
class DependencyPrinter
@depth = 0
@filter_depth = Int32::MAX

property includes = [] of String
property excludes = [] of String

getter default_paths : Array(::Path) = CrystalPath.default_paths.map { |path| ::Path[path].expand }

def initialize(@io : IO, @flat : Bool = false)
end

def enter_file(filename : String, unseen : Bool)
print_indent
print_file(filename)
unless unseen
@io.print " (duplicate skipped)"
if @depth <= @filter_depth
filter = filter?(filename)
if filter
@filter_depth = @depth
else
@filter_depth = Int32::MAX
end

print_indent
print_file(filename)
if unseen
@io.print " (filtered)" if filter
else
@io.print " (duplicate skipped)"
end
@io.puts
end
@io.puts

@depth += 1
end
Expand All @@ -41,5 +62,20 @@ module Crystal
private def print_file(filename)
@io.print ::Path[filename].relative_to?(Dir.current) || filename
end

private def filter?(filename)
paths = ::Path[filename].parents
paths << ::Path[filename]

return false if match_patterns?(includes, paths)

return true if default_paths.any? { |path| paths.includes?(path) }

match_patterns?(excludes, paths)
end

private def match_patterns?(patterns, paths)
patterns.any? { |pattern| paths.any? { |path| File.match?(pattern, path) } }
end
end
end