Sha256: b9b85a50bf4b356c369ef9ad7a5b148ffbc4e7c15159a92a192a8a142962fb69

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 KB

Contents

module GemBootstrap
  class GitUtils
    class << self
      # create new git repository from a given directory
      #
      # @param [String] base_dir the starting directory
      # @param [String] gem_name the name of the gem we are creating
      def create_git_project(base_dir, gem_name)
        base_dir = expand_path(base_dir)
        files = MiscUtils.files base_dir: base_dir,
                                non_exts: %w(Gemfile
                                             Rakefile
                                             Guardfile
                                             LICENSE
                                             .rubocop.yml
                                             .yardopts
                                             .gitignore) << gem_name,
                                exts: %w(md rb gemspec yml),
                                recursive: true
        git_init(base_dir)
        git_add(base_dir, files)
      end

      private

      # Run the `git init` on a given directory
      #
      # @param [String] base_dir the starting directory
      def git_init(base_dir)
        base_dir = expand_path(base_dir)
        # Note: need to be in the right directory for this to work
        Dir.chdir(base_dir)
        MiscUtils.shell(%w(git init) << base_dir)
      end

      # Add files and perform initial commit to the repository
      #
      # @param [String] base_dir the starting directory
      # @param [Array<String>] files list of files to be used
      def git_add(base_dir, files)
        base_dir = expand_path(base_dir)
        Dir.chdir(base_dir) do
          git = Grit::Repo.new(File.expand_path('.'))
          files.each do |file|
            git.add(file)
          end
          git.commit_index('Initial commit')
        end
      end

      def expand_path(base_dir)
        raise "Invalid directory name `{base_dir}`" if base_dir.nil? || !File.directory?(File.new(base_dir))
        File.expand_path(base_dir)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gem_bootstrap-0.3.2 lib/gem_bootstrap/git_utils.rb
gem_bootstrap-0.3.1 lib/gem_bootstrap/git_utils.rb
gem_bootstrap-0.3.0 lib/gem_bootstrap/git_utils.rb
gem_bootstrap-0.2.9 lib/gem_bootstrap/git_utils.rb
gem_bootstrap-0.2.8 lib/gem_bootstrap/git_utils.rb