<commit_before>#!/usr/bin/env ruby
# encoding: utf-8

require 'yaml'
require 'json'
require 'pathname'
require 'fileutils'
require 'digest'


buildpacks_ci_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
require_relative "#{buildpacks_ci_dir}/lib/git-client"

source_file = JSON.parse(open('source/data.json').read)
source_version = source_file.dig('version', 'ref')
stack = ENV['STACK']
build_file = JSON.parse(open("builds/binary-builds-new/dotnet-sdk/#{source_version}-#{stack}.json").read)
dotnet_sdk_dependency_url = build_file['url']
git_commit_sha = build_file.dig('git_commit_sha')
sdk_source_url = build_file.dig('source', 'url')
sdk_version = build_file['version']
tracker_story_id = build_file['tracker_story_id']

class ExtractDotnetRuntime
  def initialize(
      buildpacks_ci_dir,
      sdk_version,
      dotnet_sdk_dependency_url,
      git_commit_sha,
      sdk_source_url,
      tracker_story_id,
      stack
  )
    @buildpacks_ci_dir = buildpacks_ci_dir
    @sdk_version = sdk_version
    @dotnet_sdk_dependency_url = dotnet_sdk_dependency_url
    @dotnet_sdk_git_sha = git_commit_sha
    @dotnet_sdk_source_url = sdk_source_url

    @dotnet_sdk_tar = File.join("/tmp", "dotnet_sdk.tar.xz")
    @dotnet_sdk_dir = File.join("/tmp", "dotnet_sdk")
    @tracker_story_id = tracker_story_id

    @stack = stack
  end

  def run
    download
    extract
    retar
    write_yaml
  end

  private

  def download
    raise "Downloading dotnet sdk tar failed" unless system("curl", "-o", @dotnet_sdk_tar, @dotnet_sdk_dependency_url)
  end

  def extract
    FileUtils.mkdir_p(@dotnet_sdk_dir)
    raise "Extracting dotnet sdk failed" unless system("tar", "xf", @dotnet_sdk_tar, "-C", @dotnet_sdk_dir)
  end

  def retar
    @dotnet_runtime_versions = Dir["#{@dotnet_sdk_dir}/shared/Microsoft.NETCore.App/*"].map{ |f| Pathname.new(f).basename.to_s }

    Dir.chdir(@dotnet_sdk_dir) do
      @dotnet_runtime_versions.each do |version|
        system("tar Jcf #{dotnet_runtime_tar(version)} shared/Microsoft.NETCore.App/#{version} host *.txt") or raise "Tarring the dotnet runtime failed"
      end
    end
  end

  def dotnet_runtime_tar(version)
    File.join(@buildpacks_ci_dir, '..', 'binary-builder-artifacts', "dotnet-runtime.#{version}.linux-amd64.tar.xz")
  end

  def write_yaml
    input_dir = File.expand_path('builds')
    output_dir = File.expand_path('builds-artifacts')
    system "rsync", "-a", "#{input_dir}/", output_dir


    Dir.chdir(output_dir) do
      GitClient.set_global_config('user.email', '<EMAIL>')
      GitClient.set_global_config('user.name', 'CF Buildpacks Team CI Server')
    end

    @dotnet_runtime_versions.each do |version|
      runtime_build_file = File.join(output_dir, 'binary-builds-new', 'dotnet-runtime', "#{version}-#{@stack}.json")

      md5sum = Digest::MD5.file(dotnet_runtime_tar(version)).hexdigest
      shasum = Digest::SHA256.file(dotnet_runtime_tar(version)).hexdigest

      output_file = dotnet_runtime_tar(version).gsub('.tar.xz', "-#{@stack}-#{shasum[0..7]}.tar.xz")
      FileUtils.mv(dotnet_runtime_tar(version), output_file)

      runtime_build_data = {<commit_msg>Created correctly named directory to put runtime metadata in.<commit_after>#!/usr/bin/env ruby
# encoding: utf-8

require 'yaml'
require 'json'
require 'pathname'
require 'fileutils'
require 'digest'


buildpacks_ci_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
require_relative "#{buildpacks_ci_dir}/lib/git-client"

source_file = JSON.parse(open('source/data.json').read)
source_version = source_file.dig('version', 'ref')
stack = ENV['STACK']
build_file = JSON.parse(open("builds/binary-builds-new/dotnet-sdk/#{source_version}-#{stack}.json").read)
dotnet_sdk_dependency_url = build_file['url']
git_commit_sha = build_file.dig('git_commit_sha')
sdk_source_url = build_file.dig('source', 'url')
sdk_version = build_file['version']
tracker_story_id = build_file['tracker_story_id']

class ExtractDotnetRuntime
  def initialize(
      buildpacks_ci_dir,
      sdk_version,
      dotnet_sdk_dependency_url,
      git_commit_sha,
      sdk_source_url,
      tracker_story_id,
      stack
  )
    @buildpacks_ci_dir = buildpacks_ci_dir
    @sdk_version = sdk_version
    @dotnet_sdk_dependency_url = dotnet_sdk_dependency_url
    @dotnet_sdk_git_sha = git_commit_sha
    @dotnet_sdk_source_url = sdk_source_url

    @dotnet_sdk_tar = File.join("/tmp", "dotnet_sdk.tar.xz")
    @dotnet_sdk_dir = File.join("/tmp", "dotnet_sdk")
    @tracker_story_id = tracker_story_id

    @stack = stack
  end

  def run
    download
    extract
    retar
    write_yaml
  end

  private

  def download
    raise "Downloading dotnet sdk tar failed" unless system("curl", "-o", @dotnet_sdk_tar, @dotnet_sdk_dependency_url)
  end

  def extract
    FileUtils.mkdir_p(@dotnet_sdk_dir)
    raise "Extracting dotnet sdk failed" unless system("tar", "xf", @dotnet_sdk_tar, "-C", @dotnet_sdk_dir)
  end

  def retar
    @dotnet_runtime_versions = Dir["#{@dotnet_sdk_dir}/shared/Microsoft.NETCore.App/*"].map{ |f| Pathname.new(f).basename.to_s }

    Dir.chdir(@dotnet_sdk_dir) do
      @dotnet_runtime_versions.each do |version|
        system("tar Jcf #{dotnet_runtime_tar(version)} shared/Microsoft.NETCore.App/#{version} host *.txt") or raise "Tarring the dotnet runtime failed"
      end
    end
  end

  def dotnet_runtime_tar(version)
    File.join(@buildpacks_ci_dir, '..', 'binary-builder-artifacts', "dotnet-runtime.#{version}.linux-amd64.tar.xz")
  end

  def write_yaml
    input_dir = File.expand_path('builds')
    output_dir = File.expand_path('builds-artifacts')
    system "rsync", "-a", "#{input_dir}/", output_dir


    Dir.chdir(output_dir) do
      GitClient.set_global_config('user.email', '<EMAIL>')
      GitClient.set_global_config('user.name', 'CF Buildpacks Team CI Server')
    end

    @dotnet_runtime_versions.each do |version|
      dotnet_runtime_dir = File.join(output_dir, 'binary-builds-new', 'dotnet-runtime')
      Dir.mkdir(dotnet_runtime_dir) unless File.exists?(dotnet_runtime_dir)

      runtime_build_file = File.join(dotnet_runtime_dir, "#{version}-#{@stack}.json")

      md5sum = Digest::MD5.file(dotnet_runtime_tar(version)).hexdigest
      shasum = Digest::SHA256.file(dotnet_runtime_tar(version)).hexdigest

      output_file = dotnet_runtime_tar(version).gsub('.tar.xz', "-#{@stack}-#{shasum[0..7]}.tar.xz")
      FileUtils.mv(dotnet_runtime_tar(version), output_file)

      runtime_build_data = {
{'fraction_non_alphanumeric': 0.1318377381684081, 'fraction_numerical': 0.003073140749846343, 'mean_word_length': 4.497466216216216, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3086906', 'n_tokens_mistral': 2378, 'n_tokens_neox': 2304, 'n_words': 373}