Class: Pindo::Command::Dev::Applovin

Inherits:
Pindo::Command::Dev show all
Defined in:
lib/pindo/command/dev/applovin.rb

Constant Summary

Constants inherited from Pindo::Command

Pindo::Command::DEFAULT_OPTIONS, Pindo::Command::DEFAULT_ROOT_OPTIONS

Instance Attribute Summary

Attributes inherited from Pindo::Command

#args_help_flag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pindo::Command

run

Methods included from Funlog::Mixin

#pindo_log_instance

Methods included from Pindoconfig::Mixin

#pindo_single_config

Methods included from Githelper

#add_branch, #add_tag, #add_tag_with_check, #clone_clang_repo, #clone_devclang_repo, #clone_pindo_common_config_repo, #clone_pindo_env_config_repo, #clong_buildconfig_repo, #get_repo_base_name, #getcode_to_dir, #git_addpush_repo, #git_latest_commit_id, #local_branch_exists?, #local_tag_exists?, #prepare_gitenv, #process_need_add_files, #remote_branch_exists?, #remote_tag_exists?, #remove_branch, #remove_tag

Methods included from Executable

capture_command, #executable, execute_command, which, which!

Constructor Details

#initialize(argv) ⇒ Applovin

Returns a new instance of Applovin.



31
32
33
34
35
36
37
38
39
# File 'lib/pindo/command/dev/applovin.rb', line 31

def initialize(argv)

    @args_appstore_flag = argv.flag?('appstore', false)
    @args_install_flag = argv.flag?('install', false)
    @args_upload_flag = argv.flag?('upload', false)

    super(argv)
    @additional_args = argv.remainder!
end

Class Method Details

.optionsObject



23
24
25
26
27
28
29
# File 'lib/pindo/command/dev/applovin.rb', line 23

def self.options
  [
    ['--appstore', 'appstore模式,一般不用'],
    ['--install', '是否运行applovin脚本'],
    ['--upload', '保存ids, 留作applovin网络不好情况使用'],
  ].concat(super)
end

Instance Method Details

#get_applovin_infoplist(project_dir: nil, proj_name: nil) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/pindo/command/dev/applovin.rb', line 155

def get_applovin_infoplist(project_dir:nil, proj_name:nil)

    info_plist_path = File.join(project_dir,  "AppEntry/Info.plist")
    proj_fullname = File.join(project_dir, proj_name) + ".xcodeproj"
    if File.exist?(proj_fullname)
        project_obj = Xcodeproj::Project.open(proj_fullname)
        project_obj.targets.each do |target|

            if target.product_type.to_s.eql?("com.apple.product-type.application") then
                temp_info = target.build_configurations.first.build_settings['INFOPLIST_FILE']
                info_plist_path = File.join(project_dir, temp_info)
            end
        end
    end

    return info_plist_path
end

#req_applovin_jsonObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/pindo/command/dev/applovin.rb', line 174

def req_applovin_json

    begin
        # https://dash.applovin.com/docs/v1/skadnetwork_ids.json
        applovin_url = "https://dash.applovin.com/docs/v1/skadnetwork_ids.json"
        con = Faraday.new 
        res = con.get do |req|
            req.url applovin_url
            req.headers['Content-Type'] = 'application/json'
        end

        applovin_res_json = JSON.parse(res.body)

        return applovin_res_json
    rescue Exception => e
        return nil 
    end
    

    
end

#req_applovin_mediia_jsonObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/pindo/command/dev/applovin.rb', line 196

def req_applovin_mediia_json


    begin
        applovin_url = "https://dash.applovin.com/docs/v1/mediation_sk_adnetwork_ids"
        con = Faraday.new 
        res = con.get do |req|
            req.url applovin_url
            req.headers['Content-Type'] = 'application/json'
        end

        applovin_res_json = JSON.parse(res.body)
        return applovin_res_json
    rescue Exception => e
        return nil 
    end
end

#runObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
90
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/pindo/command/dev/applovin.rb', line 45

def run

    skadnetwork_id_array = []

    Funlog.instance.fancyinfo_start("正在请求applovin数据...")

    applovin_res_json = req_applovin_json
    # puts JSON.pretty_generate(applovin_res_json)

    if !applovin_res_json.nil? && !applovin_res_json["skadnetwork_ids"].nil?
        applovin_res_json["skadnetwork_ids"].each do |network_id|
            skadnetwork_id_array << network_id["skadnetwork_id"]
        end
    end

    applovin_media_res_json = req_applovin_mediia_json
    # puts JSON.pretty_generate(applovin_media_res_json)
    if !applovin_media_res_json.nil? && !applovin_media_res_json["mediationSKAdnetworkIdsSpecs"].nil?
        applovin_media_res_json["mediationSKAdnetworkIdsSpecs"].each do |media_item|
            skadnetwork_id_array = skadnetwork_id_array + media_item["skAdnetworkIds"]
        end
    end

    skadnetwork_id_array = skadnetwork_id_array.uniq || []

    Funlog.instance.fancyinfo_success("请求applovin数据,获取到#{skadnetwork_id_array.size}条数据...")

    if !skadnetwork_id_array.nil? && skadnetwork_id_array.size > 50
        # puts "From Applovin SKAdNetworkItems Size: #{skadnetwork_id_array.size}"
        if @args_upload_flag
            applovin_config_file = File.join(pindo_single_config.pindo_common_configdir,"applovin_client_config.json")
            origin_json = {}
            if File.exist?(applovin_config_file)
                origin_json = JSON.parse(File.read(applovin_config_file))
            end
            origin_json["SKAdNetworkItems"] = skadnetwork_id_array
            File.write(applovin_config_file, JSON.pretty_generate(origin_json))
            git_addpush_repo(path:pindo_single_config.pindo_common_configdir, message:"Applovin SKAdNetworkItems", commit_file_params:["applovin_client_config.json"])
        end
    else
        Funlog.instance.fancyinfo_start("网络拉取applovin数据太少,从缓存中获取数据...")
        config_file = File.join(File::expand_path(Pindoconfig.instance.pindo_common_configdir), "applovin_client_config.json")
        config_json = JSON.parse(File.read(config_file))
        skadnetwork_id_array = skadnetwork_id_array + config_json["SKAdNetworkItems"]
        # puts "From Config SKAdNetworkItems Size: #{skadnetwork_id_array.size}"
        Funlog.instance.fancyinfo_success("网络拉取applovin数据太少,从缓存中获取到#{skadnetwork_id_array.size}条数据...")
    end


    Funlog.instance.fancyinfo_start("正在写入Xcode中...")

    skadnetwork_id_array = skadnetwork_id_array.uniq || []

    project_root_dir = Dir.pwd
    xcodeproj_file_name = Dir.glob(File.join(project_root_dir, "/*.xcodeproj")).max_by {|f| File.mtime(f)}

    plist_file_name = File.join(project_root_dir,  "AppEntry/Info.plist")
    if !xcodeproj_file_name.nil?
        proj_name = File.basename(xcodeproj_file_name, ".xcodeproj")   
        plist_file_name = get_applovin_infoplist(project_dir:project_root_dir, proj_name:proj_name) 
    end


    info_plist_dict = {}
    if File.exist?(plist_file_name)
        info_plist_dict = Xcodeproj::Plist.read_from_path(plist_file_name)    
    end
    

    info_plist_dict["NSAdvertisingAttributionReportEndpoint"] = info_plist_dict["NSAdvertisingAttributionReportEndpoint"] || "https://postbacks-app.com"

    if !info_plist_dict.has_key?("NSAppTransportSecurity")
        info_plist_dict["NSAppTransportSecurity"] = {}
        info_plist_dict["NSAppTransportSecurity"]["NSAllowsArbitraryLoads"] = true
    end


    if !skadnetwork_id_array.nil? && skadnetwork_id_array.size > 0
        info_plist_dict["SKAdNetworkItems"] = []
        skadnetwork_id_array.each do |network_id|
            item = {}
            item["SKAdNetworkIdentifier"] = network_id
            info_plist_dict["SKAdNetworkItems"] << item
        end
    end

    Xcodeproj::Plist.write_to_path(info_plist_dict, plist_file_name)

    Funlog.instance.fancyinfo_success("Xcode中applovin数据修改完成!")
    
    if @args_install_flag && !xcodeproj_file_name.nil? && File.exist?(xcodeproj_file_name)

        pindo_common_dir = pindo_single_config.pindo_common_configdir

        app_lovin_script = File.join(pindo_common_dir, "AppLovinQualityServiceSetup-ios-dev.rb")
        if @args_appstore_flag
            app_lovin_script = File.join(pindo_common_dir, "AppLovinQualityServiceSetup-ios.rb")
        end

        project_script_file = File.join(project_root_dir, "AppLovinQualityServiceSetup-ios.rb")
        FileUtils.cp_r(app_lovin_script, project_script_file)
        command = "ruby " + project_script_file
        # puts command
        system command  
    end


end

#validate!Object



41
42
43
# File 'lib/pindo/command/dev/applovin.rb', line 41

def validate!
    super
end