<commit_before>#!/usr/bin/env ruby

require 'rubygems'
require 'fileutils'

class RailsTemplateAdjuster

  attr_accessor :build_number

  def initialize( version, build_number )
    @version      = version
    @build_number = build_number
    @incremental = @version =~ /\-SNAPSHOT$/
    @version.gsub!(/\-SNAPSHOT$/, '')

  end

  def adjust
    original = template_path
    modified = original + '.modified'
    FileUtils.rm( modified, :force => true )
    FileUtils.cp( original, modified )
    File.open( original, 'r' ) do |input|
      File.open( modified, 'w' ) do |output|
        output.puts generate_source
        input.each_line { |line| write_line( output, line ) }
      end
    end
    FileUtils.rm original
    FileUtils.mv( modified, original )
  end

  private

  def local_build?
    @build_number.nil? || @build_number == '' || @build_number == "${env.BUILD_NUMBER}"
  end

  def write_line( output, line )
    if line =~ /gem [\'\"](torquebox.*?)[\'\"]/
      output.puts line.sub( @build_number, @version ).sub( "${env.BUILD_NUMBER}", @version )
    else
      output.puts line unless line =~ /^add_source \"http\:\/\/torquebox.org\/.+?\/builds\/[0-9]+\/gem-repo\"$/
    end
  end

  def generate_source
    if local_build?
      "# Local build - no source needed"
    elsif @incremental
      "add_source \"http://torquebox.org/builds/#{@build_number}/gem-repo\""
    else
      ""
    end
  end

  def template_path
    File.dirname( __FILE__ ) + '/../target/torquebox-rake-support/share/rails/template.rb'
  end

end

RailsTemplateAdjuster.new( ARGV[0], ARGV[1] ).adjust<commit_msg>Only call add_source in Rails template for Rails 3 apps<commit_after>#!/usr/bin/env ruby

require 'rubygems'
require 'fileutils'

class RailsTemplateAdjuster

  attr_accessor :build_number

  def initialize( version, build_number )
    @version      = version
    @build_number = build_number
    @incremental = @version =~ /\-SNAPSHOT$/
    @version.gsub!(/\-SNAPSHOT$/, '')

  end

  def adjust
    original = template_path
    modified = original + '.modified'
    FileUtils.rm( modified, :force => true )
    FileUtils.cp( original, modified )
    File.open( original, 'r' ) do |input|
      File.open( modified, 'w' ) do |output|
        output.puts generate_source
        input.each_line { |line| write_line( output, line ) }
      end
    end
    FileUtils.rm original
    FileUtils.mv( modified, original )
  end

  private

  def local_build?
    @build_number.nil? || @build_number == '' || @build_number == "${env.BUILD_NUMBER}"
  end

  def write_line( output, line )
    if line =~ /gem [\'\"](torquebox.*?)[\'\"]/
      output.puts line.sub( @build_number, @version ).sub( "${env.BUILD_NUMBER}", @version )
    else
      output.puts line unless line =~ /^add_source \"http\:\/\/torquebox.org\/builds\/[0-9]+\/gem-repo\".*/
    end
  end

  def generate_source
    if local_build?
      "# Local build - no source needed"
    elsif @incremental
      "add_source \"http://torquebox.org/builds/#{@build_number}/gem-repo\" if RAILS::VERSION::MAJOR > 2"
    else
      ""
    end
  end

  def template_path
    File.dirname( __FILE__ ) + '/../target/torquebox-rake-support/share/rails/template.rb'
  end

end

RailsTemplateAdjuster.new( ARGV[0], ARGV[1] ).adjust
{'fraction_non_alphanumeric': 0.14660164583968302, 'fraction_numerical': 0.003047851264858275, 'mean_word_length': 2.852112676056338, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7141840', 'n_tokens_mistral': 1148, 'n_tokens_neox': 1110, 'n_words': 253}