1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\PHP7_Sculpin\Console\Command; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Input\InputOption; |
16
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
17
|
|
|
use Symplify\PHP7_Sculpin\Github\GihubPublishingProcess; |
18
|
|
|
use Throwable; |
19
|
|
|
|
20
|
|
|
final class PushToGithubCommand extends Command |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $outputDirectory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var GihubPublishingProcess |
29
|
|
|
*/ |
30
|
|
|
private $gihubPublishingProcess; |
31
|
|
|
|
32
|
|
|
public function __construct(string $outputDirectory, GihubPublishingProcess $gihubPublishingProcess) |
33
|
|
|
{ |
34
|
|
|
$this->outputDirectory = $outputDirectory; |
35
|
|
|
$this->gihubPublishingProcess = $gihubPublishingProcess; |
36
|
|
|
|
37
|
|
|
parent::__construct(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function configure() |
41
|
|
|
{ |
42
|
|
|
$this->setName('push-to-github'); |
43
|
|
|
$this->setDescription('Push generated site to Github pages.'); |
44
|
|
|
$this->addArgument( |
45
|
|
|
'repository-slug', |
46
|
|
|
InputArgument::REQUIRED, |
47
|
|
|
'Repository slug, e.g. "TomasVotruba/tomasvotruba.cz".' |
48
|
|
|
); |
49
|
|
|
$this->addOption('token', null, InputOption::VALUE_REQUIRED, 'Github token.'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
|
|
try { |
55
|
|
|
$this->ensureTokenOptionIsSet((string) $input->getOption('token')); |
56
|
|
|
$this->ensureGithubRepositorySlugIsValid($input->getArgument('repository-slug')); |
57
|
|
|
|
58
|
|
|
$this->gihubPublishingProcess->setupTravisIdentityToGit(); |
59
|
|
|
|
60
|
|
|
$githubRepository = sprintf( |
61
|
|
|
'https://%[email protected]/%s.git', |
62
|
|
|
$input->getOption('token'), |
63
|
|
|
$input->getArgument('repository-slug') |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$this->gihubPublishingProcess->addAndPushContentToGithubRepository($this->outputDirectory, $githubRepository); |
67
|
|
|
|
68
|
|
|
$output->writeln('<info>Website was successfully pushed to Github pages.</info>'); |
69
|
|
|
|
70
|
|
|
return 0; |
71
|
|
|
} catch (Throwable $throwable) { |
72
|
|
|
$output->writeln( |
73
|
|
|
sprintf('<error>%s</error>', $throwable->getMessage()) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
return 1; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function ensureTokenOptionIsSet(string $token) |
81
|
|
|
{ |
82
|
|
|
if ($token === '') { |
83
|
|
|
throw new \Exception('Set token value via "--token=<GITHUB_TOKEN>" option.'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function ensureGithubRepositorySlugIsValid(string $repositorySlug) |
88
|
|
|
{ |
89
|
|
|
$repositoryUrl = 'https://github.com/'.$repositorySlug; |
90
|
|
|
if (!$this->doesUrlExist($repositoryUrl)) { |
91
|
|
|
throw new \Exception(sprintf( |
92
|
|
|
'Repository "%s" is not accessible. Try fixing the "%s" slug.', $repositoryUrl, $repositorySlug |
93
|
|
|
)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function doesUrlExist(string $url) : bool |
98
|
|
|
{ |
99
|
|
|
$fileHeaders = @get_headers($url); |
100
|
|
|
if (!$fileHeaders || $fileHeaders[0] === 'HTTP/1.1 404 Not Found') { |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|