Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: web-ext sign now supports extensions without explicit IDs #340

Merged
merged 1 commit into from
Jul 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumar303 at line 74, it seems to me that in some cases the id property can be missing from the signingResult (my guess is that it could happen when the extension id is assigned statically in the manifest.json), and in that case the above line is going to print a "Extension ID: undefined" log, which can be confusing for the the user.

If the above assumptions are true, we can probably tweak it a bit, to ensure that it is going to always print a log which contains the id (even if it is the same statically assigned in the manifest.json)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you install the latest sign-addon from this branch into web-ext? mozilla/sign-addon#114 What you're describing is the behavior before that patch is applied but not after.

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(`No ID file found at: ${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