<commit_before>package arez.downstream;

import gir.Gir;
import gir.GirException;
import gir.delta.Patch;
import gir.git.Git;
import gir.io.Exec;
import gir.io.FileUtil;
import gir.ruby.Buildr;
import gir.ruby.Ruby;
import gir.sys.SystemProperty;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Nonnull;

@SuppressWarnings( "Duplicates" )
public final class BuildDownstream
{
  public static void main( final String[] args )
    throws Exception
  {
    try
    {
      run();
    }
    catch ( final Exception e )
    {
      System.err.println( "Failed command." );
      e.printStackTrace( System.err );
      System.exit( 42 );
    }
  }

  private static void run()
    throws Exception
  {
    Gir.go( () -> {
      final String version = SystemProperty.get( "arez.version" );
      final Path workingDirectory =
        Paths.get( SystemProperty.get( "arez.deploy_test.work_dir" ) ).toAbsolutePath().normalize();

      final String localRepositoryUrl = SystemProperty.get( "arez.deploy_test.local_repository_url" );

      if ( !workingDirectory.toFile().exists() )
      {
        Gir.messenger().info( "Working directory does not exist." );
        Gir.messenger().info( "Creating directory: " + workingDirectory );
        if ( !workingDirectory.toFile().mkdirs() )
        {
          Gir.messenger().error( "Failed to create working directory: " + workingDirectory );
        }
      }
      Stream.of( "arez-idlestatus" ).forEach( project -> {

        FileUtil.inDirectory( workingDirectory, () -> {
          Gir.messenger().info( "Cloning " + project + " into " + workingDirectory );
          Git.clone( "https://github.com/arez/" + project + ".git", project );
          final Path appDirectory = workingDirectory.resolve( project );
          FileUtil.inDirectory( appDirectory, () -> {
            Git.fetch();
            Git.resetBranch();
            Git.checkout();
            Git.pull();
            Git.deleteLocalBranches();
            Gir.messenger().info( "Processing branch master." );

            Git.checkout( "master" );
            Git.clean();
            final String newBranch = "master-ArezUpgrade-" + version;

            Git.checkout( newBranch, true );
            if ( Git.remoteTrackingBranches().contains( "origin/" + newBranch ) )
            {
              Git.pull();
            }
            Git.clean();

            Gir.messenger().info( "Building branch master prior to modifications." );
            boolean initialBuildSuccess = false;
            try
            {
              customizeBuildr( appDirectory, localRepositoryUrl );
              Ruby.buildr( "clean", "package", "PRODUCT_VERSION=", "PREVIOUS_PRODUCT_VERSION=" );
              initialBuildSuccess = true;
            }
            catch ( final GirException e )
            {
              Gir.messenger().info( "Failed to build branch 'master' before modifications.", e );
            }

            Git.resetBranch();
            Git.clean();

            final String group = "org.realityforge.arez";
            final Function<String, String> patchFunction1 = c -> Buildr.patchMavenCoordinates( c, group, version );
            final boolean patched =
              Patch.patchAndAddFile( appDirectory, appDirectory.resolve( "build.yaml" ), patchFunction1 );

            if ( patched )
            {
              final String message = "Update the '" + group + "' dependencies to version '" + version + "'";
              final Function<String, String> patchFunction = c -> c.replace( "### Unreleased\n\n",
                                                                             "### Unreleased\n\n" + message + "\n\n" );
              Patch.patchAndAddFile( appDirectory, appDirectory.resolve( "CHANGELOG.md" ), patchFunction );
              Git.commit( message );

              Gir.messenger().info( "Building branch master after modifications." );
              customizeBuildr( appDirectory, localRepositoryUrl );

              try
              {
                Ruby.buildr( "perform_release",
                             "LAST_STAGE=PatchChangelogPostRelease",
                             "PRODUCT_VERSION=",
                             "PREVIOUS_PRODUCT_VERSION=" );
                Git.checkout( "master" );
                Exec.system( "git", "merge", newBranch );
                Git.deleteBranch( newBranch );
              }
              catch ( final GirException e )
              {
                if ( !initialBuildSuccess )
                {
                  Gir.messenger().error( "Failed to build branch 'master' before modifications " +
                                         "but branch also failed prior to modifications.", e );
                }
                else
                {
                  Gir.messenger().error( "Failed to build branch 'master' after modifications.", e );
                }
                throw e;
              }
            }
            else
            {
              Gir.messenger().info( "Branch master not rebuilt as no modifications made." );
              Git.checkout( "master" );
              Git.deleteBranch( newBranch );
            }
          } );
        } );
      } );
    } );
  }

