Class: Pindo::XcodeResHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/xcode/xcode_res_helper.rb

Class Method Summary collapse

Class Method Details

.create_icon(icon_name: nil, new_icon_dir: nil, xcode_icon_json: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pindo/module/xcode/xcode_res_helper.rb', line 31

def create_icon(icon_name:nil, new_icon_dir:nil, xcode_icon_json:nil)
    if !File.exist?(icon_name)
        raise  Informative, "文件不存在:#{icon_name}"
    end
    xcode_icon_json["images"].each do |image_data|
        width, height = image_dimensions(image_data)
        image_name = image_data["filename"]
        next if image_name.nil? || image_name.empty?

        command = [
            'sips',
            '-s', 'format', 'png',
            '--matchTo', '/System/Library/ColorSync/Profiles/sRGB Profile.icc',
            '-z', width.to_s, height.to_s,
            icon_name,
            '--out', File.join(new_icon_dir, image_name)
        ]
        Executable.capture_command('sips', command, capture: :out)

        # if !File.exist?(File.join(new_icon_dir, image_name))
        #     raise  Informative, "生成icon失败!"
        # end
        # system("sips --matchTo '/System/Library/ColorSync/Profiles/sRGB Profile.icc' -z #{width} #{height} #{icon_name} --out #{new_icon_dir}/#{image_name}")
    end
    File.open(File.join(new_icon_dir, "Contents.json"), "w") do |f|
        f.write(JSON.pretty_generate(xcode_icon_json))
    end
end

.create_icons(icon_name: nil, new_icon_dir: nil, proj_dir: nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pindo/module/xcode/xcode_res_helper.rb', line 91

def create_icons(icon_name:nil, new_icon_dir:nil, proj_dir:nil)
    begin
        FileUtils.mkdir_p(new_icon_dir)
    rescue => e
        puts e
    end
    xcode_icon_json = if !proj_dir.nil? && macos_project?(proj_dir)
                        XcodoeResConst.xcode_macos_icon_json
                      else
                        XcodoeResConst.xcode_ios_icon_json
                      end
    create_icon(icon_name: icon_name, new_icon_dir: new_icon_dir, xcode_icon_json: xcode_icon_json)
    icon_dir = File.dirname(icon_name)
    imessage_icon = File.join(icon_dir, "icon1024_768.png")
    new_imessage_icon_dir = File.join(new_icon_dir, "imessage")

    if File.exist?(imessage_icon)
        begin
            FileUtils.mkdir_p(new_imessage_icon_dir) 
        rescue => e
            puts e
        end
        create_imessage_icon(icon_name: icon_name, image_icon_name:imessage_icon, new_icon_dir: new_imessage_icon_dir, xcode_icon_json: XcodoeResConst.xcode_ios_imessage_icon_json)
    end
end

.create_imessage_icon(icon_name: nil, image_icon_name: nil, new_icon_dir: nil, xcode_icon_json: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/pindo/module/xcode/xcode_res_helper.rb', line 61

def create_imessage_icon(icon_name:nil, image_icon_name:nil, new_icon_dir:nil, xcode_icon_json:nil)
    if !File.exist?(icon_name) || !File.exist?(image_icon_name) 
        raise  Informative, "文件不存在:#{image_icon_name} or #{icon_name}"
    end
    xcode_icon_json["images"].each do |image_data|
        width, height = image_dimensions(image_data)
        image_name = image_data["filename"]
        next if image_name.nil? || image_name.empty?

        icon_ori_name = image_icon_name
        if width.to_s.eql?(height.to_s)
            icon_ori_name = icon_name
        end

        command = [
            'sips',
            '-s', 'format', 'png',
            '--matchTo', '/System/Library/ColorSync/Profiles/sRGB Profile.icc',
            '-z', width.to_s, height.to_s,
            icon_ori_name,
            '--out', File.join(new_icon_dir, image_name)
        ]
        Executable.capture_command('sips', command, capture: :out)
    end

    File.open(File.join(new_icon_dir, "Contents.json"), "w") do |f|
        f.write(JSON.pretty_generate(xcode_icon_json))
    end
end

.image_dimensions(image_data) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/pindo/module/xcode/xcode_res_helper.rb', line 21

def image_dimensions(image_data)
    width, height = image_data.fetch("size").split("x").map(&:to_f)
    scale = image_data.fetch("scale", "1x").to_s.chomp("x").to_f

    [
        (width * scale).to_i,
        (height * scale).to_i
    ]
end

.install_icon(proj_dir: nil, new_icon_dir: nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/pindo/module/xcode/xcode_res_helper.rb', line 117

def install_icon(proj_dir:nil, new_icon_dir:nil)
    xcodeproj_file_name = Dir.glob(File.join(proj_dir, "/*.xcodeproj")).max_by {|f| File.mtime(f)}
    xcodereshandler = XcodeResHandler.new(proj_fullname: xcodeproj_file_name)
    xcodereshandler.install_icon_res(new_icon_dir: new_icon_dir)
    xcodereshandler.validate_icon_res

    new_imessage_icon_dir = File.join(new_icon_dir, "imessage")
    if File.exist?(new_imessage_icon_dir)
        xcodereshandler.install_imessage_icon_res(new_icon_dir: new_imessage_icon_dir)
        xcodereshandler.validate_imessage_icon_res
    end
end

.install_launchimg(proj_dir: nil, launchimg_pub_path: nil) ⇒ Object



130
131
132
133
134
# File 'lib/pindo/module/xcode/xcode_res_helper.rb', line 130

def install_launchimg(proj_dir:nil, launchimg_pub_path:nil)
    xcodeproj_file_name = Dir.glob(File.join(proj_dir, "/*.xcodeproj")).max_by {|f| File.mtime(f)}
    xcodereshandler = XcodeResHandler.new(proj_fullname: xcodeproj_file_name)
    xcodereshandler.install_launchimg(launchimg_pub_path:launchimg_pub_path)
end

.macos_project?(proj_dir) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
# File 'lib/pindo/module/xcode/xcode_res_helper.rb', line 11

def macos_project?(proj_dir)
    xcodeproj_file_name = Dir.glob(File.join(proj_dir, "*.xcodeproj")).max_by { |f| File.mtime(f) }
    return false if xcodeproj_file_name.nil?

    project_obj = Xcodeproj::Project.open(xcodeproj_file_name)
    sdkroot_settings = project_obj.root_object.build_configuration_list.get_setting("SDKROOT")
    sdkroot = sdkroot_settings.values.compact.first
    !sdkroot.nil? && sdkroot.eql?("macosx")
end