2
2
3
3
namespace SymfonyDocsBuilder \Command ;
4
4
5
+ use Doctrine \Common \EventManager ;
6
+ use Doctrine \RST \Builder ;
5
7
use Doctrine \RST \Event \PostBuildRenderEvent ;
6
- use Doctrine \RST \Meta \CachedMetasLoader ;
7
8
use Doctrine \RST \Meta \Metas ;
8
9
use Symfony \Component \Console \Command \Command ;
9
- use Symfony \Component \Console \Helper \ProgressBar ;
10
10
use Symfony \Component \Console \Input \InputArgument ;
11
11
use Symfony \Component \Console \Input \InputInterface ;
12
12
use Symfony \Component \Console \Input \InputOption ;
13
13
use Symfony \Component \Console \Output \OutputInterface ;
14
+ use Symfony \Component \Console \Style \SymfonyStyle ;
14
15
use Symfony \Component \Filesystem \Filesystem ;
15
- use Symfony \Component \Finder \Finder ;
16
16
use SymfonyDocsBuilder \BuildContext ;
17
17
use SymfonyDocsBuilder \CI \MissingFilesChecker ;
18
18
use SymfonyDocsBuilder \Generator \HtmlForPdfGenerator ;
19
19
use SymfonyDocsBuilder \Generator \JsonGenerator ;
20
+ use SymfonyDocsBuilder \KernelFactory ;
21
+ use SymfonyDocsBuilder \Listener \BuildProgressListener ;
20
22
use SymfonyDocsBuilder \Listener \CopyImagesDirectoryListener ;
21
23
22
24
class BuildDocsCommand extends Command
23
25
{
24
- use CommandInitializerTrait;
25
-
26
26
protected static $ defaultName = 'build:docs ' ;
27
27
28
+ private $ buildContext ;
28
29
private $ missingFilesChecker ;
30
+ /** @var SymfonyStyle */
31
+ private $ io ;
29
32
30
33
public function __construct (BuildContext $ buildContext )
31
34
{
32
35
parent ::__construct (self ::$ defaultName );
33
36
34
- $ this ->filesystem = new Filesystem ();
35
- $ this ->finder = new Finder ();
36
37
$ this ->buildContext = $ buildContext ;
37
-
38
38
$ this ->missingFilesChecker = new MissingFilesChecker ($ buildContext );
39
39
}
40
40
@@ -75,26 +75,50 @@ protected function configure()
75
75
76
76
protected function initialize (InputInterface $ input , OutputInterface $ output )
77
77
{
78
- if ($ input ->getOption ('parse-sub-path ' ) && $ input ->getOption ('output-json ' )) {
79
- throw new \InvalidArgumentException (sprintf ('Cannot pass both --parse-sub-path and --output-json options. ' ));
78
+ $ this ->io = new SymfonyStyle ($ input , $ output );
79
+
80
+ $ sourceDir = $ input ->getArgument ('source-dir ' );
81
+ if (!file_exists ($ sourceDir )) {
82
+ throw new \InvalidArgumentException (sprintf ('RST source directory "%s" does not exist ' , $ sourceDir ));
80
83
}
81
84
85
+ $ filesystem = new Filesystem ();
86
+ $ htmlOutputDir = $ input ->getArgument ('output-dir ' ) ?? $ sourceDir .'/html ' ;
87
+ if ($ input ->getOption ('disable-cache ' ) && $ filesystem ->exists ($ htmlOutputDir )) {
88
+ $ filesystem ->remove ($ htmlOutputDir );
89
+ }
82
90
83
- $ sourceDir = $ this ->initializeSourceDir ($ input , $ this ->filesystem );
84
- $ outputDir = $ input ->getArgument ('output-dir ' ) ?? $ sourceDir .'/html ' ;
91
+ $ parseSubPath = $ input ->getOption ('parse-sub-path ' );
92
+ if ($ parseSubPath && $ input ->getOption ('output-json ' )) {
93
+ throw new \InvalidArgumentException ('Cannot pass both --parse-sub-path and --output-json options. ' );
94
+ }
85
95
86
- $ this ->doInitialize ($ input , $ output , $ sourceDir , $ outputDir );
96
+ if (!file_exists ($ sourceDir .'/ ' .$ parseSubPath )) {
97
+ throw new \InvalidArgumentException (sprintf ('Given "parse-sub-path" directory "%s" does not exist ' , $ parseSubPath ));
98
+ }
87
99
88
- $ this ->builder ->getConfiguration ()->getEventManager ()->addEventListener (
89
- PostBuildRenderEvent::POST_BUILD_RENDER ,
90
- new CopyImagesDirectoryListener ($ this ->buildContext )
100
+ $ this ->buildContext ->initializeRuntimeConfig (
101
+ $ sourceDir ,
102
+ $ htmlOutputDir ,
103
+ $ parseSubPath ,
104
+ $ input ->getOption ('disable-cache ' )
91
105
);
92
106
}
93
107
94
108
protected function execute (InputInterface $ input , OutputInterface $ output )
95
109
{
96
- $ this ->startBuild ();
97
- $ buildErrors = $ this ->builder ->getErrorManager ()->getErrors ();
110
+ $ builder = new Builder (
111
+ KernelFactory::createKernel ($ this ->buildContext , $ this ->urlChecker ?? null )
112
+ );
113
+
114
+ $ this ->initializeListeners ($ builder ->getConfiguration ()->getEventManager ());
115
+
116
+ $ builder ->build (
117
+ $ this ->buildContext ->getSourceDir (),
118
+ $ this ->buildContext ->getOutputDir ()
119
+ );
120
+
121
+ $ buildErrors = $ builder ->getErrorManager ()->getErrors ();
98
122
99
123
$ this ->io ->success ('HTML rendering complete! ' );
100
124
@@ -113,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
113
137
file_put_contents ($ logPath , implode ("\n" , $ buildErrors ));
114
138
}
115
139
116
- $ metas = $ this ->getMetas ();
140
+ $ metas = $ builder ->getMetas ();
117
141
if ($ this ->buildContext ->getParseSubPath ()) {
118
142
$ this ->renderDocForPDF ($ metas );
119
143
} elseif ($ input ->getOption ('output-json ' )) {
@@ -129,11 +153,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
129
153
private function generateJson (Metas $ metas )
130
154
{
131
155
$ this ->io ->note ('Start exporting doc into json files ' );
132
- $ this ->progressBar = new ProgressBar ($ this ->output , $ this ->finder ->count ());
133
156
134
157
$ jsonGenerator = new JsonGenerator ($ metas , $ this ->buildContext );
135
158
$ jsonGenerator ->setOutput ($ this ->io );
136
- $ jsonGenerator ->generateJson ($ this ->progressBar );
137
159
}
138
160
139
161
private function renderDocForPDF (Metas $ metas )
@@ -142,15 +164,14 @@ private function renderDocForPDF(Metas $metas)
142
164
$ htmlForPdfGenerator ->generateHtmlForPdf ();
143
165
}
144
166
145
- public function preBuildRender ( )
167
+ private function initializeListeners ( EventManager $ eventManager )
146
168
{
147
- $ this -> doPreBuildRender ();
148
-
149
- $ this ->io -> note ( ' Start rendering in HTML... ' );
150
- }
169
+ $ eventManager -> addEventListener (
170
+ PostBuildRenderEvent:: POST_BUILD_RENDER ,
171
+ new CopyImagesDirectoryListener ( $ this ->buildContext )
172
+ );
151
173
152
- private function getMetas (): Metas
153
- {
154
- return $ this ->builder ->getMetas ();
174
+ $ progressListener = new BuildProgressListener ($ this ->io );
175
+ $ progressListener ->attachListeners ($ eventManager );
155
176
}
156
177
}
0 commit comments