class Licensed::Sources::Manifest::Dependency

Constants

ANY_EXCEPT_COMMENT_CLOSE_REGEX
HEADER_LICENSE_REGEX

Public Class Methods

new(name:, version:, path:, sources:, metadata: {}) click to toggle source
Calls superclass method Licensed::Dependency::new
# File lib/licensed/sources/manifest.rb, line 181
def initialize(name:, version:, path:, sources:, metadata: {})
  @sources = sources
  super(name: name, version: version, path: path, metadata: metadata)
end

Public Instance Methods

project_files() click to toggle source
Calls superclass method Licensed::Dependency#project_files
# File lib/licensed/sources/manifest.rb, line 186
def project_files
  files = super
  files.concat(source_files) if files.empty?
  files
end
source_files() click to toggle source

Returns an enumeration of Licensee::ProjectFiles::LicenseFile representing licenses found in source header comments

# File lib/licensed/sources/manifest.rb, line 194
def source_files
  @source_files ||= begin
    @sources
      .select { |file| File.file?(file) }
      .flat_map { |file| source_file_comments(file) }
      .map do |comment, file|
        Licensee::ProjectFiles::LicenseFile.new(comment, file)
      end
  end
end

Private Instance Methods

source_comment_text(comment) click to toggle source

Returns the comment text with leading * and whitespace stripped

# File lib/licensed/sources/manifest.rb, line 216
def source_comment_text(comment)
  indent = nil
  comment.lines.map do |line|
    # find the length of the indent as the number of characters
    # until the first word character
    indent ||= line[/\A([^\w]*)\w/, 1]&.size

    # insert newline for each line until a word character is found
    next "\n" unless indent

    line[/([^\w\r\n]{0,#{indent}})(.*)/m, 2]
  end.join
end
source_file_comments(file) click to toggle source

Returns all source header comments for a file

# File lib/licensed/sources/manifest.rb, line 208
def source_file_comments(file)
  file_parts = { dir: File.dirname(file), name: File.basename(file) }
  content = File.read(file)
  matches = content.scan(HEADER_LICENSE_REGEX)
  matches.map { |match| [source_comment_text(match[0]), file_parts] }
end