Class: LicenseFinder::GoVendor

Inherits:
PackageManager show all
Defined in:
lib/license_finder/package_managers/go_vendor.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PackageManager

#capture, command_exists?, current_packages, #current_packages_with_relations, installed?, package_managers

Constructor Details

#initialize(options = {}) ⇒ GoVendor

Returns a new instance of GoVendor.



6
7
8
9
# File 'lib/license_finder/package_managers/go_vendor.rb', line 6

def initialize(options={})
  super
  @full_version = options[:go_full_version]
end

Class Method Details

.package_management_commandObject



47
48
49
# File 'lib/license_finder/package_managers/go_vendor.rb', line 47

def self.package_management_command
  'go'
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
# File 'lib/license_finder/package_managers/go_vendor.rb', line 11

def active?
  return false unless self.class.installed?(@logger)

  (has_go_files? && package_path.exist?).tap do |is_active|
    logger.active self.class, is_active
  end
end

#current_packagesObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/license_finder/package_managers/go_vendor.rb', line 35

def current_packages
  deps = go_list
  vendored_deps = deps.select { |dep| package_path.join(dep).exist? }
  vendored_deps.map do |dep|
    GoPackage.from_dependency({
                               'ImportPath' => dep,
                               'InstallPath' => package_path.join(dep),
                               'Rev' => 'vendored-' + project_sha
                              }, nil, true)
  end
end

#go_listObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/license_finder/package_managers/go_vendor.rb', line 51

def go_list
  Dir.chdir(project_path) do
    # avoid checking canonical import path. some projects uses
    # non-canonical import path and rely on the fact that the deps are
    # checked in. Canonical paths are only checked by `go get'. We
    # discovered that `go list' will print a warning and unfortunately exit
    # with status code 1. Setting GOPATH to nil removes those warnings.
    ENV['GOPATH'] = nil
    val = capture('go list -f \'{{join .Deps "\n"}}\' ./...')
    return [] unless val.last
    # Select non-standard packages. Standard packages tend to be short
    # and have less than two slashes
    val.first.lines.map(&:strip).select { |l| l.split("/").length > 2 }.map { |l| l.split("/")[0..2].join("/") }.uniq
  end
end

#has_go_files?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/license_finder/package_managers/go_vendor.rb', line 19

def has_go_files?
  !Dir[project_path.join("**/*.go")].empty?
end

#package_pathObject



23
24
25
# File 'lib/license_finder/package_managers/go_vendor.rb', line 23

def package_path
  project_path.join("vendor")
end

#project_shaObject



27
28
29
30
31
32
33
# File 'lib/license_finder/package_managers/go_vendor.rb', line 27

def project_sha
  @project_sha ||= Dir.chdir(project_path) do
    val = capture('git rev-list --max-count 1 HEAD')
    raise 'git rev-list failed' unless val.last
    val.first.strip
  end
end