Skip to content

Commit

Permalink
Enhancement: Add MIT
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Jan 19, 2020
1 parent 17b867c commit a7f0b23
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ on:
- "master"

env:
MIN_COVERED_MSI: 93
MIN_MSI: 89
MIN_COVERED_MSI: 95
MIN_MSI: 91
REQUIRED_PHP_EXTENSIONS: "mbstring"

jobs:
Expand Down
30 changes: 9 additions & 21 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,19 @@ declare(strict_types=1);
use Ergebnis\License;
use Ergebnis\PhpCsFixer\Config;

$range = License\Range::since(
License\Year::fromString('2020'),
new \DateTimeZone('UTC')
);

$holder = License\Holder::fromString('Andreas Möller');

$file = License\File::create(
$license = License\Type\MIT::markdown(
__DIR__ . '/LICENSE.md',
License\Template::fromFile(__DIR__ . '/resource/license/MIT.md'),
$range,
$holder
);

$file->save();

$header = License\Header::create(
License\Template::fromFile(__DIR__ . '/resource/header.txt'),
$range,
$holder,
$file,
License\Range::since(
License\Year::fromString('2020'),
new \DateTimeZone('UTC')
),
License\Holder::fromString('Andreas Möller'),
License\Url::fromString('https://github.com/ergebnis/license')
);

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71($header->toString()));
$license->save();

$config = Config\Factory::fromRuleSet(new Config\RuleSet\Php71($license->header()));

$config->getFinder()
->ignoreDotFiles(false)
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ For a full diff see [`675601b...master`][675601b...master].
* Added `Template` ([#20]), by [@localheinz]
* Added `File` ([#33]), by [@localheinz]
* Added `Header` ([#34]), by [@localheinz]
* Added `MIT` ([#38]), by [@localheinz]

[675601b...master]: https://github.com/ergebnis/license/compare/675601b...master

Expand All @@ -27,5 +28,6 @@ For a full diff see [`675601b...master`][675601b...master].
[#20]: https://github.com/ergebnis/license/pull/20
[#33]: https://github.com/ergebnis/license/pull/33
[#34]: https://github.com/ergebnis/license/pull/34
[#38]: https://github.com/ergebnis/license/pull/38

[@localheinz]: https://github.com/localheinz
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MIN_COVERED_MSI:=93
MIN_MSI:=89
MIN_COVERED_MSI:=95
MIN_MSI:=91

.PHONY: it
it: coding-standards dependency-analysis static-code-analysis tests ## Runs the coding-standards, dependency-analysis, static-code-analysis, and tests targets
Expand Down
127 changes: 123 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,138 @@
[![Latest Stable Version](https://poser.pugx.org/ergebnis/license/v/stable)](https://packagist.org/packages/ergebnis/license)
[![Total Downloads](https://poser.pugx.org/ergebnis/license/downloads)](https://packagist.org/packages/ergebnis/license)

## Installation
Provides an abstraction of an open-source license.

:bulb: This is a great place for showing how to install the package, see below:
## Installation

Run

```
$ composer require ergebnis/license
$ composer require --dev ergebnis/license
```

## Usage

:bulb: This is a great place for showing a few usage examples!
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?

### Configuration for `friendsofphp/php-cs-fixer`

With [`friendsofphp/php-cs-fixer`](https://github.com/FriendsOfPHP/PHP-CS-Fixer) you can use the configuration file `.php_cs` to

* save the license to a file, e.g. `LICENSE` or `LICENSE.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` file:

```php
<?php

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()
->setRules([
'header_comment' => [
'comment_type' => 'PHPDoc',
'header' => $license->header(),
'location' => 'after_declare_strict',
'separate' => 'both',
],
])
->setFinder($finder);
```

:bulb: Also take a look at [`.php_cs`](.php_cs) of this project.

### GitHub Actions

When using [GitHub Actions](https://github.com/features/actions), you can set up a scheduled workflow that opens a pull request to the license year automatically on January 1st:

```yaml
name: "License"

on:
schedule:
- cron: "1 0 1 1 *"

jobs:
license:
name: "License"

runs-on: "ubuntu-latest"

steps:
- name: "Checkout"
uses: "actions/[email protected]"

- 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 --diff --diff-format=udiff --dry-run --verbose"

- name: "Open pull request updating license year"
uses: "gr2m/[email protected]"
with:
author: "Andreas Möller <[email protected]>"
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 }}"
```
:bulb: See [`crontab.guru`](https://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:

```diff
- name: "Open pull request updating license year"
uses: "gr2m/[email protected]"
with:
- author: "Andreas Möller <[email protected]>"
+ author: "ergebnis-bot <[email protected]>"
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 }}"
```
## Types

The following license types are currently available:

* [`Ergebnis\License\Type\MIT`](src/Type/MIT.php)

:bulb: Need a different license type? Feel free to open a pull request!

## Changelog

Expand Down
89 changes: 89 additions & 0 deletions src/Type/MIT.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

/**
* Copyright (c) 2020 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
*/

namespace Ergebnis\License\Type;

use Ergebnis\License\File;
use Ergebnis\License\Header;
use Ergebnis\License\Holder;
use Ergebnis\License\Period;
use Ergebnis\License\Template;
use Ergebnis\License\Url;

final class MIT
{
/**
* @var File
*/
private $file;

/**
* @var Header
*/
private $header;

private function __construct(string $name, Template $fileTemplate, Template $headerTemplate, Period $period, Holder $holder, Url $url)
{
$file = File::create(
$name,
$fileTemplate,
$period,
$holder
);

$header = Header::create(
$headerTemplate,
$period,
$holder,
$file,
$url
);

$this->file = $file;
$this->header = $header;
}

public static function markdown(string $name, Period $period, Holder $holder, Url $url): self
{
return new self(
$name,
Template::fromFile(__DIR__ . '/../../resource/license/MIT.md'),
Template::fromFile(__DIR__ . '/../../resource/header.txt'),
$period,
$holder,
$url
);
}

public static function text(string $name, Period $period, Holder $holder, Url $url): self
{
return new self(
$name,
Template::fromFile(__DIR__ . '/../../resource/license/MIT.txt'),
Template::fromFile(__DIR__ . '/../../resource/header.txt'),
$period,
$holder,
$url
);
}

public function save(): void
{
$this->file->save();
}

public function header(): string
{
return $this->header->toString();
}
}
Loading

0 comments on commit a7f0b23

Please sign in to comment.