class Picon::Generator

Constants

RESOLUTIONS_LIST_PATH

Public Class Methods

new(args = {}) click to toggle source
# File lib/picon/generator.rb, line 15
def initialize(args = {})
  if args[:current_path]
    @current_path = Pathname.new(File.expand_path(args[:current_path]))
  else
    @current_path = Pathname.pwd
  end

  @product_name = get_product_name
  @appiconset_path = get_appiconset_path
  @bundle_identifier = get_bundle_identifier
end
run() click to toggle source
# File lib/picon/generator.rb, line 11
def self.run
  new.run
end

Public Instance Methods

run() click to toggle source
# File lib/picon/generator.rb, line 27
def run
  generate_identicons
  generate_contents_json
  edit_project_pbxproj
end

Private Instance Methods

edit_project_pbxproj() click to toggle source
# File lib/picon/generator.rb, line 96
def edit_project_pbxproj
  data = ""

  pbxproj_path = Pathname.glob(@current_path.to_s + "/**/project.pbxproj").first
  pbxproj_path.open("rb") do |file|
    data = file.read
  end

  xcworkspace_path = Pathname.glob(@current_path.to_s + "/**/#{@product_name}.xcworkspace").first
  if xcworkspace_path && xcworkspace_path.exist?
    data.gsub!(/(<key>ASSETCATALOG_COMPILER_APPICON_NAME<\/key>\n\t+<string>).+(<\/string>)/) { "#{$1}Picon#{$2}" }
  else
    data.gsub!(/(ASSETCATALOG_COMPILER_APPICON_NAME = )AppIcon/) { "#{$1}Picon" }
  end

  pbxproj_path.open("wb") do |file|
    file.flush
    file << data
  end
end
generate_contents_json() click to toggle source
# File lib/picon/generator.rb, line 70
def generate_contents_json
  contents = { "images" => [] }

  resolutions = YAML.load_file(RESOLUTIONS_LIST_PATH)
  resolutions.each do |device, properties|
    properties.each do |property|
      size = property["size"].to_i
      size /= 2 if property["scale"] == "2x"

      image = {}
      image["idiom"] = device
      image["filename"] = property["filename"]
      image["size"] = "#{size}x#{size}"
      image["scale"] = property["scale"]
      contents["images"] << image
    end
  end

  contents["info"] = { "version" => 1, "author" => "picon" }

  filepath = @appiconset_path.join("Contents.json")
  filepath.open("wb") do |file|
    file << JSON.pretty_generate(contents)
  end
end
generate_identicons() click to toggle source
# File lib/picon/generator.rb, line 55
def generate_identicons
  resolutions = YAML.load_file(RESOLUTIONS_LIST_PATH)
  resolutions.each do |device, images|
    images.each do |image|
      filepath = @appiconset_path.join(image["filename"])
      next if filepath.exist?

      grid_size = 7
      square_size = image["size"].to_i / grid_size
      border_size = ((image["size"].to_i - grid_size * square_size).to_f / 2).ceil
      RubyIdenticon.create_and_save(@bundle_identifier, filepath, border_size: border_size, grid_size: grid_size, square_size: square_size)
    end
  end
end
get_appiconset_path() click to toggle source
# File lib/picon/generator.rb, line 40
def get_appiconset_path
  path = Pathname.glob(@current_path.to_s + "/**/*.xcassets").first
  path = path.join("Picon.appiconset")
  path.mkdir unless path.exist?
  path
end
get_bundle_identifier() click to toggle source
# File lib/picon/generator.rb, line 47
def get_bundle_identifier
  pathnames = Pathname.glob(@current_path.to_s + "/**/*-Info.plist")
  pathnames.reject! { |pathname| pathname.to_s =~ /Test/ }
  pathname = pathnames.first
  plist = Plist.parse_xml(pathname.to_s)
  plist["CFBundleIdentifier"].gsub!(/\${PRODUCT_NAME.*}$/) { @product_name }
end
get_product_name() click to toggle source
# File lib/picon/generator.rb, line 35
def get_product_name
  xcodeproj_path = Pathname.glob(@current_path.to_s + "/**/*.xcodeproj").first
  File.basename(xcodeproj_path.to_s, ".xcodeproj")
end