Sha256: 37948819afaba7c6a3e6531467851f352175fe9054f68fc094212b8b87d1ba5e

Contents?: true

Size: 826 Bytes

Versions: 2

Compression:

Stored size: 826 Bytes

Contents

# frozen_string_literal: true

module Spandx
  class Database
    attr_reader :path, :url

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

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

    def read(file)
      IO.read(File.join(path, file))
    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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spandx-0.1.7 lib/spandx/database.rb
spandx-0.1.6 lib/spandx/database.rb