Code Monkey home page Code Monkey logo

tooly-composer-script's Introduction

tooly-composer-script

Minimum PHP Version Latest Stable Version Total Downloads Build Status Build status SensioLabsInsight License Gitter

With tooly composer-script you can version needed PHAR files in your project's composer.json without adding them directly to a VCS,

  • to save disk space at vcs repository
  • to be sure that all developers in your project get the required toolchain
  • to prepare a CI/CD System
  • (optional) to automatically check the GPG signature verification for each tool

Every PHAR file will be saved in the composer binary directory.

Example

An real example can be found here.

Requirements

  • PHP >= 5.6
  • Composer

Install

To use the script execute the following command:

composer require --dev tm/tooly-composer-script

Then add the script in the composer.json under "scripts" with the event names you want to trigger. For example:

...
"scripts": {
    "post-install-cmd": "Tooly\\ScriptHandler::installPharTools",
    "post-update-cmd": "Tooly\\ScriptHandler::installPharTools"
  },
...

Look here for more informations about composer events.

Sample usage

The composer.json scheme has a part "extra" which is used for the script. Its described here.

In this part you can add your needed phar tools under the key "tools".

...
"extra": {
    ...
    "tools": {
      "phpunit": {
        "url": "https://phar.phpunit.de/phpunit-5.5.0.phar",
        "sign-url": "https://phar.phpunit.de/phpunit-5.5.0.phar.asc"
      },
      "phpcpd": {
        "url": "https://phar.phpunit.de/phpcpd-2.0.4.phar",
        "only-dev": true,
        "rename": true
      },
      "security-checker": {
        "url": "http://get.sensiolabs.org/security-checker.phar",
        "force-replace": true
      },
    }
    ...
  }
...

Parameters

url (required)

After you add the name of the tool as key, you need only one further parameter. The "url". The url can be a link to a specific version, such as x.y.z, or a link to the latest version for this phar.

rename (optional, default false)

Rename the downloaded tool to the name that is used as key.

sign-url (optional, default none)

If this parameter is set tooly checks if the PHAR file in url has a valid signature by comparing signature in sign-url.

This option is useful if you want to be sure that the tool is from the expected author.

Note: For the check you need a further requirement and a GPG binary in your $PATH variable.

You can add the requirement with this command: composer require tm/gpg-verifier

This check often fails if you dont has the public key from the tool author in your GPG keychain.

fallback-url (optional, default none)

This option is useful if you want to add an extra layer of stability to your daily build processes.

In case the required url is not accessible and a fallback-url is set, tooly uses the fallback url to download the phar file. The fallback url can be a link to a specific version, such as x.y.z, or a link to the latest version for this phar.

force-replace (optional, default false)

Every time you update or install with composer the phar tools are checked. You are asked if you want to overwrite the existing phar if the remote and local phar has not the same checksum.

Except you set this parameter.

This option is useful if you has a link to the latest version of a tool and always want a replacement. Or you run composer in non-interactive mode (for example in a CI system) and want a replacement.

But is also useful if some require-dev library has one of the tools as own requirement.

only-dev (optional, default true)

This parameter means that this phar is only needed in developing mode. So the command composer [install|update] --no-dev ignores this phar tool.

Note: Therefore tooly must be a no-dev requirement

A note to PhpStorm or other IDE users

To furthermore have auto-suggestion you should set the "include_path" option in the project. PhpStorm setting

Contributing

Please refer to CONTRIBUTING.md for information on how to contribute.

tooly-composer-script's People

Contributors

be-heiglandreas avatar hollodotme avatar svenjungnickel avatar tommy-muehle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tooly-composer-script's Issues

Improve performance during composer install/update events

I really like the idea of the package, but I'm worried by its performance. composer install is very fast if everything if is in place, but only adding 1 phar via tooly composer install gets already delayed by a few seconds.

Expected behavior

composer install should stay fast and be done immediately if everything is in place

Actual behavior

composer installed gets delayed by multiple seconds. It looks like the tools downloads the phar just to verify the checksum.

Steps to reproduce actual behavior

  • Configure the project for one phar
  • Run composer install multiple times

Other information (e.g. version, os system, stacktraces, etc)

I used v1.2.2 with PHP7

Handle unreachable download URLs with timeout and hint

I shortly had the case, that the URL to the codecept.phar was offline for a while. Tooly's reaction after some seconds of trial is:

