Class: Fastlane::Actions::AssociateMsStoreAction

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

Constant Summary collapse

XML_NAME =
"Package.StoreAssociation.xml".freeze

Class Method Summary collapse

Class Method Details

.acquire_authorization_tokenObject



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

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

  ms_credentials = Helper.ms_credentials
  body = {
    client_id: ms_credentials.client_id,
    client_secret: ms_credentials.client_secret,
    client_info: 1,
    grant_type: "password",
    scope: "https://graph.windows.net/.default offline_access openid profile",
    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_vsapi_locationObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 205

def self.acquire_vsapi_location
  query = {
    LinkId: "875315" # VS API constant
  }
  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")

      location = response.headers["Location"]
      uri = URI(location)
      @vsapi_host = "#{uri.scheme}://#{uri.host}"
      @vsapi_endpoint = uri.path

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

      return
    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

.app_info(app_id) ⇒ Object



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

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



237
238
239
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 237

def self.authors
  ["CheeryLee"]
end

.available_optionsObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 252

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



279
280
281
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 279

def self.category
  :project
end

.create_xml(manifest_path, developer_info, app_info) ⇒ Object



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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 34

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



233
234
235
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 233

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

.developer_infoObject



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

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



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 83

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)


241
242
243
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 241

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

.outputObject



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

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



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

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

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

    acquire_authorization_token
    acquire_vsapi_location
    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



95
96
97
98
99
# File 'lib/fastlane/plugin/sapfire/actions/associate_ms_store_action.rb', line 95

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