lib/jamf.rb in ruby-jss-1.6.4 vs lib/jamf.rb in ruby-jss-2.0.0a10

- old
+ new

@@ -1,6 +1,6 @@ -# Copyright 2020 Pixar +# Copyright 2022 Pixar # # Licensed under the Apache License, Version 2.0 (the "Apache License") # with the following modification; you may not use this file except in # compliance with the Apache License and the following modification to it: @@ -18,161 +18,106 @@ # Unless required by applicable law or agreed to in writing, software # distributed under the Apache License with the above modification is # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the Apache License for the specific # language governing permissions and limitations under the Apache License. -# -# -# -# JAMF, A Ruby module for interacting with a JAMF Pro Server via the JAMF API -# +# Required Libraries, etc +##################################### -### Standard Libraries +# Standard Libraries +# +# TODO: Move some of these to where they are first needed so they aren't +# loaded unnecessarily require 'English' require 'json' require 'yaml' require 'pathname' +require 'fileutils' require 'time' +require 'date' require 'singleton' require 'open-uri' require 'ipaddr' +require 'rexml/document' require 'base64' require 'shellwords' require 'digest' require 'open3' -### Gems - -# Used, among other places, in the Connection::APIError class +# Gems require 'immutable-struct' +require 'concurrent' -# TODO: needed? -# require 'recursive-open-struct' - -# non-api parts of Jamf module -require 'jamf/configuration' +# Load things not loaded by zeitwerk +require 'jamf/ruby_extensions' require 'jamf/exceptions' -require 'jamf/utility' -require 'jamf/validate' -require 'jamf/version' +require 'jamf/db_connection' -# backports and extensions to existing Ruby classes -require 'jamf/compatibility' -require 'jamf/ruby_extensions' +# Configure the Zeitwerk loader, See https://github.com/fxn/zeitwerk +require 'zeitwerk' +require 'zeitwerk_config' -# API connection -require 'jamf/api/connection' +# touch this file to make zeitwerk and mixins send text to stderr as things load +# or get mixed in +JAMF_VERBOSE_LOADING_FILE = Pathname.new('/tmp/ruby-jss-verbose-loading') +# Or, set this ENV var to also make zeitverk and mixins send text to stderr +JAMF_VERBOSE_LOADING_ENV = 'RUBY_JSS_VERBOSE_LOADING'.freeze -# The main module. -# See README.md -# +# touch this file to make zeitwek eager-load everything when the gem is required. +JAMF_ZEITWERK_EAGER_LOAD_FILE = Pathname.new('/tmp/ruby-jss-zeitwerk-eager-load') + +# the `Zeitwerk::Loader.for_gem` creates the loader object, and must +# happen in this file, so we pass it into a method defined in +# zeitwerk_config +setup_zeitwerk_loader Zeitwerk::Loader.for_gem + +# Jamf, A Ruby module for interacting with the JAMF Pro Server via both of its REST APIs module Jamf - # The minimum Ruby version that works with this gem - # 2.3 allows us to start using some nice features like the safe-navigation - # operator and Array#dig & Hash#dig, and such. - # - # For a list of features, see https://github.com/ruby/ruby/blob/v2_3_0/NEWS - # and http://nithinbekal.com/posts/ruby-2-3-features/ - # - MINIMUM_RUBY_VERSION = '2.3'.freeze + def self.validate_ruby_version + return if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(MINIMUM_RUBY_VERSION) - if Gem::Version.new(RUBY_VERSION) < Gem::Version.new(MINIMUM_RUBY_VERSION) - raise "Can't use the JAMF module, ruby itself must be version #{MINIMUM_RUBY_VERSION} or greater." + raise "Can't use ruby-jss #{Jamf::VERSION}, ruby itself must be version #{MINIMUM_RUBY_VERSION} or greater, this is ruby #{RUBY_VERSION}." end - # AUTOLOADING - ################################## + # Only look at the filesystem once. + def self.verbose_loading? + return @verbose_loading unless @verbose_loading.nil? - # Top-level API Base Classes - autoload :JSONObject, 'jamf/api/base_classes/json_object' - autoload :Resource, 'jamf/api/base_classes/resource' - autoload :SingletonResource, 'jamf/api/base_classes/singleton_resource' - autoload :CollectionResource, 'jamf/api/base_classes/collection_resource' + @verbose_loading = JAMF_VERBOSE_LOADING_FILE.file? + @verbose_loading ||= ENV.include? JAMF_VERBOSE_LOADING_ENV + @verbose_loading + end - # Base Classes used for JSONObject subclasses - autoload :Prestage, 'jamf/api/base_classes/prestage' + # rubocop: disable Style/StderrPuts + def self.load_msg(msg) + return unless verbose_loading? - # MixIn Modules - autoload :ChangeLog, 'jamf/api/mixins/change_log' - autoload :Extendable, 'jamf/api/mixins/extendable' - autoload :Locatable, 'jamf/api/mixins/locatable' - autoload :Referable, 'jamf/api/mixins/referable' - autoload :Searchable, 'jamf/api/mixins/searchable' - autoload :Lockable, 'jamf/api/mixins/lockable' - autoload :UnCreatable, 'jamf/api/mixins/uncreatable' - autoload :Immutable, 'jamf/api/mixins/immutable' - autoload :UnDeletable, 'jamf/api/mixins/undeletable' - autoload :BaseClass, 'jamf/api/mixins/base_class' - autoload :Pageable, 'jamf/api/mixins/pageable' - autoload :Filterable, 'jamf/api/mixins/filterable' - autoload :Sortable, 'jamf/api/mixins/sortable' - autoload :BulkDeletable, 'jamf/api/mixins/bulk_deletable' + $stderr.puts msg + end + # rubocop: enable Style/StderrPuts - # Utility modules - autoload :Validate, 'jamf/validate' + # the single instance of our configuration object + def self.config + Jamf::Configuration.instance + end - # Subclasses of JSONObject, but not Resource - autoload :AndroidDetails, 'jamf/api/json_objects/android_details' - autoload :AppleTVDetails, 'jamf/api/json_objects/appletv_details' - autoload :CellularNetwork, 'jamf/api/json_objects/cellular_network' - autoload :ChangeLogEntry, 'jamf/api/json_objects/change_log_entry' - autoload :ComputerPrestageSkipSetupItems, 'jamf/api/json_objects/computer_prestage_skip_setup_items' - autoload :Country, 'jamf/api/json_objects/country' - autoload :Criterion, 'jamf/api/json_objects/criterion' - autoload :DeviceEnrollmentDevice, 'jamf/api/json_objects/device_enrollment_device' - autoload :DeviceEnrollmentDeviceSyncState, 'jamf/api/json_objects/device_enrollment_device_sync_state' - autoload :DeviceEnrollmentSyncStatus, 'jamf/api/json_objects/device_enrollment_sync_status' - autoload :ExtensionAttributeValue, 'jamf/api/json_objects/extension_attribute_value' - autoload :InstalledApplication, 'jamf/api/json_objects/installed_application' - autoload :InstalledCertificate, 'jamf/api/json_objects/installed_certificate' - autoload :InstalledConfigurationProfile, 'jamf/api/json_objects/installed_configuration_profile' - autoload :InstalledEBook, 'jamf/api/json_objects/installed_ebook' - autoload :InstalledProvisioningProfile, 'jamf/api/json_objects/installed_provisioning_profile' - autoload :InventoryPreloadExtensionAttribute, 'jamf/api/json_objects/inventory_preload_extension_attribute' - autoload :IosDetails, 'jamf/api/json_objects/ios_details' - autoload :Locale, 'jamf/api/json_objects/locale' - autoload :Location, 'jamf/api/json_objects/location' - autoload :PrestageLocation, 'jamf/api/json_objects/prestage_location' - autoload :PrestageSyncStatus, 'jamf/api/json_objects/prestage_sync_status' - autoload :MobileDeviceDetails, 'jamf/api/json_objects/mobile_device_details' - autoload :MobileDeviceSecurity, 'jamf/api/json_objects/mobile_device_security' - autoload :MobileDevicePrestageName, 'jamf/api/json_objects/md_prestage_name' - autoload :MobileDevicePrestageNames, 'jamf/api/json_objects/md_prestage_names' - autoload :MobileDevicePrestageSkipSetupItems, 'jamf/api/json_objects/md_prestage_skip_setup_items' - autoload :PurchasingData, 'jamf/api/json_objects/purchasing_data' - autoload :PrestagePurchasingData, 'jamf/api/json_objects/prestage_purchasing_data' - autoload :PrestageScope, 'jamf/api/json_objects/prestage_scope' - autoload :PrestageAssignment, 'jamf/api/json_objects/prestage_assignment' - autoload :TimeZone, 'jamf/api/json_objects/time_zone' + # These need to come after the definition of verboase_loading? + # since they will try to use it when they get loaded + ################### - # Subclasses of SingletonResource - autoload :ClientCheckInSettings, 'jamf/api/resources/singleton_resources/client_checkin_settings' - autoload :ReEnrollmentSettings, 'jamf/api/resources/singleton_resources/reenrollment_settings' - autoload :AppStoreCountryCodes, 'jamf/api/resources/singleton_resources/app_store_country_codes' - autoload :TimeZones, 'jamf/api/resources/singleton_resources/time_zones' - autoload :Locales, 'jamf/api/resources/singleton_resources/locales' + include Jamf::Constants + extend Jamf::Utility + extend Jamf::Connection::DefaultConnection - # Subclasses of CollectionResource - autoload :AdvancedMobileDeviceSearch, 'jamf/api/resources/collection_resources/advanced_mobile_device_search' - autoload :AdvancedUserSearch, 'jamf/api/resources/collection_resources/advanced_user_search' - autoload :Attachment, 'jamf/api/resources/collection_resources/attachment' - autoload :Category, 'jamf/api/resources/collection_resources/category' - autoload :Building, 'jamf/api/resources/collection_resources/building' - autoload :Computer, 'jamf/api/resources/collection_resources/computer' - autoload :ComputerPrestage, 'jamf/api/resources/collection_resources/computer_prestage' - autoload :Department, 'jamf/api/resources/collection_resources/department' - autoload :DeviceEnrollment, 'jamf/api/resources/collection_resources/device_enrollment' - autoload :ExtensionAttribute, 'jamf/api/resources/collection_resources/extension_attribute' - autoload :InventoryPreloadRecord, 'jamf/api/resources/collection_resources/inventory_preload_record' - autoload :MobileDevice, 'jamf/api/resources/collection_resources/mobile_device' - autoload :MobileDevicePrestage, 'jamf/api/resources/collection_resources/mobile_device_prestage' - autoload :Site, 'jamf/api/resources/collection_resources/site' - autoload :Script, 'jamf/api/resources/collection_resources/script' +end # module Jamf - # other classes used as attributes inside the resource classes - autoload :IPAddress, 'jamf/api/attribute_classes/ip_address' - autoload :Timestamp, 'jamf/api/attribute_classes/timestamp' +# make sure we can run +Jamf.validate_ruby_version -end # module +# JSS module is now a synonym for Jamf module +JSS = Jamf + +# testing zeitwerk loading +eager_load_for_testing if JAMF_ZEITWERK_EAGER_LOAD_FILE.file?