Skip to content

Instantly share code, notes, and snippets.

@cvengros
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvengros/8722514 to your computer and use it in GitHub Desktop.
Save cvengros/8722514 to your computer and use it in GitHub Desktop.
Add license to all sources
#!/usr/bin/env ruby
LICENSE_TEXT = "%s Copyright (C) 2007-2014 My Cool license text %s"
SKIP_PATTERN = /.*Copyright\(C\)*/
# rb, js, hbs, css
FILE_TYPES = {
:rb => {
:pattern => '**/*.rb',
:comment => ['#', ''],
:first_line => /#!.*/
},
:js => {
:pattern => '**/*.js',
:comment => ['//', ''],
},
:hbs => {
:pattern => '**/*.hbs',
:comment => ['{{!', '}}']
},
:css => {
:pattern => '**/style.css',
:comment => ['/*', '*/']
},
:rake => {
:pattern => 'Rakefile',
:comment => ['#', '']
},
:rack => {
:pattern => 'config.ru',
:comment => ['#', '']
}
}
EXCLUDE = [
/.*\/libs\/.*/,
/dist.*/
/node_modules.*/
]
FILE_TYPES.each do |pref, file_type|
# for all files matching pattern
Dir.glob(file_type[:pattern]).each do |filename|
# if it's in excluded folders, skip
if EXCLUDE.reduce(false) {|result, pattern| result || filename =~ pattern}
puts "Skipping #{filename}, in an excluded folder"
next
end
# read file to a string
s = IO.read(filename)
first_line_end_index = s.index("\n")
if first_line_end_index
first_line = s[0, first_line_end_index]
else
first_line = s
end
# idempotent - if the first line is license, skip
if first_line =~ SKIP_PATTERN
puts "Skipping #{filename}, already has license"
next
end
license = LICENSE_TEXT % file_type[:comment]
# if the first line is something that should stay there, put it on the second line
if file_type[:first_line] && first_line =~ file_type[:first_line]
s.insert(first_line_end_index, "\n" + license)
else
# otherwise put it at the beginning
s = license + "\n" + s
end
File.open(filename, 'w') {|f| f.write(s) }
puts "Added license to #{filename}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment