Class: Packwerk::Configuration

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/packwerk/configuration.rb

Constant Summary collapse

DEFAULT_CONFIG_PATH =
"packwerk.yml"
DEFAULT_INCLUDE_GLOBS =
T.let(["**/*.{rb,rake,erb}"], T::Array[String])
DEFAULT_EXCLUDE_GLOBS =
T.let(["{bin,node_modules,script,tmp,vendor}/**/*"], T::Array[String])

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configs = {}, config_path: nil) ⇒ Configuration

Returns a new instance of Configuration.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/packwerk/configuration.rb', line 75

def initialize(configs = {}, config_path: nil)
  @include = T.let(configs["include"] || DEFAULT_INCLUDE_GLOBS, T::Array[String])
  @exclude = T.let(configs["exclude"] || DEFAULT_EXCLUDE_GLOBS, T::Array[String])
  root = config_path ? File.dirname(config_path) : "."
  @root_path = T.let(File.expand_path(root), String)
  @package_paths = T.let(configs["package_paths"] || "**/", T.any(String, T::Array[String]))
  @custom_associations = T.let((configs["custom_associations"] || []).map(&:to_sym), T::Array[Symbol])
  @associations_exclude = T.let(configs["associations_exclude"] || [], T::Array[String])
  @parallel = T.let(configs.key?("parallel") ? configs["parallel"] : true, T::Boolean)
  @cache_enabled = T.let(configs.key?("cache") ? configs["cache"] : false, T::Boolean)
  @cache_directory = T.let(Pathname.new(configs["cache_directory"] || "tmp/cache/packwerk"), Pathname)
  @config_path = config_path

  @offenses_formatter_identifier = T.let(
    configs["offenses_formatter"] || Formatters::DefaultOffensesFormatter::IDENTIFIER, String
  )

  if configs.key?("require")
    configs["require"].each do |require_directive|
      ExtensionLoader.load(require_directive, @root_path)
    end
  end
end

Instance Attribute Details

#associations_excludeObject (readonly)

Returns the value of attribute associations_exclude.



58
59
60
# File 'lib/packwerk/configuration.rb', line 58

def associations_exclude
  @associations_exclude
end

#cache_directoryObject (readonly)

Returns the value of attribute cache_directory.



64
65
66
# File 'lib/packwerk/configuration.rb', line 64

def cache_directory
  @cache_directory
end

#config_pathObject (readonly)

Returns the value of attribute config_path.



61
62
63
# File 'lib/packwerk/configuration.rb', line 61

def config_path
  @config_path
end

#custom_associationsObject (readonly)

Returns the value of attribute custom_associations.



55
56
57
# File 'lib/packwerk/configuration.rb', line 55

def custom_associations
  @custom_associations
end

#excludeObject (readonly)

Returns the value of attribute exclude.



46
47
48
# File 'lib/packwerk/configuration.rb', line 46

def exclude
  @exclude
end

#includeObject (readonly)

Returns the value of attribute include.



43
44
45
# File 'lib/packwerk/configuration.rb', line 43

def include
  @include
end

#package_pathsObject (readonly)

Returns the value of attribute package_paths.



52
53
54
# File 'lib/packwerk/configuration.rb', line 52

def package_paths
  @package_paths
end

#parallel=(value) ⇒ Object (writeonly)

Sets the attribute parallel

Parameters:

  • value

    the value to set the attribute parallel to.



67
68
69
# File 'lib/packwerk/configuration.rb', line 67

def parallel=(value)
  @parallel = value
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



49
50
51
# File 'lib/packwerk/configuration.rb', line 49

def root_path
  @root_path
end

Class Method Details

.from_path(path = Dir.pwd) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/packwerk/configuration.rb', line 15

def from_path(path = Dir.pwd)
  raise ArgumentError, "#{File.expand_path(path)} does not exist" unless File.exist?(path)

  default_packwerk_path = File.join(path, DEFAULT_CONFIG_PATH)

  if File.file?(default_packwerk_path)
    from_packwerk_config(default_packwerk_path)
  else
    new
  end
end

Instance Method Details

#cache_enabled?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/packwerk/configuration.rb', line 118

def cache_enabled?
  @cache_enabled
end

#load_pathsObject



100
101
102
103
104
105
# File 'lib/packwerk/configuration.rb', line 100

def load_paths
  @load_paths ||= T.let(
    RailsLoadPaths.for(@root_path, environment: "test"),
    T.nilable(T::Hash[String, Module]),
  )
end

#offenses_formatterObject



113
114
115
# File 'lib/packwerk/configuration.rb', line 113

def offenses_formatter
  OffensesFormatter.find(@offenses_formatter_identifier)
end

#parallel?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/packwerk/configuration.rb', line 108

def parallel?
  @parallel
end