Skip to content

Commit ad0518e

Browse files
Merge pull request #863 from appwrite/feat-cli-g2
Feat cli g2
2 parents 44764b6 + 2a10501 commit ad0518e

31 files changed

+4379
-1420
lines changed

composer.lock

Lines changed: 181 additions & 111 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function getSSLPage($url) {
4242
$platform = 'console';
4343
// $platform = 'server';
4444

45-
$spec = getSSLPage("https://raw.githubusercontent.com/appwrite/appwrite/1.5.x/app/config/specs/swagger2-latest-{$platform}.json");
45+
$spec = getSSLPage("https://raw.githubusercontent.com/appwrite/appwrite/1.6.x/app/config/specs/swagger2-latest-{$platform}.json");
4646

4747
if(empty($spec)) {
4848
throw new Exception('Failed to fetch spec from Appwrite server');
@@ -186,7 +186,7 @@ function getSSLPage($url) {
186186
->setTwitter('appwrite_io')
187187
->setDiscord('564160730845151244', 'https://appwrite.io/discord')
188188
->setDefaultHeaders([
189-
'X-Appwrite-Response-Format' => '0.15.0',
189+
'X-Appwrite-Response-Format' => '1.5.0',
190190
])
191191
;
192192

@@ -393,7 +393,7 @@ function getSSLPage($url) {
393393
;
394394

395395
$sdk->generate(__DIR__ . '/examples/apple');
396-
396+
397397
// DotNet
398398
$sdk = new SDK(new DotNet(), new Swagger2($spec));
399399

@@ -442,7 +442,7 @@ function getSSLPage($url) {
442442
// Android
443443

444444
$sdk = new SDK(new Android(), new Swagger2($spec));
445-
445+
446446
$sdk
447447
->setName('Android')
448448
->setNamespace('io appwrite')
@@ -466,7 +466,7 @@ function getSSLPage($url) {
466466

467467
// Kotlin
468468
$sdk = new SDK(new Kotlin(), new Swagger2($spec));
469-
469+
470470
$sdk
471471
->setName('Kotlin')
472472
->setNamespace('io appwrite')

src/SDK/Language.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public function getFilters(): array
8484
return [];
8585
}
8686

87+
/**
88+
* Language specific functions.
89+
* @return array
90+
*/
91+
public function getFunctions(): array
92+
{
93+
return [];
94+
}
95+
8796
protected function toPascalCase(string $value): string
8897
{
8998
return \ucfirst($this->toCamelCase($value));

src/SDK/Language/CLI.php

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,66 @@
22

33
namespace Appwrite\SDK\Language;
44

5+
use Twig\TwigFunction;
6+
57
class CLI extends Node
68
{
9+
/**
10+
* List of functions to ignore for console preview.
11+
* @var array
12+
*/
13+
private $consoleIgnoreFunctions = [
14+
'listidentities',
15+
'listmfafactors',
16+
'getprefs',
17+
'getsession',
18+
'getattribute',
19+
'listdocumentlogs',
20+
'getindex',
21+
'listcollectionlogs',
22+
'getcollectionusage',
23+
'listlogs',
24+
'listruntimes',
25+
'getusage',
26+
'getusage',
27+
'listvariables',
28+
'getvariable',
29+
'listproviderlogs',
30+
'listsubscriberlogs',
31+
'getsubscriber',
32+
'listtopiclogs',
33+
'getemailtemplate',
34+
'getsmstemplate',
35+
'getfiledownload',
36+
'getfilepreview',
37+
'getfileview',
38+
'getusage',
39+
'listlogs',
40+
'getprefs',
41+
'getusage',
42+
'listlogs',
43+
'getmembership',
44+
'listmemberships',
45+
'listmfafactors',
46+
'getmfarecoverycodes',
47+
'getprefs',
48+
'listtargets',
49+
'gettarget',
50+
];
51+
52+
/**
53+
* List of SDK services to ignore for console preview.
54+
* @var array
55+
*/
56+
private $consoleIgnoreServices = [
57+
'health',
58+
'migrations',
59+
'locale',
60+
'avatars',
61+
'project',
62+
'proxy',
63+
'vcs'
64+
];
765
/**
866
* @var array
967
*/
@@ -127,6 +185,16 @@ public function getFiles(): array
127185
'destination' => 'lib/questions.js',
128186
'template' => 'cli/lib/questions.js.twig',
129187
],
188+
[
189+
'scope' => 'default',
190+
'destination' => 'lib/validations.js',
191+
'template' => 'cli/lib/validations.js.twig',
192+
],
193+
[
194+
'scope' => 'default',
195+
'destination' => 'lib/spinner.js',
196+
'template' => 'cli/lib/spinner.js.twig',
197+
],
130198
[
131199
'scope' => 'default',
132200
'destination' => 'lib/parser.js',
@@ -152,6 +220,11 @@ public function getFiles(): array
152220
'destination' => 'lib/client.js',
153221
'template' => 'cli/lib/client.js.twig',
154222
],
223+
[
224+
'scope' => 'default',
225+
'destination' => 'lib/id.js',
226+
'template' => 'cli/lib/id.js.twig',
227+
],
155228
[
156229
'scope' => 'default',
157230
'destination' => 'lib/utils.js',
@@ -164,8 +237,28 @@ public function getFiles(): array
164237
],
165238
[
166239
'scope' => 'default',
167-
'destination' => 'lib/commands/deploy.js',
168-
'template' => 'cli/lib/commands/deploy.js.twig',
240+
'destination' => 'lib/commands/pull.js',
241+
'template' => 'cli/lib/commands/pull.js.twig',
242+
],
243+
[
244+
'scope' => 'default',
245+
'destination' => 'lib/commands/push.js',
246+
'template' => 'cli/lib/commands/push.js.twig',
247+
],
248+
[
249+
'scope' => 'default',
250+
'destination' => 'lib/commands/run.js',
251+
'template' => 'cli/lib/commands/run.js.twig',
252+
],
253+
[
254+
'scope' => 'default',
255+
'destination' => 'lib/emulation/docker.js',
256+
'template' => 'cli/lib/emulation/docker.js.twig',
257+
],
258+
[
259+
'scope' => 'default',
260+
'destination' => 'lib/emulation/utils.js',
261+
'template' => 'cli/lib/emulation/utils.js.twig',
169262
],
170263
[
171264
'scope' => 'service',
@@ -270,4 +363,18 @@ public function getParamExample(array $param): string
270363

271364
return $output;
272365
}
366+
367+
/**
368+
* Language specific filters.
369+
* @return array
370+
*/
371+
public function getFunctions(): array
372+
{
373+
return [
374+
/** Return true if the entered service->method is enabled for a console preview link */
375+
new TwigFunction('hasConsolePreview', fn($method, $service) => preg_match('/^([Gg]et|[Ll]ist)/', $method)
376+
&& !in_array(strtolower($method), $this->consoleIgnoreFunctions)
377+
&& !in_array($service, $this->consoleIgnoreServices)),
378+
];
379+
}
273380
}

src/SDK/SDK.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public function __construct(Language $language, Spec $spec)
8585
'debug' => true
8686
]);
8787

88+
/**
89+
* Add language-specific functions
90+
*/
91+
foreach ($this->language->getFunctions() as $function) {
92+
$this->twig->addFunction($function);
93+
}
94+
8895
/**
8996
* Add language specific filters
9097
*/

templates/cli/base/params.twig

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@
1111
if (!fs.lstatSync(folderPath).isDirectory()) {
1212
throw new Error('The path is not a directory.');
1313
}
14-
14+
1515
const ignorer = ignore();
1616

1717
const func = localConfig.getFunction(functionId);
1818

19+
ignorer.add('.appwrite');
20+
1921
if (func.ignore) {
2022
ignorer.add(func.ignore);
21-
log('Ignoring files using configuration from appwrite.json');
2223
} else if (fs.existsSync(pathLib.join({{ parameter.name | caseCamel | escapeKeyword }}, '.gitignore'))) {
2324
ignorer.add(fs.readFileSync(pathLib.join({{ parameter.name | caseCamel | escapeKeyword }}, '.gitignore')).toString());
24-
log('Ignoring files in .gitignore');
2525
}
26-
26+
2727
const files = getAllFiles({{ parameter.name | caseCamel | escapeKeyword }}).map((file) => pathLib.relative({{ parameter.name | caseCamel | escapeKeyword }}, file)).filter((file) => !ignorer.ignores(file));
2828

2929
await tar
@@ -77,8 +77,10 @@
7777
{% endif %}
7878
{% endfor %}
7979
{% if method.type == 'location' %}
80-
payload['project'] = localConfig.getProject().projectId
81-
payload['key'] = globalConfig.getKey();
82-
const queryParams = new URLSearchParams(payload);
83-
apiPath = `${globalConfig.getEndpoint()}${apiPath}?${queryParams.toString()}`;
84-
{% endif %}
80+
if (!overrideForCli) {
81+
payload['project'] = localConfig.getProject().projectId
82+
payload['key'] = globalConfig.getKey();
83+
const queryParams = new URLSearchParams(payload);
84+
apiPath = `${globalConfig.getEndpoint()}${apiPath}?${queryParams.toString()}`;
85+
}
86+
{% endif %}

templates/cli/base/requests/api.twig

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,27 @@
99
{% endfor %}
1010
}, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});
1111

