Class: CopyrightHeader::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/copyright_header/parser.rb', line 173

def initialize(options = {})
  @options = options
  @exclude = [ /^LICENSE(|\.txt)$/i, /^holders(|\.txt)$/i, /^README/, /^\./]
  @license = License.new(:license_file => @options[:license_file],
                         :copyright_software => @options[:copyright_software],
                         :copyright_software_description => @options[:copyright_software_description],
                         :copyright_years => @options[:copyright_years],
                         :copyright_holders => @options[:copyright_holders],
                         :word_wrap => @options[:word_wrap])
  @syntax = Syntax.new(@options[:syntax], @options[:guess_extension])
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



170
171
172
# File 'lib/copyright_header/parser.rb', line 170

def options
  @options
end

Instance Method Details

#add(dir) ⇒ Object

Add copyright header recursively



235
236
237
# File 'lib/copyright_header/parser.rb', line 235

def add(dir)
  transform(:add, dir)
end

#executeObject



185
186
187
188
189
190
191
192
193
# File 'lib/copyright_header/parser.rb', line 185

def execute
  if @options.has_key?(:add_path)
    @options[:add_path].split(File::PATH_SEPARATOR).each { |path| add(path) }
  end

  if @options.has_key?(:remove_path)
    @options[:remove_path].split(File::PATH_SEPARATOR).each { |path| remove(path) }
  end
end

#remove(dir) ⇒ Object

Remove copyright header recursively



240
241
242
# File 'lib/copyright_header/parser.rb', line 240

def remove(dir)
  transform(:remove, dir)
end

#transform(method, path) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/copyright_header/parser.rb', line 195

def transform(method, path)
  paths = []
  if File.file?(path)
    paths << path
  else
    paths << Dir.glob("#{path}/**/*")
  end

  paths.flatten!

  paths.each do |path|
    begin
      if File.file?(path)
        if @exclude.include? File.basename(path)
          puts "SKIP #{path}; excluded"
          next
        end
      else
        puts "SKIP #{path}; not file"
        next
      end

      if @syntax.supported?(path) 
        header = @syntax.header(path)
        contents = header.send(method, @license)
        if contents.nil?
          puts "SKIP #{path}; failed to generate license"
        else
          write(path, contents)
        end
      else
        puts "SKIP #{path}; unsupported"
      end
    rescue Exception => e
      puts "SKIP #{path}; #{e.message}"
    end
  end
end

#write(file, contents) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/copyright_header/parser.rb', line 244

def write(file, contents)
  puts "UPDATE #{file}"
  if @options[:dry_run] || @options[:output_dir].nil?
    puts contents
  else
    dir = "#{@options[:output_dir]}/#{File.dirname(file)}"
    FileUtils.mkpath dir unless File.directory?(dir)
    output_path = @options[:output_dir] + file
    f =File.new(output_path, 'w')
    f.write(contents)
    f.close
  end
end