[go: up one dir, main page]

Skip to content

jghg02/XCrafter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

XCrafter

A Python script to create an .xcassets folder structure for an Xcode project. The script can process images, create image sets, and optionally generate a Swift enum to reference the assets.

CleanShot.June.3.mp4

Features

  • Convert image names to snake_case.
  • Optionally create a subfolder inside the .xcassets folder.
  • Group images by their base names and create image sets.
  • Generate a Contents.json file for each image set.
  • Optionally generate a Swift enum to reference the assets.

Requirements

  • Python 3.x

Installation

  1. Clone this repository or download the script.
git clone git@github.com:jghg02/XCrafter.git
cd XCrafter
  1. Ensure you have Python 3 installed on your machine.

  2. Execute the setup.sh script and then reload your terminal.

  3. Ensure you could execute xcrafter in your terminal.

Usage

The script provides several options to customize the creation of the .xcassets folder and the Swift enum.

Command-line Arguments

• -n, --name: (Required) The name of the asset to create (will be converted to snake case).
• -i, --images_folder: (Required) The folder containing the images.
• -o, --output_folder: The output folder to save the .xcassets and Swift enum. By default the path are in your `Desktop`.
• -s, --subfolder: The name of the subfolder to create inside the .xcassets folder.
• --enum: The name of the Swift enum class to create. If not provided, the enum will not be generated.
• -h: Show helper.

Example Usage

  1. Create an .xcassets folder named MyIcon.xcassets with images from the specified folder:
xcrafter -n MyIcon -i /path/to/images_folder
  1. Create an .xcassets folder named MyIcon.xcassets with images from the specified folder and create a subfolder inside .xcassets:
xcrafter -n MyIcon -i /path/to/images_folder -s MySubfolder
  1. Create an .xcassets folder and generate a Swift enum named Icons:
xcrafter -n MyIcon -i /path/to/images_folder --enum Icons
  1. Create an .xcassets folder, create a subfolder inside .xcassets, and generate a Swift enum:
xcrafter -n MyIcon -i /path/to/images_folder -s MySubfolder --enum Icons
  1. Create an .xcassets folder, create a subfolder inside .xcassets, generate a Swift enum and specify the output folder
xcrafter -n MyIcon -i /path/to/images_folder -s MySubfolder --enum Icons -o /your/path/here

Generated Swift Enum

If the --enum parameter is provided, the script generates a Swift enum with cases for each image base name. The enum case names are in camel case, while the raw values are in snake case.

Example

Folder Images PNG XCAssest
CleanShot 2024-06-03 at 11 34 21@2x CleanShot 2024-06-03 at 11 38 22@2x
Folder Images SVG XCAssest
CleanShot 2024-06-03 at 11 45 45@2x CleanShot 2024-06-03 at 11 44 55@2x

For images ic_cog.svg and ic_undo_dos@2x.png, the generated enum would be:

import UIKit

public enum Icons: String, CaseIterable {
 case cog = "ic_cog"
 case undoDos = "ic_undo_dos"

 static var allIcons: [Icons] {
   return Icons.allCases
 }

 var iconName: String {
    return rawValue
 }
}

And then you can drag and drop into your Xcode project.