Script Tooly\ScriptHandler::installPharTools handling the post-update-cmd event terminated with an exception


  [ErrorException]
  sha1_file(http://codeception.com/codecept.phar): failed to open stream: HTTP request failed!

Unfortunately codecept was the first tool in my list, so all the following tools were not processed.

IMO this should be handled with an appropriate timeout / error handling for the HTTP operation and a hint like:

INFO: http://codeception.com/codecept.phar seems down at the moment - skipping.

Would also be nice for offline work. When travelling with ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿš† , you know. ๐Ÿ˜„

And it may be useful to optionally let one overwrite the default timeout (I guess 5 seconds is an appropriate value) for each tool in composer.json's extra section, like this:

"tools": {
    "codecept": {
        "url": "http://codeception.com/codecept.phar",
        "only-dev": true,
        "timeout": 10
    }
}

Check symlink creation in no-dev mode

If composer executed in "--no-dev" mode only symlink's to non only-dev tools should be created.
Actually symlinks to all existing PHAR tools are created.

Fix wording in processing output

Currently there is output like this, when processing an already existing file:

File "/vagrant/vendor/bin/phpunit" are already exist in given version.

Should be:

File "/vagrant/vendor/bin/phpunit" already exists in the given version.

scriptHandler removes bin/*.phar files from different packages

When I have a dependency which installs a .phar file into bin directory, then during ScriptHandler::installPharTools the phar gets removed.

Steps to reproduce:

  • Create composer.json:
{
	"name": "test/test",
	"require": {},
	"require-dev": {
		"phpstan/phpstan-shim": "^0.9.2",
		"tm/tooly-composer-script": "^1.2.5"
	},
	"scripts": {
		"post-install-cmd": [
			"Tooly\\ScriptHandler::installPharTools"
		],
		"post-update-cmd": [
			"Tooly\\ScriptHandler::installPharTools"
		]
	}
}

Expected is that after composer install two files (phpstan and phpstan.phar) files are in vendor/bin directory.

Create vendor/bin if not exists before installing tools.

When running composer install/update the first time with tooly-composer-script configured, installing the PHARs fails, because vendor/bin does not yet exist.

> post-update-cmd: Tooly\ScriptHandler::installPharTools
Process tool "phpunit" ...
Script Tooly\ScriptHandler::installPharTools handling the post-update-cmd event terminated with an exception


[ErrorException]
file_put_contents(/vagrant/vendor/bin/phpunit): failed to open stream: No such file or directory

Force overwrite option

I use tooly to load phpunit as phar. Some of my requirements also load phpunit, but via composer. Tooly runs as post command and composer has already created the phpunit file in my bin directory. The the post command asks, if I want to overwrite phpunit. How can I force this use case for a continuous integration server for example?

If bin dir does not exists composer install/update failed

Expected behavior

  • Create bin dir if it does not exist and install tools

Actual behavior

  • Installation failed with
    [ErrorException] scandir(/app/path/bin): failed to open dir: No such file or directory

Steps to reproduce actual behavior

  • Delete bin dir and run composer update
  • Error occured into $this->removeFromDir()

Other information (e.g. version, os system, stacktraces, etc)

  • tooly version 1.2.3

Function ReflectionType::__toString() is deprecated on PHPUnit 5.7 with PHP 7.4

Expected behavior

  • No error on PHPUnit execution.

Actual behavior

Function ReflectionType::__toString() is deprecated

Steps to reproduce actual behavior

  • Using the PHPUnit 5.7 to execute unit tests on php-7.4 version.

Current solutions

  • Upgrading different PHPUnit versions for php-7.4 version.
  • Defining different PHP versions to support different PHP versions.

2 Issues with `rename`: `must be absolute paths` and downloads _always_ have `.phar` extension

I am using rename, e.g.

"phpcs": {
        "url": "https://github.com/squizlabs/PHP_CodeSniffer/releases/download/3.5.5/phpcs.phar",
        "rename": true
      }

This currently (1.4.1) fails due to two issues:

  1. The composer (1.10.5) tooling for symlinks require absolute paths (this may be a change on their side, I did not investigate). When using rename the $filename is set to a plain name, phpcs in my example, when it should be /some/path/vendor/bin/phpcs.
  2. (After fixing 1) process always downloads files with a .phar extension, so that symlinkOrCopy produces dangling symlinks for my example, e.g. phpcs -> ../tm/tooly-composer-script/bin/phpcs, when the downloaded file is forced to be ../tm/tooly-composer-script/bin/phpcs.phar.

My proposed patch:

diff -Naur tooly-composer-script/src/Script/Processor.php tooly-composer-script-patch/src/Script/Processor.php
--- tooly-composer-script/src/Script/Processor.php	2019-06-24 22:52:52.000000000 +0200
+++ tooly-composer-script-patch/src/Script/Processor.php	2020-05-05 17:38:06.726662222 +0200
@@ -101,10 +101,11 @@
 
         $filename = $tool->getFilename();
         if ($tool->renameToConfigKey()) {
-            $filename = $tool->getName();
+            $filename = dirname($filename) . DIRECTORY_SEPARATOR . $tool->getName();
         }
         $composerDir = $this->configuration->getComposerBinDirectory();
         $composerPath = $composerDir . DIRECTORY_SEPARATOR . basename($filename);
+        $filename = str_replace('.phar', '', $filename) . '.phar';
 
         if (Platform::isWindows()) {
             $this->helper->getFilesystem()->copyFile($filename, $composerPath);

Deprecation notice with PHP 8.1

Not worth forking and not a bug but I know I'll be told about in the coming weeks by team members :)

Deprecation Notice: fopen(): Passing null to parameter #3 ($use_include_path) of type bool is deprecated in /app/vendor/tm/tooly-composer-script/src/Script/Helper/Downloader.php:21

Place downloaded PHARs in a .tooly/ folder

This is a proposal

Instead of downloading the .phar files directly to vendor/bin and creating symlinks in the same place, I suggest to download the .phar files to a tooly-specific folder, e.g. <ProjectDir>/.tooly/, and create a symlink from there to vendor/bin.

.tooly/ should be the default name of the folder, which can be overwritten by a new config key in the extra section of composer.json like this:

"extra": {
    "tooly-dir": "./.otherDir/",
    "tools": {
        "phpunit": {
            "url": "https://phar.phpunit.de/phpunit.phar",
            "only-dev": true
        }
    }
}

Advantages

  • Tooly would be able to prune obsolete .phar files, when removed from the extra/tools section in composer.json. This is currently not possible as there could be other files/symlinks under vendor/bin that were not created by tooly.
  • There would be a tooly-specific place for any future use, e.g. storing meta data or a composer-like .lock file.

Disadvantages

  • One more folder in the project dir that needs to be added to .gitignore

BC break

No.

(Windows) Missed symlink to phar at bin folder

Expected behavior

php-cs-fixer.phar apears at bin folder

Actual behavior

bin folder is empty

Steps to reproduce actual behavior

  1. Create dummy composer.json with the following content
{
  "name": "akuma/test1",
  "authors": [
    {
      "name": "Nikita Makarov",
      "email": "[email protected]"
    }
  ],
  "require": {},
  "require-dev": {
    "tm/tooly-composer-script": "^1.2"
  },
  "config": {
    "bin-dir": "bin"
  },
  "extra": {
    "tools": {
      "php-cs-fixer": {
        "url": "https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.13.1/php-cs-fixer.phar",
        "only-dev": true,
        "force-replace": true
      }
    }
  },
  "scripts": {
    "post-install-cmd": "Tooly\\ScriptHandler::installPharTools",
    "post-update-cmd": "Tooly\\ScriptHandler::installPharTools"
  }
}
  1. execute composer install --dev , UAC disabled and cmd terminal started as for Administrator

Other information (e.g. version, os system, stacktraces, etc)

systeminfo | findstr /B /C:"OS Version" /C:"System Type" /C:"System Locale"

OS Version:                10.0.17763 N/A Build 17763
System Type:               x64-based PC
System Locale:             en-us;English (United States)

php -i | findstr /B /C:"PHP Version" /C:"Architecture" /C:"Compiler" /C:"Configure Command" /C:"Zend Extension Build"

PHP Version => 7.1.26
Compiler => MSVC14 (Visual C++ 2015)
Architecture => x64
Configure Command => cscript /nologo configure.js  "--enable-snapshot-build" "--enable-debug-pack" "--with-pdo-oci=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--with-oci8-12c=c:\php-snap-build\deps_aux\oracle\x64\instantclient_12_1\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--without-analyzer" "--with-pgo"
Zend Extension Build => API320160303,TS,VC14

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.