Skip to content

Commit 5fe964a

Browse files
committed
Updated YAML test build with gt/e, lt/e support
1 parent 14b3bcf commit 5fe964a

15 files changed

+190
-96
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ generator/*
2323
# Elasticsearch related
2424
util/elasticsearch/
2525
util/cache/
26+
util/*.zip
2627

2728
# Sami docs generator
2829
/sami.phar

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"require-dev": {
2222
"ext-yaml": "*",
23+
"ext-zip": "*",
2324
"cpliakas/git-wrapper": "~2.0",
2425
"doctrine/inflector": "^1.3",
2526
"mockery/mockery": "^1.2",

util/ClientEndpoint.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ class ClientEndpoint extends NamespaceEndpoint
3030
protected $endpointNames = [];
3131
protected $namespace = [];
3232
protected $version; /* Elasticsearch version used to generate the class */
33-
34-
public function __construct(array $namespace, string $version)
33+
protected $buildhash; /* Elasticsearch build hash used to generate the class */
34+
35+
public function __construct(array $namespace, string $version, string $buildhash)
3536
{
3637
$this->namespace = $namespace;
3738
$this->version = $version;
39+
$this->buildhash = $buildhash;
3840
}
3941

4042
public function renderClass(): string
@@ -101,7 +103,9 @@ public function renderClass(): string
101103
$functions .= $func;
102104
}
103105
$class = str_replace(':functions', $functions, $class);
106+
$class = str_replace(':version', $this->version, $class);
107+
$class = str_replace(':buildhash', $this->buildhash, $class);
104108

105-
return str_replace(':version', $this->version, $class);
109+
return $class;
106110
}
107111
}

util/Endpoint.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Endpoint
5454
public $apiName;
5555
protected $content;
5656
protected $version;
57+
protected $buildhash;
5758
protected $parts = [];
5859
protected $requiredParts = [];
5960
protected $useNamespace = [];
@@ -64,9 +65,14 @@ class Endpoint
6465
* @param $fileName name of the file with the API specification
6566
* @param $content content of the API specification in JSON
6667
* @param $version Elasticsearch version of the API specification
68+
* @param $buildhash Elasticsearch build hash of the API specification
6769
*/
68-
public function __construct(string $fileName, string $content, string $version)
69-
{
70+
public function __construct(
71+
string $fileName,
72+
string $content,
73+
string $version,
74+
string $buildhash
75+
) {
7076
$this->apiName = basename($fileName, '.json');
7177
$parts = explode('.', $this->apiName);
7278
if (count($parts) === 1) {
@@ -91,6 +97,7 @@ public function __construct(string $fileName, string $content, string $version)
9197
}
9298
$this->content = $this->content[$this->apiName];
9399
$this->version = $version;
100+
$this->buildhash = $buildhash;
94101

95102
$this->parts = $this->getPartsFromContent($this->content);
96103
$this->requiredParts = $this->getRequiredParts($this->content);
@@ -189,6 +196,7 @@ public function renderClass(): string
189196
$class = str_replace(':set-parts', $parts, $class);
190197
$class = str_replace(':endpoint', $this->getClassName(), $class);
191198
$class = str_replace(':version', $this->version, $class);
199+
$class = str_replace(':buildhash', $this->buildhash, $class);
192200
$class = str_replace(':use-namespace', $this->getNamespaces(), $class);
193201
$class = str_replace(':properties', $this->getProperties(), $class);
194202

util/GenerateEndpoints.php

Lines changed: 137 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,15 @@
1616
declare(strict_types = 1);
1717

1818
use GitWrapper\GitWrapper;
19+
use Elasticsearch\Client;
1920
use Elasticsearch\Util\ClientEndpoint;
2021
use Elasticsearch\Util\Endpoint;
2122
use Elasticsearch\Util\NamespaceEndpoint;
2223
use Elasticsearch\Tests\Utility;
2324

2425
require_once dirname(__DIR__) . '/vendor/autoload.php';
2526

26-
27-
28-
removeDirectory(__DIR__ . '/../src/Elasticsearch/Endpoints', [
29-
__DIR__ . '/../src/Elasticsearch/Endpoints/AbstractEndpoint.php',
30-
]);
31-
removeDirectory(__DIR__ . '/../src/Elasticsearch/Namespaces', [
32-
__DIR__ . '/../src/Elasticsearch/Namespaces/AbstractNamespace.php',
33-
__DIR__ . '/../src/Elasticsearch/Namespaces/BooleanRequestWrapper.php',
34-
__DIR__ . '/../src/Elasticsearch/Namespaces/NamespaceBuilderInterface.php'
35-
]);
36-
37-
die();
38-
printf ("Generating endpoints for Elasticsearch\n");
39-
printf ("---\n");
40-
41-
$start = microtime(true);
42-
4327
$client = Utility::getClient();
44-
4528
$serverInfo = $client->info();
4629
$version = $serverInfo['version']['number'];
4730
$buildHash = $serverInfo['version']['build_hash'];
@@ -51,6 +34,21 @@
5134
exit(1);
5235
}
5336

37+
$backupFileName = sprintf(
38+
"%s/backup_endpoint_namespace_%s.zip",
39+
__DIR__,
40+
Client::VERSION
41+
);
42+
43+
printf ("Backup Endpoints and Namespaces in /src\n");
44+
backup($backupFileName);
45+
46+
cleanFolders();
47+
48+
$start = microtime(true);
49+
printf ("Generating endpoints for Elasticsearch\n");
50+
51+
$success = true;
5452
$gitWrapper = new GitWrapper();
5553
$git = $gitWrapper->workingCopy(dirname(__DIR__) . '/util/elasticsearch');
5654

@@ -63,16 +61,9 @@
6361
]
6462
);
6563
$files = explode("\n", $result);
66-
$outputDir = __DIR__ . "/output/$version";
67-
68-
// Remove the output directory
69-
printf ("Removing %s folder\n", $outputDir);
70-
removeDirectory($outputDir);
71-
mkdir($outputDir);
64+
$outputDir = __DIR__ . "/../src/Elasticsearch/";
7265

