Class: Kettle::Jem::LicenseTxtMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/kettle/jem/license_txt_migrator.rb

Overview

Recognizes MIT license text and extracts copyright lines from its preamble.

Constant Summary collapse

MIT_PHRASES =
[
  "permission is hereby granted",
  "without restriction"
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ LicenseTxtMigrator

Returns a new instance of LicenseTxtMigrator.



12
13
14
15
# File 'lib/kettle/jem/license_txt_migrator.rb', line 12

def initialize(content)
  @content = content.to_s
  @analysis = Ast::Merge::Text::FileAnalysis.new(@content)
end

Instance Method Details



22
23
24
25
26
27
28
29
30
# File 'lib/kettle/jem/license_txt_migrator.rb', line 22

def copyright_lines
  lines = @analysis.statements.select { |node| node.is_a?(Ast::Merge::Text::LineNode) }
  boundary = lines.index do |node|
    @analysis.searchable_text(nodes: [node]).downcase.include?("permission is hereby granted")
  end
  lines.first(boundary || lines.length)
    .select { |node| node.content.match?(/copyright/i) }
    .map(&:content)
end

#mit_license?Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'lib/kettle/jem/license_txt_migrator.rb', line 17

def mit_license?
  text = @analysis.searchable_text.downcase
  MIT_PHRASES.all? { |phrase| text.include?(phrase) }
end