Class: Pindo::XcodeResHandler

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

Instance Method Summary collapse

Constructor Details

#initialize(proj_fullname: nil) ⇒ XcodeResHandler

Returns a new instance of XcodeResHandler.



11
12
13
14
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 11

def initialize(proj_fullname:nil)
    @proj_fullname = proj_fullname
    @project_obj = Xcodeproj::Project.open(proj_fullname)
end

Instance Method Details

#get_xcodeproj_icon_pathObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 16

def get_xcodeproj_icon_path()

    icon_path = nil
    select_target = @project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])  }.first

    project_dir = @project_obj.project_dir
  
    if !select_target.nil?
        file_refs = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("Assets.xcassets") } || []
        file_refs.each do |file_ref|
            icon_path = File.join(file_ref.real_path,"AppIcon.appiconset")
            if File.exist?(icon_path)
                break
            else
                next
            end
        end
    end
    if icon_path.nil? || icon_path.empty? || !File.exist?(icon_path)
        icon_path_array = Dir.glob(File.join(project_dir, "**", "AppIcon.appiconset"))
        if icon_path_array.size > 1
            icon_path = icon_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/AppIcon.appiconset")}
        elsif icon_path_array.size == 1
            icon_path = icon_path_array.first
        end
    end

    if icon_path.nil? || icon_path.empty? || !File.exist?(icon_path)
        raise Informative, "没有找到Xcode icon 目录"
    end

    return icon_path
end

#get_xcodeproj_imessage_icon_pathObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 74

def get_xcodeproj_imessage_icon_path
    icon_path = nil
    select_target = @project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:messages_extension])  }.first
    project_dir = @project_obj.project_dir
    if !select_target.nil?
        file_refs = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("Assets.xcassets") } || []
        file_refs.each do |file_ref|
            icon_path = File.join(file_ref.real_path,"iMessage App Icon.stickersiconset")
            if File.exist?(icon_path)
                break
            else
                next
            end
        end

        if icon_path.nil? || icon_path.empty? || !File.exist?(icon_path)
            icon_path_array = Dir.glob(File.join(project_dir, "**", "iMessage App Icon.stickersiconset"))
            if icon_path_array.size > 1
                icon_path = icon_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/iMessage App Icon.stickersiconset")}
            elsif icon_path_array.size == 1
                icon_path = icon_path_array.first
            end
        end

        if icon_path.nil? || icon_path.empty? || !File.exist?(icon_path)
            raise Informative, "没有找到Xcode iMessage icon 目录"
        end
    end

    return icon_path
end

#get_xcodeproj_launchimg_pathObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 132

def get_xcodeproj_launchimg_path

    launchimg_path = nil
    assets_path = nil
    select_target = @project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])  }.first
    project_dir = @project_obj.project_dir
    if !select_target.nil?
        assets_objs = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("Assets.xcassets") } || []
        if assets_objs.size > 0
            assets_path = assets_objs.first.real_path
            if File.exist?(assets_path) && File.exist?(File.join(assets_path, "LaunchImage.imageset"))
                launchimg_path = File.join(assets_path, "LaunchImage.imageset")
            elsif File.exist?(assets_path) && File.exist?(File.join(assets_path, "LaunchImage.launchimage"))
                launchimg_path = File.join(assets_path, "LaunchImage.launchimage")
            end
        end
    end

    if launchimg_path.nil? || launchimg_path.empty? || !File.exist?(launchimg_path)
        launchimg_path_array = Dir.glob(File.join(project_dir, "**", "LaunchImage.imageset"))
        if launchimg_path_array.size > 1
            launchimg_path = launchimg_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/LaunchImage.imageset")}
        elsif launchimg_path_array.size == 1
            launchimg_path = launchimg_path_array.first
        end
    end

    if launchimg_path.nil? || launchimg_path.empty? || !File.exist?(launchimg_path)
        launchimg_path_array = Dir.glob(File.join(project_dir, "**", "LaunchImage.launchimage"))
        if launchimg_path_array.size > 1
            launchimg_path = launchimg_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/LaunchImage.launchimage")}
        elsif launchimg_path_array.size == 1
            launchimg_path = launchimg_path_array.first
        end
    end

    return launchimg_path
