Class: CopyrightHeader::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/copyright_header/command_line.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandLine

Returns a new instance of CommandLine.


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/copyright_header/command_line.rb', line 28

def initialize(options = {})
  begin
    @options = options
    @options[:base_path] ||= File.expand_path File.dirname(__FILE__) + "/../../"
    @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.gsub(/\/+$/, '')
      end
      
      opts.on( '--license-file FILE', 'Use FILE as header (instead of using --license argument)' ) do|file|
        @options[:license_file] = file
      end

      opts.on( '--license [' + Dir.glob(@options[:base_path] + '/licenses/*').map { |f| File.basename(f, '.erb') }.join('|') + ']', 'Use LICENSE as header' ) do|license|
        @options[:license] = license
      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 detailed description 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 legal owner of the copyright for the software. (e.g. "Erik Osterman <[email protected]>"). Repeat argument for multiple names.' ) do|name|
        @options[:copyright_holders] << name
      end

      @options[:copyright_years] ||= []
      opts.on( '--copyright-year YEAR', 'The years for which the copyright exists (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 (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 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
      end

      opts.on( '-V', '--version', 'Display version information' ) do
        STDERR.puts "CopyrightHeader #{CopyrightHeader::VERSION}",
                    "Copyright (C) 2012 Erik Osterman <[email protected]>",
                    "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>",
                    "This is free software: you are free to change and redistribute it.", 
                    "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[:license].nil?
      unless @options[:license_file].nil?
        raise AmbiguousArgumentException.new("Cannot pass both --license and --license-file arguments")
      end
      # get the license_file from the shiped files
      @options[:license_file] ||= @options[:base_path] + '/licenses/' + @options[:license] + '.erb'
    end

    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
    STDERR.puts e.message, @optparse
    exit (1)
  end
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options


27
28
29
# File 'lib/copyright_header/command_line.rb', line 27

def options
  @options
end

#optparseObject

Returns the value of attribute optparse


27
28
29
# File 'lib/copyright_header/command_line.rb', line 27

def optparse
  @optparse
end

#parserObject

Returns the value of attribute parser


27
28
29
# File 'lib/copyright_header/command_line.rb', line 27

def parser
  @parser
end

Instance Method Details

#executeObject


138
139
140
141
142
# File 'lib/copyright_header/command_line.rb', line 138

def execute
  STDERR.puts "-- DRY RUN --" if @options[:dry_run]
  @parser = CopyrightHeader::Parser.new(@options)
  @parser.execute
end