class Bashly::LibrarySource

Attributes

uri[R]

Public Class Methods

new(uri = nil) click to toggle source
# File lib/bashly/library_source.rb, line 7
def initialize(uri = nil)
  @uri = uri || File.expand_path('libraries', __dir__)
  transform_github_uri if /^github(:|-)/.match? @uri
end

Public Instance Methods

cleanup() click to toggle source
# File lib/bashly/library_source.rb, line 35
def cleanup
  FileUtils.rm_rf(File.join(Dir.tmpdir, 'bashly-libs-*'))
end
config() click to toggle source
# File lib/bashly/library_source.rb, line 16
def config
  @config ||= YAML.load_file config_path
end
config_path() click to toggle source
# File lib/bashly/library_source.rb, line 27
def config_path
  @config_path ||= if File.exist?("#{path}/libraries.yml")
    "#{path}/libraries.yml"
  else
    raise "Cannot find #{path}/libraries.yml"
  end
end
git?() click to toggle source
# File lib/bashly/library_source.rb, line 12
def git?
  /^(git|github|github-ssh):/.match? uri
end
libraries() click to toggle source
# File lib/bashly/library_source.rb, line 20
def libraries
  config.to_h do |name, spec|
    upgrade_string = "#{uri};#{name}"
    [name.to_sym, Library.new(path, spec, upgrade_string: upgrade_string)]
  end
end

Private Instance Methods

git_clone() click to toggle source
# File lib/bashly/library_source.rb, line 49
def git_clone
  dir = Dir.mktmpdir 'bashly-libs-'
  safe_run "git clone --quiet #{git_specs[:url]} #{dir}"
  safe_run %[git -C "#{dir}" checkout --quiet #{git_specs[:ref]}] if git_specs[:ref]

  "#{dir}#{git_specs[:path]}"
end
git_specs() click to toggle source
# File lib/bashly/library_source.rb, line 57
def git_specs
  @git_specs ||= begin
    parts = uri.match(%r{git:(?<url>.*\.git)(?:/)?(?<path>/[^@]+)?@?(?<ref>.*)})
    raise 'Invalid source' unless parts

    url = parts[:url]
    raise 'Invalid git URL' unless url

    path = parts[:path]
    ref = parts[:ref].empty? ? nil : parts[:ref]

    { url: url, path: path, ref: ref }
  end
end
path() click to toggle source
# File lib/bashly/library_source.rb, line 41
def path
  @path ||= if uri.start_with? 'git:'
    git_clone
  else
    uri
  end
end
safe_run(cmd) click to toggle source
# File lib/bashly/library_source.rb, line 72
def safe_run(cmd)
  raise "Failed running command:\nm`#{cmd}`" unless system cmd
end
transform_github_uri() click to toggle source
# File lib/bashly/library_source.rb, line 76
def transform_github_uri
  if (matches = uri.match(%r{github-ssh:(?<user>[^/]+)/(?<repo>[^/]+)(?<rest>.*)}))
    @uri = "git:git@github.com:#{matches[:user]}/#{matches[:repo]}.git#{matches[:rest]}"
  elsif (matches = uri.match(%r{github:(?<user>[^/]+)/(?<repo>[^/]+)(?<rest>.*)}))
    @uri = "git:https://github.com/#{matches[:user]}/#{matches[:repo]}.git#{matches[:rest]}"
  end
end