Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion bin/generate-oas.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,26 @@
exit(1);
});

$testing = in_array('--testing', $argv, true);
$language = 'en';
foreach (array_slice($argv, 1) as $arg) {
if ($arg[0] !== '-') {
$language = $arg;
break;
}
}

echo "\nCreating translated OpenAPI file ...\n";
$language = $argv[1] ?? 'en';
$generate = new Hello\OpenApi\Translate($language);
$generate->run();
$generate->printResults();

echo "\nCreating SDK-specific OpenAPI file ...\n";
$generate = new Hello\OpenApi\GenerateSdkOas();
$generate->run();

if ($testing) {
echo "\nCreating testing SDK OpenAPI file (no hideOn filtering) ...\n";
$generate = new Hello\OpenApi\GenerateSdkOas(true);
$generate->run();
}
16 changes: 14 additions & 2 deletions build
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,19 @@
set -e

DIR=$(cd `dirname $0` && pwd)
LANGUAGE="${1:-en}"
LANGUAGE="en"
TESTING=""

for arg in "$@"; do
case $arg in
--testing)
TESTING="--testing"
;;
*)
LANGUAGE="$arg"
;;
esac
done

printf "Language chosen: ${LANGUAGE}\n"

Expand All @@ -12,7 +24,7 @@ mkdir -p "${DIR}/vendor"
bash "${DIR}/bin/php" composer install
printf "\n"

bash "${DIR}/bin/php" ./bin/generate-oas.php
bash "${DIR}/bin/php" ./bin/generate-oas.php ${TESTING}

"${DIR}/bin/copy-examples-filtered" "${DIR}/examples" "${DIR}/sandbox/dotnet/src/Dropbox.SignSandbox" cs
"${DIR}/bin/copy-examples-filtered" "${DIR}/examples" "${DIR}/sandbox/java/src/main/java/com/dropbox/sign_sandbox" java
Expand Down
24 changes: 21 additions & 3 deletions generate-sdks
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ DIR=$(cd `dirname $0` && pwd)
SDKS=( dotnet java-v1 java-v2 node php python ruby )
SHOW_HELP=0
TARGET_SDK=
TESTING=0

while getopts ":t:h" opt; do
while getopts ":t:h-:" opt; do
case $opt in
t) TARGET_SDK="$OPTARG"
;;
h) SHOW_HELP=1
;;
-)
case "${OPTARG}" in
testing) TESTING=1
;;
*) echo "Invalid option --$OPTARG" >&2
;;
esac
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
Expand Down Expand Up @@ -55,8 +64,10 @@ Builds a given SDK into the sdks/[SDK] directory.
be DELETED and REBUILT!

-t target SDK, "all", "dotnet", "java-v2", "java-v1", "node", "php", "python", "ruby"
--testing use openapi-sdk-testing.yaml (includes all hideOn endpoints)
-h display this help and exit
Example: generate-sdks -t php
Example: generate-sdks -t php --testing
EOF

exit 0
Expand Down Expand Up @@ -140,9 +151,16 @@ function copy_oas()
SDK="$1"
SDK_DIR="${DIR}/sdks/${SDK}"

printf "Copying openapi-sdk.yaml ...\n"
if [[ $TESTING -eq 1 ]]; then
OAS_FILE="${DIR}/openapi-sdk-testing.yaml"
printf "Copying openapi-sdk-testing.yaml ...\n"
else
OAS_FILE="${DIR}/openapi-sdk.yaml"
printf "Copying openapi-sdk.yaml ...\n"
fi

rm -f "${SDK_DIR}/openapi-sdk.yaml"
cp -r "${DIR}/openapi-sdk.yaml" "${SDK_DIR}/openapi-sdk.yaml"
cp -r "${OAS_FILE}" "${SDK_DIR}/openapi-sdk.yaml"
}

function copy_examples()
Expand Down
15 changes: 13 additions & 2 deletions src/Hello/OpenApi/GenerateSdkOas.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@ class GenerateSdkOas

private const SURFACE_ID = 'sdk';

private bool $testing;

public function __construct(bool $testing = false)
{
$this->testing = $testing;
}

public function run(): void
{
$raw_file = new RawFile(self::ROOT_DIR . '/openapi-raw.yaml');
$translation_file = self::ROOT_DIR . '/translations/en.yaml';
$raw_file->translate(
self::SURFACE_ID,
$translation_file,
$translation_file
$translation_file,
$this->testing
);

$data = $raw_file->getData();
unset($data['tags']);
$raw_file->setData($data);

$raw_file->saveFile(self::ROOT_DIR . '/openapi-sdk.yaml');
$output_file = $this->testing
? self::ROOT_DIR . '/openapi-sdk-testing.yaml'
: self::ROOT_DIR . '/openapi-sdk.yaml';
$raw_file->saveFile($output_file);
}
}
11 changes: 6 additions & 5 deletions src/Hello/OpenApi/RawFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ public function __construct(string $filename)
public function translate(
string $surface_id,
string $translation_file,
string $fallback_file
string $fallback_file,
bool $skip_hide_on = false
): void {
$this->loadTranslations($translation_file, $fallback_file);
$result = $this->recurse($this->openapi, $surface_id);
$result = $this->recurse($this->openapi, $surface_id, $skip_hide_on);

$this->logs['translated'] = array_unique($this->logs['translated']);
$this->logs['fallback'] = array_unique($this->logs['fallback']);
Expand Down Expand Up @@ -160,18 +161,18 @@ private function loadTranslations(string $file, string $fallback): void
* searching for strings prepended with TRANSLATE_PREPEND and attempting
* to translate them.
*/
private function recurse(array $data, string $surface_id): TranslationResult
private function recurse(array $data, string $surface_id, bool $skip_hide_on = false): TranslationResult
{
$empty_by_hiding = false;

foreach ($data as $k => $v) {
if (is_iterable($v)) {
if (isset($v[self::HIDE_ON]) && $this->shouldHide($v[self::HIDE_ON], $surface_id)) {
if (!$skip_hide_on && isset($v[self::HIDE_ON]) && $this->shouldHide($v[self::HIDE_ON], $surface_id)) {
unset($data[$k]);
$empty_by_hiding = empty($data);
} else {
unset($v[self::HIDE_ON]);
$result = $this->recurse($v, $surface_id);
$result = $this->recurse($v, $surface_id, $skip_hide_on);
if ($result->isAllHidden()) {
unset($data[$k]);
} else {
Expand Down
Loading