Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Installing coding standards when executing Composer with --no-scripts #4

Closed
Wirone opened this issue Jan 11, 2017 · 7 comments
Closed
Assignees

Comments

@Wirone
Copy link

Wirone commented Jan 11, 2017

I've successfully integrated your installer and frenck/php-compatibility in Symfony application and it works on my localhost. However, when I want to use Gitlab CI it fails, because:

  • we use incenteev/composer-parameter-handler for semi-automatic parameters configuration and few of them need to be set in order to work (Gitlab CI works in non-interactive mode and takes all parameters from dist file into final parameters file). Of course I can mock them in dist file, as a workaround
  • running with --no-scripts will not trigger parameters handler and other (e.g. cache clearing) but also will not trigger installing PHPCompatibility rule set, so running phpcs will fail due Referenced sniff "PHPCompatibility" does not exist error

Is there possibility to run some custom Composer command/script and just install PHPCS plugins, without calling other scripts?

Do you have any suggestions how it should be configured and handled?

@Potherca
Copy link
Member

Potherca commented Jan 11, 2017

Good to hear you have gotten things to work!

Thumbs Up

In regards to your question, I'm not entirely sure I understand your current situation.

Am I correct in assuming your composer.json looks something similar to the example below?

{
    "require-dev": {
        "squizlabs/php_codesniffer": "^2.7",
        "dealerdirect/phpcodesniffer-composer-installer": "^0.2.1",
        "frenck/php-compatibility": "^7.1"
    },
    "scripts": {
        "post-install-cmd": [
            "# Running incenteev/composer-parameter-handler ...",
            "# Clearing cache...",
            "phpcs src"
        ]
    }
}

You would want to run phpcs on post-install-cmd but, on Gitlab CI, not the other commands?

@Potherca
Copy link
Member

Potherca commented Jan 11, 2017

@frenck The most simple way I can think of is to expose the functionality of the plugin directly so it could be called as an (aliased) composer-script command. I've drafted a working example in PR #5.

The plugin could be triggered by the following composer.json:

{
    "...": {
        "...": "..."
    },
    "scripts": {
        "codesniffer": [
            "phpcs src"
        ],
        "post-install-cmd": [
            "# Running incenteev/composer-parameter-handler ...",
            "# Clearing cache...",
            "@codesniffer"
        ],
        "run-codesniffer": [
            "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run"
            "@codesniffer"
        ]
    }
}

That way, composer install --no-scripts could be run to install all of the dependencies without triggering any composer-scripts. After that, composer run-codesniffer could be run to trigger the plugin and run the code-sniff1.

Thoughts?

1 Although the sniffer could also be run as a separate command in the build, outside of the composer file.

@Wirone
Copy link
Author

Wirone commented Jan 11, 2017

We have standard scripts definition for Symfony project:

{
    "scripts": {
        "symfony-scripts": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-install-cmd": [
            "@symfony-scripts"
        ],
        "post-update-cmd": [
            "@symfony-scripts"
        ]
    }
}

And there is gitlab-ci.yml where under before_scripts section we have composer install (and other scripts). After composer install, when Incenteev\\ParameterHandler\\ScriptHandler::buildParameters is invoked (with non-interactive mode) all parameters from dist are saved to target config file, then Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache clears the cache and raises an error if required parameters are missing or don't have valid value.

Your solution is perfect, I thought about something like that - ability to run composer install --no-scripts and then run only installation of PHPCodeSniffer standards.

For now we have mocked some parameters and polished Gitlab CI configuration so everything is working fine, but this functionality would be great anyway :-)

@Potherca
Copy link
Member

Potherca commented Feb 5, 2017

This feature was discussed with @frenck and we're going to add it to the plugin.

The plan is to add unit-tests once PR's #6 and #8 have been merged, after that my PR will be updated and this feature will be implemented.

@Potherca
Copy link
Member

This has been fixed in PR #5, should be part of the upcoming release (see issue #11).

@Wirone
Copy link
Author

Wirone commented Feb 17, 2017

Thank you very much, it works as expected 👍

Just as a note, this feature can be used like this:

{
    "scripts": {
        "install-php-compatibility-sniff": [
            "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run"
        ],
        "post-install-cmd": [
            "@install-php-compatibility-sniff"
        ],
        "post-update-cmd": [
            "@install-php-compatibility-sniff"
        ]
    },
}

You can then use composer run-script install-php-compatibility-sniff at any moment.

Also in Gitlab CI's before_script you can install vendors with --no-scripts option and then install sniff in order to use PHPCS with PHPCompatibility.

@Potherca
Copy link
Member

Smiling Face

Thanks for the feedback. I'll be sure to update the documentation (including an example).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants