Sha256: 24db69da72a8686ae8cc9963df066c7c2ee6e1152e3919034fe47e0aa242dd7b

Contents?: true

Size: 1.21 KB

Versions: 7

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module Spandx
  module Core
    class Database
      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?

        File.open(expand_path(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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spandx-0.11.0 lib/spandx/core/database.rb
spandx-0.10.1 lib/spandx/core/database.rb
spandx-0.10.0 lib/spandx/core/database.rb
spandx-0.9.0 lib/spandx/core/database.rb
spandx-0.8.0 lib/spandx/core/database.rb
spandx-0.7.0 lib/spandx/core/database.rb
spandx-0.6.0 lib/spandx/core/database.rb