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