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
- 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.
- Python 3.x
- Clone this repository or download the script.
git clone [email protected]:jghg02/XCrafter.git
cd XCrafter
-
Ensure you have Python 3 installed on your machine.
-
Execute the
setup.sh
script and then reload your terminal. -
Ensure you could execute
xcrafter
in your terminal.
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.
- Create an .xcassets folder named MyIcon.xcassets with images from the specified folder:
xcrafter -n MyIcon -i /path/to/images_folder
- 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
- Create an .xcassets folder and generate a Swift enum named Icons:
xcrafter -n MyIcon -i /path/to/images_folder --enum Icons
- 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
- 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
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.
Folder Images PNG | XCAssest |
---|---|
Folder Images SVG | XCAssest |
---|---|
For images ic_cog.svg and [email protected], 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.