Sha256: fa30f6d76e091225fd0573f4dbc39d14d14b09334db2d09a83bdc16c0c5c170f

Contents?: true

Size: 1.34 KB

Versions: 33

Compression:

Stored size: 1.34 KB

Contents

require 'tempfile'
require 'ftools'

module RSCM
  module LineEditor
    # Comments out line by line if they match the line_regex.
    # Does not comment out already commented out lines.
    # If comment_template is nil, the matching lines will be deleted
    # Returns true if at least one line is commented out or changed
    def comment_out(original, line_regex, comment_template, output)
      did_comment_out = false
      already_commented_exp = /^[#{comment_template}]/ unless comment_template.nil?
      original.each_line do |line|
        out_line = nil
        if(line_regex =~ line)
          if(already_commented_exp && already_commented_exp =~ line)
            out_line = line
          else
            did_comment_out = true
            out_line = "#{comment_template}#{line}" unless comment_template.nil?
          end
        else
          out_line = line
        end
        output << out_line unless out_line.nil?
      end
      did_comment_out
    end
    module_function :comment_out
  end
end

class File

  def File.comment_out(path, line_regex, comment_template)
    temp_file = Tempfile.new(File.basename(path))
    temp_file_path = temp_file.path
    original = File.new(path)
    RSCM::LineEditor.comment_out(original, line_regex, comment_template, temp_file)

    temp_file.close
    original.close

    File.copy(temp_file_path, path)
  end
end

Version data entries

33 entries across 33 versions & 3 rubygems

Version Path
sdague-rscm-0.5.4 lib/rscm/line_editor.rb
rscm-0.1.0.1338 lib/rscm/line_editor.rb
rscm-0.1.0.999 lib/rscm/line_editor.rb
rscm-0.1.0.1337 lib/rscm/line_editor.rb
rscm-0.3.5 lib/rscm/line_editor.rb
rscm-0.3.2 lib/rscm/line_editor.rb
rscm-0.3.13 lib/rscm/line_editor.rb
rscm-0.3.15 lib/rscm/line_editor.rb
rscm-0.2.1.1404 lib/rscm/line_editor.rb
rscm-0.3.10 lib/rscm/line_editor.rb
rscm-0.3.4 lib/rscm/line_editor.rb
rscm-0.2.0 lib/rscm/line_editor.rb
rscm-0.3.3 lib/rscm/line_editor.rb
rscm-0.3.14 lib/rscm/line_editor.rb
rscm-0.3.11 lib/rscm/line_editor.rb
rscm-0.3.12 lib/rscm/line_editor.rb
rscm-0.3.16 lib/rscm/line_editor.rb
rscm-0.1.0 lib/rscm/line_editor.rb
rscm-0.3.0 lib/rscm/line_editor.rb
rscm-0.3.1 lib/rscm/line_editor.rb