Skip to content

Commit

Permalink
feat: web-ext sign now supports extensions without explicit IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
kumar303 committed Jul 5, 2016
1 parent 8d3379a commit f911460
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 55 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"babel-preset-es2015": "6.9.0",
"babel-preset-stage-2": "6.11.0",
"chai": "3.5.0",
"copy-dir": "0.3.0",
"coveralls": "2.11.9",
"deepcopy": "0.6.3",
"eslint": "2.13.1",
Expand Down
123 changes: 94 additions & 29 deletions src/cmd/sign.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/* @flow */
import path from 'path';
import fs from 'mz/fs';
import {signAddon as defaultAddonSigner} from '../util/es6-modules';

import defaultBuilder from './build';
import {InvalidManifest} from '../errors';
import {withTempDir} from '../util/temp-dir';
import getValidatedManifest from '../util/manifest';
import {onlyErrorsWithCode, WebExtError} from '../errors';
import getValidatedManifest, {getManifestId} from '../util/manifest';
import {prepareArtifactsDir} from '../util/artifacts';
import {createLogger} from '../util/logger';

const log = createLogger(__filename);

export const extensionIdFile = '.web-extension-id';

export default function sign(
{verbose, sourceDir, artifactsDir, apiKey, apiSecret,
apiUrlPrefix, timeout}: Object,
apiUrlPrefix, id, timeout}: Object,
{build=defaultBuilder, signAddon=defaultAddonSigner,
preValidatedManifest=null}: Object = {}): Promise {

Expand All @@ -28,39 +30,102 @@ export default function sign(
}
})
.then((manifestData) => {
if (!manifestData.applications) {
// TODO: remove this when signing supports manifests
// without IDs: https://github.com/mozilla/web-ext/issues/178
throw new InvalidManifest(
'applications.gecko.id in manifest.json is required for signing');
return Promise.all([
build({sourceDir, artifactsDir: tmpDir.path()}, {manifestData}),
getIdFromSourceDir(sourceDir),
])
.then(([buildResult, idFromSourceDir]) => {
return {buildResult, manifestData, idFromSourceDir};
});
})
.then(({buildResult, manifestData, idFromSourceDir}) => {
const manifestId = getManifestId(manifestData);
if (id && manifestId) {
throw new WebExtError(
`Cannot set custom ID ${id} because manifest.json ` +
`declares ID ${manifestId}`);
}
if (manifestId) {
id = manifestId;
}
if (!id && idFromSourceDir) {
log.info(
'Using previously auto-generated extension ID: ' +
`${idFromSourceDir}`);
id = idFromSourceDir;
}
if (!id) {
log.warn('No extension ID specified (it will be auto-generated)');
}
return manifestData;
return signAddon({
apiKey,
apiSecret,
apiUrlPrefix,
timeout,
verbose,
id,
xpiPath: buildResult.extensionPath,
version: manifestData.version,
downloadDir: artifactsDir,
});
})
.then((manifestData) => {
return build(
{sourceDir, artifactsDir: tmpDir.path()},
{manifestData})
.then((buildResult) => {
return {buildResult, manifestData};
});
.then((signingResult) => {
if (signingResult.id) {
return saveIdToSourceDir(sourceDir, signingResult.id)
.then(() => signingResult);
} else {
return signingResult;
}
})
.then(({buildResult, manifestData}) => signAddon({
apiKey,
apiSecret,
apiUrlPrefix,
timeout,
verbose,
xpiPath: buildResult.extensionPath,
id: manifestData.applications.gecko.id,
version: manifestData.version,
downloadDir: artifactsDir,
}))
.then((signingResult) => {
// All information about the downloaded files would have
// already been logged by signAddon().
log.info(signingResult.success ? 'SUCCESS' : 'FAIL');
if (signingResult.success) {
log.info(`Extension ID: ${signingResult.id}`);
log.info('SUCCESS');
} else {
log.info('FAIL');
}
return signingResult;
});
}
);
}


export function getIdFromSourceDir(sourceDir: string): Promise {
const filePath = path.join(sourceDir, extensionIdFile);
return fs.readFile(filePath)
.then((content) => {
let lines = content.toString().split('\n');
lines = lines.filter((line) => {
line = line.trim();
if (line && !line.startsWith('#')) {
return line;
}
});
let id = lines[0];
log.debug(`Found extension ID ${id} in ${filePath}`);
if (!id) {
throw new WebExtError(`No ID found in extension ID file ${filePath}`);
}
return id;
})
.catch(onlyErrorsWithCode('ENOENT', () => {
log.debug(`Ignorning non-existant ID file: ${filePath}`);
}));
}


export function saveIdToSourceDir(sourceDir: string, id: string): Promise {
const filePath = path.join(sourceDir, extensionIdFile);
return fs.writeFile(filePath,
[
'# This file was created by https://github.com/mozilla/web-ext',
'# Your auto-generated extension ID for addons.mozilla.org is:',
id.toString(),
].join('\n'))
.then(() => {
log.debug(`Saved auto-generated ID ${id} to ${filePath}`);
});
}
7 changes: 7 additions & 0 deletions src/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ Example: $0 --help run.
demand: true,
type: 'string',
},
'id': {
describe:
'A custom ID for the extension. This will override ' +
'any ID declared by manifest.json.',
demand: false,
type: 'string',
},
'timeout' : {
describe: 'Number of milliseconds to wait before giving up',
type: 'number',
Expand Down
Loading

0 comments on commit f911460

Please sign in to comment.