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
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ Generate an SDK for your API

```
USAGE
$ apimatic sdk:generate --platform csharp|java|php|python|ruby|typescript|go [--spec <value>] [-d <value>] [-f]
[--zip] [-k <value>]
$ apimatic sdk:generate -l csharp|java|php|python|ruby|typescript|go [--spec <value>] [-d <value>] [-f] [--zip]
[-k <value>]

FLAGS
-d, --destination=<value> [default: ./sdk/<platform>] path where the sdk will be generated.
-d, --destination=<value> [default: ./sdk/<language>] path where the sdk will be generated.
-f, --force overwrite changes without asking for user consent.
-k, --auth-key=<value> override current authentication state with an authentication key.
--platform=<option> (required) language platform for sdk
-l, --language=<option> (required) language for sdk
<options: csharp|java|php|python|ruby|typescript|go>
--spec=<value> [default: ./src/spec] path to the folder containing the API specification file.
--zip download the generated SDK as a .zip archive
Expand All @@ -405,10 +405,10 @@ DESCRIPTION
Generate an SDK for your API

EXAMPLES
$ apimatic sdk:generate --platform=java
$ apimatic sdk:generate --language=java

$ apimatic sdk:generate --platform=csharp --spec="./src/spec"
$ apimatic sdk:generate --language=csharp --spec="./src/spec"
```

