Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 4c832bc961ef39225825c4266034e7093acb3d5b @osterman committed Jul 15, 2012
No changes.
@@ -0,0 +1,2 @@
+Erik Osterman <e@osterman.com> - Initial version
+
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in copyright_header.gemspec
+gemspec
674 LICENSE

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,43 @@
+CopyrightHeader
+===============
+
+
+Features
+------------
+
+* Add/remove a copyright headers recursively on source files
+* Customize the syntax configuration for how to write out comments
+
+Caveats
+-------
+* Will only remove headers to files that have exactly the same header as the one we added
+* Will only add headers to files which do not contain the pattern /[Cc]opyright|[Lc]icense/ in the first N lines
+
+Requirements
+------------
+
+* Ruby 1.9.2 (supported version)
+
+Installation & Usage
+--------------------
+
+ # gem install copyright_header
+ # copyright-header --help
+
+Add a GPL3 License header to a file:
+ copyright-header --add-path /tmp/test.rb --license /tmp/GPL3.txt --dry-run
+
+Remove the header created in the previous step (without --dry-run argument):
+ copyright-header --remove-path /tmp/test.rb --license /tmp/GPL3.txt --dry-run
+
+Paths can be either files or directories. It will recursively traverse the directory tree ignoring all dot files.
+
+You can specify an alternative syntax configuration file using the --syntax argument.
+
+
+Contact Information
+-------------------
+
+Erik Osterman <e@osterman.com>
+http://www.osterman.com/
+
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+$:.unshift(File.expand_path('.')) # Ruby 1.9 doesn't have . in the load path...
+$:.push(File.expand_path('lib/'))
+
+require 'rubygems'
+require 'copyright_header'
+
+command_line = CopyrightHeader::CommandLine.new
+command_line.execute
@@ -0,0 +1,61 @@
+ruby:
+ ext: ['.rb']
+ after: ['^#!']
+ comment:
+ open: '#\n'
+ close: '#\n'
+ prefix: '# '
+
+html:
+ ext: ['.html', '.htm', '.xhtml', '.xml']
+ comment:
+ open: '<!--\n'
+ close: '-->\n'
+ prefix: ' '
+
+php:
+ ext: ['.php']
+ after: [ '^#!' ]
+ comment:
+ open: '<?php \n/*\n'
+ close: ' */ ?>\n'
+ prefix: ' * '
+
+
+javacript:
+ ext: ['.js']
+ comment:
+ open: '/*\n'
+ close: ' */\n\n'
+ prefix: ' * '
+
+css:
+ ext: ['.css']
+ comment:
+ open: '/*\n'
+ close: ' */\n\n'
+ prefix: ' * '
+
+
+c:
+ ext: ['.java', '.c', '.cpp', '.cc']
+ comment:
+ open: '/*'
+ close: ' */\n\n'
+ prefix: ' * '
+
+cpp:
+ ext: ['.cpp', '.cc', '.hh', '.hpp']
+ comment:
+ open: '//\n'
+ close: '//\n\n'
+ prefix: '// '
+
+java:
+ ext: ['.java']
+ comment:
+ open: '/*\n'
+ close: ' */\n\n'
+ prefix: ' * '
+
+
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+
+$:.push File.expand_path("../lib", __FILE__)
+require "copyright_header/version"
+
+Gem::Specification.new do |s|
+ s.name = "copyright_header"
+ s.version = CopyrightHeader::VERSION
+ s.authors = ["Erik Osterman"]
+ s.email = ["e@osterman.com"]
+ s.homepage = "http://www.osterman.com/"
+ s.summary = %q{A utility to insert copyright headers into various types of source code files}
+ s.description = %q{A utility which is able to recursively insert and remove copyright headers from source code files based on file extensions.}
+ s.rubyforge_project = "copyright-header"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ spec.extra_rdoc_files = ['README.md', 'LICENSE', 'AUTHORS', 'contrib/syntax.yml']
+ s.require_paths = ["lib"]
+end
@@ -0,0 +1,7 @@
+require 'copyright_header/version'
+require 'copyright_header/command_line'
+require 'copyright_header/parser'
+
+module CopyrightHeader
+end
+
@@ -0,0 +1,112 @@
+require 'optparse'
+
+module CopyrightHeader
+ class MissingArgumentException < Exception; end
+
+ class CommandLine
+ attr_accessor :options, :parser, :optparse
+ def initialize(options = {})
+ begin
+ @options = options
+ @options[:base_path] = File.expand_path File.dirname($0) + "/../"
+ @optparse = OptionParser.new do |opts|
+ opts.banner = "Usage: #{$0} options [file]"
+
+ @options[:dry_run] = false
+ opts.on( '-n', '--dry-run', 'Output the parsed files to STDOUT' ) do
+ @options[:dry_run] = true
+ end
+
+ opts.on( '-o', '--output-dir DIR', 'Use DIR as output directory') do |dir|
+ @options[:output_dir] = dir + '/'
+ end
+
+ opts.on( '--license-file FILE', 'Use FILE as header' ) do|file|
+ @options[:license_file] = file
+ end
+
+ opts.on( '--license [GPL3|MIT]', 'Use LICENSE as header' ) do|license|
+ @options[:license] = license
+ @options[:license_file] = @options[:base_path] + '/licenses/' + license + '.erb'
+ end
+
+ opts.on( '--copyright-software NAME', 'The common name for this piece of software (e.g. "Copyright Header")' ) do|name|
+ @options[:copyright_software] = name
+ end
+
+ opts.on( '--copyright-software-description DESC', 'The common name for this piece of software (e.g. "A utility to manipulate copyright headers on source code files")' ) do|desc|
+ @options[:copyright_software_description] = desc
+ end
+
+ @options[:copyright_holders] = []
+ opts.on( '--copyright-holder NAME', 'The common name for this piece of software (e.g. "Erik Osterman <e@osterman.com>"). Repeat argument for multiple names.' ) do|name|
+ @options[:copyright_holders] << name
+ end
+
+ @options[:copyright_years] = []
+ opts.on( '--copyright-year YEAR', 'The common name for this piece of software (e.g. "2012"). Repeat argument for multiple years.' ) do|year|
+ @options[:copyright_years] << year
+ end
+
+ @options[:word_wrap] = 80
+ opts.on( '-w', '--word-wrap LEN', 'Maximum number of characters per line for license (default: 80)' ) do |len|
+ @options[:word_wrap] = len.to_i
+ end
+
+ opts.on( '-a', '--add-path PATH', 'Recursively insert header in all files found in path' ) do |path|
+ @options[:add_path] = path
+ end
+
+ opts.on( '-r', '--remove-path PATH', 'Recursively remove header in all files found in path' ) do |path|
+ @options[:remove_path] = path
+ end
+
+ @options[:syntax] = @options[:base_path] + '/contrib/syntax.yml'
+ opts.on( '-c', '--syntax FILE', 'Syntax configuration file' ) do |path|
+ @options[:syntax] = path
+ end
+
+ opts.on( '-V', '--version', 'Display version information' ) do
+ puts "CopyrightHeader #{CopyrightHeader::VERSION}"
+ puts "Copyright (C) 2012 Erik Osterman <e@osterman.com>"
+ puts "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
+ puts "This is free software: you are free to change and redistribute it."
+ puts "There is NO WARRANTY, to the extent permitted by law."
+ exit
+ end
+
+ opts.on( '-h', '--help', 'Display this screen' ) do
+ puts opts
+ exit
+ end
+ end
+
+ @optparse.parse!
+ unless @options.has_key?(:license_file)
+ raise MissingArgumentException.new("Missing --license or --license-file argument")
+ end
+
+ unless File.file?(@options[:license_file])
+ raise MissingArgumentException.new("Invalid --license or --license-file argument. Cannot open #{@options[:license_file]}")
+ end
+
+ if @options[:license]
+ raise MissingArgumentException.new("Missing --copyright-software argument") if @options[:copyright_software].nil?
+ raise MissingArgumentException.new("Missing --copyright-software-description argument") if @options[:copyright_software_description].nil?
+ raise MissingArgumentException.new("Missing --copyright-holder argument") unless @options[:copyright_holders].length > 0
+ raise MissingArgumentException.new("Missing --copyright-year argument") unless @options[:copyright_years].length > 0
+ end
+ rescue MissingArgumentException => e
+ puts e.message
+ puts @optparse
+ exit (1)
+ end
+ end
+
+ def execute
+ @parser = CopyrightHeader::Parser.new(@options)
+ @parser.execute
+ end
+ end
+end
+
Oops, something went wrong.

0 comments on commit 4c832bc

Please sign in to comment.