Module: JamfZeitwerkConfig

Defined in:
lib/jamf/zeitwerk_config.rb

Overview

Jamf’s Zeitwerk Config and processes

Constant Summary collapse

VERBOSE_LOADING_FILE =

touch this file to make zeitwerk and mixins send text to stderr as things load or get mixed in

Pathname.new('/tmp/ruby-jss-verbose-loading')
VERBOSE_LOADING_ENV =

Or, set this ENV var to also make zeitverk and mixins send text to stderr

'RUBY_JSS_VERBOSE_LOADING'
EAGER_LOAD_FILE =

touch this file to make zeitwek eager-load everything when the gem is required.

Pathname.new('/tmp/ruby-jss-zeitwerk-eager-load')

Class Method Summary collapse

Class Method Details

.eager_load_for_testingObject

For testing the Zeitwrk Loader. Normally we want autoloading on demand, eager loading loads everything so we can see it

To make this happen touch the file defined in EAGER_LOAD_FILE



218
219
220
221
222
223
224
225
# File 'lib/jamf/zeitwerk_config.rb', line 218

def self.eager_load_for_testing
  return unless EAGER_LOAD_FILE.file?

  loader.eager_load(force: true)
  warn :loaded
  # rescue Zeitwerk::NameError => e
  #   warn e.message
end

.load_msg(msg) ⇒ Object

rubocop: disable Style/StderrPuts



52
53
54
# File 'lib/jamf/zeitwerk_config.rb', line 52

def self.load_msg(msg)
  $stderr.puts msg if verbose_loading?
end

.loaderObject

The loader object for Xolo



58
59
60
# File 'lib/jamf/zeitwerk_config.rb', line 58

def self.loader
  @loader
end

.setup_zeitwerk_loader(zloader) ⇒ Object

Configure the Zeitwerk loader, See github.com/fxn/zeitwerk



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/jamf/zeitwerk_config.rb', line 63

def self.setup_zeitwerk_loader(zloader)
  @loader = zloader

  # Ignore this file (more ignores below)
  loader.ignore __FILE__

  ##### Collaped Paths

  # these paths all define classes & modules directly below 'Jamf'
  # If we didn't collapse them, then e.g.
  #   /jamf/api/base_classes/classic/group.rb
  # would be expected to define
  #   Jamf::Api::BaseClasses::Classic::Group
  # rather than what we want:
  #  Jamf::Group
  ###################################################

  loader.collapse("#{__dir__}/api")

  loader.collapse("#{__dir__}/api/classic")
  loader.collapse("#{__dir__}/api/classic/api_objects")
  loader.collapse("#{__dir__}/api/classic/base_classes")

  loader.collapse("#{__dir__}/api/jamf_pro")
  loader.collapse("#{__dir__}/api/jamf_pro/api_objects")
  loader.collapse("#{__dir__}/api/jamf_pro/mixins")
  loader.collapse("#{__dir__}/api/jamf_pro/base_classes")
  loader.collapse("#{__dir__}/api/jamf_pro/other_classes")

  ##### Inflected Paths

  # filenames => Constants, which don't adhere to zeitwerk's parsing standards.
  #
  # Mostly because the a filename like 'oapi_object' would be
  # loaded by zeitwerk expecting it to define 'OapiObject', but it really
  # defines 'OAPIObject'
  ###############################################

  # Connections
  loader.inflector.inflect 'classic_api' => 'ClassicAPI'
  loader.inflector.inflect 'jamf_pro_api' => 'JamfProAPI'
  loader.inflector.inflect 'jamf_pro_api_error' => 'JamfProAPIError'

  # API objects, resources, and mixins
  loader.inflector.inflect 'oapi_schemas' => 'OAPISchemas'
  loader.inflector.inflect 'oapi_object' => 'OAPIObject'
  loader.inflector.inflect 'oapi_validate' => 'OAPIValidate'

  loader.inflector.inflect 'jpapi_resource' => 'JPAPIResource'

  loader.inflector.inflect 'api_object' => 'APIObject'
  loader.inflector.inflect 'api_role' => 'APIRole'
  loader.inflector.inflect 'api_client' => 'APIClient'
  loader.inflector.inflect 'api_integration' => 'APIIntegration'
  loader.inflector.inflect 'xml_workaround' => 'XMLWorkaround'
  loader.inflector.inflect 'json_object' => 'JSONObject'
  loader.inflector.inflect 'vppable' => 'VPPable'
  loader.inflector.inflect 'osx_configuration_profile' => 'OSXConfigurationProfile'
  loader.inflector.inflect 'jp_extendable' => 'JPExtendable'
  loader.inflector.inflect 'mdm' => 'MDM'
  loader.inflector.inflect 'ibeacon' => 'IBeacon'
  loader.inflector.inflect 'powerbroker_identity_services' => 'PowerBroker'
  loader.inflector.inflect 'admitmac' => 'ADmitMac'
  loader.inflector.inflect 'ip_address' => 'IPAddress'
  loader.inflector.inflect 'netboot_server' => 'NetBootServer'
  loader.inflector.inflect 'vpp_account' => 'VPPAccount'
  loader.inflector.inflect 'removable_macaddr' => 'RemovableMacAddress'
  loader.inflector.inflect 'md_prestage_name' => 'MobileDevicePrestageName'
  loader.inflector.inflect 'md_prestage_names' => 'MobileDevicePrestageNames'
  loader.inflector.inflect 'md_prestage_skip_setup_items' => 'MobileDevicePrestageSkipSetupItems'
  loader.inflector.inflect 'macos_managed_updates' => 'MacOSManagedUpdates'
  loader.inflector.inflect 'macos_redeploy_mgmt_framework' => 'MacOSRedeployMgmtFramework'
  loader.inflector.inflect 'filevault' => 'FileVault'

  # deprecations, separated so they load only when used.
  # When its time to get rid of them, delete the files from the
  # 'deprecations' directory, and the matching line here.
  loader.inflector.inflect('deprecated_api_constant' => 'API')
  loader.inflector.inflect('deprecated_config_constant' => 'CONFIG')
  loader.inflector.inflect('deprecated_api_connection_class' => 'APIConnection')

  ##### Ingored Paths

  # These should be ignored, some will be required directly
  #####################################

  loader.ignore "#{__dir__}/db_connection.rb"
  loader.ignore "#{__dir__}/ruby_extensions.rb"
  loader.ignore "#{__dir__}/ruby_extensions"
  loader.ignore "#{__dir__}/exceptions.rb"
  loader.ignore "#{__dir__}/deprecations"
  loader.ignore "#{__dir__}/deprecations.rb"

  lib_dir = Pathname.new(__dir__).parent.to_s
  loader.ignore "#{lib_dir}/ruby-jss.rb"
  loader.ignore "#{lib_dir}/jss.rb"
  loader.ignore "#{lib_dir}/jss-api.rb"

  # See the comments and manual 'require' at the top of
  # lib/jamf/api/jamf_pro/api_objects/api_role.rb
  # for why we ignore this file.
  loader.ignore "#{lib_dir}/jamf/api/jamf_pro/oapi_schemas/api_role.rb"

  ##### Callbacks

  # callback for when a specific file/constant loads
  # duplicate and uncomment this if desired to react to
  # specific things loading
  #####################################
  # loader.on_load('Jamf::SomeClass') do |klass, abspath|
  #   Jamf.load_msg "I just loaded #{klass} from #{abspath}"
  # end

  # callback for when anything loads
  #  - const_path is like "Jamf::SomeClass" or "Jamf::SomeClass::SOME_CONST_ARRY"
  #  - value is the value that constant contains after loading,
  #    e.g. the class Jamf::SomeClass for 'Jamf::SomeClass' or
  #    an Array for the constant  "Jamf::SomeClass::SOME_CONST_ARRY"
  #  - abspath is the full path to the file where the constant was loaded from.
  #####################################
  loader.on_load do |const_path, value, abspath|
    load_msg "Zeitwerk just loaded #{value.class} '#{const_path}' from:\n  #{abspath}"

    # Parse OAPI_PROPERTIES into getters and setters for subclasses of
    # OAPIObject in the JPAPI.
    #
    # The class we just loaded must have this method and constant
    # and the method must not have run already for the class or any superclass.
    # This prevents running parse_oapi_properties again in subclasses that
    # don't need to do that
    if value.respond_to?(:oapi_properties_parsed?) && \
       defined?(value::OAPI_PROPERTIES) && \
       !value.oapi_properties_parsed?

      parsed = value.parse_oapi_properties
      load_msg "Parsed OAPI_PROPERTIES for #{value}" if parsed
    end

    # Generate the identifier list methods (.all_*) for subclasses of APIObject
    # in the Classic API
    if value.is_a?(Class) && value.superclass == Jamf::APIObject

      done = value.define_identifier_list_methods
      load_msg "Defined identifier list methods for #{value}" if done
    end
  end

  loader.setup
end

.verbose_loading?Boolean

Only look at the filesystem once.

Returns:

  • (Boolean)


43
44
45
46
47
48
49
# File 'lib/jamf/zeitwerk_config.rb', line 43

def self.verbose_loading?
  return @verbose_loading unless @verbose_loading.nil?

  @verbose_loading = VERBOSE_LOADING_FILE.file?
  @verbose_loading ||= ENV.include? VERBOSE_LOADING_ENV
  @verbose_loading
end