Skip to content

Instantly share code, notes, and snippets.

@programmingthomas
Last active August 29, 2015 13:56
Show Gist options
  • Save programmingthomas/9123246 to your computer and use it in GitHub Desktop.
Save programmingthomas/9123246 to your computer and use it in GitHub Desktop.
Recursively iterates over a directory containing Objective-C .h, .m and .pch files to check that their Apache license header is in the correct format (my first Ruby script...)
def copyright(fname, year, programmer)
return "// #{fname}
//
// Copyright #{year} #{programmer}
//
// Licensed under the Apache License, Version 2.0 (the \"License\");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an \"AS IS\" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License."
end
def file_matches(path, years, programmers)
file = File.open(path, "rb")
contents = file.read
basename = File.basename(path)
file.close
if not years.respond_to?("each")
years = [years]
end
if not programmers.respond_to?("each")
programmers = [programmers]
end
programmers.each do |programmer|
years.each do |year|
if contents.start_with?(copyright(basename, year, programmer))
return true
end
end
end
return false
end
def walk(dir, years, programmers)
if File.readable?(dir)
Dir.foreach(dir) do |basename|
next if basename == '.'or basename == '..'
fullname = File.join(dir, basename)
if File.directory?(fullname)
walk(fullname, years, programmers)
elsif basename.end_with?('.h', '.m', '.pch')
if File.readable?(fullname)
if not file_matches(fullname, years, programmers)
puts basename
end
end
end
end
end
end
if ARGV.length == 3
directory = ARGV[0]
programmers = ARGV[1].split(',')
years = ARGV[2].split(',')
walk(directory, years, programmers)
else
puts "Usage: ruby apache.rb directory programmers,by,comma years,by,comma"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment