Skip to content

Commit

Permalink
Merge pull request #179 from andreban/assetlinks-gen
Browse files Browse the repository at this point in the history
Generates assetlinks.json when building the App
  • Loading branch information
andreban authored May 21, 2020
2 parents 2f687c0 + 32ceb76 commit 899245b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
38 changes: 36 additions & 2 deletions packages/cli/src/lib/cmds/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

import {AndroidSdkTools, Config, GradleWrapper, JdkHelper, Log, TwaManifest}
from '@bubblewrap/core';
import {AndroidSdkTools, Config, DigitalAssetLinks, GradleWrapper, JdkHelper, KeyTool, Log,
TwaManifest} from '@bubblewrap/core';
import * as inquirer from 'inquirer';
import * as path from 'path';
import * as fs from 'fs';
import {validateKeyPassword} from '../inputHelpers';
import {PwaValidator, PwaValidationResult} from '@bubblewrap/validator';
import {printValidationResult} from '../pwaValidationHelper';
Expand Down Expand Up @@ -78,6 +79,36 @@ async function startValidation(): Promise<PwaValidationResult> {
return PwaValidator.validate(new URL(twaManifest.startUrl, twaManifest.webManifestUrl));
}

async function generateAssetLinks(keyTool: KeyTool, twaManifest: TwaManifest,
passwords: SigningKeyPasswords, log: Log): Promise<void> {
try {
const digitalAssetLinksFile = './assetlinks.json';
const keyInfo = await keyTool.keyInfo({
path: twaManifest.signingKey.path,
alias: twaManifest.signingKey.alias,
keypassword: passwords.keyPassword,
password: passwords.keystorePassword,
});

const sha256Fingerprint = keyInfo.fingerprints.get('SHA256');
if (!sha256Fingerprint) {
log.warn('Could not find SHA256 fingerprint. Skipping generating "assetlinks.json"');
return;
}

const digitalAssetLinks =
DigitalAssetLinks.generateAssetLinks(twaManifest.packageId, sha256Fingerprint);

await fs.promises.writeFile(digitalAssetLinksFile, digitalAssetLinks);

log.info(`Digital Asset Links file generated at ${digitalAssetLinksFile}`);
log.info('Read more about setting up Digital Asset Links at https://developers.google.com' +
'/web/android/trusted-web-activity/quick-start#creating-your-asset-link-file');
} catch (e) {
log.warn('Error generating "assetlinks.json"', e);
}
}

export async function build(
config: Config, args: ParsedArgs, log = new Log('build')): Promise<boolean> {
let pwaValidationPromise;
Expand All @@ -87,6 +118,7 @@ export async function build(

const jdkHelper = new JdkHelper(process, config);
const androidSdkTools = new AndroidSdkTools(process, config, jdkHelper, log);
const keyTool = new KeyTool(jdkHelper, log);

if (!await androidSdkTools.checkBuildTools()) {
console.log('Installing Android Build Tools. Please, read and accept the license agreement');
Expand Down Expand Up @@ -137,5 +169,7 @@ export async function build(
);

log.info(`Signed Android App generated at "${outputFile}"`);

await generateAssetLinks(keyTool, twaManifest, passwords, log);
return true;
}
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import {JdkHelper} from './lib/jdk/JdkHelper';
import {KeyTool} from './lib/jdk/KeyTool';
import {TwaManifest} from './lib/TwaManifest';
import {TwaGenerator} from './lib/TwaGenerator';
import {DigitalAssetLinks} from './lib/DigitalAssetLinks';
import * as util from './lib/util';

export {AndroidSdkTools,
Config,
DigitalAssetLinks,
GradleWrapper,
JdkHelper,
KeyTool,
Expand Down
25 changes: 25 additions & 0 deletions packages/core/src/lib/DigitalAssetLinks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export class DigitalAssetLinks {
static generateAssetLinks(applicationId: string, sha256Fingerprint: string): string {
return `[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target" : { "namespace": "android_app", "package_name": "${applicationId}",
"sha256_cert_fingerprints": ["${sha256Fingerprint}"] }
}]\n`;
}
}
35 changes: 35 additions & 0 deletions packages/core/src/spec/lib/DigitalAssetLinksSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {DigitalAssetLinks} from '../../lib/DigitalAssetLinks';

describe('DigitalAssetLinks', () => {
describe('#generateAssetLinks', () => {
it('Generates the assetlinks markup', () => {
const packageName = 'com.test.twa';
const fingerprint = 'FINGERPRINT';
const digitalAssetLinks =
JSON.parse(DigitalAssetLinks.generateAssetLinks(packageName, fingerprint));
expect(digitalAssetLinks.length).toBe(1);
expect(digitalAssetLinks[0].relation.length).toBe(1);
expect(digitalAssetLinks[0].relation[0]).toBe('delegate_permission/common.handle_all_urls');
expect(digitalAssetLinks[0].target.namespace).toBe('android_app');
expect(digitalAssetLinks[0].target.package_name).toBe(packageName);
expect(digitalAssetLinks[0].target.sha256_cert_fingerprints.length).toBe(1);
expect(digitalAssetLinks[0].target.sha256_cert_fingerprints[0]).toBe(fingerprint);
});
});
});

0 comments on commit 899245b

Please sign in to comment.