|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace loophp\GrumphpLicenseTask\Task; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use GrumPHP\Fixer\FixResult; |
|
9
|
|
|
use GrumPHP\Runner\FixableTaskResult; |
|
10
|
|
|
use GrumPHP\Runner\TaskResult; |
|
11
|
|
|
use GrumPHP\Runner\TaskResultInterface; |
|
12
|
|
|
use GrumPHP\Task\Config\TaskConfigInterface; |
|
13
|
|
|
use GrumPHP\Task\Context\ContextInterface; |
|
14
|
|
|
use GrumPHP\Task\Context\GitPreCommitContext; |
|
15
|
|
|
use GrumPHP\Task\Context\RunContext; |
|
16
|
|
|
use GrumPHP\Task\TaskInterface; |
|
17
|
|
|
use GrumPHP\Util\Paths; |
|
18
|
|
|
use loophp\GrumphpLicenseTask\Service\LicenseManagerInterface; |
|
19
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
20
|
|
|
|
|
21
|
|
|
final class License implements TaskInterface |
|
22
|
|
|
{ |
|
23
|
|
|
private TaskConfigInterface $config; |
|
24
|
|
|
|
|
25
|
|
|
private LicenseManagerInterface $licenseManager; |
|
26
|
|
|
|
|
27
|
|
|
private Paths $paths; |
|
28
|
|
|
|
|
29
|
1 |
|
public function __construct(LicenseManagerInterface $licenseManager, Paths $paths) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$this->licenseManager = $licenseManager; |
|
32
|
1 |
|
$this->paths = $paths; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function canRunInContext(ContextInterface $context): bool |
|
36
|
|
|
{ |
|
37
|
|
|
return $context instanceof GitPreCommitContext || $context instanceof RunContext; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function getConfig(): TaskConfigInterface |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->config; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function getConfigurableOptions(): OptionsResolver |
|
46
|
|
|
{ |
|
47
|
|
|
$resolver = new OptionsResolver(); |
|
48
|
|
|
|
|
49
|
|
|
$resolver->setDefaults([ |
|
50
|
|
|
'name' => null, |
|
51
|
|
|
'input' => null, |
|
52
|
|
|
'output' => 'LICENSE.txt', |
|
53
|
|
|
'date_from' => null, |
|
54
|
|
|
'holder' => null, |
|
55
|
|
|
]); |
|
56
|
|
|
|
|
57
|
|
|
$resolver->addAllowedTypes('name', ['null', 'string']); |
|
58
|
|
|
$resolver->addAllowedTypes('input', ['null', 'string']); |
|
59
|
|
|
$resolver->addAllowedTypes('output', ['string']); |
|
60
|
|
|
$resolver->addAllowedTypes('date_from', ['null', 'int']); |
|
61
|
|
|
$resolver->addAllowedTypes('holder', ['null', 'string']); |
|
62
|
|
|
|
|
63
|
|
|
return $resolver; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function run(ContextInterface $context): TaskResultInterface |
|
67
|
|
|
{ |
|
68
|
|
|
$config = $this->getConfig()->getOptions(); |
|
69
|
|
|
|
|
70
|
|
|
if (null !== $config['name'] && null !== $config['input']) { |
|
71
|
|
|
throw new Exception('Both params cannot be set.'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (null === $config['name'] && null !== $config['input']) { |
|
75
|
|
|
$license = $this->licenseManager->getLicenseFromFile( |
|
76
|
|
|
$config['input'], |
|
77
|
|
|
$config['holder'], |
|
78
|
|
|
$config['date_from'] |
|
79
|
|
|
); |
|
80
|
|
|
} else { |
|
81
|
|
|
$license = $this->licenseManager->getLicenseFromName( |
|
82
|
|
|
$config['name'], |
|
83
|
|
|
$config['holder'], |
|
84
|
|
|
$config['date_from'] |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$cwd = $this->paths->getProjectDir(); |
|
89
|
|
|
|
|
90
|
|
|
$existing = file_exists(sprintf('%s/%s', $cwd, $config['output'])); |
|
91
|
|
|
|
|
92
|
|
|
if (true === $existing) { |
|
93
|
|
|
$content = file_get_contents(sprintf('%s/%s', $cwd, $config['output'])); |
|
94
|
|
|
|
|
95
|
|
|
if (false !== $content && (string) $license === $content) { |
|
96
|
|
|
return TaskResult::createPassed($this, $context); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (false === $existing) { |
|
101
|
|
|
$saveToFile = sprintf('%s/%s', $cwd, $config['output']); |
|
102
|
|
|
|
|
103
|
|
|
return new FixableTaskResult( |
|
104
|
|
|
TaskResult::createFailed($this, $context, 'The license file does not exist.'), |
|
105
|
|
|
static function () use ($license, $saveToFile): FixResult { |
|
106
|
|
|
$license |
|
107
|
|
|
->toFile($saveToFile) |
|
108
|
|
|
->save(); |
|
109
|
|
|
|
|
110
|
|
|
return FixResult::success(TaskResultInterface::PASSED); |
|
111
|
|
|
} |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return new FixableTaskResult( |
|
116
|
|
|
TaskResult::createFailed($this, $context, 'The license file exists and is not valid.'), |
|
117
|
|
|
static function () use ($license, $cwd, $config): FixResult { |
|
118
|
|
|
$license |
|
119
|
|
|
->toFile(sprintf('%s/%s', $cwd, $config['output'])) |
|
120
|
|
|
->save(); |
|
121
|
|
|
|
|
122
|
|
|
return FixResult::success(TaskResultInterface::PASSED); |
|
123
|
|
|
} |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function withConfig(TaskConfigInterface $config): TaskInterface |
|
128
|
|
|
{ |
|
129
|
|
|
$new = clone $this; |
|
130
|
|
|
$new->config = $config; |
|
131
|
|
|
|
|
132
|
|
|
return $new; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|