7366
$endpointDir = "$outputDir/Endpoints/";
74-
printf ("Creating folder %s\n", $endpointDir);
75-
mkdir ($endpointDir);
7667

7768
$countEndpoint = 0;
7869
$namespaces = [];
@@ -82,119 +73,183 @@
8273
if (empty($file) || (basename($file) === '_common.json')) {
8374
continue;
8475
}
85-
printf("Generation %s...", basename($file));
76+
printf("Generating %s...", basename($file));
8677

87-
$endpoint = new Endpoint($file, $git->run('show', [':' . trim($file)]), $version);
78+
$endpoint = new Endpoint($file, $git->run('show', [':' . trim($file)]), $version, $buildHash);
8879

8980
$dir = $endpointDir . NamespaceEndpoint::normalizeName($endpoint->namespace);
9081
if (!file_exists($dir)) {
9182
mkdir($dir);
9283
}
84+
$outputFile = sprintf("%s/%s.php", $dir, $endpoint->getClassName());
9385
file_put_contents(
94-
sprintf("%s/%s.php", $dir, $endpoint->getClassName()),
86+
$outputFile,
9587
$endpoint->renderClass()
9688
);
89+
if (!isValidPhpSyntax($outputFile)) {
90+
printf("Error: syntax error in %s\n", $outputFile);
91+
$success = false;
92+
break;
93+
}
9794

9895
printf("done\n");
9996

10097
$namespaces[$endpoint->namespace][] = $endpoint;
10198
$countEndpoint++;
10299
}
103100

101+
if (!$success) {
102+
printf ("Roll back to the previous Endpoints and Namespace (ver. %s)\n", Client::VERSION);
103+
cleanFolders();
104+
restore($backupFileName);
105+
exit(1);
106+
}
107+
104108
// Generate namespaces
105109
$namespaceDir = "$outputDir/Namespaces/";
106-
printf ("Creating folder %s\n", $namespaceDir);
107-
mkdir($namespaceDir);
108-
109110
$countNamespace = 0;
110111
$clientFile = "$outputDir/Client.php";
111112

