This project provides a composer
package with an abstraction of an open-source license.
Run
composer require --dev ergebnis/license
Sometimes open source maintainers complain about the burden of managing an open-source project. Sometimes they argue that contributors opening pull requests to update license years unnecessarily increase their workload.
Of course, all of this can be automated, can't it?
With friendsofphp/php-cs-fixer
you can use the configuration file .php-cs-fixer.php
to
- save the license to a file, e.g.
LICENSE
orLICENSE.md
- specify a file-level header using the
header_comment
fixer that will be replaced in PHP files
Here's an example of a .php-cs-fixer.php
file for an open-source project using the MIT
license type:
<?php
declare(strict_types=1);
/**
* Copyright (c) 2020-2022 Andreas Möller
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/ergebnis/license
*/
use Ergebnis\License;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$license = License\Type\MIT::text(
__DIR__ . '/LICENSE',
License\Range::since(
License\Year::fromString('2020'),
new DateTimeZone('UTC')
),
License\Holder::fromString('Andreas Möller'),
License\Url::fromString('https://github.com/ergebnis/license')
);
$license->save();
$finder = Finder::create()->in(__DIR__);
return Config::create()
->setFinder($finder)
->setRules([
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $license->header(),
'location' => 'after_declare_strict',
'separate' => 'both',
],
]);
💡 Also take a look at .php-cs-fixer.php
of this project.
Here's an example of a .php-cs-fixer.php
file for a closed-source project using the None
license type:
<?php
declare(strict_types=1);
/**
* Copyright (c) 2011-2019 Andreas Möller
*
* @see https://github.com/localheinz/localheinz.com
*/
use Ergebnis\License;
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$license = License\Type\None::text(
License\Range::since(
License\Year::fromString('2020'),
new DateTimeZone('UTC')
),
License\Holder::fromString('Andreas Möller'),
License\Url::fromString('https://github.com/localheinz/localheinz.com')
);
$finder = Finder::create()->in(__DIR__);
return Config::create()
->setFinder($finder)
->setRules([
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $license->header(),
'location' => 'after_declare_strict',
'separate' => 'both',
],
]);
When using GitHub Actions, you can set up a scheduled workflow that opens a pull request to the license year automatically on January 1st:
name: "License"
on:
schedule:
- cron: "1 0 1 1 *"
jobs:
license:
name: "License"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout"
uses: "actions/checkout@v2.0.0"
- name: "Install dependencies with composer"
run: "composer install --no-interaction --no-progress --no-suggest"
- name: "Run friendsofphp/php-cs-fixer"
run: "vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php --diff --dry-run --verbose"
- name: "Open pull request updating license year"
uses: "gr2m/create-or-update-pull-request-action@v1.2.9"
with:
author: "Andreas Möller <am@localheinz.com>"
branch: "feature/license-year"
body: |
This PR
- [x] updates the license year
commit-message: "Enhancement: Update license year"
path: "."
title: "Enhancement: Update license year"
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
💡 See crontab.guru
if you need help scheduling the workflow.
Note that pull requests opened or commits pushed by GitHub Actions will not trigger a build. As an alternative, you can set up a bot user:
- name: "Open pull request updating license year"
uses: "gr2m/create-or-update-pull-request-action@v1.2.9"
with:
- author: "Andreas Möller <am@localheinz.com>"
+ author: "ergebnis-bot <bot@ergebn.is>"
branch: "feature/license-year"
body: |
This PR
- [x] updates the license year
commit-message: "Enhancement: Update license year"
path: "."
title: "Enhancement: Update license year"
env:
- GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
+ GITHUB_TOKEN: "${{ secrets.ERGEBNIS_BOT_TOKEN }}"
The following license types are currently available:
💡 Need a different license type? Feel free to open a pull request!
The maintainers of this project record notable changes to this project in a changelog.
The maintainers of this project suggest following the contribution guide.
The maintainers of this project ask contributors to follow the code of conduct.
The maintainers of this project provide limited support.
You can support the maintenance of this project by sponsoring @localheinz or requesting an invoice for services related to this project.
This project supports PHP versions with active and security support.
The maintainers of this project add support for a PHP version following its initial release and drop support for a PHP version when it has reached the end of security support.
This project has a security policy.
This project uses the MIT license.
Follow @localheinz and @ergebnis on Twitter.