Class: Playwire::IOS

Inherits:
Object
  • Object
show all
Defined in:
lib/playwire/ios.rb

Instance Method Summary collapse

Instance Method Details

#load_config_and_set_gam_app_id(publisher_id, app_id, user_plist_path = nil) ⇒ Object



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
# File 'lib/playwire/ios.rb', line 70

def load_config_and_set_gam_app_id(publisher_id, app_id, user_plist_path=nil)
  ### Look for the 'Podfile' to confirm Playwire SDK installation 
  podfile = seek_for_podfile()
  return unless podfile

  ### Use 'Info.plist' provided by a pub or looking for 'Info.plist' paths for all targets, which include Playwire SDK
  plist_paths = user_plist_path.nil? ? seek_for_plists(podfile) : validate_plist_path_from_user(user_plist_path)
  return unless plist_paths

  ### Load config file with metadata provided by a pub or return error if the config file does't exist
  json = Playwire::Network.load_config(publisher_id, app_id)
  if json.nil?
    Playwire::Logger.error("No config file found for the publisher. Check the entered metadata or contact Playwire Account Manager to resolve this issue.")
    return
  end

  ### Extract 'gamAppId' or return error if the 'gamAppId' is omitted
  gam_app_id = json.dig('settings', 'gamAppId')
  if gam_app_id.nil?
    Playwire::Logger.error("No 'gamAppId' found for the publisher. Contact Playwire Account Manager to resolve this issue.")
    return
  end

  plist_paths.each { |path|
    ### Open and read the 'Info.plist' file
    plist = Playwire::Plist.read(path)
    if plist.nil?
      Playwire::Logger.error("Could not read the 'Info.plist' file at path #{path}.")
      return
    end

    ### Insert 'gamAppId' to the 'Info.plist' file
    plist[Playwire::PWC::Plist::PWC_GAD_APP_ID_KEY] = gam_app_id
    ### Save the modified 'Info.plist' file
    Playwire::Plist.write(plist, path)
  }

  Playwire::Logger.success("The 'Info.plist' has been updated with 'gamAppId' = #{gam_app_id}.")
end

#load_skadnetwork_and_set_to_info_plist(user_plist_path = nil) ⇒ Object



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
# File 'lib/playwire/ios.rb', line 110

def load_skadnetwork_and_set_to_info_plist(user_plist_path=nil)
  ### Look for the 'Podfile' to confirm Playwire SDK installation 
  podfile = seek_for_podfile()
  return unless podfile

  ### Use 'Info.plist' provided by a pub or looking for 'Info.plist' paths for all targets, which include Playwire SDK
  plist_paths = user_plist_path.nil? ? seek_for_plists(podfile) : validate_plist_path_from_user(user_plist_path)
  return unless plist_paths

  ### Load JSON with SKAdNetwork items or return error if such JSON file does't exist
  json = Playwire::Network.load_sk_ad_network_items()
  if json.nil?
    Playwire::Logger.error("No SKAdNetwork items found. Contact Playwire tech team to resolve this issue.")
    return
  end

  ### Create SKAdNetwork items set to avoid duplicates
  fetched_ids = json[Playwire::PWC::Plist::PWC_SK_AD_NETWORK_ITEMS_KEY].to_set

  plist_paths.each { |path|
    ### Open and read the 'Info.plist' file
    plist = Playwire::Plist.read(path)
    if plist.nil?
      Playwire::Logger.error("Could not read the 'Info.plist' file at path #{path}.")
      return
    end

    ### Read existing SKAdNetwork items from the 'Info.plist' file
    ad_network_items = plist[Playwire::PWC::Plist::PWC_SK_AD_NETWORK_ITEMS_KEY] || []
    existing_ids = ad_network_items.map { |item| item[Playwire::PWC::Plist::PWC_SK_AD_NETWORK_ID_KEY] }.compact.to_set

    ### Merge existing SKAdNetwork items with downloaded ones and insert to the 'Info.plist' file  
    ad_network_ids = (existing_ids + fetched_ids).map { |id| {Playwire::PWC::Plist::PWC_SK_AD_NETWORK_ID_KEY => id } }
    plist[Playwire::PWC::Plist::PWC_SK_AD_NETWORK_ITEMS_KEY] = ad_network_ids

    ### Save the modified 'Info.plist' file
    Playwire::Plist.write(plist, path)
  }

  Playwire::Logger.success("The 'Info.plist' has been updated with SKAdNetwork items.")
end

#seek_for_plists(podfile) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/playwire/ios.rb', line 27

def seek_for_plists(podfile)
  ### Iterate over targets in the 'Podfile' to check which one contains Playwire SDK dependency
  targets = podfile.target_definition_list.select { |target|
    target.dependencies.map { |dependency| dependency.name }.count { |name| name =~ /^Playwire.*$/ } > 0
  }

  ### Return error if Playwire SDK dependency hasn't been found
  if targets.empty?
    Playwire::Logger.error("No 'Playwire SDK' dependency found in the Podfile.")
    return nil
  end

  ### Look for 'Info.plist' paths in the targets from the 'Podfile'
  plists = targets.map { |target|
    Dir.glob(File.join(Pathname.pwd, '**', '*')).filter { |file| file =~ /.+#{target.name}\/(?!\bPods\b).*\/Info\.plist/ }
  }.flatten

  ### Return error if 'Info.plist' files haven't been found
  if plists.empty?
    Playwire::Logger.error("No 'Info.plist' found in the project directory. Use --plist option to define the 'Info.plist' path manually.")
    return nil
  end

  return plists
end

#seek_for_podfileObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/playwire/ios.rb', line 15

def seek_for_podfile
  ### Looking for the 'Podfile' in the root of project directory 
  podfile_path = Pathname.pwd + 'Podfile'
  begin
    ### Use 'cocoapods-core' gem to parse the 'Podfile'
    return Pod::Podfile.from_file(podfile_path)
  rescue
    Playwire::Logger.error("No 'Podfile' found in the project directory.")
    return nil
  end
end

#validate_plist_path_from_user(plist_path) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/playwire/ios.rb', line 53

def validate_plist_path_from_user(plist_path)
  ### Converts any pathname to an absolute pathname.
  path = File.expand_path(plist_path, __FILE__)

  ### Verify that file exists and the path doesn't point to directory. 
  isExist = File.exist?(path)
  isDirectory = File.directory?(path)

  ### Return error if the file doesn't meet condition.
  if !isExist || isDirectory
    Playwire::Logger.error("No a '.plist' file found at the path provided by '--plist'. Check if the '.plist' file exists at the path.")
    return nil
  end

  return [path]
end