Module: Capistrano::SyntaxChecks

Defined in:
lib/capistrano/ext/syntax_checking/erb.rb,
lib/capistrano/ext/syntax_checking/haml.rb,
lib/capistrano/ext/syntax_checking/ruby.rb,
lib/capistrano/ext/syntax_checking/javascript.rb

Class Method Summary collapse

Class Method Details

.check_erb(paths, options = {}) ⇒ Object

Check ERB syntax. The paths argument takes a single string or array of paths to check. Options may be:

  • verbose - if true, enable verbose output to standard output. If false, only errors will be output.



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
62
63
64
# File 'lib/capistrano/ext/syntax_checking/erb.rb', line 14

def check_erb(paths, options = {})
  verbose = options[:verbose]
  begin
    require "erb"
    errors = false
    file_names = [paths].flatten.compact.map { |path|
      Dir.glob(File.join(path, "**", "*.{erb,rhtml}"))
    }.flatten
    if file_names.any?
      file_names.each_with_index do |file_name, index|
        if verbose
          $stdout.write("\r#{index} files checked (ctrl-C to skip)")
          $stdout.flush
        end
        old_stderr = $stderr
        begin
          $stderr = File.open("/dev/null", 'w')
          template = ERB.new(File.read(file_name), nil, "-")
          begin
            template.result
          rescue SyntaxError => e
            old_stderr << "\rSyntax error in ERB template #{file_name}: #{e}\n"
            old_stderr.flush
            errors = true
          rescue Exception
            # Ignore
          end
        ensure
          $stderr = old_stderr
        end
      end
      if errors
        if verbose
          print "\r"
        end
        abort("One or more syntax errors found. Fix and try again.")
      else
        if verbose
          puts ", no problems found."
        end
      end
    else
      puts("No ERB files to check.")
    end
  rescue Interrupt
    if verbose
      puts
    end
    puts("Canceled, skipping.")
  end
end

.check_haml(paths, options = {}) ⇒ Object

Check syntax in HAML files. The paths argument takes a single string or array of paths to check. Options may be:

  • verbose - if true, enable verbose output to standard output. If false, only errors will be output.



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
# File 'lib/capistrano/ext/syntax_checking/haml.rb', line 20

def check_haml(paths, options = {})
  if defined?(::Haml)          
    verbose = options[:verbose]
    begin
      errors = false
      file_names = [paths].flatten.compact.map { |path|
        Dir.glob(File.join(path, "**", "*.haml"))
      }.flatten
      file_names.delete_if { |d| File.directory?(d) }
      if file_names.any?
        file_names.each_with_index do |file_name, index|
          begin
            ::Haml::Engine.new(File.read(file_name), :cache => false, :read_cache => false)
          rescue => e
            puts "\r#{file_name}:#{e.line}: #{e.message}"
            errors = true
          end
          if verbose
            $stdout.write("\r#{index + 1} files checked (ctrl-C to skip)")
            $stdout.flush
          end
        end
        if errors
          print("\r")
          abort("One or more syntax errors found. Fix and try again.")
        else
          puts ", no problems found."
        end
      else
        if verbose
          puts("No HAML files to check.")
        end
      end
    rescue Interrupt
      puts
      puts("Canceled, skipping.")
    end
  else
    $stderr << "HAML syntax checks disabled as HAML is not installed.\n"
  end
end

.check_javascript(paths, options = {}) ⇒ Object

Check syntax in JavaScript files. The paths argument takes a single string or array of paths to check. Options may be:

  • verbose - if true, enable verbose output to standard output. If false, only errors will be output.



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
62
63
64
# File 'lib/capistrano/ext/syntax_checking/javascript.rb', line 14

def check_javascript(paths, options = {})
  verbose = options[:verbose]
  begin
    errors = false
    file_names = [paths].flatten.compact.map { |path|
      Dir.glob(File.join(path, "**", "*.js"))
    }.flatten
    if file_names.any?
      jar_path = File.join(File.dirname(__FILE__), "../../../closure/compiler.jar")
      if verbose
        print("Checking #{file_names.length} files with Closure")
      end
      command_line = "java -jar #{jar_path} " <<
        "--compilation_level WHITESPACE_ONLY " <<
        "--process_closure_primitives false " <<
        "--warning_level QUIET " <<
        "--js_output_file /dev/null " <<
        "--third_party "
      command_line << file_names.map { |fn| "--js '#{fn}' 2>&1"}.join(' ')
      IO.popen(command_line, 'r') do |input|
        input.each_line do |line|
          unless line =~ /^\d+ error(s)/
            print("\r")
            puts(line)
          end
          errors = true
        end
      end
      if errors
        if verbose
          print("\r")
        end
        abort("One or more syntax errors found. Fix and try again.")
      else
        if verbose
          puts ", no problems found."
        end
      end
    else
      if verbose
        puts("No JavaScript files to check.")
      end
    end
  rescue Interrupt
    if verbose
      puts
    end
    puts("Canceled, skipping.")
  end

end

.check_ruby(paths, options = {}) ⇒ Object

Check syntax in Ruby files. The paths argument takes a single string or array of paths to check. Options may be:

  • verbose - if true, enable verbose output to standard output. If false, only errors will be output.



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
# File 'lib/capistrano/ext/syntax_checking/ruby.rb', line 14

def check_ruby(paths, options = {})
  verbose = options[:verbose]
  begin
    errors = false
    files = [paths].flatten.compact.map { |path|
      Dir.glob(File.join(path, "**", "*.rb"))
    }.flatten
    files.delete_if { |d| File.directory?(d) }
    if files.any?
      files.each_with_index do |file_name, index|
        IO.popen("ruby -c '#{file_name}' 2>&1", "r") do |input|
          input.each_line do |line|
            unless line =~ /Syntax OK|\d: warning:/
              print("\r")
              puts(line)
              errors = true
            end
          end
        end
        if verbose
          $stdout.write("\r#{index + 1} files checked (ctrl-C to skip)")
          $stdout.flush
        end
      end
      if errors
        if verbose
          print("\r")
        end
        abort("One or more syntax errors found. Fix and try again.")
      else
        if verbose
          puts ", no problems found."
        end
      end
    else
      if verbose
        puts("No files to check.")
      end
    end
  rescue Interrupt
    if verbose
      puts
    end
    puts("Canceled, skipping.")
  end
end