<commit_before>module Buildr
  
  # To start the nailgun server run the ng:start task, this will
  # download, compile and installed nailgun if needed, afterwards
  # it will start the nailgun server.
  module Nailgun 
    VERSION = '0.7.1'
    NAME = "nailgun-#{VERSION}"
    URL = "http://downloads.sourceforge.net/nailgun/#{NAME}.zip"
    ARTIFACT_SPEC = "com.martiansoftware:nailgun:jar:#{VERSION}"

    class << self
      attr_accessor :artifact
      attr_accessor :iface, :port, :runtime_pool_size
      attr_accessor :jruby_home, :home
    end

    self.jruby_home = if PLATFORM =~ /java/
                        Config::CONFIG['prefix']
                      else
                        ENV['JRUBY_HOME'] || File.join(ENV['HOME'], '.jruby')
                      end
    self.home = ENV['NAILGUN_HOME'] || File.join(jruby_home, 'tool', 'nailgun')
    self.iface = 'localhost'
    self.port = 2113
    self.runtime_pool_size = 0
        
    namespace :nailgun do
      tmp = lambda { |*files| File.join(Dir.tmpdir, "nailgun", *files) }
      dist_zip = Buildr.download(tmp[NAME + ".zip"] => URL)
      dist_dir = Buildr.unzip(tmp[NAME] => dist_zip)
      
      if File.exist?(File.join(home, NAME + ".jar"))
        ng_jar = file(File.join(home, NAME + ".jar"))
      else
        ng_jar = file(tmp[NAME, NAME, NAME+".jar"] => dist_dir)
      end
      
      self.artifact = Buildr.artifact(ARTIFACT_SPEC).from(ng_jar)
      
      compiled_bin = tmp[NAME, NAME, 'ng']
      compiled_bin << '.exe' if Config::CONFIG['host_os'] =~ /mswin/i
      compiled_bin = file(compiled_bin => dist_dir.target) do |task|
        unless task.to_s.pathmap('%x') == '.exe'
          Dir.chdir(task.to_s.pathmap('%d')) do
            puts "Compiling #{task.to_s}"
            system('make', task.to_s.pathmap('%f')) or
              fail "Nailgun binary compilation failed."
          end
        end
      end

      installed_bin = file(File.join(home, 
          compiled_bin.to_s.pathmap('%f')) => compiled_bin) do |task|
        mkpath task.to_s.pathmap('%d'), :verbose => false
        cp compiled_bin.to_s, task.to_s, :verbose => false
      end

      task :boot => artifact do
        if $nailgun_server
          raise "Already nunning on Nailgun server: #{$nailgun_server}"
        end
        BOOT.call
      end
      
      task :start => [installed_bin, :boot] do
        factory = BuildrFactory.new(runtime_pool_size)
        $nailgun_server = BuildrServer.new(iface, port, factory)
        puts "Starting #{$nailgun_server}"
        $nailgun_server.start_server
      end
    end # namespace :nailgun

    
  end # module Nailgun

  Nailgun::BOOT = lambda do
    require 'jruby'
    
    class Application
      def buildfile(dir = nil, candidates = nil)
        Nailgun::Util.find_buildfile(dir || Dir.pwd, candidates || @rakefiles)
      end

      def clear_invoked
        tasks.each { |t| t.instance_variable_set(:@already_invoked, false) }
      end
    end

    module Nailgun
      class ::ConcreteJavaProxy
        def self.jclass(name = nil)
          name ||= self.java_class.name
          Util.class_for_name(name)
        end
        
        def self.jnew(*args)
          objs = []
          classes = args.map do |a|
            case a
            when nil
              obj << nil
              nil
            when Hash
              objs << a.keys.first
              cls = a.values.first
              cls = Util.proxy_class(cls) if String == cls
              cls
            else
              objs << a
              a.java_class
            end
          end
          classes = classes.to_java(java.lang.Class)
          ctor = jclass.getDeclaredConstructor(classes)
          ctor.setAccessible(true)
          ctor.newInstance(objs.to_java(java.lang.Object))
        end
      end
      
      module Util
        extend self

        def class_for_name(name)
          java.lang.Class.forName(name)
        end
        
        def add_to_sysloader(path)
          sysloader = java.lang.ClassLoader.system_class_loader
          add_url_method = class_for_name('java.net.URLClassLoader').
            getDeclaredMethod('addURL', [java.net.URL].to_java(java.lang.Class))
          add_url_method.accessible = true
          add_url_method.invoke(sysloader, [java.io.File.new(path.to_s).
                                            toURL].to_java(java.net.URL))
        end
        add_to_sysloader Nailgun.artifact
                
        def proxy_class(name)
          JavaUtilities.get_proxy_class(name)
        end

        def find_buildfile(pwd, candidates)
          buildfile = candidates.find { |c| File.file?(File.join(pwd, c)) }
          return File.expand_path(buildfile, pwd) if buildfile
          updir = File.dirname(pwd)
          return nil if File.expand_path(updir) == File.expand_path(pwd)
          find_buildfile(updir, candidates)
        end

        def benchmark(action = 'Completed', verbose = true)
          result = nil
          times = Benchmark.measure do
            result = yield
          end
          if verbose
            real = []
            real << ("%ih" % (times.real / 3600)) if times.real >= 3600
            real << ("%im" % ((times.real / 60) % 60)) if times.real >= 60
            real << ("%.3fs" % (times.real % 60))
            puts "#{action} in #{real.join}"
          end
          result
        end

        import org.jruby.RubyIO 
        def redirect_stdio(runtime, nail)
          result = nil
          stdin  = runtime.global_variables.get('$stdin')
          stdout = runtime.global_variables.get('$stdout')
          stderr = runtime.global_variables.get('$stderr')

          set_in = lambda do |i|
            runtime.global_variables.set('$stdin', i)
            runtime.object.send(:remove_const, 'STDIN')
            runtime.object.send(:const_set, 'STDIN', i)
          end
          
          begin
            p nail.in
            input  = RubyIO.jnew(runtime, java.lang.System.in => java.io.InputStream)
            output = RubyIO.jnew(runtime, nail.out => java.io.OutputStream)
            error = RubyIO.jnew(runtime, nail.err => java.io.OutputStream)
            #stdin.reopen(input, 'r') # not working on jruby :(
            #set_in.call(input)
            stdout.reopen(output)
            stderr.reopen(error)
            result = yield
          ensure
            input  = RubyIO.jnew(runtime, java.lang.System.in => java.io.InputStream)
            output = RubyIO.jnew(runtime, java.lang.System.out => java.io.OutputStream)
            error = RubyIO.jnew(runtime, java.lang.System.err => java.io.OutputStream)
            #set_in.call(input)
            stdout.reopen(output)
            stderr.reopen(error)
          end
          result
        end
        
      end

      class BuildrNail 
        include org.apache.buildr.BuildrNail
        Main = Util.proxy_class 'org.apache.buildr.BuildrNail$Main'
        
        attr_reader :buildfile<commit_msg>Redirect stdio to nailgun client.<commit_after>module Buildr
  
  # To start the nailgun server run the ng:start task, this will
  # download, compile and installed nailgun if needed, afterwards
  # it will start the nailgun server.
  #
  # Issues:
  #   JRuby 1.1RC2 fails at reopening STDIN, so the workaround is
  #   to redefine the $stdin global and STDIN constant. 
  #   Kernel#gets, readline, etc, must be avoided and you should use
  #   $stdin#gets
  module Nailgun 
    VERSION = '0.7.1'
    NAME = "nailgun-#{VERSION}"
    URL = "http://downloads.sourceforge.net/nailgun/#{NAME}.zip"
    ARTIFACT_SPEC = "com.martiansoftware:nailgun:jar:#{VERSION}"

    class << self
      attr_accessor :artifact
      attr_accessor :iface, :port, :runtime_pool_size
      attr_accessor :jruby_home, :home
    end

    self.jruby_home = if PLATFORM =~ /java/
                        Config::CONFIG['prefix']
                      else
                        ENV['JRUBY_HOME'] || File.join(ENV['HOME'], '.jruby')
                      end
    self.home = ENV['NAILGUN_HOME'] || File.join(jruby_home, 'tool', 'nailgun')
    self.iface = 'localhost'
    self.port = 2113
    self.runtime_pool_size = 0
        
    namespace :nailgun do
      tmp = lambda { |*files| File.join(Dir.tmpdir, "nailgun", *files) }
      dist_zip = Buildr.download(tmp[NAME + ".zip"] => URL)
      dist_dir = Buildr.unzip(tmp[NAME] => dist_zip)
      
      if File.exist?(File.join(home, NAME + ".jar"))
        ng_jar = file(File.join(home, NAME + ".jar"))
      else
        ng_jar = file(tmp[NAME, NAME, NAME+".jar"] => dist_dir)
      end
      
      self.artifact = Buildr.artifact(ARTIFACT_SPEC).from(ng_jar)
      
      compiled_bin = tmp[NAME, NAME, 'ng']
      compiled_bin << '.exe' if Config::CONFIG['host_os'] =~ /mswin/i
      compiled_bin = file(compiled_bin => dist_dir.target) do |task|
        unless task.to_s.pathmap('%x') == '.exe'
          Dir.chdir(task.to_s.pathmap('%d')) do
            puts "Compiling #{task.to_s}"
            system('make', task.to_s.pathmap('%f')) or
              fail "Nailgun binary compilation failed."
          end
        end
      end

      installed_bin = file(File.join(home, 
          compiled_bin.to_s.pathmap('%f')) => compiled_bin) do |task|
        mkpath task.to_s.pathmap('%d'), :verbose => false
        cp compiled_bin.to_s, task.to_s, :verbose => false
      end

      task :boot => artifact do |task|
        if $nailgun_server
          raise "Already nunning on Nailgun server: #{$nailgun_server}"
        end
        tasks = Rake.application.instance_eval { @top_level_tasks.dup }
        tasks.delete_if do |t| 
          t =~ /^(buildr:initialize|(ng|nailgun):.+)$/
        end
        unless tasks.empty?
          raise "Don't specify more targets when starting Nailgun server"
        end
        BOOT.call
      end
      
      task :start => [installed_bin, :boot] do
        factory = BuildrFactory.new(runtime_pool_size)
        $nailgun_server = BuildrServer.new(iface, port, factory)
        puts "Starting #{$nailgun_server}"
        $nailgun_server.start_server
      end
    end # namespace :nailgun

    
  end # module Nailgun

  Nailgun::BOOT = lambda do
    require 'jruby'
    
    class ::ConcreteJavaProxy
      def self.jclass(name = nil)
        name ||= self.java_class.name
        Nailgun::Util.class_for_name(name)
      end
      
      def self.jnew(*args)
        objs = []
        classes = args.map do |a|
          case a
          when nil
            obj << nil
            nil
          when Hash
            objs << a.keys.first
            cls = a.values.first
            cls = Nailgun::Util.proxy_class(cls) if String == cls
            cls
          else
            objs << a
            a.java_class
          end
        end
        classes = classes.to_java(java.lang.Class)
        ctor = jclass.getDeclaredConstructor(classes)
        ctor.setAccessible(true)
        ctor.newInstance(objs.to_java(java.lang.Object))
      end
    end
    
    class Application
      def buildfile(dir = nil, candidates = nil)
        Nailgun::Util.find_buildfile(dir || Dir.pwd, candidates || @rakefiles)
      end

      def clear_invoked
        tasks.each { |t| t.instance_variable_set(:@already_invoked, false) }
      end
    end

    module Nailgun
      
      module Util
        extend self

        def class_for_name(name)
          java.lang.Class.forName(name)
        end
        
        def add_to_sysloader(path)
          sysloader = java.lang.ClassLoader.system_class_loader
          add_url_method = class_for_name('java.net.URLClassLoader').
            getDeclaredMethod('addURL', [java.net.URL].to_java(java.lang.Class))
          add_url_method.accessible = true
          add_url_method.invoke(sysloader, [java.io.File.new(path.to_s).
                                            toURL].to_java(java.net.URL))
        end
        add_to_sysloader Nailgun.artifact
                
        def proxy_class(name)
          JavaUtilities.get_proxy_class(name)
        end

        def find_buildfile(pwd, candidates)
          buildfile = candidates.find { |c| File.file?(File.join(pwd, c)) }
          return File.expand_path(buildfile, pwd) if buildfile
          updir = File.dirname(pwd)
          return nil if File.expand_path(updir) == File.expand_path(pwd)
          find_buildfile(updir, candidates)
        end

        def benchmark(action = 'Completed', verbose = true)
          result = nil
          times = Benchmark.measure do
            result = yield
          end
          if verbose
            real = []
            real << ("%ih" % (times.real / 3600)) if times.real >= 3600
            real << ("%im" % ((times.real / 60) % 60)) if times.real >= 60
            real << ("%.3fs" % (times.real % 60))
            puts "#{action} in #{real.join}"
          end
          result
        end

        import org.jruby.RubyIO 
        def redirect_stdio(runtime, nail)
          result = nil
          stdin  = runtime.global_variables.get('$stdin')
          stdout = runtime.global_variables.get('$stdout')
          stderr = runtime.global_variables.get('$stderr')

          set_in = lambda do |i|
            runtime.global_variables.set('$stdin', i)
            runtime.object.send(:remove_const, 'STDIN')
            runtime.object.send(:const_set, 'STDIN', i)
          end
          
          begin
            input  = RubyIO.jnew(runtime, java.lang.System.in => java.io.InputStream)
            output = RubyIO.jnew(runtime, nail.out => java.io.OutputStream)
            error = RubyIO.jnew(runtime, nail.err => java.io.OutputStream)
            #stdin.reopen(input, 'r') # not working on jruby :(
            set_in.call(input)
            stdout.reopen(output)
            stderr.reopen(error)
            result = yield
          ensure
            input  = RubyIO.jnew(runtime, java.lang.System.in => java.io.InputStream)
            output = RubyIO.jnew(runtime, java.lang.System.out => java.io.OutputStream)
            error = RubyIO.jnew(runtime, java.lang.System.err => java.io.OutputStream)
            set_in.call(input)
            stdout.reopen(output)
            stderr.reopen(error)
          end
          result
        end
        
      end

      class BuildrNail 
        include org.apache.buildr.BuildrNail
        Main = Util.proxy_class 'org.apache.buildr.BuildrNail$Main'
        
        attr_reader :buildfile
{'fraction_non_alphanumeric': 0.11071946921003525, 'fraction_numerical': 0.003663003663003663, 'mean_word_length': 1.8642121931908155, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3581622', 'n_tokens_mistral': 4557, 'n_tokens_neox': 4380, 'n_words': 931}