Making WordPress.org


Ignore:
Timestamp:
07/20/2020 04:24:31 AM (6 years ago)
Author:
tellyworth
Message:

Plugin dir: refactor asset discovery code to be reusable.

Needed for block checker; see #5303.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    r10050 r10079  
    530530                        $build_files = array();
    531531
    532                         if ( ! empty( $potential_block_directories ) ) {
    533                                 $potential_block_directories = array_unique( $potential_block_directories );
    534 
    535                                 foreach ( $potential_block_directories as $block_dir ) {
    536                                         // dirname() returns . when there is no directory separator present.
    537                                         if ( '.' === $block_dir ) {
    538                                                 $block_dir = '';
    539                                         }
    540 
    541                                         // First look for a dedicated "build" or "dist" directory.
    542                                         foreach ( array( 'build', 'dist' ) as $dirname ) {
    543                                                 if ( is_dir( "$base_dir/$block_dir/$dirname" ) ) {
    544                                                         $build_files += Filesystem::list_files( "$base_dir/$block_dir/$dirname", true, '!\.(?:js|jsx|css)$!i' );
    545                                                 }
    546                                         }
    547 
    548                                         // There must be at least on JS file, so if only css was found, keep looking.
    549                                         if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
    550                                                 // Then check for files in the current directory with "build" or "min" in the filename.
    551                                                 $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '![_\-\.]+(?:build|dist|min)[_\-\.]+!i' );
    552                                         }
    553 
    554                                         if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
    555                                                 // Finally, just grab whatever js/css files there are in the current directory.
    556                                                 $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '#(?<!webpack\.config)\.(?:js|jsx|css)$#i' );
    557                                         }
    558                                 }
    559                         }
    560 
    561                         if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
    562                                 // Nothing in the potential block directories. Check if we somehow missed build/dist directories in the root.
    563                                 foreach ( array( 'build', 'dist' ) as $dirname ) {
    564                                         if ( is_dir( "$base_dir/$dirname" ) ) {
    565                                                 $build_files += Filesystem::list_files( "$base_dir/$dirname", true, '!\.(?:js|jsx|css)$!i' );
    566                                         }
    567                                 }
    568                         }
    569 
    570                         if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
    571                                 // Still nothing. Take on last wild swing.
    572                                 $build_files += Filesystem::list_files( $base_dir, false, '!\.(?:js|jsx|css)$!i' );
    573                         }
     532                        $build_files = self::find_possible_block_assets( $base_dir, $potential_block_directories );
    574533
    575534                        foreach ( $build_files as $file ) {
     
    733692                return $files;
    734693        }
     694
     695        /**
     696         * Find likely JS and CSS block asset files in a given directory.
     697         *
     698         * @param string $base_dir Base path in which to search.
     699         * @param array $potential_block_directories Subdirectories likely to contain block assets, if known. Optional.
     700         *
     701         * @return array
     702         */
     703        static function find_possible_block_assets( $base_dir, $potential_block_directories = null ) {
     704                if ( empty( $potential_block_directories ) || !is_array( $potential_block_directories ) ) {
     705                        $potential_block_directories = array( '.' );
     706                }
     707
     708                $build_files = array();
     709
     710                foreach ( $potential_block_directories as $block_dir ) {
     711                        // dirname() returns . when there is no directory separator present.
     712                        if ( '.' === $block_dir ) {
     713                                $block_dir = '';
     714                        }
     715
     716                        // First look for a dedicated "build" or "dist" directory.
     717                        foreach ( array( 'build', 'dist' ) as $dirname ) {
     718                                if ( is_dir( "$base_dir/$block_dir/$dirname" ) ) {
     719                                        $build_files += Filesystem::list_files( "$base_dir/$block_dir/$dirname", true, '!\.(?:js|jsx|css)$!i' );
     720                                }
     721                        }
     722
     723                        // There must be at least on JS file, so if only css was found, keep looking.
     724                        if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     725                                // Then check for files in the current directory with "build" or "min" in the filename.
     726                                $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '![_\-\.]+(?:build|dist|min)[_\-\.]+!i' );
     727                        }
     728
     729                        if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     730                                // Finally, just grab whatever js/css files there are in the current directory.
     731                                $build_files += Filesystem::list_files( "$base_dir/$block_dir", false, '#(?<!webpack\.config)\.(?:js|jsx|css)$#i' );
     732                        }
     733                }
     734
     735                if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     736                        // Nothing in the potential block directories. Check if we somehow missed build/dist directories in the root.
     737                        foreach ( array( 'build', 'dist' ) as $dirname ) {
     738                                if ( is_dir( "$base_dir/$dirname" ) ) {
     739                                        $build_files += Filesystem::list_files( "$base_dir/$dirname", true, '!\.(?:js|jsx|css)$!i' );
     740                                }
     741                        }
     742                }
     743
     744                if ( empty( preg_grep( '!\.(?:js|jsx)$!i', $build_files ) ) ) {
     745                        // Still nothing. Take on last wild swing.
     746                        $build_files += Filesystem::list_files( $base_dir, false, '!\.(?:js|jsx|css)$!i' );
     747                }
     748
     749                return array_unique( $build_files );
     750        }
    735751}
Note: See TracChangeset for help on using the changeset viewer.