112113
foreach ($namespaces as $name => $endpoints) {
113114
if (empty($name)) {
114-
$clientEndpoint = new ClientEndpoint(array_keys($namespaces), $version);
115+
$clientEndpoint = new ClientEndpoint(array_keys($namespaces), $version, $buildHash);
115116
foreach ($endpoints as $ep) {
116117
$clientEndpoint->addEndpoint($ep);
117118
}
118119
file_put_contents(
119120
$clientFile,
120121
$clientEndpoint->renderClass()
121122
);
123+
if (!isValidPhpSyntax($clientFile)) {
124+
printf("Error: syntax error in %s\n", $clientFile);
125+
$success = false;
126+
break;
127+
}
122128
$countNamespace++;
123129
continue;
124130
}
125-
$namespace = new NamespaceEndpoint($name, $version);
131+
$namespace = new NamespaceEndpoint($name, $version, $buildHash);
126132
foreach ($endpoints as $ep) {
127133
$namespace->addEndpoint($ep);
128134
}
135+
$namespaceFile = $namespaceDir . $namespace->getNamespaceName() . 'Namespace.php';
129136
file_put_contents(
130-
$namespaceDir . $namespace->getNamespaceName() . 'Namespace.php',
137+
$namespaceFile,
131138
$namespace->renderClass()
132139
);
140+
if (!isValidPhpSyntax($namespaceFile)) {
141+
printf("Error: syntax error in %s\n", $namespaceFile);
142+
$success = false;
143+
break;
144+
}
133145
$countNamespace++;
134146
}
135147

