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.



176
177
178
179
180
181
182
183
184
185
186
# File 'lib/copyright_header/parser.rb', line 176

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.



173
174
175
# File 'lib/copyright_header/parser.rb', line 173

def options
  @options
end

Instance Method Details

#add(dir) ⇒ Object

Add copyright header recursively



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

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

#executeObject



188
189
190
191
192
193
194
195
196
# File 'lib/copyright_header/parser.rb', line 188

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



245
246
247
# File 'lib/copyright_header/parser.rb', line 245

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

#transform(method, path) ⇒ Object



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
233
234
235
236
237
# File 'lib/copyright_header/parser.rb', line 198

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)
          STDERR.puts "SKIP #{path}; excluded"
          next
        end
      elsif File.directory?(path)
        next
      else
        STDERR.puts "SKIP #{path}; not file"
        next
      end

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

#write(file, contents) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/copyright_header/parser.rb', line 249

def write(file, contents)
  if @options[:dry_run] 
    STDERR.puts "UPDATE #{file} [dry-run]"
    STDERR.puts contents
  elsif @options[:output_dir].nil?
    STDERR.puts "UPDATE #{file} [no output-dir]"
    STDERR.puts contents
  else
    dir = "#{@options[:output_dir]}/#{File.dirname(file).gsub(/^\/+/, '')}"
    STDERR.puts "UPDATE #{file} [output-dir #{dir}]"
    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