Guest User

Untitled

a guest
Jan 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. class UIKitImage
  2. attr_accessor :iphone1x, :iphone2x, :ipad, :baseName, :resourcePath, :fullPath
  3.  
  4. def to_s
  5. kinds = Array.new
  6. kinds << "@1x" if @iphone1x == true
  7. kinds << "@2x" if @iphone2x == true
  8. kinds << "~ipad" if @ipad == true
  9.  
  10. return @baseName + " " + kinds.join(" ")
  11. end
  12. end
  13.  
  14. class NilClass
  15. def length
  16. 0
  17. end
  18. end
  19.  
  20. class String
  21. def substr_from(idx)
  22. self[idx, self.length-idx]
  23. end
  24. end
  25.  
  26. resourcesDir = "/Users/dave/Developer/UIKit/Resources/"
  27. kinds = {
  28. "~iphone" => "iphone1x",
  29. "@2x" => "iphone2x",
  30. "@2x~iphone" => "iphone2x",
  31. "~iphone@2x" => "iphone2x",
  32. "~ipad" => "ipad"
  33. }
  34.  
  35. images = Hash.new
  36. resources = Dir.glob("#{resourcesDir}**/*.png");
  37. resources.each do|file|
  38. basename = File.basename(file, ".png")
  39.  
  40. # attempt to find the suffix used by the file
  41. property = nil
  42. suffix = nil
  43. kinds.keys.sort.each do|potentialSuffix|
  44. if basename.downcase.end_with?(potentialSuffix) then
  45. if potentialSuffix.length > suffix.length then
  46. property = kinds[potentialSuffix]
  47. suffix = potentialSuffix
  48. end
  49. end
  50. end
  51.  
  52. # figure out the actual basename
  53. property = "iphone1x" if property == nil
  54.  
  55. basename = basename[0, basename.length-suffix.length] if suffix.length > 0
  56.  
  57. # retrieve the image object
  58. image = images[basename]
  59. if image == nil then
  60. image = UIKitImage.new
  61. image.fullPath = file
  62. image.baseName = basename
  63.  
  64. image.resourcePath = file.substr_from(resourcesDir.length)
  65.  
  66. if basename == 126 or basename == 64 then
  67. puts "ERROR: #{file} #{basename} #{suffix}"
  68. end
  69. images[basename] = image
  70. end
  71.  
  72. # set the property value
  73. image.send("#{property}=", true)
  74. end
  75.  
  76. images.keys.sort.each do|basename|
  77. puts images[basename]
  78. end
Add Comment
Please, Sign In to add comment