Skip to content

Commit af6ed6d

Browse files
authored
Ensure that included files are actually included (#26)
This resolves an issue where an ignored file will supercede an explicit include. Example: 1. If `.distfiles` includes `/node_modules/clipboard/**/*.min.js` 2. If `.distignore` includes `/node_modules`, it will override the above The fix is a `.distfiles` exists and has contents, `.distignore` is not observed - because `.distfiles` a safelist of files with more flexibility than `.distignore` in its syntax.
2 parents c962d6d + 5b9e907 commit af6ed6d

File tree

2 files changed

+72
-19
lines changed

2 files changed

+72
-19
lines changed

pup

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace StellarWP\Pup;
55

6-
const PUP_VERSION = '1.3.7';
6+
const PUP_VERSION = '1.3.8';
77
define( '__PUP_DIR__', __DIR__ );
88

99
if ( ! \Phar::running() ) {

src/Commands/Package.php

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
8484
$output->writeln( '<fg=green>✓</> Updating version files...Complete.' );
8585

8686
$output->writeln( '<fg=gray>- Synchronizing files to zip directory...</>' );
87+
88+
$distfiles = $this->getDistfilesLines( $this->getSourceDir( $root ) );
89+
if ( ! empty( $distfiles ) ) {
90+
$distfiles_message = '>>> Your project has a <fg=yellow>.distfiles</> file, so <fg=yellow>.distignore</> and pup\'s default ignore rules will not be used.';
91+
$output->writeln( "<fg=gray>{$distfiles_message}</>" );
92+
}
93+
8794
$pup_zip_dir = $config->getZipDir();
8895

8996
DirectoryUtils::rmdir( $pup_zip_dir );
@@ -201,6 +208,22 @@ protected function createZip( string $dir_to_zip, string $zip_filename, string $
201208
return 0;
202209
}
203210

211+
/**
212+
* Get the default things to exclude from sync.
213+
*
214+
* @return array<int, string>
215+
*/
216+
public function getDefaultIgnoreLines(): array {
217+
$working_dir = App::getConfig()->getWorkingDir();
218+
$zip_dir = str_replace( $working_dir, '', App::getConfig()->getZipDir() );
219+
220+
return [
221+
'.puprc',
222+
'.pup-*',
223+
$zip_dir,
224+
];
225+
}
226+
204227
/**
205228
* Get the files to exclude from sync.
206229
*
@@ -230,33 +253,54 @@ public function getIgnoreLines( string $source ): array {
230253
}
231254

232255
/**
233-
* Get the files to include in sync.
256+
* Get the distfiles lines to include in sync.
234257
*
235258
* @param string $source
236259
*
237260
* @return array<int, string>
238261
*/
239-
public function getIncludeLines( string $source ): array {
240-
$include = [];
241-
$include_files = [
242-
'.pup-distinclude',
243-
'.pup-distfiles',
244-
];
262+
public function getDistfilesLines( string $source ): array {
263+
$include = [];
264+
$include_file = '.pup-distfiles';
245265

246-
foreach ( $include_files as $include_file ) {
247-
if ( ! file_exists( $source . $include_file ) ) {
248-
continue;
249-
}
266+
if ( ! file_exists( $source . $include_file ) ) {
267+
return [];
268+
}
250269

251-
$lines = file( $source . $include_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
270+
$lines = file( $source . $include_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
252271

253-
if ( ! $lines ) {
254-
continue;
255-
}
272+
if ( ! $lines ) {
273+
return [];
274+
}
275+
276+
$include = array_merge( $include, $lines );
277+
278+
return $include;
279+
}
280+
281+
/**
282+
* Get the distinclude lines to include in sync.
283+
*
284+
* @param string $source
285+
*
286+
* @return array<int, string>
287+
*/
288+
public function getDistincludeLines( string $source ): array {
289+
$include = [];
290+
$include_file = '.pup-include';
291+
292+
if ( ! file_exists( $source . $include_file ) ) {
293+
return [];
294+
}
295+
296+
$lines = file( $source . $include_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
256297

257-
$include = array_merge( $include, $lines );
298+
if ( ! $lines ) {
299+
return [];
258300
}
259301

302+
$include = array_merge( $include, $lines );
303+
260304
return $include;
261305
}
262306

@@ -392,8 +436,17 @@ protected function syncFiles( string $root, string $destination ): int {
392436

393437
$this->buildSyncFiles( $source );
394438

395-
$include = $this->getIncludeLines( $source );
396-
$ignore = $this->getIgnoreLines( $source );
439+
$distfiles = $this->getDistfilesLines( $source );
440+
$distinclude = $this->getDistincludeLines( $source );
441+
$include = array_merge( $distfiles, $distinclude );
442+
443+
$ignore = $this->getDefaultIgnoreLines();
444+
445+
// We only observe .distignore if there is no .distfiles files.
446+
if ( empty( $distfiles ) ) {
447+
$ignore = array_merge( $ignore, $this->getIgnoreLines( $source ) );
448+
}
449+
397450
$results = $this->migrateNegatedLines( $include, $ignore );
398451
$include = $results['include'];
399452
$ignore = $results['ignore'];

0 commit comments

Comments
 (0)