1919use Elasticsearch \Util \ClientEndpoint ;
2020use Elasticsearch \Util \Endpoint ;
2121use Elasticsearch \Util \NamespaceEndpoint ;
22+ use Elasticsearch \Tests \Utility ;
2223
2324require_once dirname (__DIR__ ) . '/vendor/autoload.php ' ;
2425
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+
2541$ start = microtime (true );
26- if (!isset ($ argv [1 ])) {
27- print_usage_msg ();
28- exit (1 );
29- }
30- if ($ argv [1 ] < '7.3.0 ' ) {
31- printf ("Error: the version must be >= 7.4.0 \n" );
42+
43+ $ client = Utility::getClient ();
44+
45+ $ serverInfo = $ client ->info ();
46+ $ version = $ serverInfo ['version ' ]['number ' ];
47+ $ buildHash = $ serverInfo ['version ' ]['build_hash ' ];
48+
49+ if (version_compare ($ version , '7.4.0 ' , '< ' )) {
50+ printf ("Error: the ES version must be >= 7.4.0 \n" );
3251 exit (1 );
3352}
3453
35- $ ver = $ argv [1 ];
36- $ version = 'v ' . $ ver ;
37-
3854$ gitWrapper = new GitWrapper ();
3955$ git = $ gitWrapper ->workingCopy (dirname (__DIR__ ) . '/util/elasticsearch ' );
4056
41- $ git ->run ('fetch ' , ['--all ' ]);
42- $ tags = explode ("\n" , $ git ->run ('tag ' ));
43- if (!in_array ($ version , $ tags )) {
44- $ branches = explode ("\n" , $ git ->run ('branch ' , ['-r ' ]));
45- array_walk ($ branches , function (&$ value , &$ key ) {
46- $ value = trim ($ value );
47- });
48- $ version = "origin/ $ ver " ;
49- if (!in_array ($ version , $ branches )) {
50- printf ("Error: the version %s specified doesnot exist \n" , $ version );
51- exit (1 );
52- }
53- }
54-
55- $ git ->run ('checkout ' , [$ version ]);
57+ $ git ->run ('checkout ' , [$ buildHash ]);
5658$ result = $ git ->run (
5759 'ls-files ' ,
5860 [
6163 ]
6264);
6365$ files = explode ("\n" , $ result );
64- $ outputDir = __DIR__ . "/output/ $ ver " ;
66+ $ outputDir = __DIR__ . "/output/ $ version " ;
6567
6668// Remove the output directory
6769printf ("Removing %s folder \n" , $ outputDir );
8284 }
8385 printf ("Generation %s... " , basename ($ file ));
8486
85- $ endpoint = new Endpoint ($ file , $ git ->run ('show ' , [': ' . trim ($ file )]), $ ver );
87+ $ endpoint = new Endpoint ($ file , $ git ->run ('show ' , [': ' . trim ($ file )]), $ version );
8688
8789 $ dir = $ endpointDir . NamespaceEndpoint::normalizeName ($ endpoint ->namespace );
8890 if (!file_exists ($ dir )) {
109111
110112foreach ($ namespaces as $ name => $ endpoints ) {
111113 if (empty ($ name )) {
112- $ clientEndpoint = new ClientEndpoint (array_keys ($ namespaces ), $ ver );
114+ $ clientEndpoint = new ClientEndpoint (array_keys ($ namespaces ), $ version );
113115 foreach ($ endpoints as $ ep ) {
114116 $ clientEndpoint ->addEndpoint ($ ep );
115117 }
120122 $ countNamespace ++;
121123 continue ;
122124 }
123- $ namespace = new NamespaceEndpoint ($ name , $ ver );
125+ $ namespace = new NamespaceEndpoint ($ name , $ version );
124126 foreach ($ endpoints as $ ep ) {
125127 $ namespace ->addEndpoint ($ ep );
126128 }
131133 $ countNamespace ++;
132134}
133135
136+ # Clean
134137$ end = microtime (true );
135138printf ("\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 " );
136142
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+ } else {
150+ $ options = array ('add_path ' => 'sources/ ' , 'remove_all_path ' => TRUE );
151+ $ zip ->addGlob ('*.{php,txt} ' , GLOB_BRACE , $ options );
152+ $ zip ->close ();
153+ }
137154// End
138155
139156function print_usage_msg (): void
@@ -143,17 +160,41 @@ function print_usage_msg(): void
143160}
144161
145162// Remove directory recursively
146- function removeDirectory ($ directory )
163+ function removeDirectory ($ directory, array $ omit = [] )
147164{
148165 foreach (glob ("{$ directory }/* " ) as $ file )
149166 {
150167 if (is_dir ($ file )) {
151- removeDirectory ($ file );
168+ if (!in_array ($ file , $ omit )) {
169+ //removeDirectory($file);
170+ printf ("Elimino directory %s \n" , $ file );
171+ }
152172 } else {
153- unlink ($ file );
173+ if (!in_array ($ file , $ omit )) {
174+ //unlink($file);
175+ printf ("Elimino file %s \n" , $ file );
176+ }
154177 }
155178 }
156- if (is_dir ($ directory )) {
157- rmdir ($ directory );
179+ if (is_dir ($ directory ) && empty ($ omit )) {
180+ //rmdir($directory);
181+ printf ("Elimino directory %s \n" , $ directory );
158182 }
159- }
183+ }
184+
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+ }
0 commit comments