Created
May 31, 2012 20:33
-
-
Save aegzorz/2846024 to your computer and use it in GitHub Desktop.
Generating resource IDs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
header_subpath = "Resources.h" | |
resource_categories = { | |
".pdf" => "File", | |
".png" => "Image", | |
".jpg" => "Image" | |
} | |
def wikify(phrase) | |
phrase = phrase.tr( "-", " " ).tr( "_", " " ).tr( "/", " " ) | |
phrase.gsub!(/^[a-z]|\s+[a-z]/) { |a| a.upcase } | |
phrase.gsub!(/\s/, '') | |
return phrase | |
end | |
def app_resources(categories) | |
resources = [] | |
extensions = categories.keys | |
Dir.glob( "**/*" ) do |filename| | |
if extensions.include?( File.extname( filename ) ) | |
resources << filename | |
end | |
end | |
resources | |
end | |
def write_header(path, resources, categories) | |
File.open( path, 'w' ) do |header| | |
header.puts( "static NSString* __AppResources[] = {" ) | |
resources.each do |resource| | |
header.puts( "\t@\"#{resource}\"," ) | |
end | |
header.puts( "\tnil" ) | |
header.puts( "};" ) | |
header.puts | |
index = 0 | |
resources.each do |resource| | |
extension = File.extname(resource) | |
identifier = wikify( resource.chomp( extension ) ) | |
header.puts( "static const NSUInteger k" + categories[ extension ] + identifier + " = #{index};" ) | |
index += 1 | |
end | |
header.puts | |
header.puts( "static __inline__ NSString* GetResourcePath( const NSUInteger identifier ) {" ) | |
header.puts( "\treturn ( identifier < #{resources.length} ) ? __AppResources[ identifier ] : nil;" ) | |
header.puts( "}" ) | |
header.puts | |
header.puts( "static __inline__ UIImage* GetImageWithID( const NSUInteger identifier ) {" ) | |
header.puts( "\treturn [UIImage imageNamed:GetResourcePath( identifier )];" ) | |
header.puts( "}" ) | |
end | |
end | |
header_path = File.join( ENV['PROJECT_DIR'], header_subpath ) | |
write_header( header_path, app_resources( resource_categories ), resource_categories ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment