Module: MagicComment

Defined in:
lib/magic-comment.rb

Class Method Summary collapse

Class Method Details

.process(*paths) ⇒ Object

Options :

  • paths



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/magic-comment.rb', line 9

def self.process(*paths)
  # defaults
  paths.flatten!
  paths.push Dir.pwd if paths.empty?

  encoding = "coding: utf-8"
  default_comment = "# {encoding}\n"

  # TODO : add options for recursivity (and application of the script to a single file)
  extensions = {
    '.rb' => default_comment,
    '.rake' => default_comment,
    '.haml' => "-#{default_comment}",
  }

  files = []
  paths.each do |path|
    path = Pathname.new path
    if path.file?
      files.push path
    elsif path.directory?
      files += Dir.glob path.join('**', "*{#{extensions.keys.join ','}}")
    end
  end

  default_comment.sub! '{encoding}', encoding
  extensions.each_key do |key|
    extensions[key].sub! '{encoding}', encoding
  end

  files.each do |filename|
    file = File.new(filename, "r+")
    magic_comment = extensions[ File.extname(file) ] || default_comment
    lines = file.readlines

    # remove current encoding comment(s)
    while lines[0].match(/^-?# ?(-\*-)? ?(en)?coding/)
      lines.shift
    end

    # set current encoding
    lines.unshift magic_comment

    body = lines.join

    file.pos = 0
    file.write body
    file.truncate(body.bytesize)
    file.close
  end

  puts "Magic comments set for #{files.length} source files"
end