Skip to content

Instantly share code, notes, and snippets.

@cvengros
Last active August 29, 2015 13:55

Revisions

  1. cvengros revised this gist Jan 30, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion add_licenses.rb
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@
    first_line = s
    end

    # idempotent - if the first line is gooddata license, skip
    # idempotent - if the first line is license, skip
    if first_line =~ SKIP_PATTERN
    puts "Skipping #{filename}, already has license"
    next
  2. cvengros created this gist Jan 30, 2014.
    82 changes: 82 additions & 0 deletions add_licenses.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    #!/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 gooddata 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