2
2
3
3
namespace SymfonyDocsBuilder \Release ;
4
4
5
- use Symfony \Component \HttpClient \Exception \ClientException ;
6
- use Symfony \Component \Process \Exception \ProcessFailedException ;
7
- use Symfony \Component \Process \Process ;
8
5
use Symfony \Contracts \HttpClient \HttpClientInterface ;
6
+ use SymfonyDocsBuilder \Phar \Compiler ;
9
7
10
8
class Releaser
11
9
{
@@ -15,34 +13,27 @@ class Releaser
15
13
16
14
/** @var HttpClientInterface */
17
15
private $ client ;
16
+ private $ compiler ;
18
17
19
- public function __construct (GithubApiHttpClientFactory $ githubApiHttpClientFactory )
18
+ public function __construct (HttpClientInterface $ client , Compiler $ compiler )
20
19
{
21
- $ this ->client = $ githubApiHttpClientFactory ->createHttpClient ();
20
+ $ this ->client = $ client ;
21
+ $ this ->compiler = $ compiler ;
22
22
}
23
23
24
- public function doRelease (string $ tag , string $ name = 'Symfony docs builder %s ' , string $ description = 'Symfony docs builder %s ' )
24
+ public function createRelease (string $ tag , string $ name = 'Symfony docs builder %s ' , string $ description = 'Symfony docs builder %s ' )
25
25
{
26
26
if (!preg_match ('/^v\d+\.\d+\.\d+$/ ' , $ tag )) {
27
27
throw new \RuntimeException (sprintf ('"%s" is not a valid tag. ' , $ tag ));
28
28
}
29
29
30
- $ this ->compilePhar ();
30
+ $ this ->compiler -> compile ();
31
31
32
32
$ this ->addAssetToRelease ($ releaseId = $ this ->createDraftRelease ($ tag , $ name , $ description ));
33
33
34
34
$ this ->publishRelease ($ releaseId );
35
35
}
36
36
37
- /**
38
- * @throws ProcessFailedException
39
- */
40
- private function compilePhar (): void
41
- {
42
- $ process = Process::fromShellCommandline ('./bin/compile ' , __DIR__ .'/../.. ' );
43
- $ process ->mustRun ();
44
- }
45
-
46
37
private function createDraftRelease (string $ tag , string $ name , string $ description ): int
47
38
{
48
39
try {
@@ -62,14 +53,15 @@ private function createDraftRelease(string $tag, string $name, string $descripti
62
53
);
63
54
64
55
return (int ) $ response ->toArray ()['id ' ];
65
- } catch (ClientException $ exception ) {
56
+ } catch (\ RuntimeException $ exception ) {
66
57
if (401 === $ exception ->getCode ()) {
67
- $ message = 'Invalid token ' ;
58
+ $ message = 'Error while trying to create release: Invalid token. ' ;
68
59
} else {
69
- $ message = 'Maybe the tag name already exists? ' ;
60
+ $ message = 'Error while trying to create release. ' ;
70
61
}
71
62
72
- throw new \RuntimeException (sprintf ('Error while trying to create release: %s. ' , $ message ), 0 , $ exception );
63
+ // todo: create new exception which can be exploited in ./bin/create_release
64
+ throw new \RuntimeException ($ message , 0 , $ exception );
73
65
}
74
66
}
75
67
@@ -89,7 +81,7 @@ private function addAssetToRelease(int $releaseId): void
89
81
'body ' => file_get_contents (__DIR__ .'/../../docs.phar ' ),
90
82
]
91
83
);
92
- } catch (ClientException $ exception ) {
84
+ } catch (\ RuntimeException $ exception ) {
93
85
$ this ->deleteRelease ($ releaseId );
94
86
throw new \RuntimeException ('Error while adding asset to release. ' , 0 , $ exception );
95
87
}
@@ -107,9 +99,9 @@ private function publishRelease(int $releaseId): void
107
99
],
108
100
]
109
101
);
110
- } catch (ClientException $ exception ) {
102
+ } catch (\ RuntimeException $ exception ) {
111
103
$ this ->deleteRelease ($ releaseId );
112
- throw new \RuntimeException ('Error while publishing release. ' , 0 , $ exception );
104
+ throw new \RuntimeException ('Error while publishing release. Maybe the tag name already exists? ' , 0 , $ exception );
113
105
}
114
106
}
115
107
@@ -120,7 +112,7 @@ private function deleteRelease(int $releaseId): void
120
112
'DELETE ' ,
121
113
sprintf ('https://api.github.com/repos/%s/%s/releases/%s ' , self ::GITHUB_USER , self ::GITHUB_REPO , $ releaseId )
122
114
);
123
- } catch (ClientException $ exception ) {
115
+ } catch (\ RuntimeException $ exception ) {
124
116
throw new \RuntimeException ('Error while deleting release. ' , 0 , $ exception );
125
117
}
126
118
}
0 commit comments