Skip to content

Commit

Permalink
Merge pull request #863 from appwrite/feat-cli-g2
Browse files Browse the repository at this point in the history
Feat cli g2
  • Loading branch information
TorstenDittmann authored Jul 3, 2024
2 parents 44764b6 + 2a10501 commit ad0518e
Show file tree
Hide file tree
Showing 31 changed files with 4,379 additions and 1,420 deletions.
292 changes: 181 additions & 111 deletions composer.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function getSSLPage($url) {
$platform = 'console';
// $platform = 'server';

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

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

Expand Down Expand Up @@ -393,7 +393,7 @@ function getSSLPage($url) {
;

$sdk->generate(__DIR__ . '/examples/apple');

// DotNet
$sdk = new SDK(new DotNet(), new Swagger2($spec));

Expand Down Expand Up @@ -442,7 +442,7 @@ function getSSLPage($url) {
// Android

$sdk = new SDK(new Android(), new Swagger2($spec));

$sdk
->setName('Android')
->setNamespace('io appwrite')
Expand All @@ -466,7 +466,7 @@ function getSSLPage($url) {

// Kotlin
$sdk = new SDK(new Kotlin(), new Swagger2($spec));

$sdk
->setName('Kotlin')
->setNamespace('io appwrite')
Expand Down
9 changes: 9 additions & 0 deletions src/SDK/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public function getFilters(): array
return [];
}

/**
* Language specific functions.
* @return array
*/
public function getFunctions(): array
{
return [];
}

protected function toPascalCase(string $value): string
{
return \ucfirst($this->toCamelCase($value));
Expand Down
111 changes: 109 additions & 2 deletions src/SDK/Language/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,66 @@

namespace Appwrite\SDK\Language;

use Twig\TwigFunction;

class CLI extends Node
{
/**
* List of functions to ignore for console preview.
* @var array
*/
private $consoleIgnoreFunctions = [
'listidentities',
'listmfafactors',
'getprefs',
'getsession',
'getattribute',
'listdocumentlogs',
'getindex',
'listcollectionlogs',
'getcollectionusage',
'listlogs',
'listruntimes',
'getusage',
'getusage',
'listvariables',
'getvariable',
'listproviderlogs',
'listsubscriberlogs',
'getsubscriber',
'listtopiclogs',
'getemailtemplate',
'getsmstemplate',
'getfiledownload',
'getfilepreview',
'getfileview',
'getusage',
'listlogs',
'getprefs',
'getusage',
'listlogs',
'getmembership',
'listmemberships',
'listmfafactors',
'getmfarecoverycodes',
'getprefs',
'listtargets',
'gettarget',
];

/**
* List of SDK services to ignore for console preview.
* @var array
*/
private $consoleIgnoreServices = [
'health',
'migrations',
'locale',
'avatars',
'project',
'proxy',
'vcs'
];
/**
* @var array
*/
Expand Down Expand Up @@ -127,6 +185,16 @@ public function getFiles(): array
'destination' => 'lib/questions.js',
'template' => 'cli/lib/questions.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/validations.js',
'template' => 'cli/lib/validations.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/spinner.js',
'template' => 'cli/lib/spinner.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/parser.js',
Expand All @@ -152,6 +220,11 @@ public function getFiles(): array
'destination' => 'lib/client.js',
'template' => 'cli/lib/client.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/id.js',
'template' => 'cli/lib/id.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/utils.js',
Expand All @@ -164,8 +237,28 @@ public function getFiles(): array
],
[
'scope' => 'default',
'destination' => 'lib/commands/deploy.js',
'template' => 'cli/lib/commands/deploy.js.twig',
'destination' => 'lib/commands/pull.js',
'template' => 'cli/lib/commands/pull.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/commands/push.js',
'template' => 'cli/lib/commands/push.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/commands/run.js',
'template' => 'cli/lib/commands/run.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/emulation/docker.js',
'template' => 'cli/lib/emulation/docker.js.twig',
],
[
'scope' => 'default',
'destination' => 'lib/emulation/utils.js',
'template' => 'cli/lib/emulation/utils.js.twig',
],
[
'scope' => 'service',
Expand Down Expand Up @@ -270,4 +363,18 @@ public function getParamExample(array $param): string

return $output;
}

/**
* Language specific filters.
* @return array
*/
public function getFunctions(): array
{
return [
/** Return true if the entered service->method is enabled for a console preview link */
new TwigFunction('hasConsolePreview', fn($method, $service) => preg_match('/^([Gg]et|[Ll]ist)/', $method)
&& !in_array(strtolower($method), $this->consoleIgnoreFunctions)
&& !in_array($service, $this->consoleIgnoreServices)),
];
}
}
7 changes: 7 additions & 0 deletions src/SDK/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public function __construct(Language $language, Spec $spec)
'debug' => true
]);

/**
* Add language-specific functions
*/
foreach ($this->language->getFunctions() as $function) {
$this->twig->addFunction($function);
}

/**
* Add language specific filters
*/
Expand Down
20 changes: 11 additions & 9 deletions templates/cli/base/params.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
if (!fs.lstatSync(folderPath).isDirectory()) {
throw new Error('The path is not a directory.');
}

const ignorer = ignore();

const func = localConfig.getFunction(functionId);

ignorer.add('.appwrite');

if (func.ignore) {
ignorer.add(func.ignore);
log('Ignoring files using configuration from appwrite.json');
} else if (fs.existsSync(pathLib.join({{ parameter.name | caseCamel | escapeKeyword }}, '.gitignore'))) {
ignorer.add(fs.readFileSync(pathLib.join({{ parameter.name | caseCamel | escapeKeyword }}, '.gitignore')).toString());
log('Ignoring files in .gitignore');
}

const files = getAllFiles({{ parameter.name | caseCamel | escapeKeyword }}).map((file) => pathLib.relative({{ parameter.name | caseCamel | escapeKeyword }}, file)).filter((file) => !ignorer.ignores(file));

await tar
Expand Down Expand Up @@ -77,8 +77,10 @@
{% endif %}
{% endfor %}
{% if method.type == 'location' %}
payload['project'] = localConfig.getProject().projectId
payload['key'] = globalConfig.getKey();
const queryParams = new URLSearchParams(payload);
apiPath = `${globalConfig.getEndpoint()}${apiPath}?${queryParams.toString()}`;
{% endif %}
if (!overrideForCli) {
payload['project'] = localConfig.getProject().projectId
payload['key'] = globalConfig.getKey();
const queryParams = new URLSearchParams(payload);
apiPath = `${globalConfig.getEndpoint()}${apiPath}?${queryParams.toString()}`;
}
{% endif %}
24 changes: 19 additions & 5 deletions templates/cli/base/requests/api.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,27 @@
{% endfor %}
}, payload{% if method.type == 'location' %}, 'arraybuffer'{% endif %});

