Sha256: 8a1fe8ccdc8ea3c98546c5ad8bb60ce0bbd3aa6a3f8d1a137b00f1f70e64f0ce

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require 'ostruct'

module MultiRepo
  class Repo
    attr_reader :name, :config, :path
    attr_accessor :dry_run

    def initialize(name, path: nil, config: nil, dry_run: false)
      @name    = name
      @dry_run = dry_run
      @config  = OpenStruct.new(config || {})
      @path    = path || MultiRepo.repos_dir.join(name)
    end

    alias to_s inspect

    def git
      @git ||= MultiRepo::Service::Git.new(path: path, clone_source: config.clone_source || "git@github.com:#{name}.git", dry_run: dry_run)
    end

    def chdir
      git # Ensures the clone exists
      Dir.chdir(path) { yield }
    end

    def short_name
      name.split("/").last
    end

    def write_file(file, content, **kwargs)
      if dry_run
        puts "** dry-run: Writing #{path.join(file).expand_path}".light_black
      else
        chdir { File.write(file, content) }
      end
    end

    def rm_file(file)
      return unless path.join(file).exist?

      if dry_run
        puts "** dry-run: Removing #{path.join(file).expand_path}".light_black
      else
        chdir { FileUtils.rm_f(file) }
      end
    end

    def detect_readme_file
      chdir do
        %w[README.md README README.txt].detect do |f|
          File.exist?(f)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
multi_repo-0.5.0 lib/multi_repo/repo.rb
multi_repo-0.4.0 lib/multi_repo/repo.rb