class LA_Helper

Public Class Methods

clone_with_comments(f) click to toggle source
# File lib/la_helper.rb, line 5
def self.clone_with_comments(f)
  unless f == "./la_helper.rb"
    puts "\e[31mWe're going to copy\e[0m \e[36m#{File.basename(f, File.extname(f))}#{File.extname(f)}\e[0m \e[31mto another file with '_commented' added to the filename.\e[0m"
    puts "\e[31mThen we will clean all the comments out of\e[0m \e[36m#{File.basename(f, File.extname(f))}#{File.extname(f)}.\e[0m."
    puts "If you don't want that, type 'N'."
    puts "If you do want that, hit RETURN."

    user_input = $stdin.gets.downcase.chomp

    puts

    unless user_input == "n"

      puts "Opening the file..."
      filename = File.basename(f, File.extname(f))

      # determine which lines to remove based on file extension
      flag_char = ""
      file_ext = File.extname(f)
      if file_ext == ".rb"
        flag_char = "#"
      elsif file_ext == ".js"
        flag_char = "//"
      end

      filename = File.basename(f, File.extname(f))

      # copy original file to filename_commented
      commented_file = filename.downcase + "_commented" + File.extname(f)
      FileUtils.cp(f, commented_file)

      temp_file = filename.downcase + "_temp" + File.extname(f)

      tf = File.new(temp_file, "w+")

      File.open(f, 'r+').each do |line|
        # write a clean copy of the original file to filename_temp
        unless line.lstrip[0] == flag_char || line.lstrip[0..1] == flag_char
          tf.write(line)
          tf.write("\n")
        end
      end

      puts "\e[31mRemoving original file #{f}...\e[0m"
      FileUtils.rm(f)

      puts "Renaming \e[32m#{temp_file} to #{f}...\e[0m"
      FileUtils.mv(temp_file, f)
      puts
    end
  end
end