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

Fixes #1335 Added parameter to build command to set custom filename #1741

Closed
wants to merge 2 commits into from
Closed

Fixes #1335 Added parameter to build command to set custom filename #1741

wants to merge 2 commits into from

Conversation

birtony
Copy link

@birtony birtony commented Oct 27, 2019

Fixes #1335
Implemented logic for the custom filename option parameter --filename for the build command.

src/cmd/build.js Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Oct 28, 2019

Coverage Status

Coverage remained the same at 100.0% when pulling c40cc67 on birtony:issue#1335 into 685e466 on mozilla:master.

@birtony birtony requested a review from rpl October 29, 2019 15:42
Copy link
Member

@rpl rpl left a comment

Choose a reason for hiding this comment

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

Hi @birtony! follows a more complete round of review comments.

Let me know if you have any doubts, questions or if you get stuck on any of the following request for changes.

@@ -4415,7 +4415,8 @@
"requires": {
"import-fresh": "^2.0.0",
"is-directory": "^0.3.1",
"js-yaml": "^3.13.1"
"js-yaml": "^3.13.1",
"parse-json": "^4.0.0"
Copy link
Member

Choose a reason for hiding this comment

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

uhm... it doesn't seem that this PR includes any changes to the dependencies part of the package.json, and so there shouldn't be any change to package-lock.json, and so I'm wondering how this ended up into the PR.
have you installed the dependency using npm install instead of npm ci? using which version of npm?

Anyway, besides my curiosity about "how it did happen", we should remove this change from this PR.

Copy link
Author

@birtony birtony Oct 30, 2019

Choose a reason for hiding this comment

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

My bad, @rpl. I have used npm install, version 6.11.3. I will remove the change in my next commit!

});
let packageName;
if (filename) {
packageName = `${filename}.zip`;
Copy link
Member

Choose a reason for hiding this comment

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

I was expecting that filename would be the full filename including the extension, at least if the filename we got has already an extension like ".zip" or ".xpi" (and this would also have the nice side-effect of making web-ext build able to create files already using the .xpi extension).

Copy link
Member

Choose a reason for hiding this comment

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

I'm also thinking that we should likely validate a bit the filename we got.

e.g. we may at least check that it is a simple file name and not a relative or absolute path, so that the file will still be stored in the artifacts dir (as the build command currently does).

From a quick look to the nodejs API docs, it seems to me that nodejs path.parse method may be useful to achieve that.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, I see. I will work on implementing this.

Copy link
Author

@birtony birtony Nov 19, 2019

Choose a reason for hiding this comment

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

Hey @rpl, do you think something like this would be sufficient?

if (filename) {
    try {
      const valFilename = path.parse(filename);
      if (valFilename.dir !== '') {
        throw new UsageError(
          'Filename cannot contain any path'
        );
      } else {
        packageName = `${filename}`;
      }
    } catch (error) {
      throw new UsageError(
        `Error validating the filename: ${error}`);
    }
  } else {
    // set default filename
    // ...
}

Copy link
Author

Choose a reason for hiding this comment

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

Should we also limit the user on the number of extension types he/she can give to the filename?

Copy link
Member

Choose a reason for hiding this comment

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

Should we also limit the user on the number of extension types he/she can give to the filename?

You mean to only allow some kind of extensions (like .zip and .xpi) to be used as the extension in the final filename, right?
yeah that sounds reasonable to me.

Hey @rpl, do you think something like this would be sufficient?

I was thinking that we may first replace the part to interpolate and then do something like:

export function getPackageNameFromTemplate(
  filenameTemplate: string,
  manifestData: ExtensionManifest
): string {
  ...
  const packageName = ...;

  // Validate the resulting packageName string, after interpolating the manifest property
  // specified in the template string.
  const parsed = path.parse(packageName);
  if (parsed.dir) {
    throw new UsageError(
      `Invalid filename template "${filenameTemplate}". ` +
      `Filename "${packageName}" should not contain a path`
    );
  }
  if (!['.zip', '.xpi'].includes(parsed.ext)) {
    throw new UsageError(
      `Invalid filename template "${filenameTemplate}". ` +
      `Filename "${packageName}" should have a zip or xpi extension`
    );
  }

  return packageName;

@@ -381,6 +381,15 @@ Example: $0 --help run.
default: true,
type: 'boolean',
},
'filename': {
alias: 'f',
Copy link
Member

Choose a reason for hiding this comment

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

"f" is already been used in the web-ext run command as a shortcut for "--firefox-binary", even if this is technically only available for the build command I would still prefer to possibly using a different single char for this option or alternatively we may also just don't specify a single char alias.

Also, I'm wondering if this is the reason for the failure triggered on the travis CI windows workers: https://travis-ci.org/mozilla/web-ext/jobs/604599391#L677
But it is surprising that it is only failing on windows and not on the travis CI linux workers as well.

Copy link
Author

Choose a reason for hiding this comment

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

That is an interesting error, yet I am not sure what causes it on Windows. It seems to me that leaving a single char alias empty would be a better choice in this case . I just can't think of any other intuitive alias for the filename option.

Copy link
Member

Choose a reason for hiding this comment

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

sure, omitting a single char alias for this option sounds good to me too.

messageFile, manifestData,
});
let packageName;
if (filename) {
Copy link
Member

Choose a reason for hiding this comment

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

The proposed implementation does not support variables as requested in #1335.

Let's undo the current change to this file, and do something like this:

filename = filename.replace( ... , ... );
const packageName = safeFilename(filename);

If you don't know exactly how the replacement should be implemented, look at the getDefaultLocalizedName method for an example. To start simple, you could support the variable names "name" and "version".

Copy link
Member

@rpl rpl left a comment

Choose a reason for hiding this comment

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

@birtony Apologies for missing your last round of questions (I thought that this PR was just waiting fro the changes mentioned by me and Rob in the previous review round).

I've replied inline to the your questions, and also added some additional comments related to the "filename template interpolation" part (previously mentioned in Rob's comment).

Let me know if you have all the needed details or if you have any other doubts or questions.

});
let packageName;
if (filename) {
packageName = `${filename}.zip`;
Copy link
Member

Choose a reason for hiding this comment

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

As Rob pointed out in his comment, the original request in #1335 is to also allow the user to specify some manifest properties to be interpolated into the final filename, and so here we should be calling a newly defined helper method, e.g. one with the following type signature:

function getPackageNameFromTemplate(
  filenameTemplate: string,
  manifestData: ExtensionManifest
): string 

As Rob mentioned, we could use an approach similar to the one used internally by getDefaultLocalizedName, which should look like:

filenameTemplate.replace(/<<REGEXP>>/g, (match, manifestProperty) => {
   ...
   // <- find and return manifestPropertyValue (or throw if the property can't be found
   // or isn't a string) 
});

As suggested by Kumar in the issue comments, we could aim for template strings that look like: '{applications.gecko.id}-{version}.xpi'
And so the regexp should match on strings enclosed between the { and } characters and the enclosed string should only allow alphanumeric characters and the . as the property separator character.

@@ -72,6 +72,21 @@ describe('build', () => {
});
});

it('gives the correct custom name to an extension', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Let's also unit test the new helper method that does just interpolate the template string given the manifest data object (so that we can easily cover more of the other scenarios, like the error scenarios where the helper method is meant to throw).

@rpl
Copy link
Member

rpl commented Jan 22, 2020

I'm closing this PR because it is not yet complete and in the meantime there are some conflicts (with changes landed in master in the meantime) that have to be solved.

If you still want to work on this and the issue has not been fixed yet, feel free to update this PR and mention me in a comment and I'll be happy to re-open this PR.

@rpl rpl closed this Jan 22, 2020
@escapewindow
Copy link
Contributor

@birtony are you planning on picking this back up? If not, do you mind if I try addressing these review comments?

@birtony
Copy link
Author

birtony commented May 4, 2020

@escapewindow things got busy for me, so go ahead! Let me know if I could be of any help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to web-ext build: --filename="{applications.gecko.id}-{version}.zip"
5 participants