lib/u3d/hub_modules_parser.rb in u3d-1.2.3 vs lib/u3d/hub_modules_parser.rb in u3d-1.3.0

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + ## --- BEGIN LICENSE BLOCK --- # Copyright (c) 2016-present WeWantToKnow AS # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -26,23 +28,38 @@ require 'u3d_core/helper' module U3d module HubModulesParser class << self - HUB_MODULES_NAME = '%<version>s-%<os>s-modules.json'.freeze + HUB_MODULES_NAME = '%<version>s-%<os>s-modules.json' def load_modules(version, os: U3dCore::Helper.operating_system, offline: false) path = modules_path(version, os) - unless File.file?(path) && File.size(path) > 0 - return [] if offline # Should not raise, not all versions have hub modules - versions = download_modules(os: os) + # force download if no hub file present + download_modules(os: os) if !File.file?(path) && File.size(path).positive? && !offline - unless versions.include? version - UI.verbose "No modules registered for UnityHub for version #{version}" + unless File.file?(path) && File.size(path).positive? + UI.verbose "No modules registered for UnityHub for version #{version}" + # cached_versions.keys.map{|s| UnityVersionNumber.new(s)} + # searching for closest version + files = Dir.glob("#{default_modules_path}/*-#{os}-modules.json") + + vn = UnityVersionNumber.new(version) + + versions_and_paths = files.map do |p| + v = File.basename(p).split('-')[0] + [UnityVersionNumber.new(v), p] + end + # filtered by version major.minor.patch (same major & minor and patch higher or equal) + versions_and_paths = versions_and_paths.select { |a| a[0].parts[0] == vn.parts[0] && a[0].parts[1] == vn.parts[1] && a[0].parts[2] >= vn.parts[2] } + + if versions_and_paths.empty? + UI.info "No closest version from UnityHub found for version #{version}" return [] end + path = versions_and_paths.first[1] end return JSON.parse(File.read(path)) end @@ -79,10 +96,10 @@ def write_modules(build, os) path = modules_path(build['version'], os) Utils.ensure_dir(File.dirname(path)) - File.open(path, 'w') { |file| file.write build['modules'].to_json } + File.write(path, build['modules'].to_json) end end end end