Skip to content

Instantly share code, notes, and snippets.

@igor-makarov
Created July 26, 2019 16:19
Show Gist options
  • Save igor-makarov/79845f8fbd72854d47db78edaf15cfcc to your computer and use it in GitHub Desktop.
Save igor-makarov/79845f8fbd72854d47db78edaf15cfcc to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pathname'
require 'set'
require 'tempfile'
dir = File.dirname(__FILE__)
file = ARGV[1] || "#{dir}/../../SharedModules/Styling/UIImage-Generated.swift"
files = Dir["#{Dir.pwd}/Moovit/**/*.{png,pdf,jpg}"] + Dir["#{Dir.pwd}/Flavors/*/WLASlices.xcassets/**/*.{png,pdf,jpg}"]
names = SortedSet.new
files.each do |filename|
next if filename.include? "AppGlobalImages"
path = Pathname.new(filename).split
dir = path[-1].to_s
if dir.end_with? "imageset"
name = dir.split(".").first
elsif match = path.last.to_s.match(/([^@.]*)(?:@[0-9]x)?\..*/)
name = match[1]
else
name = nil
end
names.add(name) if name
end
temp = Tempfile.new('rfileimage')
File.open(temp, "w") do |outfile|
outfile.puts "import UIKit"
outfile.puts "@objc public extension UIImage {"
outfile.puts " @objc public static let Keys = ImageKeysStore()"
outfile.puts ""
names.each do |n|
next if n.include? "-"
outfile.puts " @objc public static let #{n} = #imageLiteral(resourceName: \"#{n}\")"
end
outfile.puts "}"
outfile.puts ""
outfile.puts "public class ImageKeysStore : NSObject {"
names.each do |n|
next if n.include?("-")
outfile.puts " @objc public var #{n}: String { get { return \"#{n}\"} }"
end
outfile.puts "}"
outfile.puts ""
end
FileUtils.mv(temp, file, :force => true) if !File.exist?(file) || !FileUtils.identical?(temp, file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment