set/abstract/mod_assets.rb in card-mod-assets-0.13.4 vs set/abstract/mod_assets.rb in card-mod-assets-0.14.0

- old
+ new

@@ -1,170 +1,155 @@ include_set Abstract::Pointer +include_set Abstract::ReadOnly -def mod_name - left&.codename.to_s.sub(/^mod_/, "") +def item_cards _args={} + local_group_cards end -def mod - @mod ||= Cardio::Mod.dirs.fetch_mod(mod_name) +def item_names _args={} + local_group_cards.map(&:name) end -def assets_path - return unless mod&.assets_path.present? - - File.join mod&.assets_path, subpath +# group cards that don't refer to remote sources +def local_group_cards + @local_group_cards ||= + if manifest_exists? + local_manifest_group_cards + else + [folder_group_card].compact + end end -def manifest_path +def folder_group_card return unless assets_path + card = new_assets_group_card local_group_name, folder_group_type_id + card.assets_path = assets_path + card +end - File.join(assets_path, "manifest.yml") +def local_manifest_group_cards + manifest.map do |group_name, config| + next if remote_group?(group_name, config) + new_local_manifest_group_card group_name + end.compact end -def expected_item_keys - return [] unless assets_dir_exists? +def remote_group_urls + return unless manifest_exists? + manifest_group_items "remote" +end - if manifest_exists? - manifest.keys.map { |group_key| "#{name}+#{group_key}".to_name.key } - else - ["#{name}+#{local_group_name}".to_name.key] - end +def content? + assets_path end -def local_group_name - "local" +def mod_name + left&.codename.to_s.sub(/^mod_/, "") end -def update_items - # return unless groups_changed? +def mod + @mod ||= Cardio::Mod.fetch mod_name +end - delete_unused_items do - self.content = "" - return unless assets_dir_exists? - - ensure_update_items - save! - end +def manifest_exists? + @manifest_exists = !manifest_path.nil? if @manifest_exists.nil? + @manifest_exists end -def ensure_update_items - if manifest_exists? - ensure_manifest_groups_cards - else - ensure_item local_group_name, local_folder_group_type_id - end +def assets_path + @assets_path ||= mod&.subpath "assets", subpath end -def delete_unused_items - @old_items = ::Set.new item_keys - yield - remove_deprecated_items @old_items +def manifest_path + @manifest_path ||= mod&.subpath "assets", subpath, "manifest.yml" end -def assets_dir_exists? - path = assets_path - path && Dir.exist?(path) +def local_group_name + "local" end -def manifest_exists? - manifest_path && File.exist?(manifest_path) +def remote_group? name, _config + name == "remote" # || config["remote"] end def manifest_group_items group_name - manifest.dig(group_name, "items") || [] + manifest&.dig(group_name, "items") || [] end def manifest_group_minimize? group_name manifest.dig group_name, "minimize" end def manifest - @manifest ||= YAML.load_file manifest_path + @manifest ||= load_manifest end -def with_manifest_groups - manifest.each_pair do |key, config| - yield key, config - end +def load_manifest + return unless manifest_exists? + manifest = YAML.load_file manifest_path + validate_manifest manifest + manifest end -def ensure_manifest_groups_cards - with_manifest_groups { |group_name, config| new_manifest_group group_name, config } -end - -def new_manifest_group group_name, config - type_id = - config["remote"] ? ::Card::RemoteManifestGroupID : local_manifest_group_type_id - ensure_item group_name, type_id -end - -def ensure_item field, type_id - item_name = "#{name}+#{field}" - ensure_item_content item_name - - card = Card[item_name] - args = ensure_item_args field, type_id, item_name - return if item_already_coded? card, args - - ensure_item_save card, args - card.try :update_machine_output -end - -def item_already_coded? card, args - card&.type_id == args[:type_id] && card.codename == args[:codename] -end - -def ensure_item_content item_name - @old_items.delete item_name.to_name.key - add_item item_name -end - -def ensure_item_save card, args - if card - card.update args - else - Card.create! args +def validate_manifest manifest + if (remote_index = manifest.keys.find_index("remote")) && remote_index.positive? + raise_manifest_error "only the first group can be a remote group" end + manifest.each do |name, config| + validate_manifest_item name, config + end end -def ensure_item_args field, type_id, name +def group_card_args field, type_id, name { type_id: type_id, codename: "#{mod_name}_group__#{field}", name: name } end -def refresh_output force: false - update_items - item_cards.each do |item_card| - item_card.try :refresh_output, force: force - end +def source_changed? since: + source_updates = + if manifest_exists? + [manifest_updated_at, local_manifest_group_cards.map(&:last_file_change)].flatten + else + folder_group_card&.paths&.map { |path| File.mtime(path) } + end + + return unless source_updates.present? + + source_updates.max > since end -def make_machine_output_coded verbose=false - item_cards.each do |item_card| - puts "coding machine output for #{item_card.name}" if verbose - item_card.try(:make_machine_output_coded, mod_name) - end +def manifest_updated_at + return unless manifest_exists? + File.mtime(manifest_path) end def no_action? - new? && !assets_dir_exists? + new? && !assets_path end private -# def groups_changed? -# expected_items = expected_item_keys -# actual_items = item_keys -# difference = (expected_items + actual_items) - (expected_items & actual_items) -# difference.present? -# end +def new_local_manifest_group_card group_name + card = new_assets_group_card group_name, local_manifest_group_type_id + card.group_name = group_name + card +end -def remove_deprecated_items items - items.each do |deprecated_item| - next unless (item_card = Card.fetch(deprecated_item)) - item_card.update codename: nil - item_card.delete - end +def new_assets_group_card group_name, type_id + item_name = "#{name}+group: #{group_name}" + card = Card.new group_card_args(group_name, type_id, item_name) + card +end + +def validate_manifest_item name, config + raise_manifest_error "no items section in group \"#{name}\"" unless config["items"] + return if config["items"].is_a? Array + + raise_manifest_error "items section \"#{name}\" must contain a list" +end + +def raise_manifest_error msg + raise Card::Error, "invalid manifest format in #{manifest_path}: #{msg}" end