require 'open-uri' require 'fileutils' require 'zip' require_relative 'asset_type' module KCommercial module Resources class StarSource class PathSource # is common resources attr_reader :is_common_asset attr_accessor :assets_name, :assets_type, :assets, :app def initialize(assets_name, assets_type, path, app = 'default') @is_common_asset = app == 'default' @app = app @assets_name = assets_name @assets_type = assets_type @assets = [] prepare(path) end def assets_from_directory(directory, asset_class) asset_paths = directory.glob('**/*.cdasset') asset_paths.map do |path| name = path.basename('.*').to_s asset_class.new(name, path) end end def prepare(path) return [] unless @assets_name && path asset_paths = [] if is_common_asset asset_paths << path.join(@assets_name.to_s) else asset_paths = path.glob("**/#{@assets_name}") end asset_paths.each do |path| if [ KCommercial::Resources::AssetType::Font].include? @assets_type dir_assets = assets_from_directory path, HashAsset elsif [KCommercial::Resources::AssetType::Color, KCommercial::Resources::AssetType::I18n].include? @assets_type dir_assets = assets_from_directory path, StringAsset elsif [KCommercial::Resources::AssetType::Image].include? @assets_type dir_assets = assets_from_directory path, ArrayHashAsset else dir_assets = assets_from_directory path, Asset end merge_assets(dir_assets) end end def merge_assets(assets) if @assets != assets && assets result = assets + @assets @assets = result.uniq(&:name) end end # 合并数据源,被merge的优先级较高,会替换前边的 def merge(path_source) raise 'merge error,raise' unless path_source.assets_type == assets_type if path_source != self result = @assets + path_source.assets @assets = result.uniq(&:name) end end end # The url for the star platform # @return [String] attr_reader :url attr_reader :default_path, :app_paths, :all_assets_maps # The local storage path # @return [Pathname] attr_reader :root_path # @param [String] url # @param [Pathname] root_path def initialize(url, root_path) @url = url @root_path = root_path @temp_path = root_path.split[0].join("temp_#{Time.now.to_i}") @default_path = root_path.join('defaults') @all_assets_maps = {} end def load_assets(type) directory_name = type_directory_mapper[type] return nil unless directory_name sources = app_paths.map { |c| PathSource.new(directory_name, type, c, c.basename.to_s) } common_source = sources.find_all(&:is_common_asset) sources.each { |items| items.merge(common_source.first) } if common_source.size.positive? all_assets_maps[directory_name] = sources sources end def struct_assets_from_directory(directory) asset_paths = directory.glob('**/*.cdasset') asset_paths.map do |path| name = path.basename('.*').to_s HashAsset.new(name, path) end end def type_directory_mapper { AssetType::Color => 'Colors', AssetType::File => 'Files', AssetType::Font => 'Fonts', AssetType::I18n => 'I18ns', AssetType::Image => 'Images', } end # Update the source def update # 先在临时目录下载 download_resources(url) # 最后rename rename_resources end def download_resources(url) KCommercial::UI.info "download the source #{url}" download = URI.open(url) Zip::File.open_buffer(download) do |zip| zip.each do |entry| path = @temp_path.join(entry.name) entry.extract(path) end end end def rename_resources if root_path.exist? FileUtils.rm_r(root_path) end File.rename(@temp_path, root_path) @app_paths = root_path.children.find_all(&:directory?) || [] if @root_path.exist? end end end end