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

As a new developer using wp-scripts, I expect the zip package generated by plugin-zip to be compatible with plugin updates. #60481

Closed
nicolasgalvez opened this issue Apr 4, 2024 · 0 comments · Fixed by #61375
Labels
[Tool] WP Scripts /packages/scripts [Type] Enhancement A suggestion for improvement.

Comments

@nicolasgalvez
Copy link
Contributor

What problem does this address?

I am confused about the differences between the plugin-zip command and the zip requirements for plugin updates.

Issue

As a new developer using wp-scripts, I expect the zip package generated by plugin-zip to be compatible with plugin updates.

Actual behavior

  1. The plugin-zip adds files to the root of the zip file.
  2. The plugin update system requires a directory with the same name as the plugin in the root of the zip, and the plugin files should be inside that. Running the update on the backend fails.

Details

I expect the zip package to have the tree specified in the best practices for plugin development page.

/plugin-name
     plugin-name.php
     uninstall.php
     /languages
     /includes
     /admin
          /js
          /css
          /images
     /public
          /js
          /css
          /images

I made a plugin called example-plugin using wp-scripts. I then used the plugin-zip command to generate a zip file and uploaded it to my site using the admin backend as usual. It worked as expected.

However, when I tried to update my plugin after checking in a new version, it failed because the zip file did not have a directory named example-plugin.

When I examined the zip file, I noticed the tree was like this:

 plugin-name.php
 uninstall.php
 /languages
 /includes
 /admin
      /js
      /css
      /images
 /public
      /js
      /css
      /images

I looked to see if there was an option to pass to plugin-zip to alter the behavior but didn't find one in the documentation. I looked at the source code on github but didn't see any way to do this.

As a workaround, in my plugin's github action, I unzip the package after running plugin-zip (to take advantage of the file filtering from package.json), create a directory, move the files, and then zip again.

Here's the relevant part of my action for reference:

      - name: Build JS
        run: npm run build

      - name: Run wp-scripts plugin-zip # Use the wp-scripts command to create a zip with only the files needed.
        run: npm run plugin-zip

      - name: Unzip the plugin # This is a workaround because the plugin-zip command doesn't place the files in a directory with the plugin name.
        run: |
          unzip $PLUGIN_NAME.zip -d $PLUGIN_NAME
          rm $PLUGIN_NAME.zip

      - name: Re-zip the plugin # The WP updater expects the plugin files to be in a directory with the plugin name.
        run: zip -r $PLUGIN_NAME.zip $PLUGIN_NAME

Now the plugin will update when I update it from the admin/plugin page.

Note that I am using yahnis-elsts/plugin-update-checker which I have used before. But I also tried this by writing an update checker manually and am not confident that I did it right because it made a bunch of directories in the plugins directory like example-plugin-1.

What is your proposed solution?

Ideally there is already a way to do this that I don't know about. In that case, I would update the documentation to show how to specify a root directory in the zip package.

Otherwise, I would suggest adding a flag to plugin-zip to allow this behavior such as plugin-directory=name. where the name is optional, and the directory name might be pulled from the last portion of the name in the package.json file: "name": "my-namespace/example-plugin",

Or, maybe yahnis-elsts/plugin-update-checker is no longer the correct tool to update plugins. Perhaps there could be a link to an example plugin with the update code.

I'm happy to take a stab at doing this, since I want to get involved with core development. Thanks everyone!

@nicolasgalvez nicolasgalvez added the [Type] Enhancement A suggestion for improvement. label Apr 4, 2024
@gziolo gziolo added the [Tool] WP Scripts /packages/scripts label Apr 4, 2024
nicolasgalvez added a commit to nicolasgalvez/gutenberg that referenced this issue Oct 26, 2024
Require an argument for root-folder.
Add option no-root-folder to zip files without a root folder for backwards compatibility.
Update documentation, and changelog.
(WordPress#60481)
gziolo added a commit that referenced this issue Oct 29, 2024
#61375)

* Scripts: Add a --zip-root-folder argument to the plugin-zip command (#60481)

* Scripts: Change the plugin-zip command to add a folder in the plugin zip file by default

1. The plugin-zip command will now create a root folder in the zip file matching the name of the plugin. This aligns with the documentation stating that "By default, it uses Plugin Handbook best practices to discover files."
2. `--zip-root-folder` argument from my previous commit is now named `--root-folder`
3. The --root-folder argument will allow specifying a custom folder, or no folder for backwards compatibility.

Example usage:

{{{
npm run plugin-zip --root-folder='foo'
}}}

Follow-up to [61375], [60481].

Props justlevine, gziolo.

* Add CHANGELOG entry

* Update CHANGELOG.md

* Scripts: Modify plugin-zip to add no-root-folder and root-folder options
Require an argument for root-folder.
Add option no-root-folder to zip files without a root folder for backwards compatibility.
Update documentation, and changelog.
(#60481)

* Scripts: adjust message for plugin-zip command

Apply suggestions from code review. props: @gziolo

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>

* Update packages/scripts/README.md

Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>

---------

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>

Co-authored-by: nicolasgalvez <nickgalvez@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: justlevine <justlevine@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
karthick-murugan pushed a commit to karthick-murugan/gutenberg that referenced this issue Nov 13, 2024
WordPress#61375)

* Scripts: Add a --zip-root-folder argument to the plugin-zip command (WordPress#60481)

* Scripts: Change the plugin-zip command to add a folder in the plugin zip file by default

1. The plugin-zip command will now create a root folder in the zip file matching the name of the plugin. This aligns with the documentation stating that "By default, it uses Plugin Handbook best practices to discover files."
2. `--zip-root-folder` argument from my previous commit is now named `--root-folder`
3. The --root-folder argument will allow specifying a custom folder, or no folder for backwards compatibility.

Example usage:

{{{
npm run plugin-zip --root-folder='foo'
}}}

Follow-up to [61375], [60481].

Props justlevine, gziolo.

* Add CHANGELOG entry

* Update CHANGELOG.md

* Scripts: Modify plugin-zip to add no-root-folder and root-folder options
Require an argument for root-folder.
Add option no-root-folder to zip files without a root folder for backwards compatibility.
Update documentation, and changelog.
(WordPress#60481)

* Scripts: adjust message for plugin-zip command

Apply suggestions from code review. props: @gziolo

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>

* Update packages/scripts/README.md

Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>

---------

Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>

Co-authored-by: nicolasgalvez <nickgalvez@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: justlevine <justlevine@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Tool] WP Scripts /packages/scripts [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants