Last active
April 10, 2016 03:59
-
-
Save Revolucent/97d166f027286530b620 to your computer and use it in GitHub Desktop.
Insert copyright headers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# The MIT License (MIT) | |
# | |
# Copyright (c) 2016 Gregory Higley (Prosumma) | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
# Usage is pretty straightforward. However, one word of warning: | |
# This utility will DESTRUCTIVELY REMOVE the very first comment | |
# that occurs before the start of any code. This is so that any | |
# existing header can be replaced. If that doesn't work for you, | |
# you'll have to edit your files manually. You can always try it | |
# out and, since I know you're one of those smart people, undo | |
# the change with your preferred version control system. | |
# | |
# If called without argument, this utility searches for a file called | |
# LICENSE in the current directory. If it does not find that, it searches | |
# for a file called COPYRIGHT in the current directory. If it does not | |
# find that, it assumes a file called COPYRIGHT exists in ~/bin. The | |
# contents of this file will be the header. | |
# | |
# To manually specify a header file, use -c or --copyright. Any other | |
# argument passed is assumed to be the directory whose files are to be | |
# searched. | |
# | |
# Currently, this utility only supports file types that use c-style comments | |
# with the extensions .swift, .h, .c, .cpp, and .m. It's easy enough | |
# to add others. | |
require 'ostruct' | |
require 'optparse' | |
require 'io/console' | |
$options = OpenStruct::new | |
$options.copyright = nil | |
$options.path = nil | |
option_parser = OptionParser::new do |opts| | |
opts.on('-c', '--copyright=PATH', 'Path to the copyright file') do |opt| | |
$options.copyright = File::expand_path(opt) | |
end | |
end | |
$options.path = File::expand_path(option_parser.parse(ARGV)[0] || '.') | |
unless $options.copyright | |
if File::exists? 'COPYRIGHT' | |
$options.copyright = File::expand_path('COPYRIGHT') | |
elsif File::exists? 'LICENSE' | |
$options.copyright = File::expand_path('LICENSE') | |
else | |
$options.copyright = File::join(File::dirname(__FILE__), 'COPYRIGHT') | |
end | |
end | |
class CStyleCommentHandler | |
def initialize(path_regex) | |
@path_regex = path_regex | |
end | |
def process(path) | |
return false unless path =~ @path_regex | |
contents = nil | |
File::open path do |fd| | |
contents = fd.read | |
end | |
# Look for the first comment block | |
code = [] | |
state = nil # :in_group, :in_single, :after | |
contents.each_line do |line| | |
if code.length > 0 | |
code << line | |
elsif state == :after | |
code << line if line.strip.length > 0 | |
elsif state == :in_group | |
state = :after if line =~ /^\s*\*\// | |
elsif state == :in_single | |
state = :after if line.strip.length == 0 | |
elsif line =~ /^\s*\/\*/ | |
state = :in_group | |
elsif line =~ /^\s*\/\// | |
state = :in_single | |
end | |
end | |
code.insert 0, '' | |
code.insert 0, '*/' | |
code.insert 0, COPYRIGHT | |
code.insert 0, '/*' | |
File::open path, 'w' do |fd| | |
fd.puts code | |
end | |
true | |
end | |
end | |
HANDLERS = [CStyleCommentHandler::new(/\.(?:swift|h|c|cpp|m)$/i)] | |
puts "The path '#{$options.path}' and all of its subdirectories will be searched for files to which to add the copyright header found at #{$options.copyright}." | |
input = [(print 'Proceed? [yN] '), STDIN.getch][1] | |
exit unless input =~ /^y$/i | |
File::open($options.copyright, 'r') do |fd| | |
COPYRIGHT = fd.read | |
end | |
Dir::chdir $options.path | |
Dir::glob '**/*' do |path| | |
HANDLERS.each do |handler| | |
next if handler.process path | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment