# frozen_string_literal: true module KCommercial module Resources class Asset class TraitRow # @return [Map] attr_accessor :traits attr_accessor :resource attr_accessor :origin_resource end # @return [Pathname] attr_reader :root_path # This asset is valid or not # @return [Boolean] attr_reader :valid # @return [Array(TraitRow)] attr_reader :trait_rows attr_reader :name def initialize(name, root_path) @name = name @root_path = root_path @valid = false decode! end private def decode! decode_trait_rows end def decode_trait_rows contents_path = root_path.join('Contents.yaml') return unless contents_path.exist? rows = YAML.load_file(contents_path) @trait_rows = [] for row_hash in rows row_object = decode_row(row_hash) return unless row_object @trait_rows << row_object end return if @trait_rows.count == 0 @valid = true end def decode_row(row_hash) traits = row_hash[:traits] || row_hash["traits"] traits = {} unless traits resource = row_hash[:resource] || row_hash["resource"] return unless resource resource_decoded = decode_resource(resource) return unless resource_decoded row_object = TraitRow.new() row_object.origin_resource = resource row_object.traits = traits row_object.resource = resource_decoded row_object end def decode_resource(resource) resource_path = root_path.join(resource) return unless resource_path.exist? resource_path end end class HashAsset < Asset def decode_resource(resource) return unless resource.is_a? Hash resource end end class StringAsset < Asset def decode_resource(resource) return unless resource resource.to_s end end class ArrayHashAsset < Asset private def decode_resource(resource) return unless resource.is_a? Array name = nil resource.each do |res| if res['platform'].downcase == 'ios' name = res['res'] break end end return match_file_path(name) end private def match_file_path(name) return unless name #直接匹配路径 resource_path = root_path.join(name) return resource_path if resource_path.exist? #手动拼接倍数匹配路径 resource_paths = root_path.glob("*") names = %W[#{name}@1x #{name}@2x #{name}@3x] resource_paths.each do |resource_path| name = resource_path.basename('.*').to_s return resource_path if names.include?(name) end return nil end end end end