Sha256: 83e059fb246728ab763437dad778d7081eb001f6f2f714b6dc6c51b1490b1517

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require "git"

module Fontist
  class Repo
    class << self
      def setup(name, url)
        ensure_private_formulas_path_exists
        fetch_repo(name, url)
        Index.rebuild
      end

      def update(name)
        path = repo_path(name)
        unless Dir.exist?(path)
          raise(Errors::RepoNotFoundError, "No such repo '#{name}'.")
        end

        git = Git.open(path)
        git.pull("origin", git.current_branch)

        Index.rebuild
      rescue Git::GitExecuteError => e
        raise Errors::RepoCouldNotBeUpdatedError.new(<<~MSG.chomp)
          Formulas repo '#{name}' could not be updated.
          Please consider reinitializing it with:
            fontist remove #{name}
            fontist setup #{name} REPO_URL

          Git error:
          #{e.message}
        MSG
      end

      def remove(name)
        path = repo_path(name)
        unless Dir.exist?(path)
          raise(Errors::RepoNotFoundError, "No such repo '#{name}'.")
        end

        FileUtils.rm_r(path)
        Index.rebuild
      end

      def list
        Dir.glob(Fontist.private_formulas_path.join("*"))
          .select { |path| File.directory?(path) }
          .map { |path| File.basename(path) }
      end

      private

      def ensure_private_formulas_path_exists
        Fontist.private_formulas_path.tap do |path|
          FileUtils.mkdir_p(path) unless Dir.exist?(path)
        end
      end

      def fetch_repo(name, url)
        path = repo_path(name)
        if Dir.exist?(path)
          Git.open(path).pull
        else
          repo = Git.clone(url, path, depth: 1)
          if repo.branches[:main].name != repo.current_branch
            # https://github.com/ruby-git/ruby-git/issues/531
            repo.checkout(:main).pull
          end
        end
      end

      def repo_path(name)
        Fontist.private_formulas_path.join(name)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fontist-1.15.1 lib/fontist/repo.rb
fontist-1.15.0 lib/fontist/repo.rb
fontist-1.14.6 lib/fontist/repo.rb