Skip to content

Commit

Permalink
added support for autodetecting file extensions using the github-ling…
Browse files Browse the repository at this point in the history
…uist gem.
  • Loading branch information
osterman committed Apr 3, 2013
1 parent 0a32128 commit 3801158
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions copyright-header.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.extra_rdoc_files = ['README.md', 'LICENSE', 'AUTHORS', 'contrib/syntax.yml' ]
s.require_paths = ["lib"]
s.add_dependency('github-linguist', '~> 2.6.7')
end
9 changes: 7 additions & 2 deletions lib/copyright_header/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,19 @@ def initialize(options = {})
@options[:word_wrap] = len.to_i
end

opts.on( '-a', '--add-path PATH', 'Recursively insert header in all files found in path (allows multiple pathes separated by platform path-separator)' ) do |path|
opts.on( '-a', '--add-path PATH', 'Recursively insert header in all files found in path (allows multiple paths separated by platform path-separator "' + File::PATH_SEPARATOR + '")' ) do |path|
@options[:add_path] = path
end

opts.on( '-r', '--remove-path PATH', 'Recursively remove header in all files found in path (allows multiple pathes separated by platform path-separator)' ) do |path|
opts.on( '-r', '--remove-path PATH', 'Recursively remove header in all files found in path (allows multiple paths separated by platform path-separator "' + File::PATH_SEPARATOR + '")' ) do |path|
@options[:remove_path] = path
end

@options[:guess_extension] = false
opts.on( '-g', '--guess-extension', 'Use the GitHub Linguist gem to guess the extension of the source code when no extension can be determined (experimental).' ) do
@options[:guess_extension] = true
end

@options[:syntax] ||= @options[:base_path] + '/contrib/syntax.yml'
opts.on( '-c', '--syntax FILE', 'Syntax configuration file' ) do |path|
@options[:syntax] = path
Expand Down
14 changes: 11 additions & 3 deletions lib/copyright_header/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require 'yaml'
require 'erb'
require 'ostruct'
require 'linguist'

module CopyrightHeader
class FileNotFoundException < Exception; end
Expand Down Expand Up @@ -131,7 +132,10 @@ def has_copyright?(lines = 10)
end

class Syntax
def initialize(config)
attr_accessor :guess_extension

def initialize(config, guess_extension = false)
@guess_extension = guess_extension
@config = {}
syntax = YAML.load_file(config)
syntax.each_value do |format|
Expand All @@ -146,7 +150,11 @@ def initialize(config)
end

def ext(file)
File.extname(file)
extension = File.extname(file)
if @guess_extension && (extension.nil? || extension.empty?)
extension = Linguist::FileBlob.new(file).language.primary_extension
end
return extension
end

def supported?(file)
Expand All @@ -171,7 +179,7 @@ def initialize(options = {})
:copyright_years => @options[:copyright_years],
:copyright_holders => @options[:copyright_holders],
:word_wrap => @options[:word_wrap])
@syntax = Syntax.new(@options[:syntax])
@syntax = Syntax.new(@options[:syntax], @options[:guess_extension])
end

def execute
Expand Down

0 comments on commit 3801158

Please sign in to comment.