Skip to content
Snippets Groups Projects
Utils.php 2.12 KiB
Newer Older
jurgenhaas's avatar
jurgenhaas committed

namespace LakeDrops\Component\Composer;

/**
 * Manages composer.json files.
 */
final class Utils {

jurgenhaas's avatar
jurgenhaas committed
   * List of all ignored git patterns.
   *
jurgenhaas's avatar
jurgenhaas committed
  private static array $ignoredGitPatterns;
jurgenhaas's avatar
jurgenhaas committed

jurgenhaas's avatar
jurgenhaas committed
   * Imploded list of all git lfs patterns.
   *
jurgenhaas's avatar
jurgenhaas committed
  private static string $lfsGitPatterns;
  /**
   * Make sure that git init was called.
   */
  protected static function gitInit(): void {
    if (!file_exists('.git')) {
      passthru(escapeshellcmd('git init --initial-branch=develop'));
jurgenhaas's avatar
jurgenhaas committed
   * Helper function to call git.
   *
jurgenhaas's avatar
jurgenhaas committed
   *   The git command to execute.
   * @param string|null $path
jurgenhaas's avatar
jurgenhaas committed
   *   The optional path where to execute the git command.
jurgenhaas's avatar
jurgenhaas committed
  public static function git(string $command, ?string $path = NULL): void {
    $prefix = $path === NULL ? '' : 'cd ' . $path . ' && ';
    passthru($prefix . escapeshellcmd('git -c "user.email=composer@lakedrops.com" ' . $command));
jurgenhaas's avatar
jurgenhaas committed
   * Helper function to add a new pattern to .gitignore.
   *
jurgenhaas's avatar
jurgenhaas committed
   *   The pattern to be added.
  public static function gitIgnore(string $pattern): void {
    if (!isset(self::$ignoredGitPatterns)) {
      if (file_exists('.gitignore')) {
        self::$ignoredGitPatterns = explode(PHP_EOL, file_get_contents('.gitignore'));
      }
      else {
        self::$ignoredGitPatterns = [];
      }
    }

jurgenhaas's avatar
jurgenhaas committed
    if (in_array($pattern, self::$ignoredGitPatterns, TRUE)) {
      return;
    }

    self::$ignoredGitPatterns[] = $pattern;
    self::git('ignore ' . $pattern);
  }

jurgenhaas's avatar
jurgenhaas committed
   * Helper function to add a new pattern to git LFS.
   *
jurgenhaas's avatar
jurgenhaas committed
   *   The pattern to be added.
jurgenhaas's avatar
jurgenhaas committed
  public static function gitLfs(string $pattern): void {
    if (!isset(self::$lfsGitPatterns)) {
      $output = [];
      exec('git lfs track', $output);
      self::$lfsGitPatterns = implode(PHP_EOL, $output);
    }

    if (strpos(self::$lfsGitPatterns, $pattern)) {
      return;
    }

    self::$lfsGitPatterns = $pattern . PHP_EOL;
    self::git('lfs track ' . $pattern);
  }

jurgenhaas's avatar
jurgenhaas committed
}