Class: Packwerk::PackageSet

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
Enumerable
Defined in:
lib/packwerk/package_set.rb

Overview

A set of Packages as well as methods to parse packages from the filesystem.

Constant Summary collapse

Elem =
type_member { { fixed: Package } }
PACKAGE_CONFIG_FILENAME =
"package.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packages) ⇒ PackageSet

Returns a new instance of PackageSet.



79
80
81
82
83
84
85
# File 'lib/packwerk/package_set.rb', line 79

def initialize(packages)
  # We want to match more specific paths first
  sorted_packages = packages.sort_by { |package| -package.name.length }
  packages = sorted_packages.each_with_object({}) { |package, hash| hash[package.name] = package }
  @packages = T.let(packages, T::Hash[String, Package])
  @package_from_path = T.let({}, T::Hash[String, T.nilable(Package)])
end

Instance Attribute Details

#packagesObject (readonly)

Returns the value of attribute packages.



76
77
78
# File 'lib/packwerk/package_set.rb', line 76

def packages
  @packages
end

Class Method Details

.load_all_from(root_path, package_pathspec: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/packwerk/package_set.rb', line 24

def load_all_from(root_path, package_pathspec: nil)
  package_paths = package_paths(root_path, package_pathspec || "**")

  packages = package_paths.map do |path|
    root_relative = path.dirname.relative_path_from(root_path)
    Package.new(name: root_relative.to_s, config: YAML.load_file(path, fallback: nil))
  end

  create_root_package_if_none_in(packages)

  new(packages)
end

.package_paths(root_path, package_pathspec, exclude_pathspec = []) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/packwerk/package_set.rb', line 44

def package_paths(root_path, package_pathspec, exclude_pathspec = [])
  exclude_pathspec = Array(exclude_pathspec).dup
    .push(Bundler.bundle_path.join("**").to_s)
    .map { |glob| File.expand_path(glob) }

  glob_patterns = Array(package_pathspec).map do |pathspec|
    File.join(root_path, pathspec, PACKAGE_CONFIG_FILENAME)
  end

  Dir.glob(glob_patterns)
    .map { |path| Pathname.new(path).cleanpath }
    .reject { |path| exclude_path?(exclude_pathspec, path) }
end

Instance Method Details

#each(&blk) ⇒ Object



88
89
90
# File 'lib/packwerk/package_set.rb', line 88

def each(&blk)
  packages.values.each(&blk)
end

#fetch(name) ⇒ Object



93
94
95
# File 'lib/packwerk/package_set.rb', line 93

def fetch(name)
  packages[name]
end

#package_from_path(file_path) ⇒ Object



98
99
100
101
# File 'lib/packwerk/package_set.rb', line 98

def package_from_path(file_path)
  path_string = file_path.to_s
  @package_from_path[path_string] ||= T.must(packages.values.find { |package| package.package_path?(path_string) })
end