end

#install_icon_res(new_icon_dir: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 50

def install_icon_res(new_icon_dir:nil)
    icon_path = get_xcodeproj_icon_path()
    begin
        FileUtils.rm_rf(icon_path)
        FileUtils.mkdir_p(icon_path)
    rescue StandardError => e
    end
    files = Dir.glob("#{new_icon_dir}/*").select { |file| File.file?(file) }
    files.each do |file|
        FileUtils.cp(file, icon_path)
    end
end

#install_imessage_icon_res(new_icon_dir: nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 106

def install_imessage_icon_res(new_icon_dir:nil)
    icon_path = get_xcodeproj_imessage_icon_path
    begin
        FileUtils.rm_rf(icon_path)
        FileUtils.mkdir_p(icon_path)
    rescue StandardError => e
    end

    files = Dir.glob("#{new_icon_dir}/*").select { |file| File.file?(file) }
    files.each do |file|
        FileUtils.cp(file, icon_path)
    end
end

#install_launchimg(launchimg_pub_path: nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 171

def install_launchimg(launchimg_pub_path:nil)

    xcodeproj_launchimg_path = get_xcodeproj_launchimg_path
    pub_launchimg_files = Dir.glob(File.join(launchimg_pub_path, "*.{png,jpg}"))

    if xcodeproj_launchimg_path.nil? || !File.exist?(xcodeproj_launchimg_path)
        if !pub_launchimg_files.nil? && pub_launchimg_files.size > 0
            raise  Informative, "有需要替换的资源,但是未找到工程启动图目录,替换启动图失败!"
        end
        return
    end

    # if pub_launchimg_files.nil? || pub_launchimg_files.size == 0
    #     raise  Informative, "有需要替换的资源,但是配置中未放置替换的启动图!"
    # end

    xcodeproj_launchimg_json_file = File.join(xcodeproj_launchimg_path, "Contents.json")
    if !File.exist?(xcodeproj_launchimg_json_file)
        raise  Informative, "工程启动图目录缺少Contents.json,替换启动图失败!"
    end

    xcodeproj_launchimg_json = JSON.parse(File.read(xcodeproj_launchimg_json_file)) 
    image_array = xcodeproj_launchimg_json["images"].select { |image| !image["filename"].nil? && !image["filename"].empty? }
    
    if pub_launchimg_files.size == image_array.size
        if image_array.size == 1
            launchimg_filename = image_array.first["filename"]
            launchimg_full_filename = File.join(xcodeproj_launchimg_path, launchimg_filename)
            FileUtils.cp(pub_launchimg_files.first, launchimg_full_filename)
        else
            raise  Informative, "启动图有多张,替换异常,遇到这种情况再处理!"
        end
    else
        raise  Informative, "工程中启动图数目和配置仓库的图片数目不一致"
    end

end

#validate_icon_resObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 63

def validate_icon_res()
    icon_path = get_xcodeproj_icon_path()
    xcode_ios_icon_json = XcodoeResConst.xcode_ios_icon_json
    xcode_ios_icon_json["images"].each do |image_data|
        image_name = image_data["filename"]
        if !File.exist?(File.join(icon_path, image_name))
            raise  Informative, "Xcode生成icon 有缺失文件:#{File.join(icon_path, image_name)}"
        end
    end
end

#validate_imessage_icon_resObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 121

def validate_imessage_icon_res()
    icon_path = get_xcodeproj_imessage_icon_path
    xcode_ios_icon_json = XcodoeResConst.xcode_ios_imessage_icon_json
    xcode_ios_icon_json["images"].each do |image_data|
        image_name = image_data["filename"]
        if !File.exist?(File.join(icon_path, image_name))
            raise  Informative, "Xcode生成icon 有缺失文件:#{File.join(icon_path, image_name)}"
        end
    end
end