Skip to content

Commit

Permalink
✨ first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Kanti committed Dec 13, 2022
1 parent 1095da4 commit ea0c0b3
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Tasks

on: [push, pull_request]

jobs:
lint-php:
name: "php: ${{ matrix.php }}"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [ '7.3', '7.4', '8.0', '8.1' ]
steps:
- name: Setup PHP with PECL extension
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
- uses: actions/checkout@v2
- uses: actions/cache@v2
with:
path: ~/.composer/cache/files
key: ${{ runner.os }}-${{ matrix.php }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-${{ matrix.php }}-composer-
- run: composer install --no-interaction --no-progress
- run: ./vendor/bin/grumphp run --ansi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
composer.lock
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# @no-ci composer plugin

## install

``composer req andersundsehr/no-ci``

## what dose it do

This plugin adds the possibility to add `@no-ci`/`@ci` to any composer script.
you can Add it in front of every possible composer script. [documentation](https://getcomposer.org/doc/articles/scripts.md#writing-custom-commands)

### Example composer.json
````json
{
"scripts": {
"test": [
"@no-ci @php vendor/bin/phpunit -c phpunit.xml",
"@ci @php vendor/bin/phpunit -c phpunit-ci.xml"
],
"other:examples": [
"@no-ci Composer\\Config::disableProcessTimeout",
"@no-ci @clearCache",
"@no-ci @composer install",
"@no-ci @php script.php",
"@no-ci @putenv COMPOSER=phpstan-composer.json",
"@no-ci ls -alh"
],
"clearCache": "rm -rf var"
}
}
````
47 changes: 47 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name" : "andersundsehr/no-ci",
"description" : "composer @no-ci addon",
"type" : "composer-plugin",
"license" : "GPL-2.0-or-later",
"autoload" : {
"psr-4" : {
"AUS\\NoCi\\" : "src/"
}
},
"authors" : [
{
"name" : "Matthias Vogel",
"email" : "m.vogel@andersundsehr.com"
}
],
"scripts" : {
"test": "@no-ci @php -v"
},
"require" : {
"composer-plugin-api" : "^2.0",
"ondram/ci-detector": "^4.1",
"php": "^7.3 || ^8.0"
},
"require-dev": {
"composer/composer": "^2.0",
"pluswerk/grumphp-config": "^5"
},
"extra": {
"class": "AUS\\NoCi\\Plugin",
"pluswerk/grumphp-config": {
"auto-setting": true
},
"grumphp": {
"config-default-path": "vendor/pluswerk/grumphp-config/grumphp.yml"
}
},
"config": {
"allow-plugins": {
"typo3/cms-composer-installers": true,
"typo3/class-alias-loader": true,
"phpro/grumphp": true,
"phpstan/extension-installer": true,
"pluswerk/grumphp-config": true
}
}
}
70 changes: 70 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace AUS\NoCi;

use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use OndraM\CiDetector\CiDetector;
use Composer\EventDispatcher\Event as BaseEvent;

class Plugin implements PluginInterface, EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'no-ci' => 'noCi',
'ci' => 'ci',
];
}

public function noCi(Event $event): void
{
if ($this->isCi()) {
$event->getIO()->write('<warning>CI detected, skipping: ' . implode(' ', $event->getArguments()) . '</warning>');
return;
}
$this->runOriginalScript($event);
}

public function ci(Event $event): void
{
if (!$this->isCi()) {
$event->getIO()->write('<warning>no CI detected, skipping: ' . implode(' ', $event->getArguments()) . '</warning>');
return;
}
$this->runOriginalScript($event);
}

private function runOriginalScript(Event $event): void
{
$eventName = 'run-@no-ci-' . implode('-', $event->getArguments());
$dispatcher = $event->getComposer()->getEventDispatcher();
$childEvent = new Event($eventName, $event->getComposer(), $event->getIO(), $event->isDevMode(), [], $event->getFlags());
if (!$dispatcher->hasEventListeners($childEvent)) {
$dispatcher->addListener($eventName, implode(' ', $event->getArguments()));
}
$dispatcher->dispatch(null, $childEvent);
}

public function activate(Composer $composer, IOInterface $io)
{
}

public function deactivate(Composer $composer, IOInterface $io)
{
}

public function uninstall(Composer $composer, IOInterface $io)
{
}

private function isCi(): bool
{
return (new CiDetector())->isCiDetected();
}
}

0 comments on commit ea0c0b3

Please sign in to comment.