Skip to content

Instantly share code, notes, and snippets.

@aripalo
Last active October 19, 2016 22:07
Show Gist options
  • Save aripalo/b544370b124b23de8c07f81b38e6721f to your computer and use it in GitHub Desktop.
Save aripalo/b544370b124b23de8c07f81b38e6721f to your computer and use it in GitHub Desktop.
Parse .ebignore rules and collect included/excluded files
# get files (also hidden ones with dot prefix)
files = Dir.glob("{**/*}", File::FNM_DOTMATCH)
# Remove folders from array as (git)ignore doesn't care about empty folders
files = files.reject { |f| File.directory?(f) }
# Remove .ebignore itself
files = files.reject { |f| f == '.ebignore' }
# Read the .ebignore file
ebignore_path = File.join(Dir.pwd, '.ebignore')
ebignore_rules = File.readlines(ebignore_path)
ebignore_rules = ebignore_rules.reject {|line| line.empty? || line == "\n"}.reject {|line| line.start_with?('#')}.map {|line| line.strip}
# Arrays to store files
included_files = [] # a.k.a. files that will be zipped
excluded_files = [] # a.k.a. ignored files
# Go through each ebignore rule for file
files.each do |f|
ebignore_rules.each do |ignore_pattern|
# account for 'dir' and 'dir/' patterns
if File.directory?(ignore_pattern.gsub(/^!/, ''))
ignore_pattern.gsub!(/\/$/, '').concat('/**')
end
# handle inverted rules (e.g. !foo) and normal rules
if ignore_pattern.start_with?('!')
matches_rule = File.fnmatch(ignore_pattern.gsub(/^!/, ''), f, File::FNM_DOTMATCH)
if matches_rule then included_files.push(f) end
else
matches_rule = File.fnmatch(ignore_pattern, f, File::FNM_DOTMATCH)
if matches_rule then excluded_files.push(f) end
end
end
end
# Remove possible duplicates
included_files.uniq
excluded_files.uniq
# Not sure about this logic...? (have to test more)
# works at least for "include everything except"-type of .ebignore files, like:
# *
# !foo
# !bar
excluded_files = excluded_files.reject{ |e| included_files.include? e }
# Print out the results
puts "excluded = ignored files:"
puts excluded_files
puts ""
puts "included = files that are monitred by git:"
puts included_files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment