Class: Fastlane::Actions::AssociateMsStoreAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb

Constant Summary collapse

"2264307".freeze
"2263650".freeze
VS_CLIENT_ID =
"04f0c124-f2bc-4f59-8241-bf6df9866bbd".freeze
XML_NAME =
"Package.StoreAssociation.xml".freeze

Class Method Summary collapse

Class Method Details

.acquire_authorization_token(resource) ⇒ Object



168
169
170
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
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 168

def self.acquire_authorization_token(resource)
  UI.message("Acquiring authorization token ...")

  ms_credentials = Helper.ms_credentials
  body = {
    client_id: VS_CLIENT_ID,
    grant_type: "password",
    scope: "#{resource}/.default",
    username: ms_credentials.username,
    password: ms_credentials.password
  }
  headers = {
    "x-anchormailbox": "upn:#{ms_credentials.username}",
    "x-client-sku": "fastlane-sapfire-plugin",
    "Accept": "application/json"
  }
  connection = Faraday.new("https://login.microsoftonline.com")
  request_body = URI.encode_www_form(body)

  begin
    response = connection.post("/#{ms_credentials.tenant_id}/oauth2/v2.0/token", request_body, headers)
    data = JSON.parse(response.body)

    if response.status == 200
      @token = data["access_token"]
      UI.message("Authorization token was obtained")

      return
    end

    error = data["error"]
    error_description = data["error_description"]

    UI.user_error!("Request returned the error.\nCode: #{error}.\nDescription: #{error_description}")
  rescue StandardError => ex
    UI.user_error!("Authorization failed: #{ex}")
  end
end

.acquire_dev_center_locationObject



207
208
209
210
211
212
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 207

def self.acquire_dev_center_location
  location = acquire_fw_url(DEV_CENTER_FW_LINK)
  UI.message("Dev Center location: #{location}")

  location
end

.acquire_fw_url(link_id) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 223

def self.acquire_fw_url(link_id)
  query = {
    LinkId: link_id
  }
  connection = Faraday.new("https://go.microsoft.com")

  begin
    response = connection.get("/fwlink", query)

    if response.status == 302
      raise "'Location' header isn't presented" unless response.headers.include?("Location")

      return response.headers["Location"]
    end

    UI.user_error!("Request returned the error: #{response.status}")
  rescue StandardError => ex
    UI.user_error!("Failed to get VS API endpoint location: #{ex}")
  end
end

.acquire_vs_api_locationObject



214
215
216
217
218
219
220
221
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 214

def self.acquire_vs_api_location
  location = acquire_fw_url(VS_API_FW_LINK)
  uri = URI(location)
  @vsapi_host = "#{uri.scheme}://#{uri.host}"
  @vsapi_endpoint = uri.path

  UI.message("VS API location: #{location}")
end

.app_info(app_id) ⇒ Object



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
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 136

def self.app_info(app_id)
  UI.message("Obtaining application info ...")

  headers = {
    "Authorization": "Bearer #{@token}",
    "Accept": "application/json",
    "MS-Contract-Version": "1"
  }
  query = {
    setvar: "fltaad:1"
  }
  connection = Faraday.new(@vsapi_host)

  begin
    response = connection.get("#{@vsapi_endpoint}/applications", query, headers)

    if response.status == 200
      data = JSON.parse(response.body)
      product = data["Products"].find { |x| x["LandingUrl"].include?(app_id) }
      app_info = AppInfo.new(product["MainPackageIdentityName"], product["ReservedNames"])

      UI.message("Application info was obtained")

      return app_info
    end

    UI.user_error!("Request returned the error: #{response.code}")
  rescue StandardError => ex
    UI.user_error!("Application info request failed: #{ex}")
  end
end

.authorsObject



248
249
250
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 248

def self.authors
  ["CheeryLee"]
end

.available_optionsObject



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 263

def self.available_options
  [
    FastlaneCore::ConfigItem.new(
      key: :manifest,
      env_name: "SF_PROJECT_MANIFEST",
      description: "Path to the APPX package manifest",
      optional: false,
      type: String,
      verify_block: proc do |value|
        UI.user_error!("Path to the APPX package manifest is invalid") unless value && !value.empty?
        UI.user_error!("The provided path doesn't point to AppxManifest-file") unless
          File.exist?(File.expand_path(value)) && value.end_with?(".appxmanifest")
      end
    ),
    FastlaneCore::ConfigItem.new(
      key: :app_id,
      env_name: "SF_APP_ID",
      description: "The Microsoft Store ID of an application",
      optional: false,
      type: String,
      verify_block: proc do |value|
        UI.user_error!("The Microsoft Store ID can't be empty") unless value && !value.empty?
      end
    )
  ]
end

.categoryObject



290
291
292
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 290

def self.category
  :project
end

.create_xml(manifest_path, developer_info, app_info) ⇒ Object



38
39
40
41
42
43
44
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
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 38

def self.create_xml(manifest_path, developer_info, app_info)
  appxmanifest_xml = get_appxmanifest_xml(manifest_path)

  UI.message("Set identity name: #{app_info.identity}")

  begin
    identity_entry = appxmanifest_xml.elements["Package"]
                                     .elements["Identity"]
    identity_entry.attributes["Name"] = app_info.identity
    identity_entry.attributes["Publisher"] = developer_info.publisher

    properties_entry = appxmanifest_xml.elements["Package"]
                                       .elements["Properties"]
    properties_entry.elements["DisplayName"].text = app_info.names[0] unless app_info.names.empty?
    properties_entry.elements["PublisherDisplayName"].text = developer_info.display_name
  rescue StandardError => ex
    UI.user_error!("Can't update app manifest: #{ex}")
  end

  save_xml(appxmanifest_xml, manifest_path)

  UI.message("Set publisher data: #{developer_info.publisher}")
  UI.message("Set publisher display name: #{developer_info.display_name}")

  document = REXML::Document.new
  document.xml_decl.version = "1.0"
  document.xml_decl.encoding = "utf-8"
  xmlns_args = {
    "xmlns" => "http://schemas.microsoft.com/appx/2010/storeassociation"
  }
  store_association = document.add_element("StoreAssociation", xmlns_args)
  store_association.add_element("Publisher").text = developer_info.publisher
  store_association.add_element("PublisherDisplayName").text = developer_info.display_name
  store_association.add_element("DeveloperAccountType").text = "WSA"
  store_association.add_element("GeneratePackageHash").text = "http://www.w3.org/2001/04/xmlenc#sha256"

  product_reserved_info = store_association.add_element("ProductReservedInfo")
  product_reserved_info.add_element("MainPackageIdentityName").text = app_info.identity

  reserved_names = product_reserved_info.add_element("ReservedNames")
  app_info.names.each do |x|
    reserved_names.add_element("ReservedName").text = x
  end

  working_directory = File.dirname(manifest_path)
  path = File.join(working_directory, XML_NAME)
  save_xml(document, path)
end

.descriptionObject



244
245
246
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 244

def self.description
  "Makes a local app manifest needed for Microsoft Store association"
end

.developer_infoObject



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
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 105

def self.developer_info
  UI.message("Obtaining developer info ...")

  headers = {
    "Authorization": "Bearer #{@token}",
    "Accept": "application/json",
    "MS-Contract-Version": "1"
  }
  query = {
    setvar: "fltaad:1"
  }
  connection = Faraday.new(@vsapi_host)

  begin
    response = connection.get("#{@vsapi_endpoint}/developer", query, headers)

    if response.status == 200
      data = JSON.parse(response.body)
      developer_info = DeveloperInfo.new(data["PublisherDisplayName"], data["Publisher"])

      UI.message("Developer info was obtained")

      return developer_info
    end

    UI.user_error!("Request returned the error: #{response.status}")
  rescue StandardError => ex
    UI.user_error!("Developer info request failed: #{ex}")
  end
end

.get_appxmanifest_xml(manifest_path) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 87

def self.get_appxmanifest_xml(manifest_path)
  file = File.open(manifest_path, "r")

  begin
    document = REXML::Document.new(file)
    file.close
    document
  rescue REXML::ParseException => ex
    UI.user_error!("Can't parse Package.appxmanifest: #{ex}")
  end
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 252

def self.is_supported?(platform)
  [:windows].include?(platform)
end

.outputObject



256
257
258
259
260
261
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 256

def self.output
  [
    ["SF_PROJECT_MANIFEST", "Path to the APPX package manifest"],
    ["SF_APP_ID", "The Microsoft Store ID of an application"]
  ]
end

.run(params) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 19

def self.run(params)
  FastlaneCore::PrintTable.print_values(config: params, title: "Summary for associate_ms_store")

  begin
    UI.message("Creating #{XML_NAME}...")

    dev_center_url = acquire_dev_center_location
    acquire_vs_api_location
    acquire_authorization_token(dev_center_url)
    ms_developer_info = developer_info
    ms_app_info = app_info(params[:app_id])
    create_xml(params[:manifest], ms_developer_info, ms_app_info)

    UI.message("#{XML_NAME} successfully created")
  rescue StandardError => ex
    UI.user_error!("Something went wrong while associating the project: #{ex}")
  end
end

.save_xml(document, path) ⇒ Object



99
100
101
102
103
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 99

def self.save_xml(document, path)
  file = File.open(path, "w")
  document.write(file)
  file.close
end