Sha256: 128b8cd0cc79b52c611db3287873eda5d83ce8ba04365f57dfb68572ab083513

Contents?: true

Size: 1.47 KB

Versions: 64

Compression:

Stored size: 1.47 KB

Contents

# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2021

require 'time'

module RuboCop
  module Cop
    module Instana
      # Ensures the license header is present in each ruby file
      class LicenseHeader < Base
        extend AutoCorrector

        MSG = 'The license header should be present in each file.'.freeze
        HEADER = '(c) Copyright IBM Corp.'.freeze
        HEADER_TEMPLATE = <<~HERE.freeze
          # (c) Copyright IBM Corp. %d
          # (c) Copyright Instana Inc. %d
        HERE

        def on_new_investigation
          first_statement = processed_source.tokens.detect { |t| t.type != :tCOMMENT }
          file_name = first_statement.pos.source_buffer.name
          header_comment = processed_source.comments.detect do |comment|
            first_statement_line = first_statement.pos.line
            comment_line = comment.loc.line

            (comment_line < first_statement_line) && comment.text.include?(HEADER)
          end

          return if header_comment

          add_offense(first_statement.pos) do |corrector|
            current_year = Time.now.year
            created_time = `git log --diff-filter=A --follow --format=%aD -1 -- #{file_name}`
            created_year = created_time.empty? ? current_year : Time.parse(created_time).year

            header_text = format(HEADER_TEMPLATE, current_year, created_year)
            corrector.insert_before(first_statement.pos, "\n#{header_text}\n")
          end
        end
      end
    end
  end
end

Version data entries

64 entries across 64 versions & 1 rubygems

Version Path
instana-1.217.1 extras/license_header.rb
instana-1.217.0 extras/license_header.rb
instana-1.216.0 extras/license_header.rb
instana-1.215.1 extras/license_header.rb
instana-1.215.0 extras/license_header.rb
instana-1.214.4 extras/license_header.rb
instana-1.214.3 extras/license_header.rb
instana-1.214.2 extras/license_header.rb
instana-1.214.1 extras/license_header.rb
instana-1.214.0 extras/license_header.rb
instana-1.213.3 extras/license_header.rb
instana-1.213.2 extras/license_header.rb
instana-1.213.1 extras/license_header.rb
instana-1.213.0 extras/license_header.rb
instana-1.212.0 extras/license_header.rb
instana-1.211.0 extras/license_header.rb
instana-1.210.1 extras/license_header.rb
instana-1.210.0 extras/license_header.rb
instana-1.209.8 extras/license_header.rb
instana-1.209.7 extras/license_header.rb