# frozen_string_literal: true require 'json' require_relative 'image' require_relative 'imageset' module XCAssets class XCAssets attr_reader :name def initialize(name) @name = name @imagesets = [] end def add_imageset(imageset) @imagesets << imageset end def save(directory) root = directory.join("#{name}.xcassets") root.rmtree if root.exist? root.mkpath write_imagesets(root) write_contents_json(root) end private def write_imagesets(root) @imagesets.each do |imageset| path = root.join("#{imageset.name}.imageset") path.rmtree if path.exist? path.mkpath imageset.images.each do |image| if image.resource FileUtils.cp image.resource, path.join(image.filename) end end contents_path = path.join("Contents.json") File.open(contents_path, 'w+') do |file| file.write JSON.pretty_generate(imageset.contents) end end end def assets_filename "#{name}.xcassets" end def write_contents_json(root) path = root.join('Contents.json') contents = { :info => { :author => 'xcode', :version => 1 } } contents_string = JSON.pretty_generate(contents) File.open(path, 'w+') do |file| file.write contents_string end end end end