Class: Licensed::Source::Go
- Inherits:
-
Object
- Object
- Licensed::Source::Go
- Defined in:
- lib/licensed/source/go.rb
Class Method Summary collapse
Instance Method Summary collapse
- #dependencies ⇒ Object
- #enabled? ⇒ Boolean
-
#go_source? ⇒ Boolean
Returns whether go source is found.
-
#go_std_packages ⇒ Object
Returns a list of go standard packages.
-
#gopath ⇒ Object
Returns a GOPATH value from either a configuration value or ENV, with the configuration value preferred over the ENV var.
-
#homepage(import_path) ⇒ Object
Returns the homepage for a package import_path.
-
#initialize(config) ⇒ Go
constructor
A new instance of Go.
-
#non_vendored_import_path(import_path) ⇒ Object
Returns the import path parameter without the vendor component.
-
#package_info(package = nil) ⇒ Object
Returns package information, or {} if package isn’t found.
-
#package_info_command(package) ⇒ Object
Returns package information as a JSON string.
-
#packages ⇒ Object
Returns an array of dependency package import paths.
-
#root_package ⇒ Object
Returns the info for the package under test.
-
#search_root(package_dir) ⇒ Object
Returns the root directory to search for a package license.
-
#vendored_path?(path) ⇒ Boolean
Returns whether a package is vendored or not based on the package import_path.
Constructor Details
#initialize(config) ⇒ Go
Returns a new instance of Go.
13 14 15 |
# File 'lib/licensed/source/go.rb', line 13 def initialize(config) @config = config end |
Class Method Details
.type ⇒ Object
9 10 11 |
# File 'lib/licensed/source/go.rb', line 9 def self.type "go" end |
Instance Method Details
#dependencies ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/licensed/source/go.rb', line 21 def dependencies @dependencies ||= with_configured_gopath do packages.map do |package_name| package = package_info(package_name) import_path = non_vendored_import_path(package_name) if package.empty? next if @config.ignored?("type" => Go.type, "name" => package_name) raise "couldn't find package for #{import_path}" end package_dir = package["Dir"] Dependency.new(package_dir, { "type" => Go.type, "name" => import_path, "summary" => package["Doc"], "homepage" => homepage(import_path), "search_root" => search_root(package_dir), "version" => Licensed::Git.version(package_dir) }) end.compact end end |
#enabled? ⇒ Boolean
17 18 19 |
# File 'lib/licensed/source/go.rb', line 17 def enabled? Licensed::Shell.tool_available?("go") && go_source? end |
#go_source? ⇒ Boolean
Returns whether go source is found
126 127 128 |
# File 'lib/licensed/source/go.rb', line 126 def go_source? @go_source ||= with_configured_gopath { Licensed::Shell.success?("go", "doc") } end |
#go_std_packages ⇒ Object
Returns a list of go standard packages
131 132 133 |
# File 'lib/licensed/source/go.rb', line 131 def go_std_packages @std_packages ||= Licensed::Shell.execute("go", "list", "std").lines.map(&:strip) end |
#gopath ⇒ Object
Returns a GOPATH value from either a configuration value or ENV, with the configuration value preferred over the ENV var
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/licensed/source/go.rb', line 137 def gopath return @gopath if defined?(@gopath) path = @config.dig("go", "GOPATH") @gopath = if path.nil? || path.empty? ENV["GOPATH"] else root = begin Licensed::Git.repository_root rescue Licensed::Shell::Error Pathname.pwd end File.(path, root) end end |
#homepage(import_path) ⇒ Object
Returns the homepage for a package import_path. Assumes that the import path itself is a url domain and path
47 48 49 50 51 52 53 |
# File 'lib/licensed/source/go.rb', line 47 def homepage(import_path) return unless import_path # hacky but generally works due to go packages looking like # "github.com/..." or "golang.org/..." "https://#{import_path}" end |
#non_vendored_import_path(import_path) ⇒ Object
Returns the import path parameter without the vendor component
import_path - Package import path with vendor component
97 98 99 100 101 |
# File 'lib/licensed/source/go.rb', line 97 def non_vendored_import_path(import_path) return unless import_path return import_path unless vendored_path?(import_path) import_path.split("vendor/")[1] end |
#package_info(package = nil) ⇒ Object
Returns package information, or {} if package isn’t found
package - Go package import path
106 107 108 109 110 |
# File 'lib/licensed/source/go.rb', line 106 def package_info(package = nil) info = package_info_command(package) return {} if info.empty? JSON.parse(info) end |
#package_info_command(package) ⇒ Object
Returns package information as a JSON string
package - Go package import path
115 116 117 118 |
# File 'lib/licensed/source/go.rb', line 115 def package_info_command(package) package ||= "" Licensed::Shell.execute("go", "list", "-json", package, allow_failure: true) end |
#packages ⇒ Object
Returns an array of dependency package import paths
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/licensed/source/go.rb', line 56 def packages return [] unless root_package["Deps"] # don't include go std packages # don't include packages under the root project that aren't vendored root_package["Deps"] .uniq .select { |d| !go_std_packages.include?(d) } .select { |d| !d.start_with?(root_package["ImportPath"]) || vendored_path?(d) } .select do |d| # this removes the packages listed in `go list std` as "vendor/golang_org/*" but are vendored # as "vendor/golang.org/*" go_std_packages.none? do |std_pkg| std_pkg.sub(%r{^vendor/golang_org/}, "#{root_package["ImportPath"]}/vendor/golang.org/") == d end end end |
#root_package ⇒ Object
Returns the info for the package under test
121 122 123 |
# File 'lib/licensed/source/go.rb', line 121 def root_package @root_package ||= package_info end |
#search_root(package_dir) ⇒ Object
Returns the root directory to search for a package license
package - package object obtained from package_info
77 78 79 80 81 82 83 84 |
# File 'lib/licensed/source/go.rb', line 77 def search_root(package_dir) # search root choices: # 1. vendor folder if package is vendored # 2. GOPATH # 3. nil (no search up directory hierarchy) return package_dir.match("^(.*/vendor)/.*$")[1] if vendored_path?(package_dir) gopath end |
#vendored_path?(path) ⇒ Boolean
Returns whether a package is vendored or not based on the package import_path
path - Package path to test
90 91 92 |
# File 'lib/licensed/source/go.rb', line 90 def vendored_path?(path) path && path.include?("vendor/") end |