136-
# Clean
137148
$end = microtime(true);
138-
printf("\nGenerated %d endpoints and %d namespaces in %.3f seconds\n.", $countEndpoint, $countNamespace, $end - $start);
139-
printf ("---\n");
140-
141-
printf("Backup Endpoints and Namespaces in /src");
142-
143-
$file = Client::Version;
144-
145-
$zip = new ZipArchive();
146-
$ret = $zip->open('application.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
147-
if ($ret !== TRUE) {
148-
printf('Failed with code %d', $ret);
149+
if ($success) {
150+
printf("\nGenerated %d endpoints and %d namespaces in %.3f seconds\n.", $countEndpoint, $countNamespace, $end - $start);
149151
} else {
150-
$options = array('add_path' => 'sources/', 'remove_all_path' => TRUE);
151-
$zip->addGlob('*.{php,txt}', GLOB_BRACE, $options);
152-
$zip->close();
153-
}
154-
// End
152+
printf ("Roll back to the previous Endpoints and Namespace (ver. %s)\n", Client::VERSION);
153+
cleanFolders();
154+
restore($backupFileName);
155+
}
155156

156-
function print_usage_msg(): void
157-
{
158-
printf("Usage: php %s <ES_VERSION>\n", basename(__FILE__));
159-
printf("where <ES_VERSION> is the Elasticsearch version to check (version must be >= 7.4.0).\n");
160-
}
157+
/**
158+
* FUNCTIONS
159+
*/
161160

162-
// Remove directory recursively
163-
function removeDirectory($directory, array $omit = [])
161+
/**
162+
* Remove a directory recursively
163+
*/
164+
function removeDirectory($directory, array $omit = [])
164165
{
165166
foreach(glob("{$directory}/*") as $file)
166167
{
167168
if(is_dir($file)) {
168169
if (!in_array($file, $omit)) {
169-
//removeDirectory($file);
170-
printf("Elimino directory %s\n", $file);
170+
removeDirectory($file, $omit);
171171
}
172172
} else {
173173
if (!in_array($file, $omit)) {
174-
//unlink($file);
175-
printf("Elimino file %s\n", $file);
174+
@unlink($file);
176175
}
177176
}
178177
}
179-
if (is_dir($directory) && empty($omit)) {
180-
//rmdir($directory);
181-
printf("Elimino directory %s\n", $directory);
178+
if (is_dir($directory)) {
179+
@rmdir($directory);
182180
}
183181
}
184182

185-
# Copy files and directory recursively
186-
function copyAll(string $src, string $dst) {
187-
$dir = opendir($src);
188-
@mkdir($dst);
189-
while(false !== ( $file = readdir($dir)) ) {
190-
if (( $file != '.' ) && ( $file != '..' )) {
191-
if ( is_dir($src . '/' . $file) ) {
192-
copyAll($src . '/' . $file, $dst . '/' . $file);
193-
}
194-
else {
195-
copy($src . '/' . $file, $dst . '/' . $file);
196-
}
197-
}
198-
}
199-
closedir($dir);
200-
}
183+
/**
184+
* Remove Endpoints, Namespaces and Client in src/Elasticsearch
185+
*/
186+
function cleanFolders()
187+
{
188+
removeDirectory(__DIR__ . '/../src/Elasticsearch/Endpoints', [
189+
__DIR__ . '/../src/Elasticsearch/Endpoints/AbstractEndpoint.php',
190+
]);
191+
removeDirectory(__DIR__ . '/../src/Elasticsearch/Namespaces', [
192+
__DIR__ . '/../src/Elasticsearch/Namespaces/AbstractNamespace.php',
193+
__DIR__ . '/../src/Elasticsearch/Namespaces/BooleanRequestWrapper.php',
194+
__DIR__ . '/../src/Elasticsearch/Namespaces/NamespaceBuilderInterface.php'
195+
]);
196+
@unlink(__DIR__ . '/../src/Elasticsearch/Client.php');
197+
}
198+
199+
/**
200+
* Backup Endpoints, Namespaces and Client in src/Elasticsearch
201+
*/
202+
function backup(string $fileName)
203+
{
204+
$zip = new ZipArchive();
205+
$result = $zip->open($fileName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
206+
if ($result !== true) {
207+
printf("Error opening the zip file %s: %s\n", $fileName, $result);
208+
exit(1);
209+
} else {
210+
$zip->addFile(__DIR__ . '/../src/Elasticsearch/Client.php', 'Client.php');
211+
$zip->addGlob(__DIR__ . '/../src/Elasticsearch/Namespaces/*.php', GLOB_BRACE, [
212+
'remove_path' => __DIR__ . '/../src/Elasticsearch'
213+
]);
214+
// Add the Endpoints (including subfolders)
215+
foreach(glob(__DIR__ . '/../src/Elasticsearch/Endpoints/*') as $file) {
216+
if (is_dir($file)) {
217+
$zip->addGlob("$file/*.php", GLOB_BRACE, [
218+
'remove_path' => __DIR__ . '/../src/Elasticsearch'
219+
]);
220+
} else {
221+
$zip->addGlob("$file", GLOB_BRACE, [
222+
'remove_path' => __DIR__ . '/../src/Elasticsearch'
223+
]);
224+
}
225+
}
226+
$zip->close();
227+
}
228+
}
229+
230+
/**
231+
* Restore Endpoints, Namespaces and Client in src/Elasticsearch
232+
*/
233+
function restore(string $fileName)
234+
{
235+
$zip = new ZipArchive();
236+
$result = $zip->open($fileName);
237+
if ($result !== true) {
238+
printf("Error opening the zip file %s: %s\n", $fileName, $result);
239+
exit(1);
240+
}
241+
$zip->extractTo(__DIR__ . '/../src/Elasticsearch');
242+
$zip->close();
243+
}
244+
245+
/**
246+
* Check if the generated code has a valid PHP syntax
247+
*/
248+
function isValidPhpSyntax(string $filename): bool
249+
{
250+
if (file_exists($filename)) {
251+
$result = exec("php -l $filename");
252+
return false !== strpos($result, "No syntax errors");
253+
}
254+
return false;
255+
}

0 commit comments

Comments
 (0)