Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encoding comment only if files syntax is not ok #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/magic_encoding 100644 → 100755
Expand Up @@ -2,6 +2,6 @@


# A simple tool to prepend magic comments for encoding to multiple ".rb" files # A simple tool to prepend magic comments for encoding to multiple ".rb" files


require 'magic_encoding' require File.join(File.dirname(__FILE__), *%w[.. lib magic_encoding])


AddMagicComment.process(ARGV) AddMagicComment.process(ARGV)
7 changes: 7 additions & 0 deletions lib/magic_encoding.rb
Expand Up @@ -2,6 +2,7 @@


# A simple library to prepend magic comments for encoding to multiple ".rb" files # A simple library to prepend magic comments for encoding to multiple ".rb" files


require 'open3'
module AddMagicComment module AddMagicComment


# Options : # Options :
Expand Down Expand Up @@ -29,6 +30,7 @@ def self.process(options)
extensions.each do |ext, comment_style| extensions.each do |ext, comment_style|
rbfiles = File.join(directory ,'**', '*.'+ext) rbfiles = File.join(directory ,'**', '*.'+ext)
Dir.glob(rbfiles).each do |filename| Dir.glob(rbfiles).each do |filename|
next if syntax_ok?(filename)
file = File.new(filename, "r+") file = File.new(filename, "r+")


lines = file.readlines lines = file.readlines
Expand All @@ -54,6 +56,11 @@ def self.process(options)
puts "Magic comments set for #{count} source files" puts "Magic comments set for #{count} source files"
end end


def self.syntax_ok?(filename)
stdin, stdout, stderr = Open3.popen3("ruby -c #{filename}")
stdout.read.include?("Syntax OK")
end

end end


class String class String
Expand Down