  private static void customizeBuildr( @Nonnull final Path appDirectory, @Nonnull final String localRepositoryUrl )
  {
    try<commit_msg>Block to lambda<commit_after>package arez.downstream;

import gir.Gir;
import gir.GirException;
import gir.delta.Patch;
import gir.git.Git;
import gir.io.Exec;
import gir.io.FileUtil;
import gir.ruby.Buildr;
import gir.ruby.Ruby;
import gir.sys.SystemProperty;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Function;
import java.util.stream.Stream;
import javax.annotation.Nonnull;

@SuppressWarnings( "Duplicates" )
public final class BuildDownstream
{
  public static void main( final String[] args )
    throws Exception
  {
    try
    {
      run();
    }
    catch ( final Exception e )
    {
      System.err.println( "Failed command." );
      e.printStackTrace( System.err );
      System.exit( 42 );
    }
  }

  private static void run()
    throws Exception
  {
    Gir.go( () -> {
      final String version = SystemProperty.get( "arez.version" );
      final Path workingDirectory =
        Paths.get( SystemProperty.get( "arez.deploy_test.work_dir" ) ).toAbsolutePath().normalize();

      final String localRepositoryUrl = SystemProperty.get( "arez.deploy_test.local_repository_url" );

      if ( !workingDirectory.toFile().exists() )
      {
        Gir.messenger().info( "Working directory does not exist." );
        Gir.messenger().info( "Creating directory: " + workingDirectory );
        if ( !workingDirectory.toFile().mkdirs() )
        {
          Gir.messenger().error( "Failed to create working directory: " + workingDirectory );
        }
      }
      Stream.of( "arez-idlestatus" ).forEach( project -> FileUtil.inDirectory( workingDirectory, () -> {
        Gir.messenger().info( "Cloning " + project + " into " + workingDirectory );
        Git.clone( "https://github.com/arez/" + project + ".git", project );
        final Path appDirectory = workingDirectory.resolve( project );
        FileUtil.inDirectory( appDirectory, () -> {
          Git.fetch();
          Git.resetBranch();
          Git.checkout();
          Git.pull();
          Git.deleteLocalBranches();
          Gir.messenger().info( "Processing branch master." );

          Git.checkout( "master" );
          Git.clean();
          final String newBranch = "master-ArezUpgrade-" + version;

          Git.checkout( newBranch, true );
          if ( Git.remoteTrackingBranches().contains( "origin/" + newBranch ) )
          {
            Git.pull();
          }
          Git.clean();

          Gir.messenger().info( "Building branch master prior to modifications." );
          boolean initialBuildSuccess = false;
          try
          {
            customizeBuildr( appDirectory, localRepositoryUrl );
            Ruby.buildr( "clean", "package", "PRODUCT_VERSION=", "PREVIOUS_PRODUCT_VERSION=" );
            initialBuildSuccess = true;
          }
          catch ( final GirException e )
          {
            Gir.messenger().info( "Failed to build branch 'master' before modifications.", e );
          }

          Git.resetBranch();
          Git.clean();

          final String group = "org.realityforge.arez";
          final Function<String, String> patchFunction1 = c -> Buildr.patchMavenCoordinates( c, group, version );
          final boolean patched =
            Patch.patchAndAddFile( appDirectory, appDirectory.resolve( "build.yaml" ), patchFunction1 );

          if ( patched )
          {
            final String message = "Update the '" + group + "' dependencies to version '" + version + "'";
            final Function<String, String> patchFunction = c -> c.replace( "### Unreleased\n\n",
                                                                           "### Unreleased\n\n" + message + "\n\n" );
            Patch.patchAndAddFile( appDirectory, appDirectory.resolve( "CHANGELOG.md" ), patchFunction );
            Git.commit( message );

            Gir.messenger().info( "Building branch master after modifications." );
            customizeBuildr( appDirectory, localRepositoryUrl );

            try
            {
              Ruby.buildr( "perform_release",
                           "LAST_STAGE=PatchChangelogPostRelease",
                           "PRODUCT_VERSION=",
                           "PREVIOUS_PRODUCT_VERSION=" );
              Git.checkout( "master" );
              Exec.system( "git", "merge", newBranch );
              Git.deleteBranch( newBranch );
            }
            catch ( final GirException e )
            {
              if ( !initialBuildSuccess )
              {
                Gir.messenger().error( "Failed to build branch 'master' before modifications " +
                                       "but branch also failed prior to modifications.", e );
              }
              else
              {
                Gir.messenger().error( "Failed to build branch 'master' after modifications.", e );
              }
              throw e;
            }
          }
          else
          {
            Gir.messenger().info( "Branch master not rebuilt as no modifications made." );
            Git.checkout( "master" );
            Git.deleteBranch( newBranch );
          }
        } );
      } ) );
    } );
  }

  private static void customizeBuildr( @Nonnull final Path appDirectory, @Nonnull final String localRepositoryUrl )
  {
    try
{'fraction_non_alphanumeric': 0.11549113737075332, 'fraction_numerical': 0.0007385524372230429, 'mean_word_length': 1.826983298538622, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '6987578', 'n_tokens_mistral': 2862, 'n_tokens_neox': 2786, 'n_words': 653}