Class: Licensed::Dependency
- Inherits:
-
Licensee::Projects::FSProject
- Object
- Licensee::Projects::FSProject
- Licensed::Dependency
- Defined in:
- lib/licensed/dependency.rb
Direct Known Subclasses
Sources::Bundler::Dependency, Sources::Gradle::Dependency, Sources::Manifest::Dependency, Sources::NPM::Dependency, Sources::NuGet::NuGetDependency
Constant Summary collapse
- LEGAL_FILES_PATTERN =
/#{File::SEPARATOR}(AUTHORS|NOTICE|LEGAL)(?:\..*)?\z/i
Instance Attribute Summary collapse
-
#additional_terms ⇒ Object
readonly
Returns the value of attribute additional_terms.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
-
#errors? ⇒ Boolean
Returns true if the dependency has any errors, false otherwise.
-
#exist? ⇒ Boolean
Returns whether the dependency exists locally.
-
#initialize(name:, version:, path:, search_root: nil, metadata: {}, errors: []) ⇒ Dependency
constructor
Create a new project dependency.
-
#license_contents ⇒ Object
Returns the license text content from all matched sources except the package file, which doesn’t contain license text.
-
#license_key ⇒ Object
Returns a string representing the dependencys license.
-
#metadata ⇒ Object
Returns a hash of basic metadata about the dependency - name, version, type, etc.
-
#notice_contents ⇒ Object
Returns legal notices found at the dependency path.
-
#project_files ⇒ Object
Override the behavior of Licensee::Projects::FSProject#project_files to include additional license terms.
-
#record ⇒ Object
Returns a record for this dependency including metadata and legal contents.
Constructor Details
#initialize(name:, version:, path:, search_root: nil, metadata: {}, errors: []) ⇒ Dependency
Create a new project dependency
name - unique dependency name version - dependency version path - absolute file path to the dependency, to find license contents search_root - (optional) the root location to search for dependency license contents metadata - (optional) additional dependency data to cache errors - (optional) errors encountered when evaluating dependency
Returns a new dependency object. Dependency metadata and license contents are available if no errors are set on the dependency.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/licensed/dependency.rb', line 25 def initialize(name:, version:, path:, search_root: nil, metadata: {}, errors: []) @name = name @version = version @metadata = @errors = errors path = path.to_s @path = path @additional_terms = [] # enforcing absolute paths makes life much easier when determining # an absolute file path in #notices if File.exist?(path) && !Pathname.new(path).absolute? # this is an internal error related to source implementation and # should be raised, not stored to be handled by reporters raise ArgumentError, "dependency path #{path} must be absolute" end super(path, search_root: search_root, detect_readme: true, detect_packages: true) end |
Instance Attribute Details
#additional_terms ⇒ Object (readonly)
Returns the value of attribute additional_terms.
12 13 14 |
# File 'lib/licensed/dependency.rb', line 12 def additional_terms @additional_terms end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
10 11 12 |
# File 'lib/licensed/dependency.rb', line 10 def errors @errors end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/licensed/dependency.rb', line 8 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/licensed/dependency.rb', line 11 def path @path end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
9 10 11 |
# File 'lib/licensed/dependency.rb', line 9 def version @version end |
Instance Method Details
#errors? ⇒ Boolean
Returns true if the dependency has any errors, false otherwise
55 56 57 |
# File 'lib/licensed/dependency.rb', line 55 def errors? errors.any? end |
#exist? ⇒ Boolean
Returns whether the dependency exists locally
46 47 48 49 50 51 52 |
# File 'lib/licensed/dependency.rb', line 46 def exist? # some types of dependencies won't necessarily have a path that exists, # but they can still find license contents between the given path and # the search root # @root is defined File.exist?(path) || File.exist?(@root) end |
#license_contents ⇒ Object
Returns the license text content from all matched sources except the package file, which doesn’t contain license text.
76 77 78 79 80 81 82 83 |
# File 'lib/licensed/dependency.rb', line 76 def license_contents files = matched_files.reject { |f| f == package_file } .group_by(&:content) .map { |content, sources| { "sources" => license_content_sources(sources), "text" => content } } files << generated_license_contents if files.empty? files.compact end |
#license_key ⇒ Object
Returns a string representing the dependencys license
69 70 71 72 |
# File 'lib/licensed/dependency.rb', line 69 def license_key return "none" unless license license.key end |
#metadata ⇒ Object
Returns a hash of basic metadata about the dependency - name, version, type, etc
103 104 105 106 107 108 109 110 111 |
# File 'lib/licensed/dependency.rb', line 103 def { # can be overriden by values in @metadata "name" => name, "version" => version }.merge( @metadata ) end |
#notice_contents ⇒ Object
Returns legal notices found at the dependency path
93 94 95 96 97 98 99 100 |
# File 'lib/licensed/dependency.rb', line 93 def notice_contents Dir.glob(dir_path.join("*")) .grep(LEGAL_FILES_PATTERN) .select { |path| File.file?(path) } .sort # sorted by the path .map { |path| { "sources" => normalize_source_path(path), "text" => read_file_with_encoding_check(path) } } .select { |notice| notice["text"].length > 0 } # files with content only end |
#project_files ⇒ Object
Override the behavior of Licensee::Projects::FSProject#project_files to include additional license terms
88 89 90 |
# File 'lib/licensed/dependency.rb', line 88 def project_files super + additional_license_terms_files end |
#record ⇒ Object
Returns a record for this dependency including metadata and legal contents
60 61 62 63 64 65 66 |
# File 'lib/licensed/dependency.rb', line 60 def record @record ||= DependencyRecord.new( metadata: , licenses: license_contents, notices: notice_contents ) end |