<commit_before>require 'rake'

module Paco
module Env
  class Unity < Base
    include Rake::FileUtilsExt

    attr_reader :unity_path, :project_path

    def initialize
      super

      raise 'error. check PACO_UNITY_PATH env.' \
        if !ENV['PACO_UNITY_PATH'] || !File.exist?(ENV['PACO_UNITY_PATH'])
      raise 'error. check PACO_UNITY_PROJECT_PATH env.' \
        if !ENV['PACO_UNITY_PROJECT_PATH'] || !File.exist?(ENV['PACO_UNITY_PROJECT_PATH'])
      raise 'erorr. check PACO_TEST_UNITY_TEST_TOOLS_PATH env.' \
        if !ENV['PACO_TEST_UNITY_TEST_TOOLS_PATH'] || !File.exist?(ENV['PACO_TEST_UNITY_TEST_TOOLS_PATH'])

      @unity_path   = ENV['PACO_UNITY_PATH'].strip.sub('/$','')
      @project_path = ENV['PACO_UNITY_PROJECT_PATH']
    end

    def build(spec)
      assets = spec.files.select{|f| f.match(/^Assets\//) }
      files  = spec.files - assets

      # Assets 以下を unitypackage にビルドする
      if assets.size > 0 then
        sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode -quit -logFile -projectPath '%s' -exportPackage %s %s",
          @unity_path,
          @project_path,
          spec.files.select{|f| f.match(/^Assets\//) }.join(' '),
          spec.name + '.unitypackage'
        )

        files.push spec.name + '.unitypackage'
      end

      files
    end

    def install(file)
      files = unzip(file)
      specfile = files.select{|f| f.match(/^.+\.paco$/) }.first
      resolve_dependency(specfile)

      files.each do |file|
        if file.match(/\.unitypackage$/) then
          import_package(file)
        end
      end
    end

    def test
      setup_test

      begin
        execute_unity_method('UnityTest.Batch.RunIntegrationTests')
      rescue
        # ignore exit code
      end

      begin
        execute_unity_method('UnityTest.Batch.RunUnitTests')
      rescue
        # ignore exit code
      end
    end

    def create_empty_project
      sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode -quit -logFile -createProject '%s'",
        @unity_path,
        @test_path
      )
    end

    def execute_unity_method(method)
      sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode -logFile -projectPath '%s' -executeMethod %s -resultsFileDirectory='%s'",
        @unity_path,
        @test_path,
        method,
        @test_path
      )
    end

    def import_package(package_path)
      sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode -quit -logFile -projectPath '%s' -importPackage '%s'",
        @unity_path,
        @project_path,
        package_path
      )
    end

    def setup_test
      FileUtils.mkdir_p(@test_path) unless Dir.exist?(@test_path)

      create_empty_project

      # import UnityTestTools package
      import_package(ENV['PACO_TEST_UNITY_TEST_TOOLS_PATH'])

      # @TODO refactor later
      Rake::Task[:install].invoke
    end
  end
end
end<commit_msg>Add verbose switch<commit_after>require 'rake'

module Paco
module Env
  class Unity < Base
    include Rake::FileUtilsExt

    attr_reader :unity_path, :project_path, :verbose

    def initialize
      super

      @verbose = (ENV['VERBOSE']) ? "-logFile" : ""

      raise 'error. check PACO_UNITY_PATH env.' \
        if !ENV['PACO_UNITY_PATH'] || !File.exist?(ENV['PACO_UNITY_PATH'])
      raise 'error. check PACO_UNITY_PROJECT_PATH env.' \
        if !ENV['PACO_UNITY_PROJECT_PATH'] || !File.exist?(ENV['PACO_UNITY_PROJECT_PATH'])
      raise 'erorr. check PACO_TEST_UNITY_TEST_TOOLS_PATH env.' \
        if !ENV['PACO_TEST_UNITY_TEST_TOOLS_PATH'] || !File.exist?(ENV['PACO_TEST_UNITY_TEST_TOOLS_PATH'])

      @unity_path   = ENV['PACO_UNITY_PATH'].strip.sub('/$','')
      @project_path = ENV['PACO_UNITY_PROJECT_PATH']
    end

    def build(spec)
      assets = spec.files.select{|f| f.match(/^Assets\//) }
      files  = spec.files - assets

      # Assets 以下を unitypackage にビルドする
      if assets.size > 0 then
        sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode -quit #{@verbose} -projectPath '%s' -exportPackage %s %s",
          @unity_path,
          @project_path,
          spec.files.select{|f| f.match(/^Assets\//) }.join(' '),
          spec.name + '.unitypackage'
        )

        files.push spec.name + '.unitypackage'
      end

      files
    end

    def install(file)
      files = unzip(file)
      specfile = files.select{|f| f.match(/^.+\.paco$/) }.first
      resolve_dependency(specfile)

      files.each do |file|
        if file.match(/\.unitypackage$/) then
          import_package(file)
        end
      end
    end

    def test
      setup_test

      begin
        execute_unity_method('UnityTest.Batch.RunIntegrationTests')
      rescue
        # ignore exit code
      end

      begin
        execute_unity_method('UnityTest.Batch.RunUnitTests')
      rescue
        # ignore exit code
      end
    end

    def create_empty_project
      sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode -quit #{@verbose} -createProject '%s'",
        @unity_path,
        @test_path
      )
    end

    def execute_unity_method(method)
      sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode #{@verbose} -projectPath '%s' -executeMethod %s -resultsFileDirectory='%s'",
        @unity_path,
        @test_path,
        method,
        @test_path
      )
    end

    def import_package(package_path)
      sh sprintf("'%s/Unity.app/Contents/MacOS/Unity' -batchmode -quit #{@verbose} -projectPath '%s' -importPackage '%s'",
        @unity_path,
        @project_path,
        package_path
      )
    end

    def setup_test
      FileUtils.mkdir_p(@test_path) unless Dir.exist?(@test_path)

      create_empty_project

      # import UnityTestTools package
      import_package(ENV['PACO_TEST_UNITY_TEST_TOOLS_PATH'])

      # @TODO refactor later
      Rake::Task[:install].invoke
    end
  end
end
end
{'fraction_non_alphanumeric': 0.12133105802047782, 'fraction_numerical': 0.00034129692832764505, 'mean_word_length': 2.4496762801648027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1318556', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1964, 'n_words': 371}