require 'pathname/common_prefix' module Licensed module Source class Manifest def initialize(config) @config = config end def enabled? @config.enabled?(type) && File.exist?(manifest_path) end def type 'manifest' end def dependencies @dependencies ||= packages.map do |package_name, sources| Dependency.new(common_dir(sources), { 'type' => type, 'name' => package_name, 'version' => package_version(sources) }) end end def common_dir(sources) common_prefix = Pathname.common_prefix(*sources) # if there's no common prefix, use the directory of the first source common_prefix = Pathname.new(sources[0]).dirname unless common_prefix Pathname.new(@config.path).join(common_prefix).expand_path.to_path end def package_version(sources) return if sources.nil? || sources.empty? # return the latest version from the sources sources.map { |s| source_version_command(s) } .max_by { |sha| commit_date_command(sha) } end def commit_date_command(sha) `git show -s -1 --format=%ct #{sha}`.strip end def source_version_command(source) `git rev-list -1 HEAD -- #{source}`.strip end def packages manifest.each_with_object({}) do |(src, package_name), hsh| next if src.nil? || src.empty? hsh[package_name] ||= [] hsh[package_name] << src end end def manifest JSON.parse(File.read(manifest_path)) end def manifest_path @config.path.join('manifest.json') end end end end