Skip to content

Commit

Permalink
Set Dart2 as default, better format Dart code with dartfmt (OpenAPITo…
Browse files Browse the repository at this point in the history
…ols#949)

* change dart default to v2

* add dart2 author

* add dartfmt to postProcess file in dart generator

* add dartfmt to format dart code
  • Loading branch information
wing328 committed Sep 2, 2018
1 parent a7ff9b8 commit e2a5423
Show file tree
Hide file tree
Showing 113 changed files with 2,615 additions and 3,282 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se

| | Languages/Frameworks |
|-|-|
**API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C#** (.net 2.0, 3.5 or later), **C++** (cpprest, Qt5, Tizen), **Clojure**, **Dart**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client), **Kotlin**, **Lua**, **Node.js** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types) **Objective-C**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x), **Typescript** (AngularJS, Angular (2.x - 6.x), Aurelia, Fetch, Inversify, jQuery, Node)
**API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C#** (.net 2.0, 3.5 or later), **C++** (cpprest, Qt5, Tizen), **Clojure**, **Dart (1.x, 2.x)**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client), **Kotlin**, **Lua**, **Node.js** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types) **Objective-C**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x), **Typescript** (AngularJS, Angular (2.x - 6.x), Aurelia, Fetch, Inversify, jQuery, Node)
**Server stubs** | **Ada**, **C#** (ASP.NET Core, NancyFx), **C++** (Pistache, Restbed), **Erlang**, **Go**, **Haskell** (Servant), **Java** (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, RestEasy, Play Framework, [PKMST](https://github.com/ProKarma-Inc/pkmst-getting-started-examples)), **Kotlin** (Spring Boot), **PHP** (Laravel, Lumen, Slim, Silex, [Symfony](https://symfony.com/), [Zend Expressive](https://github.com/zendframework/zend-expressive)), **Python** (Flask), **NodeJS**, **Ruby** (Sinatra, Rails5), **Rust** (rust-server), **Scala** ([Finch](https://github.com/finagle/finch), [Lagom](https://github.com/lagom/lagom), Scalatra)
**API documentation generators** | **HTML**, **Confluence Wiki**
**Configuration files** | [**Apache2**](https://httpd.apache.org/)
Expand Down Expand Up @@ -516,7 +516,8 @@ Here is a list of template creators:
* C# (.NET 4.5 refactored): @jimschubert
* Clojure: @xhh
* Dart: @yissachar
* Dart (refactored in 2.4.0): @joernahrens
* Dart (refactor): @joernahrens
* Dart 2: @swipesight
* Elixir: @niku
* Elm: @trenneman
* Eiffel: @jvelilla
Expand Down
8 changes: 4 additions & 4 deletions bin/dart-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ fi
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"

# Generate non-browserClient
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/openapi -DhideGenerationTimestamp=true -DbrowserClient=false $@"
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/openapi -DhideGenerationTimestamp=true -DbrowserClient=false --additional-properties supportDart2=false $@"

# then options to generate the library for vm would be:
#ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/openapi_vm -DbrowserClient=false -DpubName=openapi_vm $@"
#ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/openapi_vm -DbrowserClient=false -DpubName=openapi_vm --additional-properties supportDart2=false $@"
java $JAVA_OPTS -jar $executable $ags

# Generate browserClient
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/openapi-browser-client -DhideGenerationTimestamp=true -DbrowserClient=true $@"
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/openapi-browser-client -DhideGenerationTimestamp=true -DbrowserClient=true --additional-properties supportDart2=false $@"
java $JAVA_OPTS -jar $executable $ags

# Generate non-browserClient and put it to the flutter sample app
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/flutter_petstore/openapi -DhideGenerationTimestamp=true -DbrowserClient=false $@"
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart/flutter_petstore/openapi -DhideGenerationTimestamp=true -DbrowserClient=false --additional-properties supportDart2=false $@"
java $JAVA_OPTS -jar $executable $ags

# There is a proposal to allow importing different libraries depending on the environment:
Expand Down
6 changes: 3 additions & 3 deletions bin/dart2-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ fi
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"

# Generate non-browserClient
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/openapi -DhideGenerationTimestamp=true -DbrowserClient=false --additional-properties supportDart2=true $@"
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/openapi -DhideGenerationTimestamp=true -DbrowserClient=false $@"

# then options to generate the library for vm would be:
#ags="generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/openapi_vm -DbrowserClient=false -DpubName=openapi_vm $@"
java $JAVA_OPTS -jar $executable $ags

# Generate browserClient
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/openapi-browser-client -DhideGenerationTimestamp=true -DbrowserClient=true --additional-properties supportDart2=true $@"
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/openapi-browser-client -DhideGenerationTimestamp=true -DbrowserClient=true $@"
java $JAVA_OPTS -jar $executable $ags

# Generate non-browserClient and put it to the flutter sample app
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/flutter_petstore/openapi -DhideGenerationTimestamp=true -DbrowserClient=false --additional-properties supportDart2=true $@"
ags="generate -t modules/openapi-generator/src/main/resources/dart -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g dart -o samples/client/petstore/dart2/flutter_petstore/openapi -DhideGenerationTimestamp=true -DbrowserClient=false $@"
java $JAVA_OPTS -jar $executable $ags

# There is a proposal to allow importing different libraries depending on the environment:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@

import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.parser.util.SchemaTypeUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import java.util.Set;

public class DartClientCodegen extends DefaultCodegen implements CodegenConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(DartClientCodegen.class);
Expand Down Expand Up @@ -127,7 +130,7 @@ public DartClientCodegen() {
cliOptions.add(new CliOption(PUB_DESCRIPTION, "Description in generated pubspec"));
cliOptions.add(new CliOption(USE_ENUM_EXTENSION, "Allow the 'x-enum-values' extension for enums"));
cliOptions.add(new CliOption(CodegenConstants.SOURCE_FOLDER, "source folder for generated code"));
cliOptions.add(CliOption.newBoolean(SUPPORT_DART2, "support dart2").defaultValue(Boolean.FALSE.toString()));
cliOptions.add(CliOption.newBoolean(SUPPORT_DART2, "support dart2").defaultValue(Boolean.TRUE.toString()));
}

@Override
Expand All @@ -142,13 +145,17 @@ public String getName() {

@Override
public String getHelp() {
return "Generates a Dart client library.";
return "Generates a Dart (1.x or 2.x) client library.";
}

@Override
public void processOpts() {
super.processOpts();

if (StringUtils.isEmpty(System.getenv("DART_FMT_PATH"))) {
LOGGER.info("Environment variable DART_FMT_PATH not defined so the Dart code may not be properly formatted. To define it, try 'export DART_FMT_PATH=/usr/local/bin/dartfmt' (Linux/Mac)");
}

if (additionalProperties.containsKey(BROWSER_CLIENT)) {
this.setBrowserClient(convertPropertyToBooleanAndWriteBack(BROWSER_CLIENT));
} else {
Expand Down Expand Up @@ -193,11 +200,16 @@ public void processOpts() {
additionalProperties.put("modelDocPath", modelDocPath);

final Object isSupportDart2 = additionalProperties.get(SUPPORT_DART2);
if (Boolean.TRUE.equals(isSupportDart2) || (isSupportDart2 instanceof String && Boolean.parseBoolean((String)isSupportDart2))) {
embeddedTemplateDir = templateDir = "dart2";
} else {
if (Boolean.FALSE.equals(isSupportDart2) || (isSupportDart2 instanceof String && !Boolean.parseBoolean((String)isSupportDart2))) {
// dart 1.x
LOGGER.info("Dart version: 1.x");
supportingFiles.add(new SupportingFile("analysis_options.mustache", "", ".analysis_options"));
} else {
// dart 2.x
LOGGER.info("Dart version: 2.x");
embeddedTemplateDir = templateDir = "dart2";
}

final String libFolder = sourceFolder + File.separator + "lib";
supportingFiles.add(new SupportingFile("pubspec.mustache", "", "pubspec.yaml"));
supportingFiles.add(new SupportingFile("api_client.mustache", libFolder, "api_client.dart"));
Expand Down Expand Up @@ -496,4 +508,43 @@ public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}

@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
return;
}

String dartFmtPath = System.getenv("DART_FMT_PATH");
if (StringUtils.isEmpty(dartFmtPath)) {
return; // skip if DART_FMT_PATH env variable is not defined
}

// only procees the following type (or we can simply rely on the file extension to check if it's a Dart file)
Set<String> supportedFileType = new HashSet<String>(
Arrays.asList(
"supporting-mustache",
"model-test",
"model",
"api-test",
"api"));
if (!supportedFileType.contains(fileType)) {
return;
}

// only process files with dart extension
if ("dart".equals(FilenameUtils.getExtension(file.toString()))) {
// currently only support "dartfmt -w yourcode.dart"
String command = dartFmtPath + " -w " + file.toString();
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
if (p.exitValue() != 0) {
LOGGER.error("Error running the command ({}): {}", command, p.exitValue());
}
} catch (Exception e) {
LOGGER.error("Error running the command ({}): {}", command, e.getMessage());
}
LOGGER.info("Successfully executed: " + command);
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.0-SNAPSHOT
3.3.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,4 @@ part 'model/pet.dart';
part 'model/tag.dart';
part 'model/user.dart';


ApiClient defaultApiClient = new ApiClient();
Loading

0 comments on commit e2a5423

Please sign in to comment.