Sha256: 4fc2f93515a16e69043d18d82d401daccce43c97fcf8129832dbfef5c934b767

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class Git
      attr_reader :path, :url

      def initialize(url:)
        @url = url
        @path = path_for(url)
      end

      def update!
        dotgit? ? pull! : clone!
      end

      def expand_path(relative_path)
        File.join(path, relative_path)
      end

      def read(path)
        update! unless dotgit?

        full_path = expand_path(path)
        IO.read(full_path) if File.exist?(full_path)
      end

      def open(path, mode: 'r')
        update! unless dotgit?

        full_path = expand_path(path)
        return unless File.exist?(full_path)

        File.open(full_path, mode) do |io|
          yield io
        end
      end

      private

      def path_for(url)
        uri = URI.parse(url)
        name = uri.path.gsub(/\.git$/, '')
        File.expand_path(File.join(Dir.home, '.local', 'share', name))
      end

      def dotgit?
        File.directory?(File.join(path, '.git'))
      end

      def clone!
        system('git', 'clone', '--quiet', url, path)
      end

      def pull!
        within do
          system('git', 'pull', '--no-rebase', '--quiet', 'origin', 'master')
        end
      end

      def within
        Dir.chdir(path) do
          yield
        end
      end
    end

    Database = Git
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spandx-0.12.3 lib/spandx/core/git.rb
spandx-0.12.2 lib/spandx/core/git.rb
spandx-0.12.1 lib/spandx/core/git.rb
spandx-0.12.0 lib/spandx/core/git.rb