_See code: [src/commands/sdk/generate.ts](https://github.com/apimatic/apimatic-cli/blob/alpha/src/commands/sdk/generate.ts)_
_See code: [src/commands/sdk/generate.ts](https://github.com/apimatic/apimatic-cli/blob/v1.1.0-alpha.21/src/commands/sdk/generate.ts)_
<!-- commandsstop -->
30 changes: 15 additions & 15 deletions src/actions/sdk/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { SdkContext } from "../../types/sdk-context.js";
import { Platforms } from "@apimatic/sdk";
import { SpecContext } from "../../types/spec-context.js";
import { SdkGeneratePrompts } from "../../prompts/sdk/generate.js";
import { LanguagePlatform } from "../../types/sdk/generate.js";
import { Language } from "../../types/sdk/generate.js";

export class GenerateAction {
private readonly prompts: SdkGeneratePrompts = new SdkGeneratePrompts();
Expand All @@ -28,7 +28,7 @@ export class GenerateAction {
public readonly execute = async (
specDirectory: DirectoryPath,
sdkDirectory: DirectoryPath,
platform: LanguagePlatform,
language: Language,
commandName: string,
force: boolean,
zipSdk: boolean
Expand All @@ -42,7 +42,7 @@ export class GenerateAction {
return ActionResult.error(`Unable to locate a valid "src" directory. Navigate to the directory containing your APIMatic Portal source or set up a new project by running apimatic portal:quickstart.`);
}

const sdkContext = new SdkContext(sdkDirectory, platform);
const sdkContext = new SdkContext(sdkDirectory, language);
if (!force && (await sdkContext.exists()) && !(await this.prompts.overwriteSdk(sdkDirectory))) {
return ActionResult.error(
"Please enter a different destination folder or remove the existing files and try again."
Expand All @@ -55,8 +55,8 @@ export class GenerateAction {
const specZipPath = new FilePath(tempDirectory, new FileName("spec.zip"));
await this.zipArchiver.archive(specDirectory, specZipPath);

const sdkPlatform = this.convertSimplePlatformToPlatform(platform as LanguagePlatform);
const response = await this.portalService.generateSdk(specZipPath, sdkPlatform, this.configDir, commandName, this.authKey);
const platform = this.convertLanguageToPlatform(language);
const response = await this.portalService.generateSdk(specZipPath, platform, this.configDir, commandName, this.authKey);

if (!response.isSuccess()) {
this.prompts.displaySdkGenerationErrorMessage();
Expand All @@ -73,24 +73,24 @@ export class GenerateAction {
});
};

private convertSimplePlatformToPlatform(languagePlatform: LanguagePlatform): Platforms {
switch (languagePlatform) {
case LanguagePlatform.CSHARP:
private convertLanguageToPlatform(language: Language): Platforms {
switch (language) {
case Language.CSHARP:
return Platforms.CsNetStandardLib;
case LanguagePlatform.JAVA:
case Language.JAVA:
return Platforms.JavaEclipseJreLib;
case LanguagePlatform.PHP:
case Language.PHP:
return Platforms.PhpGenericLibV2;
case LanguagePlatform.PYTHON:
case Language.PYTHON:
return Platforms.PythonGenericLib;
case LanguagePlatform.RUBY:
case Language.RUBY:
return Platforms.RubyGenericLib;
case LanguagePlatform.TYPESCRIPT:
case Language.TYPESCRIPT:
return Platforms.TsGenericLib;
case LanguagePlatform.GO:
case Language.GO:
return Platforms.GoGenericLib;
default:
throw new Error(`Unknown LanguagePlatform: ${languagePlatform}`);
throw new Error(`Unknown LanguagePlatform: ${language}`);
}
}
}
23 changes: 12 additions & 11 deletions src/commands/sdk/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,53 +3,54 @@ import { DirectoryPath } from "../../types/file/directoryPath.js";
import { FlagsProvider } from "../../types/flags-provider.js";
import { SdkGeneratePrompts } from "../../prompts/sdk/generate.js";
import { GenerateAction } from "../../actions/sdk/generate.js";
import { LanguagePlatform } from "../../types/sdk/generate.js";
import { Language } from "../../types/sdk/generate.js";

const DEFAULT_WORKING_DIRECTORY = "./";

export default class SdkGenerate extends Command {
static description = "Generate an SDK for your API";
static flags = {
platform: Flags.string({
language: Flags.string({
char: 'l',
required: true,
options: Object.values(LanguagePlatform).map((p) => p.toString()),
description: `language platform for sdk`
options: Object.values(Language).map((p) => p.toString()),
description: `language for which the sdk will be generated.`
}),
spec: Flags.string({
description: "path to the folder containing the API specification file.",
default: "./src/spec"
}),
destination: Flags.string({
char: "d",
description: `[default: ./sdk/<platform>] path where the sdk will be generated.`
description: `[default: ./sdk/<language>] path where the sdk will be generated.`
}),
...FlagsProvider.force,
zip: Flags.boolean({
default: false,
description: "download the generated SDK as a .zip archive"
description: "download the generated SDK as a .zip archive."
}),
...FlagsProvider.authKey
};

static examples = [
`apimatic sdk:generate --platform=java`,
`apimatic sdk:generate --platform=csharp --spec="./src/spec"`
`apimatic sdk:generate --language=java`,
`apimatic sdk:generate --language=csharp --spec="./src/spec"`
];

private readonly prompts: SdkGeneratePrompts = new SdkGeneratePrompts();

async run() {
const {
flags: { platform, spec, destination, force, zip: zipSdk, "auth-key": authKey }
flags: { language, spec, destination, force, zip: zipSdk, "auth-key": authKey }
} = await this.parse(SdkGenerate);

const workingDirectory = new DirectoryPath(DEFAULT_WORKING_DIRECTORY);
const specDirectory = new DirectoryPath(spec);

const sdkDirectory = destination ? new DirectoryPath(destination) : workingDirectory.join("sdk").join(platform);
const sdkDirectory = destination ? new DirectoryPath(destination) : workingDirectory.join("sdk").join(language);

var action = new GenerateAction(this.getConfigDir(), authKey);
const result = await action.execute(specDirectory, sdkDirectory, platform as LanguagePlatform, SdkGenerate.id, force, zipSdk);
const result = await action.execute(specDirectory, sdkDirectory, language as Language, SdkGenerate.id, force, zipSdk);
result.mapAll(
() => this.prompts.displayOutroMessage(sdkDirectory),
(message) => this.prompts.logError(message)
Expand Down
4 changes: 2 additions & 2 deletions src/types/sdk-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { DirectoryPath } from "./file/directoryPath.js";
import { FilePath } from "./file/filePath.js";
import { FileName } from "./file/fileName.js";
import { ZipService } from "../infrastructure/zip-service.js";
import { LanguagePlatform } from "./sdk/generate.js";
import { Language } from "./sdk/generate.js";

export class SdkContext {

private readonly fileService = new FileService();
private readonly zipService = new ZipService();

constructor(private readonly sdkDirectory: DirectoryPath,
private readonly platform: LanguagePlatform) {
private readonly platform: Language) {
}

public get ZipPath(): FilePath {
Expand Down
19 changes: 1 addition & 18 deletions src/types/sdk/generate.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
export type GenerationIdParams = {
file: string;
url: string;
platform: string;
};

export type DownloadSDKParams = {
codeGenId: string;
zip: boolean;
zippedSDKPath: string;
sdkFolderPath: string;
};

export type SDKGenerateUnprocessableError = {
message: string;
};

export enum LanguagePlatform {
export enum Language {
CSHARP = "csharp",
JAVA = "java",
PHP = "php",
Expand Down