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