12-
{% if method.type == 'location' %}
13-
fs.writeFileSync(destination, response);
12+
{%~ if method.type == 'location' %}
13+
if (overrideForCli) {
14+
response = Buffer.from(response);
15+
}
1416

15-
{% endif %}
17+
fs.writeFileSync(destination, response);
18+
{%~ endif %}
1619
if (parseOutput) {
20+
{%~ if hasConsolePreview(method.name,service.name) %}
21+
if(console) {
22+
showConsoleLink('{{service.name}}', '{{ method.name }}'
23+
{%- for parameter in method.parameters.path -%}{%- set param = (parameter.name | caseCamel | escapeKeyword) -%}{%- if param ends with 'Id' -%}, {{ param }} {%- endif -%}{%- endfor -%}
24+
);
25+
} else {
26+
parse(response)
27+
success()
28+
}
29+
{%~ else %}
1730
parse(response)
1831
success()
32+
{%~ endif %}
1933
}
20-
21-
return response;
34+
35+
return response;

templates/cli/base/requests/file.twig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% for parameter in method.parameters.all %}
22
{% if parameter.type == 'file' %}
33
const size = {{ parameter.name | caseCamel | escapeKeyword }}.size;
4-
4+
55
const apiHeaders = {
66
{% for parameter in method.parameters.header %}
77
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
@@ -45,7 +45,7 @@
4545
}
4646

4747
let uploadableChunkTrimmed;
48-
48+
4949
if(currentPosition + 1 >= client.CHUNK_SIZE) {
5050
uploadableChunkTrimmed = uploadableChunk;
5151
} else {
@@ -99,17 +99,17 @@
9999
}
100100

101101
{% if method.packaging %}
102-
fs.unlinkSync(filePath);
102+
await fs.unlink(filePath,()=>{});
103103
{% endif %}
104104
{% if method.type == 'location' %}
105105
fs.writeFileSync(destination, response);
106106
{% endif %}
107-
107+
108108
if (parseOutput) {
109109
parse(response)
110110
success()
111111
}
112112

113113
return response;
114114
{% endif %}
115-
{% endfor %}
115+
{% endfor %}

0 commit comments

Comments
 (0)