Sha256: 8f6c05336e3183e01cdf30970f2290a30da1cf5a6cf8ab5b87639b7d4bfbc007

Contents?: true

Size: 1.9 KB

Versions: 13

Compression:

Stored size: 1.9 KB

Contents

require 'fileutils'
require 'json'

class XcodeAssetManager
  def initialize(asset_catalog_path)
    @asset_catalog_path = asset_catalog_path
  end

  def add(source)
        # Fetches only files in the specified source directory
        Dir.glob(File.join(source, '*.{png,jpg,jpeg,svg,heic,pdf}')).each do |file_path|
          if File.file?(file_path) # Ensure it's a file and not a directory
                filename_without_extension = File.basename(file_path, File.extname(file_path))
                add_image(filename_without_extension, file_path, '1x')
            end
        end
    end
    
  def add_image(image_name, image_path, scale = '1x')
    # Create the image set directory if it doesn't exist
    image_set_path = File.join(@asset_catalog_path, "#{image_name}.imageset")
    FileUtils.mkdir_p(image_set_path)

    # Copy the image file to the image set directory
    destination_path = File.join(image_set_path, "#{image_name}@#{scale}.png")
    FileUtils.cp(image_path, destination_path)

    # Update or create the Contents.json file
    contents_json_path = File.join(image_set_path, 'Contents.json')
    contents = if File.exist?(contents_json_path)
                 JSON.parse(File.read(contents_json_path))
               else
                 { "images" => [], "info" => { "version" => 1, "author" => "solara" } }
               end

    # Add or update the image entry
    image_entry = {
      "idiom" => "universal",
      "filename" => "#{image_name}@#{scale}.png",
      "scale" => scale
    }

    existing_entry = contents["images"].find { |img| img["scale"] == scale }
    if existing_entry
      existing_entry.merge!(image_entry)
    else
      contents["images"] << image_entry
    end

    # Write the updated Contents.json
    File.write(contents_json_path, JSON.pretty_generate(contents))

    Solara.logger.debug("XcodeAssetManager: Image '#{image_name}' added successfully at scale #{scale}.")
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
solara-0.7.4 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.7.3 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.7.2 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.7.1 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.7.0 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.6.0 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.5.0 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.4.0 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.3.0 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.2.4 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.2.3 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.2.2 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb
solara-0.2.1 solara/lib/core/scripts/platform/ios/xcode_asset_manager.rb