Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sichacvah/3efba35da56199568b9a41bc266eaf0d to your computer and use it in GitHub Desktop.

Select an option

Save sichacvah/3efba35da56199568b9a41bc266eaf0d to your computer and use it in GitHub Desktop.
Ruby Script to create AssetCatalogs file in iOS Application.It basically fetches all the .png files from the application folder and subfolders and create a single AssetCatalog files inside the application.Most of the design developer collaboration issues will be sorted out with this script.Ask the designer to give all the assets and put into the…
require 'find'
require 'fileutils'
require 'tmpdir'
def createAssetCatalogFile()
message = '
Creating Asset Catalogs file ...'
puts message
fileName = "images.xcassets"
projectFolderName = Dir.getwd
# Create asset catalog file if doesnt exists
if File.directory?("#{fileName}") == false
Dir.mkdir(fileName)
end
# Fetch all the png files from the project folder
Dir.glob("#{projectFolderName}/**/*.png").each do |asset|
#Jump into asset catalog folder
Dir.chdir(fileName)
assetName = File.basename(asset,'.*')
assetNameWithout2x = ""
if assetName.include? "@2x"
assetNameWithout2x = assetName.gsub("@2x", "")
imageSetName = "#{ assetNameWithout2x }.imageset"
else
imageSetName = "#{ assetName }.imageset"
end
#If directory doesnt exists, create new ImageSet directory which holds images
if File.directory?("#{imageSetName}") == false
Dir.mkdir(imageSetName)
end
#Jump into ImageSet directory
Dir.chdir(imageSetName)
FileUtils.mv("#{asset}", "#{assetName}.png")
#Create Json File which needs to be present for every ImageSet
createJsonfile(assetNameWithout2x)
#Jump back to parent directory
Dir.chdir(projectFolderName)
end
end
def createJsonfile(fileName)
fileName_1x = "#{fileName}.png"
fileName_2x = "#{fileName}@2x.png"
content = '{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "'"#{ fileName_1x }"'"
},
{
"idiom" : "universal",
"scale" : "2x",
"filename" : "'"#{ fileName_2x }"'"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}'
target = "Contents.json"
File.open(target, "w+") do |f|
f.write(content)
end
end
createAssetCatalogFile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment