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

Exclude some common directories #32

Merged
merged 12 commits into from
Jul 2, 2018
Merged

Exclude some common directories #32

merged 12 commits into from
Jul 2, 2018

Conversation

swissspidy
Copy link
Member

Does not yet contain a CLI argument to extend/override exclusions.

See #27.

@danielbachhuber
Copy link
Member

Does not yet contain a CLI argument to extend/override exclusions.

Should it be a CLI argument or an environment variable?

@schlessera
Copy link
Member

As this is meant to extract code from a WordPress plugin or theme, I wonder whether it wouldn't be preferable to use a WordPress filter to make the exclusions customizable from within a plugin/theme...

@swissspidy
Copy link
Member Author

As this is meant to extract code from a WordPress plugin or theme, I wonder whether it wouldn't be preferable to use a WordPress filter to make the exclusions customizable from within a plugin/theme...

wp i18n make-pot is something that's being run during a build step. There's no WordPress involved there.

Should it be a CLI argument or an environment variable?

Good question!

An env var might be easier when dealing with multiple projects. That way you don't have to add the same exclusions over and over again.

I also wonder whether this option should just extend default exclusions like node_modules or override them. I tend to think the former makes more sense.

@danielbachhuber
Copy link
Member

Here's some prior art: https://github.com/wp-cli/find-command#using

[--include_ignored_paths=<paths>]
	Include additional ignored paths as CSV (e.g. '/sys-backup/,/temp/').

@schlessera
Copy link
Member

schlessera commented Jun 5, 2018

wp i18n make-pot is something that's being run during a build step. There's no WordPress involved there.

Ah, right, it uses @when before_wp_load.

I'm not sure using environment variables is useful in this case. A parameter would allow for immediate configuration, and you can still configure it per-user or per-site through the wp-cli.yml file.

@swissspidy
Copy link
Member Author

I'm sticking with an extra argument for now.

Wondering though if I should rename exclude to ignore 🤷‍♂️

@swissspidy swissspidy requested a review from a team June 19, 2018 14:23
"""

When I run `wp i18n make-pot foo-plugin foo-plugin.pot`
And the foo-plugin.pot file should not contain:
Copy link
Member

Choose a reason for hiding this comment

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

Should be Then ... for readability.

"""

When I run `wp i18n make-pot foo-plugin foo-plugin.pot --exclude=foo,bar`
And the foo-plugin.pot file should not contain:
Copy link
Member

Choose a reason for hiding this comment

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

Should be Then ... for readability.

foreach ( $files as $file ) {
if ( $file->isFile() && 'php' === $file->getExtension()) {
if ( $file->isFile() ) {
Copy link
Member

Choose a reason for hiding this comment

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

CS: double space.

@@ -133,4 +151,5 @@ protected static function getFilesFromDirectory( $dir ) {

return $filtered_files;
}

Copy link
Member

Choose a reason for hiding this comment

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

CS: Redundant new line.

new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS ),
function ( $file, $key, $iterator ) use ( $exclude ) {
/** @var SplFileInfo $file */
if ( in_array( $file->getBasename(), $exclude, true ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

This will fail if a path is returned with a trailing slash. Using trailing slashes is often done for avoiding partial matches. This either needs specific handling of trailing slashes, or an explicit mention in the argument documentation.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think getBasename() ever returns a path with a trailing slash. The values in $exclude could have indeed have trailing slashes, yes.

I guess I can safely add a change to remove any slashes at the beginning and the end of the excluded directories/files and document this.

I am being ignored
"""

Scenario: Skips additionally excluded folders
Copy link
Member

Choose a reason for hiding this comment

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

Tests should cover these as well to make sure they work (and continue to do so):

  • subfolder (deeper than in immediate root)
  • file
  • file in subfolder (deeper than in immediate root)
  • partial match (NOT being excluded)

Copy link
Member Author

Choose a reason for hiding this comment

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

Agreed. Will add these in a spare moment.

Also, I was just wondering whether glob-like patterns should be supported using something like fnmatch, but that's probably something for a future iteration.

Copy link
Member

@schlessera schlessera left a comment

Choose a reason for hiding this comment

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

Looks good apart from the 1 test case where I think we had different interpretations.

@@ -595,3 +595,150 @@ Feature: Generate a POT file of a WordPress plugin
"""
I am being ignored
"""

Scenario: Skips excluded subfolders
Copy link
Member

Choose a reason for hiding this comment

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

For this scenario, I was thinking more along the lines of adding an exclude that contains a subfolder, like this:

wp i18n make-pot foo-plugin foo-plugin.pot --exclude=some/sub/folder

I don't think testing the other way around (omitting a subfolder that was part of parent folder being excluded) is needed, as all files within a parent folder would have the same problem then.

@@ -128,6 +128,13 @@ function ( $file, $key, $iterator ) use ( $exclude ) {
return false;
}

// Check for more complex paths, e.g. /some/sub/folder.
foreach( $exclude as $path_or_file ) {
if ( substr( $file->getPathname(), - strlen( $path_or_file ) ) === $path_or_file ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

There's probably a better way to do this, but I couldn't think of one right now.


// Check for more complex paths, e.g. /some/sub/folder.
foreach( $exclude as $path_or_file ) {
if ( substr( $file->getPathname(), - strlen( '/' . $path_or_file ) ) === '/' . $path_or_file ) {
Copy link
Member

Choose a reason for hiding this comment

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

You should use substr_compare instead as it is optimized towards the comparison:

$substring = '/' . $path_or_file;
if ( substr_compare( $file->getPathname(), $substring, - strlen( $substring ) ) === 0 ) { }

Copy link
Member

Choose a reason for hiding this comment

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

Also, I'm wondering how safe this is when using multibyte strings. Off the top of my head, I can't think of a scenario where the comparison would fail to produce the correct result, but I'm not 100% sure. Do you think this might be an issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good call.

I can't think of a scenario either right now, but you never know :-)

We could use mb_ereg in this case:

if ( false !== mb_ereg( preg_quote( '/' . $path_or_file ) . '$', $file->getPathname() ) ) {
	return false;
}

Feels more readable anyway.

@schlessera schlessera merged commit f56e9eb into master Jul 2, 2018
@schlessera schlessera deleted the 27-exclude-option branch July 2, 2018 18:34
@schlessera
Copy link
Member

Merged now. Good work, @swissspidy!

@schlessera schlessera added this to the 0.1.0 milestone Jul 2, 2018
schlessera added a commit that referenced this pull request Jan 6, 2022
Exclude some common directories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants