Module: MCC
- Defined in:
- lib/mcc.rb
Constant Summary collapse
- RE =
/^#.*coding.*:.*$/
Class Method Summary collapse
- .include_magick_comment?(file) ⇒ Boolean
- .include_shebang?(file) ⇒ Boolean
- .insert_magick_comment(file, magick_comment) ⇒ Object
- .run(files, format = '# coding: utf-8') ⇒ Object
Class Method Details
.include_magick_comment?(file) ⇒ Boolean
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/mcc.rb', line 20 def self.include_magick_comment?(file) File.open(file) do |f| 2.times do f.readline =~ RE and return true end end false rescue EOFError false end |
.include_shebang?(file) ⇒ Boolean
50 51 52 53 54 55 56 |
# File 'lib/mcc.rb', line 50 def self.include_shebang?(file) File.open(file) do |f| return !!(f.readline =~ /^#!/) end rescue EOFError false end |
.insert_magick_comment(file, magick_comment) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/mcc.rb', line 31 def self.insert_magick_comment(file, magick_comment) include_shebang = include_shebang?(file) tmp = Tempfile.open(File.basename(file)) File.open(file) do |f| tmp.puts f.readline if include_shebang tmp.puts magick_comment f.each do |line| tmp.puts line end end tmp.flush FileUtils.cp(tmp, file) ensure tmp.close end |
.run(files, format = '# coding: utf-8') ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/mcc.rb', line 9 def self.run(files, format = '# coding: utf-8') files.each do |file| unless include_magick_comment?(file) puts "#{file} => insert a magick comment!" insert_magick_comment(file, format) else puts "#{file} => ok!" end end end |