forked from Autosde/autosde-oas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Autoload files on demand to avoid loading the whole library
Modeled after: xlab-si/intersight-sdk-ruby#14 Note, this saves around 6 MB of memory by delay loading 214 files. Future plugins could use useAutoload option to autoload. This was added in 6.1.0 of the openapi-generator: OpenAPITools/openapi-generator#13153 Note, this library has non-standard class names, even within the same plugin, such as SDE capitalized in AutoSDEProject but not in AutosdeOpenapiClient. To workaround this, I had to validate that all classes we try to autoload can actually be loaded. I've added a variation of the script from xlab-si/intersight-sdk-ruby#14 which checks all classes can be loaded.
- Loading branch information
Showing
2 changed files
with
157 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require "active_support/core_ext/string/inflections" | ||
|
||
inflections = { | ||
"AutoSdeProject" => "AutoSDEProject", | ||
"AutoSdeProjectApi" => "AutoSDEProjectApi", | ||
"AutoSdeRole" => "AutoSDERole", | ||
"AutoSdeRoleApi" => "AutoSDERoleApi", | ||
"StorageHostWwpnCandidates" => "StorageHostWWPNCandidates", | ||
"StorageHostWwpnCandidatesApi" => "StorageHostWWPNCandidatesApi" | ||
} | ||
|
||
klasses = [] | ||
content = "" | ||
|
||
Dir.chdir("./generated") do | ||
File.readlines("lib/autosde_openapi_client.rb").each do |line| | ||
if line.match?(/autosde_openapi_client\/(?:models|api)\//) | ||
model = line.split("'")[1] | ||
klass = File.basename(model).camelize | ||
klass = inflections.key?(klass) ? inflections[klass] : klass | ||
line = "AutosdeOpenapiClient.autoload :#{klass}, '#{model}'\n" | ||
klasses << klass | ||
end | ||
content << line | ||
end | ||
|
||
File.write("lib/autosde_openapi_client.rb", content) | ||
|
||
$LOAD_PATH << Dir.pwd | ||
require "lib/autosde_openapi_client" | ||
|
||
klasses.each do |klass| | ||
fq_klass = "AutosdeOpenapiClient::#{klass}" | ||
begin | ||
fq_klass.constantize | ||
rescue => err | ||
puts "Failed to load #{fq_klass}, check file naming and add to inflections for non-standard names." | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters