<commit_before>require 'tempfile'

module Baustelle
  module Jenkins
    class JobTemplate
      def initialize(template, options={})
        @template = template
        @options = options
      end

      def render(prefix: '')
        groovy_template = Tempfile.open(['job', '.groovy'], groovy_scripts_dir)
        groovy_template.puts render_groovy
        groovy_template.close
        Dir.mktmpdir do |output_dir|
          Dir.chdir(job_dsl_dir) do
            path = File.join('jobs', File.basename(groovy_template.path))
            system "gradle -q xml -Psource=#{path} -PoutputDir=#{output_dir}"
          end

          Dir[File.join(output_dir, "*.xml")].inject({}) do |result, filename|
            result[prefix + File.basename(filename, '.xml')] = File.read(filename)
            result
          end
        end
      end

      def render_groovy
        ERB.new(@template).result(binding)
      end

      def method_missing(name)
        @options[name.to_sym] || @options[name]
      end

      private

      def job_dsl_dir
        File.expand_path(File.join(__FILE__, '../../../../ext/jenkins_dsl'))
      end

      def groovy_scripts_dir
        File.join(job_dsl_dir, "jobs")
      end
    end
  end
end<commit_msg>Allow including scripts in jenkins job templates<commit_after>require 'tempfile'

module Baustelle
  module Jenkins
    class JobTemplate
      def initialize(template, options={})
        @template = template
        @options = options
      end

      def render(prefix: '')
        groovy_template = Tempfile.open(['job', '.groovy'], groovy_scripts_dir)
        groovy_template.puts render_groovy
        groovy_template.close
        Dir.mktmpdir do |output_dir|
          Dir.chdir(job_dsl_dir) do
            path = File.join('jobs', File.basename(groovy_template.path))
            system "gradle -q xml -Psource=#{path} -PoutputDir=#{output_dir}"
          end

          Dir[File.join(output_dir, "*.xml")].inject({}) do |result, filename|
            result[prefix + File.basename(filename, '.xml')] = File.read(filename)
            result
          end
        end
      end

      def render_groovy
        ERB.new(@template).result(binding)
      end

      def method_missing(name)
        @options[name.to_sym] || @options[name]
      end

      private

      def include(path)
        template_dir = File.dirname(@template_path)
        File.read(File.join(template_dir, path, '.groovy'))
      end

      def job_dsl_dir
        File.expand_path(File.join(__FILE__, '../../../../ext/jenkins_dsl'))
      end

      def groovy_scripts_dir
        File.join(job_dsl_dir, "jobs")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11411411411411411, 'fraction_numerical': 0.0, 'mean_word_length': 2.183990442054958, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '6799258', 'n_tokens_mistral': 845, 'n_tokens_neox': 804, 'n_words': 165}