Skip to content

Commit 34a334a

Browse files
committed
Adding some better root path determination and logging
1 parent 58f2f63 commit 34a334a

6 files changed

Lines changed: 87 additions & 62 deletions

File tree

bin/php-cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ if (PHP_SAPI !== 'cli') {
66

77
require __DIR__.'/../vendor/autoload.php';
88

9+
if (!(bool)getenv('PHPCS_PHAR')) {
10+
define('PHPCS_ROOT', realpath(__DIR__.'/../'));
11+
}
12+
913
use MyENA\CloudStackClientGenerator\Application;
1014
use Symfony\Component\Console\Formatter\OutputFormatter;
1115
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
@@ -14,13 +18,4 @@ use Symfony\Component\Console\Output\ConsoleOutput;
1418
error_reporting(-1);
1519

1620
$application = new Application('PHP CloudStack', '3.0.0');
17-
18-
// Shamelessly borrowed from Composer...
19-
$styles = [
20-
'highlight' => new OutputFormatterStyle('red'),
21-
'warning' => new OutputFormatterStyle('black', 'yellow'),
22-
];
23-
$formatter = new OutputFormatter(null, $styles);
24-
$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, null, $formatter);
25-
26-
$application->run(null);
21+
$application->run();

output/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/Command/AbstractCommand.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ protected function parseYAML(string $file, string $env = ''): bool {
228228

229229
foreach ($parsed as $k => $v) {
230230
if ('path' === substr($k, -4)) {
231-
$key = substr($k, 0, strlen($k) - 4).'Path';
232-
} else {
233-
$key = $k;
231+
$k = substr($k, 0, strlen($k) - 4).'Path';
232+
} else if ('out' === $k) {
233+
$k = 'outputDir';
234+
$v = $this->tryResolvePath($v);
234235
}
235-
$this->config->{'set'.ucfirst($key)}($v);
236+
$this->config->{'set'.ucfirst($k)}($v);
236237
}
237238

238239
return true;
@@ -249,14 +250,24 @@ protected function getClient(): Client {
249250
}
250251

251252
/**
253+
* Will attempt to detect and expand a relative path.
254+
*
255+
* // TODO: This is probably a bad idea and I should stop being lazy.
256+
*
252257
* @param string $in
253258
* @return string
254259
*/
255260
protected function tryResolvePath(string $in): string {
256261
if (0 === strpos($in, './')) {
257-
return __DIR__.'/../../'.substr($in, 2);
262+
if ($rp = realpath(PHPCS_ROOT.'/'.substr($in, 2))) {
263+
return $rp;
264+
}
265+
return PHPCS_ROOT.'/'.substr($in, 2);
258266
} else if (0 !== strpos($in, '/')) {
259-
return __DIR__.'/../../'.ltrim($in, "/");
267+
if ($rp = realpath(PHPCS_ROOT.'/'.ltrim($in, "/"))) {
268+
return $rp;
269+
}
270+
return PHPCS_ROOT.'/'.ltrim($in, "/");
260271
} else {
261272
return $in;
262273
}

src/Command/BuildCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ protected function execute(InputInterface $input, OutputInterface $output) {
200200
<<<PHP
201201
#!/usr/bin/env php
202202
<?php
203-
putenv('PHPCS_PHAR=1');
204203
Phar::mapPhar('php-cs.phar');
204+
putenv('PHPCS_PHAR=1');
205+
define('PHPCS_ROOT', Phar::running(false));
205206
require 'phar://php-cs.phar/bin/php-cs';
206207
__HALT_COMPILER(); ?>
207208
PHP

src/Command/GenerateClientCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ protected function execute(InputInterface $input, OutputInterface $output) {
5151
return 1;
5252
}
5353

54+
$this->log->info("Generating client against host \"{$this->config->getHost()}\"");
55+
5456
$generator = new Generator($this->config);
5557

5658
try {

src/Generator.php

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ public function __construct(Configuration $configuration) {
4949
$this->config = $configuration;
5050
$this->client = new Client($configuration);
5151

52-
$twigLoader = new \Twig_Loader_Filesystem(__DIR__ . '/../templates');
53-
$this->twig = new \Twig_Environment($twigLoader, ['debug' => true, 'strict_variables' => true, 'autoescape' => false]);
52+
$twigLoader = new \Twig_Loader_Filesystem(__DIR__.'/../templates');
53+
$this->twig =
54+
new \Twig_Environment($twigLoader, ['debug' => true, 'strict_variables' => true, 'autoescape' => false]);
5455
$this->twig->addExtension(new \Twig_Extensions_Extension_Text());
5556
$this->twig->addFilter(
5657
new \Twig_Filter(
5758
'ucfirst',
58-
function ($in) {
59+
function($in) {
5960
return ucfirst($in);
6061
},
6162
['is_safe' => ['html']]
@@ -83,7 +84,7 @@ function ($in) {
8384
}
8485

8586
$this->requestDir = sprintf('%s/CloudStackRequest', $this->srcDir);
86-
if (!is_dir($this->requestDir)&& !mkdir($this->requestDir)) {
87+
if (!is_dir($this->requestDir) && !mkdir($this->requestDir)) {
8788
throw new \RuntimeException(sprintf('Unable to create directory "%s"', $this->requestDir));
8889
}
8990
}
@@ -92,88 +93,106 @@ function ($in) {
9293
* Execute generation of CloudStack API client
9394
*/
9495
public function generate() {
96+
$log = $this->config->getLogger();
97+
98+
$log->info("Compiling APIs from {$this->config->getHost()}...");
9599
$this->compileAPIs();
96100
ksort($this->apis, SORT_NATURAL);
97101

102+
$log->info('Writing static templates...');
98103
$this->writeOutStaticTemplates();
104+
$log->info('Writing Client class...');
99105
$this->writeOutClient();
100106

107+
$log->info('Writing Request Models...');
101108
$this->writeOutRequestModels();
109+
$log->info('Writing Shared Response Models...');
102110
$this->writeOutSharedResponseModels();
111+
$log->info('Writing Response Models...');
103112
$this->writeOutResponseModels();
104113
}
105114

115+
/**
116+
* @param string $file
117+
* @param string $data
118+
* @return bool|int
119+
*/
120+
protected function writeFile(string $file, string $data) {
121+
$this->config->getLogger()->debug('Writing '.mb_strlen($data).' bytes to '.$file);
122+
return file_put_contents($file, $data);
123+
}
124+
106125
protected function writeOutStaticTemplates() {
107126
$args = ['config' => $this->config, 'capabilities' => $this->getCapabilities()];
108127

109-
file_put_contents(
110-
$this->config->getOutputDir() . '/LICENSE',
111-
file_get_contents(__DIR__ . '/../LICENSE')
128+
$this->writeFile(
129+
$this->config->getOutputDir().'/LICENSE',
130+
file_get_contents(__DIR__.'/../LICENSE')
112131
);
113132

114-
file_put_contents(
115-
$this->config->getOutputDir() . '/composer.json',
133+
$this->writeFile(
134+
$this->config->getOutputDir().'/composer.json',
116135
$this->twig->load('composer.json.twig')->render($args)
117136
);
118137

119-
file_put_contents(
120-
$this->srcDir . '/CloudStackConfiguration.php',
138+
$this->writeFile(
139+
$this->srcDir.'/CloudStackConfiguration.php',
121140
$this->twig->load('configuration.php.twig')->render($args)
122141
);
123142

124-
file_put_contents(
125-
$this->filesDir . '/constants.php',
143+
$this->writeFile(
144+
$this->filesDir.'/constants.php',
126145
$this->twig->load('constants.php.twig')->render($args)
127146
);
128147

129-
file_put_contents(
148+
$this->writeFile(
130149
$this->srcDir.'/CloudStackEventTypes.php',
131150
$this->twig->load('eventTypes.php.twig')->render($args)
132151
);
133152

134-
file_put_contents(
135-
$this->responseDir . '/AsyncJobStartResponse.php',
153+
$this->writeFile(
154+
$this->responseDir.'/AsyncJobStartResponse.php',
136155
$this->twig->load('responses/asyncJobStart.php.twig')->render($args)
137156
);
138157

139-
file_put_contents(
140-
$this->responseDir . '/AccessVmConsoleProxyResponse.php',
158+
$this->writeFile(
159+
$this->responseDir.'/AccessVmConsoleProxyResponse.php',
141160
$this->twig->load('responses/accessVmConsoleProxy.php.twig')->render($args)
142161
);
143162

144-
file_put_contents(
145-
$this->responseTypesDir . '/DateType.php',
163+
$this->writeFile(
164+
$this->responseTypesDir.'/DateType.php',
146165
$this->twig->load('responses/dateType.php.twig')->render($args)
147166
);
148167

149-
file_put_contents(
150-
$this->srcDir . '/CloudStackHelpers.php',
168+
$this->writeFile(
169+
$this->srcDir.'/CloudStackHelpers.php',
151170
$this->twig->load('helpers.php.twig')->render($args)
152171
);
153172

154-
file_put_contents(
173+
$this->writeFile(
155174
$this->requestDir.'/CloudStackRequestInterfaces.php',
156175
$this->twig->load('requests/interfaces.php.twig')->render($args)
157176
);
158177

159-
file_put_contents(
178+
$this->writeFile(
160179
$this->requestDir.'/AccessVmConsoleProxyRequest.php',
161180
$this->twig->load('requests/accessVmConsoleProxy.php.twig')->render($args)
162181
);
163182

164-
file_put_contents(
183+
$this->writeFile(
165184
$this->srcDir.'/CloudStackExceptions.php',
166185
$this->twig->load('exceptions.php.twig')->render($args)
167186
);
168187
}
169188

170189
protected function writeOutClient() {
171-
file_put_contents(
172-
$this->srcDir . '/CloudStackClient.php',
190+
$this->writeFile(
191+
$this->srcDir.'/CloudStackClient.php',
173192
$this->twig->load('client.php.twig')->render([
174-
'config' => $this->config,
193+
'config' => $this->config,
175194
'capabilities' => $this->getCapabilities(),
176-
'apis' => $this->apis,
195+
'apis' => $this->apis,
177196
])
178197
);
179198
}
@@ -182,13 +201,13 @@ protected function writeOutRequestModels() {
182201
$capabilities = $this->getCapabilities();
183202
$template = $this->twig->load('requests/model.php.twig');
184203

185-
foreach($this->apis as $api) {
204+
foreach ($this->apis as $api) {
186205
$className = $api->getRequestClassName();
187-
file_put_contents(
188-
$this->requestDir . '/' .$className . '.php',
206+
$this->writeFile(
207+
$this->requestDir.'/'.$className.'.php',
189208
$template->render([
190-
'api' => $api,
191-
'config' => $this->config,
209+
'api' => $api,
210+
'config' => $this->config,
192211
'capabilities' => $capabilities,
193212
])
194213
);
@@ -201,13 +220,12 @@ protected function writeOutSharedResponseModels() {
201220

202221
foreach ($this->sharedObjectMap as $name => $class) {
203222
$class->getProperties()->nameSort();
204-
205223
$className = $class->getClassName();
206-
file_put_contents(
207-
$this->responseDir . '/' . $className . '.php',
224+
$this->writeFile(
225+
$this->responseDir.'/'.$className.'.php',
208226
$template->render([
209-
'obj' => $class,
210-
'config' => $this->config,
227+
'obj' => $class,
228+
'config' => $this->config,
211229
'capabilities' => $capabilities,
212230
])
213231
);
@@ -222,11 +240,11 @@ protected function writeOutResponseModels() {
222240
$response = $api->getResponse();
223241
$className = $response->getClassName();
224242

225-
file_put_contents(
226-
$this->responseDir . '/' . $className . '.php',
243+
$this->writeFile(
244+
$this->responseDir.'/'.$className.'.php',
227245
$template->render([
228-
'obj' => $response,
229-
'config' => $this->config,
246+
'obj' => $response,
247+
'config' => $this->config,
230248
'capabilities' => $capabilities,
231249
])
232250
);

0 commit comments

Comments
 (0)