Module: Licensed::Sources::ContentVersioning

Included in:
Commands::Environment::AppEnvironment, Go, Manifest
Defined in:
lib/licensed/sources/helpers/content_versioning.rb

Constant Summary collapse

GIT =
"git".freeze
CONTENTS =
"contents".freeze

Instance Method Summary collapse

Instance Method Details

#contents_hash(paths) ⇒ Object

Find the version for a list of paths using their file contents

paths - list of paths to find version

Returns a hash of the path contents as an identifier for the group



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/licensed/sources/helpers/content_versioning.rb', line 59

def contents_hash(paths)
  return if paths.nil?

  paths = paths.compact.select { |path| File.file?(path) }
  return if paths.empty?
  # rubocop:disable GitHub/InsecureHashAlgorithm
  paths.sort
       .reduce(Digest::XXHash64.new, :file)
       .digest
       .to_s(16) # convert to hex
  # rubocop:enable GitHub/InsecureHashAlgorithm
end

#contents_version(*paths) ⇒ Object

Find the version for a list of paths using the version strategy specified for the source from the configuration

paths - list of paths to find version

Returns a version identifier for the given files



17
18
19
20
21
22
23
24
# File 'lib/licensed/sources/helpers/content_versioning.rb', line 17

def contents_version(*paths)
  case version_strategy
  when CONTENTS
    contents_hash(paths)
  when GIT
    git_version(paths)
  end
end

#git_version(paths) ⇒ Object

Find the version for a list of paths using Git commit information

paths - list of paths to find version

Returns the most recent git SHA from the given paths



46
47
48
49
50
51
52
# File 'lib/licensed/sources/helpers/content_versioning.rb', line 46

def git_version(paths)
  return if paths.nil?

  paths.map { |path| Licensed::Git.version(path) }
       .reject { |sha| sha.to_s.empty? }
       .max_by { |sha| Licensed::Git.commit_date(sha) }
end

#version_strategyObject

Returns the version strategy configured for the source



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/licensed/sources/helpers/content_versioning.rb', line 27

def version_strategy
  # default to git for backwards compatible behavior
  @version_strategy ||= begin
     case config.fetch("version_strategy", nil)
     when CONTENTS
       CONTENTS
     when GIT
       GIT
     else
       Licensed::Git.available? ? GIT : CONTENTS
     end
  end
end