diff --git a/.gitattributes b/.gitattributes index 89ec439..70c39de 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,7 +3,13 @@ # Do not include the following files when creating repo artifacts, e.g. the zip /.* export-ignore /build/ export-ignore +/src/ export-ignore /tests/ export-ignore /CHANGELOG.md export-ignore /CONTRIBUTING.md export-ignore /phpunit.xml.dist export-ignore +/box.json export-ignore +/composer.build.json export-ignore +/composer.release.json export-ignore +/bin/build-phar.sh export-ignore +/bin/phpmnd.src export-ignore diff --git a/.gitignore b/.gitignore index a9e7ac2..ccfb13d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /build /vendor /.phpunit.result.cache +/composer.release.json /composer.lock diff --git a/README.md b/README.md index 6564bb8..bdb17b7 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,23 @@ Afterwards make sure you have the global Composer binaries directory in your ``P ```bash $ export PATH="$PATH:$HOME/.composer/vendor/bin" ``` +### For Development + +Because this repo is now a phar-first repo, you need to manually use the dev composer.json: + +``` +mv composer.json composer.release.json +cp composer.build.json composer.json +composer install +``` + +If you wish to build the phar: + +``` +./bin/build-phar.sh +``` + +If you wish to test the phar file, it will not work if there is an existing vendor directory. ## Usage Example @@ -178,6 +195,11 @@ $percent = $number / intval(100); ``` would mark 100 as not magic. +## Installation + + + + ## Contributing Please see [CONTRIBUTING.md](CONTRIBUTING.md) for more information. diff --git a/bin/build-phar.sh b/bin/build-phar.sh new file mode 100755 index 0000000..a84a3f7 --- /dev/null +++ b/bin/build-phar.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash + +if ! command -v box 2>&1 > /dev/null; then + echo "It doesn't look like you have the box CLI installed for creating PHARs..." + read -p "Would you like to install it now? (y/N) " YES_NO + if [ "$YES_NO" != "y" ]; then + echo "Please install https://github.com/box-project/box to continue." + exit 1 + fi + + wget -O box.phar "https://github.com/box-project/box/releases/latest/download/box.phar" + wget -O box.phar.asc "https://github.com/box-project/box/releases/latest/download/box.phar.asc" + + # Check that the signature matches + gpg --keyserver hkps://keys.openpgp.org --recv-keys 41539BBD4020945DB378F98B2DF45277AEF09A2F + gpg --verify box.phar.asc box.phar + + rm box.phar.asc + mv box.phar box + chmod +x box + + echo sudo mv box /usr/local/bin + sudo mv box /usr/local/bin +fi + +# Run composer install to insure that everything is good. +echo "Running a fresh \`composer install --no-dev\` to insure the latest dependencies are installed..." +echo "Removing vendor/ and composer.lock ..." +rm -v composer.lock +rm -rf vendor +git checkout composer.json +mv composer.json composer.release.json +cp composer.build.json composer.json +composer install --no-dev + + +# Build the phar +echo "Building phpmnd.phar..." +box compile + +mv composer.release.json composer.json +rm -rf vendor diff --git a/bin/createPhar b/bin/createPhar deleted file mode 100755 index c6f99b5..0000000 --- a/bin/createPhar +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env php -addFile('bin/phpmnd'); -$dirs = [ - 'src', - 'vendor', -]; -foreach($dirs as $dir) { - addDir($dir, $phar); -} -$phar->setStub( - "#!/usr/bin/env php - getPathname(); - $path = $dir . substr($fullPath, $codeLength); - if (is_file($path)) { - $phar->addFile($path); - } - } -} - diff --git a/bin/phpmnd b/bin/phpmnd index 3c2cdf0..c36dae3 100755 --- a/bin/phpmnd +++ b/bin/phpmnd @@ -1,61 +1,14 @@ #!/usr/bin/env php ')) { - \fwrite( - \STDERR, - \sprintf( - 'This version of PHPMND is supported on PHP 7.4.' . \PHP_EOL . - 'You are using PHP %s%s.' . \PHP_EOL, - \PHP_VERSION, - \defined('PHP_BINARY') ? ' (' . \PHP_BINARY . ')' : '' - ) - ); - exit(1); +if (!file_exists(__DIR__ . '/phpmnd.phar') || is_dir(__DIR__ . '/../vendor')) { + // Run the internal phpmnd binary directly. + require __DIR__ . '/phpmnd.src'; + exit; } -// PHPMND autoloading -(static function (): void { - if (\file_exists($autoload = __DIR__ . '/../../../autoload.php')) { - // Is installed via Composer - include_once $autoload; - - return; - } - - if (\file_exists($autoload = __DIR__ . '/../vendor/autoload.php')) { - // Is installed locally - include_once $autoload; - - return; - } - - \fwrite( - \STDERR, - <<<'ERROR' -You need to set up the project dependencies using Composer: - $ composer install -You can learn all about Composer on https://getcomposer.org/. -ERROR - ); - - throw new RuntimeException('Unable to find the Composer autoloader.'); -})(); - -// Project (third-party) autoloading -(static function (): void { - if (\file_exists($autoload = \getcwd() . '/vendor/autoload.php')) { - include_once $autoload; - } -})(); +Phar::loadPhar(__DIR__ . '/phpmnd.phar', 'phpmnd.phar'); -(new Application(Container::create()))->run(); +require 'phar://phpmnd.phar/bin/phpmnd.src'; diff --git a/bin/phpmnd.phar b/bin/phpmnd.phar new file mode 100755 index 0000000..0d7a561 Binary files /dev/null and b/bin/phpmnd.phar differ diff --git a/bin/phpmnd.src b/bin/phpmnd.src new file mode 100755 index 0000000..3c2cdf0 --- /dev/null +++ b/bin/phpmnd.src @@ -0,0 +1,61 @@ +#!/usr/bin/env php +')) { + \fwrite( + \STDERR, + \sprintf( + 'This version of PHPMND is supported on PHP 7.4.' . \PHP_EOL . + 'You are using PHP %s%s.' . \PHP_EOL, + \PHP_VERSION, + \defined('PHP_BINARY') ? ' (' . \PHP_BINARY . ')' : '' + ) + ); + exit(1); +} + +// PHPMND autoloading +(static function (): void { + if (\file_exists($autoload = __DIR__ . '/../../../autoload.php')) { + // Is installed via Composer + include_once $autoload; + + return; + } + + if (\file_exists($autoload = __DIR__ . '/../vendor/autoload.php')) { + // Is installed locally + include_once $autoload; + + return; + } + + \fwrite( + \STDERR, + <<<'ERROR' +You need to set up the project dependencies using Composer: + $ composer install +You can learn all about Composer on https://getcomposer.org/. +ERROR + ); + + throw new RuntimeException('Unable to find the Composer autoloader.'); +})(); + +// Project (third-party) autoloading +(static function (): void { + if (\file_exists($autoload = \getcwd() . '/vendor/autoload.php')) { + include_once $autoload; + } +})(); + +(new Application(Container::create()))->run(); diff --git a/box.json b/box.json new file mode 100644 index 0000000..ea1eac3 --- /dev/null +++ b/box.json @@ -0,0 +1,3 @@ +{ + "main": "bin/phpmnd.src" +} diff --git a/composer.build.json b/composer.build.json new file mode 100644 index 0000000..6aa3628 --- /dev/null +++ b/composer.build.json @@ -0,0 +1,50 @@ +{ + "name": "povils/phpmnd", + "type": "application", + "description": "A tool to detect Magic numbers in codebase", + "license": "MIT", + "authors": [ + { + "name": "Povilas Susinskas", + "email": "povilassusinskas@gmail.com" + } + ], + "support": { + "issues": "https://github.com/povils/phpmnd/issues" + }, + "require": { + "php": "^7.4 || ^8.0", + "composer-runtime-api": "^2.0", + "nikic/php-parser": "^4.18 || ^5.0", + "php-parallel-lint/php-console-highlighter": "^1.0", + "phpunit/php-timer": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0", + "symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0", + "symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0" + }, + "require-dev": { + "phpunit/phpunit": "^9.6", + "squizlabs/php_codesniffer": "^2.8.1||^3.5" + }, + "autoload": { + "psr-4": { + "Povils\\PHPMND\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Povils\\PHPMND\\Tests\\": "tests/" + } + }, + "config": { + "sort-packages": true + }, + "bin": [ + "bin/phpmnd", + "bin/phpmnd.phar" + ], + "scripts": { + "test": "phpunit", + "cs-check": "phpcs -p --standard=PSR2 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/Fixtures/Files", + "cs-fix": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests --ignore=tests/Fixtures/Files" + } +} diff --git a/composer.json b/composer.json index e782733..5e79285 100644 --- a/composer.json +++ b/composer.json @@ -14,14 +14,14 @@ }, "require": { "php": "^7.4 || ^8.0", - "composer-runtime-api": "^2.0", + "composer-runtime-api": "^2.0" + }, + "require-dev": { "nikic/php-parser": "^4.18 || ^5.0", "php-parallel-lint/php-console-highlighter": "^1.0", "phpunit/php-timer": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0", "symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0", - "symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0" - }, - "require-dev": { + "symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0", "phpunit/phpunit": "^9.6", "squizlabs/php_codesniffer": "^2.8.1||^3.5" },