Class: Packwerk::PackageSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/packwerk/package_set.rb

Overview

A set of Packages as well as methods to parse packages from the filesystem. : [Elem = Package]

Constant Summary collapse

PACKAGE_CONFIG_FILENAME =
"package.yml"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(packages) ⇒ PackageSet

: (Array packages) -> void



68
69
70
71
72
73
74
# File 'lib/packwerk/package_set.rb', line 68

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 = packages #: Hash[String, Package]
  @package_from_path = {} #: Hash[String, Package?]
end

Instance Attribute Details

#packagesObject (readonly)

: Hash[String, Package]



65
66
67
# File 'lib/packwerk/package_set.rb', line 65

def packages
  @packages
end

Class Method Details

.load_all_from(root_path, package_pathspec: nil) ⇒ Object

: (String root_path, ?package_pathspec: path_spec?) -> PackageSet



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/packwerk/package_set.rb', line 19

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

: (String root_path, path_spec package_pathspec, ?path_spec? exclude_pathspec) -> Array



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/packwerk/package_set.rb', line 33

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

: { (Package arg0) -> untyped } -> untyped



78
79
80
# File 'lib/packwerk/package_set.rb', line 78

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

#fetch(name) ⇒ Object

: (String name) -> Package?



83
84
85
# File 'lib/packwerk/package_set.rb', line 83

def fetch(name)
  packages[name]
end

#package_from_path(file_path) ⇒ Object

: ((Pathname | String) file_path) -> Package



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

def package_from_path(file_path)
  path_string = file_path.to_s
  @package_from_path[path_string] ||= packages.values.find { |package| package.package_path?(path_string) } #: as !nil
end