• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

68publishers / tracy-git-version / 5982437370

26 Aug 2023 03:00AM UTC coverage: 90.041% (-2.2%) from 92.276%
5982437370

Pull #4

github

web-flow
Merge 60e13f581 into fa82db0dd
Pull Request #4: Feature/git executable

465 of 465 new or added lines in 32 files covered. (100.0%)

434 of 482 relevant lines covered (90.04%)

0.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.93
/src/Bridge/Symfony/Console/Command/ExportRepositoryCommand.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace SixtyEightPublishers\TracyGitVersion\Bridge\Symfony\Console\Command;
6

7
use JsonException;
8
use RuntimeException;
9
use SixtyEightPublishers\TracyGitVersion\Exception\ExportConfigException;
10
use SixtyEightPublishers\TracyGitVersion\Export\Config;
11
use SixtyEightPublishers\TracyGitVersion\Export\LocalDirectoryExporter;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Logger\ConsoleLogger;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use function assert;
18
use function chmod;
19
use function dirname;
20
use function file_exists;
21
use function file_put_contents;
22
use function is_dir;
23
use function is_string;
24
use function json_encode;
25
use function mkdir;
26
use function sprintf;
27

28
final class ExportRepositoryCommand extends Command
29
{
30
    protected static $defaultName = 'export-repository';
31

32
    protected function configure(): void
33
    {
34
        $this->setDescription('Clones important things from a git repository to the output directory.')
1✔
35
            ->addOption('config', null, InputOption::VALUE_REQUIRED, 'The path to a config file.')
1✔
36
            ->addOption('output-file', null, InputOption::VALUE_REQUIRED, 'The filename of the output file.')
1✔
37
            ->addOption('dump-only', null, InputOption::VALUE_NONE, 'Dumps exported file into the console only.');
1✔
38
    }
1✔
39

40
    /**
41
     * @throws JsonException
42
     * @throws ExportConfigException
43
     */
44
    protected function execute(InputInterface $input, OutputInterface $output): int
45
    {
46
        $logger = new ConsoleLogger($output);
1✔
47
        $outputFile = $input->getOption('output-file');
1✔
48
        $dumpOnly = $input->getOption('dump-only');
1✔
49

50
        $configPath = $input->getOption('config');
1✔
51
        assert(null === $configPath || is_string($configPath));
52
        $config = $this->getConfig($configPath);
1✔
53

54
        if (is_string($outputFile)) {
1✔
55
            $config->setOutputFile($outputFile);
1✔
56
        }
57

58
        $export = (new LocalDirectoryExporter())->export($config, null);
1✔
59

60
        $pretty = $dumpOnly ? JSON_PRETTY_PRINT : 0;
1✔
61
        $json = json_encode($export, JSON_THROW_ON_ERROR | $pretty);
1✔
62

63
        if ($dumpOnly) {
1✔
64
            $output->writeln($json);
1✔
65

66
            return 0;
1✔
67
        }
68

69
        $outputFile = $config->getOption(Config::OPTION_OUTPUT_FILE);
1✔
70
        assert(is_string($outputFile));
71

72
        $this->writeExport($json, $outputFile);
1✔
73

74
        $logger->info(sprintf(
1✔
75
            'Repository has been successfully exported into %s.',
76
            $outputFile,
1✔
77
        ));
78

79
        return 0;
1✔
80
    }
81

82
    /**
83
     * @throws ExportConfigException
84
     */
85
    private function getConfig(?string $configFile): Config
86
    {
87
        return null !== $configFile ? $this->loadConfig($configFile) : Config::createDefault();
1✔
88
    }
89

90
    /**
91
     * @throws ExportConfigException
92
     */
93
    private function loadConfig(string $configFile): Config
94
    {
95
        if (!file_exists($configFile)) {
1✔
96
            throw ExportConfigException::configFileNotFound($configFile);
1✔
97
        }
98

99
        $config = include $configFile;
1✔
100

101
        if (!$config instanceof Config) {
1✔
102
            throw ExportConfigException::configCantBeLoadedFromFile($configFile);
×
103
        }
104

105
        return $config;
1✔
106
    }
107

108
    /**
109
     * @throws RuntimeException
110
     */
111
    private function writeExport(string $export, string $outputFile): void
112
    {
113
        $dir = dirname($outputFile);
1✔
114

115
        if (!is_dir($dir) && !@mkdir($dir, 0777, true) && !is_dir($dir)) {
1✔
116
            throw new RuntimeException(sprintf(
×
117
                'Unable to create directory %s',
118
                $dir,
×
119
            ));
120
        }
121

122
        if (false === @file_put_contents($outputFile, $export)) {
1✔
123
            throw new RuntimeException(sprintf(
×
124
                'Unable to write config into file %s',
125
                $outputFile,
×
126
            ));
127
        }
128

129
        if (false === @chmod($outputFile, 0666)) {
1✔
130
            throw new RuntimeException(sprintf(
×
131
                'Unable to chmod config file %s with mode 0666.',
132
                $outputFile,
×
133
            ));
134
        }
135
    }
1✔
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc