Class: Licensed::Source::Go
- Inherits:
-
Object
- Object
- Licensed::Source::Go
- Defined in:
- lib/licensed/source/go.rb
Instance Method Summary collapse
- #dependencies ⇒ Object
- #enabled? ⇒ Boolean
-
#git? ⇒ Boolean
Returns whether git commands are available.
-
#go_source? ⇒ Boolean
Returns whether go source is found.
-
#go_std_packages ⇒ Object
Returns a list of go standard packages.
-
#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.
-
#package_version(package_directory) ⇒ Object
Returns the most recent git SHA for a package, or nil if SHA is not available.
-
#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.
- #type ⇒ Object
-
#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.
7 8 9 |
# File 'lib/licensed/source/go.rb', line 7 def initialize(config) @config = config end |
Instance Method Details
#dependencies ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/licensed/source/go.rb', line 19 def dependencies @dependencies ||= 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' => type, 'name' => package_name) raise "couldn't find package for #{import_path}" end package_dir = package['Dir'] Dependency.new(package_dir, { 'type' => type, 'name' => import_path, 'summary' => package['Doc'], 'homepage' => homepage(import_path), 'search_root' => search_root(package_dir), 'version' => package_version(package_dir) }) end.compact end |
#enabled? ⇒ Boolean
15 16 17 |
# File 'lib/licensed/source/go.rb', line 15 def enabled? @config.enabled?(type) && go_source? end |
#git? ⇒ Boolean
Returns whether git commands are available
134 135 136 |
# File 'lib/licensed/source/go.rb', line 134 def git? @git ||= `which git 2>/dev/null` && $CHILD_STATUS.success? end |
#go_source? ⇒ Boolean
Returns whether go source is found
124 125 126 |
# File 'lib/licensed/source/go.rb', line 124 def go_source? @go_source ||= `go doc 2>/dev/null` && $CHILD_STATUS.success? end |
#go_std_packages ⇒ Object
Returns a list of go standard packages
129 130 131 |
# File 'lib/licensed/source/go.rb', line 129 def go_std_packages @std_packages ||= `go list std`.lines.map(&:strip) 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
43 44 45 46 47 48 49 |
# File 'lib/licensed/source/go.rb', line 43 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
96 97 98 99 100 |
# File 'lib/licensed/source/go.rb', line 96 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
105 106 107 108 109 |
# File 'lib/licensed/source/go.rb', line 105 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
114 115 116 |
# File 'lib/licensed/source/go.rb', line 114 def package_info_command(package) `go list -json #{package}` end |
#package_version(package_directory) ⇒ Object
Returns the most recent git SHA for a package, or nil if SHA is not available
package_directory - package location
79 80 81 82 83 |
# File 'lib/licensed/source/go.rb', line 79 def package_version(package_directory) return unless git? && package_directory `cd #{package_directory} && git rev-list -1 HEAD -- .`.strip end |
#packages ⇒ Object
Returns an array of dependency package import paths
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/licensed/source/go.rb', line 52 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) } end |
#root_package ⇒ Object
Returns the info for the package under test
119 120 121 |
# File 'lib/licensed/source/go.rb', line 119 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
66 67 68 69 70 71 72 73 |
# File 'lib/licensed/source/go.rb', line 66 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) ENV.fetch('GOPATH', nil) end |
#type ⇒ Object
11 12 13 |
# File 'lib/licensed/source/go.rb', line 11 def type 'go' end |
#vendored_path?(path) ⇒ Boolean
Returns whether a package is vendored or not based on the package import_path
path - Package path to test
89 90 91 |
# File 'lib/licensed/source/go.rb', line 89 def vendored_path?(path) path && path.include?('vendor/') end |