|
21 | 21 | use Symfony\Component\Filesystem\Filesystem; |
22 | 22 | use Symfony\Component\Filesystem\Path; |
23 | 23 |
|
24 | | -use function Safe\file_get_contents; |
25 | 24 | use function rtrim; |
26 | 25 | use function str_contains; |
27 | 26 | use function str_replace; |
|
34 | 33 | final readonly class Bootstrapper implements BootstrapperInterface |
35 | 34 | { |
36 | 35 | /** |
37 | | - * @param Filesystem|null $filesystem |
38 | | - * @param HistoryGeneratorInterface|null $historyGenerator |
39 | | - * @param KeepAChangelogConfigRenderer|null $configRenderer |
| 36 | + * Initializes the `Bootstrapper` with optional dependencies. |
| 37 | + * |
| 38 | + * @param Filesystem $filesystem filesystem instance for file operations, allowing for easier testing and potential customization |
| 39 | + * @param HistoryGeneratorInterface $historyGenerator history generator instance for generating changelog history |
| 40 | + * @param KeepAChangelogConfigRenderer $configRenderer config renderer instance for rendering keep-a-changelog configuration |
40 | 41 | */ |
41 | 42 | public function __construct( |
42 | | - private ?Filesystem $filesystem = null, |
43 | | - private ?HistoryGeneratorInterface $historyGenerator = null, |
44 | | - private ?KeepAChangelogConfigRenderer $configRenderer = null, |
| 43 | + private Filesystem $filesystem = new Filesystem(), |
| 44 | + private HistoryGeneratorInterface $historyGenerator = new HistoryGenerator(), |
| 45 | + private KeepAChangelogConfigRenderer $configRenderer = new KeepAChangelogConfigRenderer(), |
45 | 46 | ) {} |
46 | 47 |
|
47 | 48 | /** |
| 49 | + * Bootstraps changelog automation assets in the given working directory. |
| 50 | + * |
48 | 51 | * @param string $workingDirectory |
49 | 52 | * |
50 | 53 | * @return BootstrapResult |
51 | 54 | */ |
52 | 55 | public function bootstrap(string $workingDirectory): BootstrapResult |
53 | 56 | { |
54 | | - $filesystem = $this->filesystem ?? new Filesystem(); |
55 | 57 | $configPath = Path::join($workingDirectory, '.keep-a-changelog.ini'); |
56 | 58 | $changelogPath = Path::join($workingDirectory, 'CHANGELOG.md'); |
57 | 59 |
|
58 | 60 | $configCreated = false; |
59 | 61 | $changelogCreated = false; |
60 | 62 | $unreleasedCreated = false; |
61 | 63 |
|
62 | | - if (! $filesystem->exists($configPath)) { |
63 | | - $filesystem->dumpFile($configPath, ($this->configRenderer ?? new KeepAChangelogConfigRenderer())->render()); |
| 64 | + if (! $this->filesystem->exists($configPath)) { |
| 65 | + $this->filesystem->dumpFile($configPath, $this->configRenderer->render()); |
64 | 66 | $configCreated = true; |
65 | 67 | } |
66 | 68 |
|
67 | | - if (! $filesystem->exists($changelogPath)) { |
68 | | - $filesystem->dumpFile( |
69 | | - $changelogPath, |
70 | | - ($this->historyGenerator ?? new HistoryGenerator()) |
71 | | - ->generate($workingDirectory), |
72 | | - ); |
| 69 | + if (! $this->filesystem->exists($changelogPath)) { |
| 70 | + $this->filesystem->dumpFile($changelogPath, $this->historyGenerator->generate($workingDirectory)); |
73 | 71 | $changelogCreated = true; |
74 | 72 |
|
75 | 73 | return new BootstrapResult($configCreated, $changelogCreated, $unreleasedCreated); |
76 | 74 | } |
77 | 75 |
|
78 | | - $contents = file_get_contents($changelogPath); |
| 76 | + $contents = $this->filesystem->readFile($changelogPath); |
79 | 77 |
|
80 | 78 | if (! str_contains($contents, '## Unreleased - ')) { |
81 | | - $filesystem->dumpFile($changelogPath, $this->prependUnreleasedSection($contents)); |
| 79 | + $this->filesystem->dumpFile($changelogPath, $this->prependUnreleasedSection($contents)); |
82 | 80 | $unreleasedCreated = true; |
83 | 81 | } |
84 | 82 |
|
|
0 commit comments