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

piping and bug fix for when files are not UTF-8 #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions bin/license_header
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,15 @@ opts = OptionParser.new do |opts|
end
opts.parse!

if command.nil? or ARGV.empty?
begin
piped_text = ''
piped_text += $stdin.read_nonblock(4096) while 1
rescue EOFError, Errno::EWOULDBLOCK
end
required_files = piped_text.split("\n")
@prompt = false if ! required_files.empty?

if command.nil? or ( ARGV.empty? and required_files.empty? )
puts opts
exit 1
end
Expand All @@ -79,7 +87,7 @@ targets = ARGV.collect do |spec|
end

auditor = LicenseHeader::Auditor.new(options)
files = auditor.audit(*targets)
files = auditor.audit(targets, required_files)
invalid = (files[:missing]+files[:present]).sort
present = (files[:present]+files[:valid]).sort
case command
Expand Down
12 changes: 10 additions & 2 deletions lib/license_header/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ module LicenseHeader
:css => { :pre => '/* ', :each => ' * ', :post => '*/', :sep => true, :exts => %w(.css .scss) },
:erb => { :pre => '<%#', :each => '', :post => '%>', :sep => false, :exts => %w(.erb) },
:haml => { :pre => '-#', :each => ' ', :sep => true, :exts => %w(.haml) },
:as => { :pre => '/* ', :each => ' * ', :post => '*/', :sep => true, :exts => %w(.as) },
:html => { :pre => '<!--', :each => '', :post => '-->', :sep => false, :exts => %w(.html) },
:htm => { :pre => '<!--', :each => '', :post => '-->', :sep => false, :exts => %w(.htm) },
:java => { :pre => '/* ', :each => ' * ', :post => '*/', :sep => true, :exts => %w(.java) },
:javascript => { :pre => '/* ', :each => ' * ', :post => '*/', :sep => true, :exts => %w(.js .json) },
:ruby => { :each => '# ', :sep => true, :exts => %w(.rb .rake .coffee .pp) },
}
Expand All @@ -36,7 +39,7 @@ def initialize(configuration)
end

# Create a list of all files grouped by type and return the map
def audit(*patterns)
def audit(patterns, files = [])
result = Hash.new { |h,k| h[k] = [] }

Dir.glob(patterns).each do |entry|
Expand All @@ -52,6 +55,11 @@ def audit(*patterns)
result[evaluate_header(entry, format)] << entry
end

files.each do |entry|
format = determine_format(entry)
result[evaluate_header(entry, format)] << entry
end if files

return result
end

Expand Down Expand Up @@ -112,7 +120,7 @@ def determine_format(file)
end

def read_file(file)
File.read(file).chomp.split(/\n/)
File.read(file).encode!('UTF-8', 'UTF-8', :invalid => :replace).chomp.split(/\n/)
end

def write_file(file, content)
Expand Down