Class: Licensed::Sources::Dep

Inherits:
Source
  • Object
show all
Defined in:
lib/licensed/sources/dep.rb

Instance Attribute Summary

Attributes inherited from Source

#config

Instance Method Summary collapse

Methods inherited from Source

#dependencies, full_type, #ignored?, inherited, #initialize, type, type_and_version

Constructor Details

This class inherits a constructor from Licensed::Sources::Source

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/licensed/sources/dep.rb', line 7

def enabled?
  go_dep_available?
end

#enumerate_dependenciesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/licensed/sources/dep.rb', line 11

def enumerate_dependencies
  packages.map do |package|
    package_dir = config.pwd.join("vendor", package[:name])
    search_root = config.pwd.join("vendor", package[:project])

    Dependency.new(
      name: package[:name],
      version: package[:version],
      path: package_dir.to_s,
      search_root: search_root.to_s,
      metadata: {
        "type"        => Dep.type,
        "homepage"    => homepage(package[:name])
      }
    )
  end
end

#go_dep_available?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/licensed/sources/dep.rb', line 57

def go_dep_available?
  gopkg_lock_path.exist?
end

#go_std_package?(import_path) ⇒ Boolean

Returns whether the package is part of the go std list. Replaces “golang.org” with “golang_org” to match packages listed in ‘go list std` as “vendor/golang_org/*” but are vendored as “vendor/golang.org/*”

Returns:

  • (Boolean)


52
53
54
55
# File 'lib/licensed/sources/dep.rb', line 52

def go_std_package?(import_path)
  return true if go_std_packages.include? "vendor/#{import_path}"
  go_std_packages.include? "vendor/#{import_path.sub(/^golang.org/, "golang_org")}"
end

#go_std_packagesObject

Returns a list of go standard packages



66
67
68
69
70
71
# File 'lib/licensed/sources/dep.rb', line 66

def go_std_packages
  @std_packages ||= begin
    return [] unless Licensed::Shell.tool_available?("go")
    Licensed::Shell.execute("go", "list", "std").lines.map(&:strip)
  end
end

#gopkg_lock_pathObject



61
62
63
# File 'lib/licensed/sources/dep.rb', line 61

def gopkg_lock_path
  config.pwd.join("Gopkg.lock")
end

#homepage(import_path) ⇒ Object

Returns the pkg.go.dev page for a package.



44
45
46
47
# File 'lib/licensed/sources/dep.rb', line 44

def homepage(import_path)
  return unless import_path
  "https://pkg.go.dev/#{import_path}"
end

#packagesObject

Returns an array of dependency packages specified from Gopkg.lock



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/licensed/sources/dep.rb', line 30

def packages
  gopkg_lock = Tomlrb.load_file(gopkg_lock_path, symbolize_keys: true)
  return [] unless gopkg_lock && gopkg_lock[:projects]

  gopkg_lock[:projects].flat_map do |project|
    # map each package to a full import path
    # then return a hash for each import path containing the path and the version
    project[:packages].map { |package| package == "." ? project[:name] : "#{project[:name]}/#{package}" }
                      .reject { |import_path| go_std_package?(import_path) }
                      .map { |import_path| { name: import_path, version: project[:revision], project: project[:name] } }
  end
end