Skip to content

Commit

Permalink
Ensure that included files are actually included (#26)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
borkweb authored Dec 13, 2024
2 parents c962d6d + 5b9e907 commit af6ed6d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pup
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace StellarWP\Pup;

const PUP_VERSION = '1.3.7';
const PUP_VERSION = '1.3.8';
define( '__PUP_DIR__', __DIR__ );

if ( ! \Phar::running() ) {
Expand Down
89 changes: 71 additions & 18 deletions src/Commands/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ protected function execute( InputInterface $input, OutputInterface $output ) {
$output->writeln( '<fg=green>✓</> Updating version files...Complete.' );

$output->writeln( '<fg=gray>- Synchronizing files to zip directory...</>' );

$distfiles = $this->getDistfilesLines( $this->getSourceDir( $root ) );
if ( ! empty( $distfiles ) ) {
$distfiles_message = '>>> Your project has a <fg=yellow>.distfiles</> file, so <fg=yellow>.distignore</> and pup\'s default ignore rules will not be used.';
$output->writeln( "<fg=gray>{$distfiles_message}</>" );
}

$pup_zip_dir = $config->getZipDir();

DirectoryUtils::rmdir( $pup_zip_dir );
Expand Down Expand Up @@ -201,6 +208,22 @@ protected function createZip( string $dir_to_zip, string $zip_filename, string $
return 0;
}

/**
* Get the default things to exclude from sync.
*
* @return array<int, string>
*/
public function getDefaultIgnoreLines(): array {
$working_dir = App::getConfig()->getWorkingDir();
$zip_dir = str_replace( $working_dir, '', App::getConfig()->getZipDir() );

return [
'.puprc',
'.pup-*',
$zip_dir,
];
}

/**
* Get the files to exclude from sync.
*
Expand Down Expand Up @@ -230,33 +253,54 @@ public function getIgnoreLines( string $source ): array {
}

/**
* Get the files to include in sync.
* Get the distfiles lines to include in sync.
*
* @param string $source
*
* @return array<int, string>
*/
public function getIncludeLines( string $source ): array {
$include = [];
$include_files = [
'.pup-distinclude',
'.pup-distfiles',
];
public function getDistfilesLines( string $source ): array {
$include = [];
$include_file = '.pup-distfiles';

foreach ( $include_files as $include_file ) {
if ( ! file_exists( $source . $include_file ) ) {
continue;
}
if ( ! file_exists( $source . $include_file ) ) {
return [];
}

$lines = file( $source . $include_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );
$lines = file( $source . $include_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );

if ( ! $lines ) {
continue;
}
if ( ! $lines ) {
return [];
}

$include = array_merge( $include, $lines );

return $include;
}

/**
* Get the distinclude lines to include in sync.
*
* @param string $source
*
* @return array<int, string>
*/
public function getDistincludeLines( string $source ): array {
$include = [];
$include_file = '.pup-include';

if ( ! file_exists( $source . $include_file ) ) {
return [];
}

$lines = file( $source . $include_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES );

$include = array_merge( $include, $lines );
if ( ! $lines ) {
return [];
}

$include = array_merge( $include, $lines );

return $include;
}

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

$this->buildSyncFiles( $source );

$include = $this->getIncludeLines( $source );
$ignore = $this->getIgnoreLines( $source );
$distfiles = $this->getDistfilesLines( $source );
$distinclude = $this->getDistincludeLines( $source );
$include = array_merge( $distfiles, $distinclude );

$ignore = $this->getDefaultIgnoreLines();

// We only observe .distignore if there is no .distfiles files.
if ( empty( $distfiles ) ) {
$ignore = array_merge( $ignore, $this->getIgnoreLines( $source ) );
}

$results = $this->migrateNegatedLines( $include, $ignore );
$include = $results['include'];
$ignore = $results['ignore'];
Expand Down

0 comments on commit af6ed6d

Please sign in to comment.