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(package_path.join(dep))
                              }, nil, true)
  end
end

#go_listObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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.
    orig_gopath = ENV['GOPATH']
    ENV['GOPATH'] = nil
    val = capture('go list -f "{{join .Deps \"\n\"}}" ./...')
    ENV['GOPATH'] = orig_gopath
    return [] unless val.last
    # Select non-standard packages. `go list std` returns the list of standard
    # dependencies. We then filter those dependencies out of the full list of
    # dependencies.
    deps = val.first.split("\n")
    capture('go list std').first.split("\n").each do |std|
      deps.delete_if do |dep|
        dep =~ /(\/|^)#{std}(\/|$)/
      end
    end
    deps.map do |d|
      dep_parts = d.split('/')
      if dep_parts.length > 2
        dep_parts[0..2].join('/')
      else
        d
      end
    end
  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_sha(path) ⇒ Object



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

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