{% if method.type == 'location' %}
fs.writeFileSync(destination, response);
{%~ if method.type == 'location' %}
if (overrideForCli) {
response = Buffer.from(response);
}

{% endif %}
fs.writeFileSync(destination, response);
{%~ endif %}
if (parseOutput) {
{%~ if hasConsolePreview(method.name,service.name) %}
if(console) {
showConsoleLink('{{service.name}}', '{{ method.name }}'
{%- for parameter in method.parameters.path -%}{%- set param = (parameter.name | caseCamel | escapeKeyword) -%}{%- if param ends with 'Id' -%}, {{ param }} {%- endif -%}{%- endfor -%}
);
} else {
parse(response)
success()
}
{%~ else %}
parse(response)
success()
{%~ endif %}
}
return response;

return response;
10 changes: 5 additions & 5 deletions templates/cli/base/requests/file.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% for parameter in method.parameters.all %}
{% if parameter.type == 'file' %}
const size = {{ parameter.name | caseCamel | escapeKeyword }}.size;

const apiHeaders = {
{% for parameter in method.parameters.header %}
'{{ parameter.name }}': ${{ parameter.name | caseCamel | escapeKeyword }},
Expand Down Expand Up @@ -45,7 +45,7 @@
}

let uploadableChunkTrimmed;

if(currentPosition + 1 >= client.CHUNK_SIZE) {
uploadableChunkTrimmed = uploadableChunk;
} else {
Expand Down Expand Up @@ -99,17 +99,17 @@
}

{% if method.packaging %}
fs.unlinkSync(filePath);
await fs.unlink(filePath,()=>{});
{% endif %}
{% if method.type == 'location' %}
fs.writeFileSync(destination, response);
{% endif %}

if (parseOutput) {
parse(response)
success()
}

return response;
{% endif %}
{% endfor %}
{% endfor %}
Loading

0 comments on commit ad0518e

Please sign in to comment.