Skip to content

Commit f40b5dd

Browse files
authored
Merge pull request #31 from wp-cli/30-create-directory-if-not-exists
Add `--create-target-dir` flag
2 parents 5331d0e + f7cc42b commit f40b5dd

File tree

4 files changed

+67
-16
lines changed

4 files changed

+67
-16
lines changed

README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ Create a distribution .zip or .tar.gz based on a plugin or theme's .distignore f
55

66
[![CircleCI](https://circleci.com/gh/wp-cli/dist-archive-command/tree/master.svg?style=svg)](https://circleci.com/gh/wp-cli/dist-archive-command/tree/master)
77

8-
Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contributing)
8+
Quick links: [Using](#using) | [Installing](#installing) | [Contributing](#contributing) | [Support](#support)
99

1010
## Using
1111

1212
~~~
13-
wp dist-archive <path> [<target>] [--format=<format>]
13+
wp dist-archive <path> [<target>] [--create-target-dir] [--format=<format>]
1414
~~~
1515

1616
For a plugin in a directory 'wp-content/plugins/hello-world', this command
@@ -39,6 +39,9 @@ script in each project.
3939
[<target>]
4040
Path and file name for the distribution archive. Defaults to project directory name plus version, if discoverable.
4141

42+
[--create-target-dir]
43+
Automatically create the target directory as needed.
44+
4245
[--format=<format>]
4346
Choose the format for the archive.
4447
---
@@ -50,7 +53,7 @@ script in each project.
5053

5154
## Installing
5255

53-
Installing this package requires WP-CLI v0.23.0 or greater. Update to the latest stable release with `wp cli update`.
56+
Installing this package requires WP-CLI v2 or greater. Update to the latest stable release with `wp cli update`.
5457

5558
Once you've done so, you can install this package with:
5659

@@ -62,30 +65,25 @@ We appreciate you taking the initiative to contribute to this project.
6265

6366
Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.
6467

68+
For a more thorough introduction, [check out WP-CLI's guide to contributing](https://make.wordpress.org/cli/handbook/contributing/). This package follows those policy and guidelines.
69+
6570
### Reporting a bug
6671

6772
Think you’ve found a bug? We’d love for you to help us get it fixed.
6873

6974
Before you create a new issue, you should [search existing issues](https://github.com/wp-cli/dist-archive-command/issues?q=label%3Abug%20) to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version.
7075

71-
Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/wp-cli/dist-archive-command/issues/new) with the following:
72-
73-
1. What you were doing (e.g. "When I run `wp post list`").
74-
2. What you saw (e.g. "I see a fatal about a class being undefined.").
75-
3. What you expected to see (e.g. "I expected to see the list of posts.")
76-
77-
Include as much detail as you can, and clear steps to reproduce if possible.
76+
Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please [create a new issue](https://github.com/wp-cli/dist-archive-command/issues/new). Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, [review our bug report documentation](https://make.wordpress.org/cli/handbook/bug-reports/).
7877

7978
### Creating a pull request
8079

8180
Want to contribute a new feature? Please first [open a new issue](https://github.com/wp-cli/dist-archive-command/issues/new) to discuss whether the feature is a good fit for the project.
8281

83-
Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request to make sure it's a pleasant experience:
82+
Once you've decided to commit the time to seeing your pull request through, [please follow our guidelines for creating a pull request](https://make.wordpress.org/cli/handbook/pull-requests/) to make sure it's a pleasant experience. See "[Setting up](https://make.wordpress.org/cli/handbook/pull-requests/#setting-up)" for details specific to working on this package locally.
83+
84+
## Support
8485

85-
1. Create a feature branch for each contribution.
86-
2. Submit your pull request early for feedback.
87-
3. Include functional tests with your changes. [Read the WP-CLI documentation](https://wp-cli.org/docs/pull-requests/#functional-tests) for an introduction.
88-
4. Follow the [WordPress Coding Standards](http://make.wordpress.org/core/handbook/coding-standards/).
86+
Github issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support
8987

9088

9189
*This README.md is generated dynamically from the project's codebase using `wp scaffold package-readme` ([doc](https://github.com/wp-cli/scaffold-package-command#wp-scaffold-package-readme)). To suggest changes, please submit a pull request against the corresponding part of the codebase.*

composer.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
"extra": {
2525
"commands": [
2626
"dist-archive"
27-
]
27+
],
28+
"readme": {
29+
"shields": [
30+
"[![CircleCI](https://circleci.com/gh/wp-cli/dist-archive-command/tree/master.svg?style=svg)](https://circleci.com/gh/wp-cli/dist-archive-command/tree/master)"
31+
]
32+
}
2833
},
2934
"autoload": {
3035
"psr-4": {

features/dist-archive.feature

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,26 @@ Feature: Generate a distribution archive of a project
148148
| format | extension | extract |
149149
| zip | zip | unzip |
150150
| targz | tar.gz | tar -zxvf |
151+
152+
Scenario: Create directories automatically if requested
153+
Given a WP install
154+
155+
When I run `wp scaffold plugin hello-world`
156+
Then the wp-content/plugins/hello-world directory should exist
157+
And the wp-content/plugins/hello-world/hello-world.php file should exist
158+
And the wp-content/plugins/hello-world/.travis.yml file should exist
159+
And the wp-content/plugins/hello-world/bin directory should exist
160+
161+
When I try `wp dist-archive wp-content/plugins/hello-world {RUN_DIR}/some/nested/folder/hello-world.zip`
162+
Then STDERR should contain:
163+
"""
164+
Error: Target directory does not exist
165+
"""
166+
167+
When I run `wp dist-archive --create-target-dir wp-content/plugins/hello-world {RUN_DIR}/some/nested/folder/hello-world.zip`
168+
Then STDOUT should be:
169+
"""
170+
Success: Created hello-world.zip
171+
"""
172+
And STDERR should be empty
173+
And the {RUN_DIR}/some/nested/folder/hello-world.zip file should exist

src/Dist_Archive_Command.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use WP_CLI\Utils;
4+
35
/**
46
* Create a distribution archive based on a project's .distignore file.
57
*/
@@ -34,6 +36,9 @@ class Dist_Archive_Command {
3436
* [<target>]
3537
* : Path and file name for the distribution archive. Defaults to project directory name plus version, if discoverable.
3638
*
39+
* [--create-target-dir]
40+
* : Automatically create the target directory as needed.
41+
*
3742
* [--format=<format>]
3843
* : Choose the format for the archive.
3944
* ---
@@ -119,6 +124,14 @@ public function __invoke( $args, $assoc_args ) {
119124

120125
chdir( dirname( $path ) );
121126

127+
if ( Utils\get_flag_value( $assoc_args, 'create-target-dir' ) ) {
128+
$this->maybe_create_directory( $archive_file );
129+
}
130+
131+
if ( ! is_dir( dirname( $archive_file ) ) ) {
132+
WP_CLI::error( "Target directory does not exist: {$archive_file}" );
133+
}
134+
122135
if ( 'zip' === $assoc_args['format'] ) {
123136
$excludes = implode( ' --exclude ', $ignored_files );
124137
if ( ! empty( $excludes ) ) {
@@ -149,4 +162,16 @@ function( $ignored_file ) {
149162
}
150163
}
151164

165+
/**
166+
* Create the directory for a target file if it does not exist yet.
167+
*
168+
* @param string $archive_file Path and filename of the target file.
169+
* @return void
170+
*/
171+
private function maybe_create_directory( $archive_file ) {
172+
$directory = dirname( $archive_file );
173+
if ( ! is_dir( $directory ) ) {
174+
mkdir( $directory, $mode = 0777, $recursive = true );
175+
}
176+
}
152177
}

0 commit comments

Comments
 (0)