Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
Use new README.md format.
Browse files Browse the repository at this point in the history
  • Loading branch information
landrito committed May 5, 2017
1 parent 3258315 commit d284d84
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
*/
package com.google.api.codegen.transformer;

import com.google.api.codegen.ReleaseLevel;

/** A PackageMetadataNamer provides language-specific strings for metadata views. */
public class PackageMetadataNamer {

Expand All @@ -40,4 +42,21 @@ public String getOutputFileName() {
public String getNotImplementedString(String feature) {
return "$ NOT IMPLEMENTED: " + feature + " $";
}

public String getReleaseAnnotation(ReleaseLevel releaseLevel) {
switch (releaseLevel) {
case UNSET_RELEASE_LEVEL:
// fallthrough
case ALPHA:
return "Alpha";
case BETA:
return "Beta";
case GA:
return "Production/Stable";
case DEPRECATED:
return "Inactive";
default:
throw new IllegalStateException("Invalid development status");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,29 @@

import com.google.api.codegen.InterfaceView;
import com.google.api.codegen.TargetLanguage;
import com.google.api.codegen.config.FlatteningConfig;
import com.google.api.codegen.config.GapicProductConfig;
import com.google.api.codegen.config.PackageMetadataConfig;
import com.google.api.codegen.nodejs.NodeJSUtils;
import com.google.api.codegen.transformer.DynamicLangApiMethodTransformer;
import com.google.api.codegen.transformer.FileHeaderTransformer;
import com.google.api.codegen.transformer.GapicInterfaceContext;
import com.google.api.codegen.transformer.GapicMethodContext;
import com.google.api.codegen.transformer.InitCodeTransformer;
import com.google.api.codegen.transformer.ModelToViewTransformer;
import com.google.api.codegen.transformer.ModelTypeTable;
import com.google.api.codegen.transformer.PackageMetadataTransformer;
import com.google.api.codegen.transformer.TestCaseTransformer;
import com.google.api.codegen.util.js.JSTypeTable;
import com.google.api.codegen.util.testing.StandardValueProducer;
import com.google.api.codegen.util.testing.ValueProducer;
import com.google.api.codegen.viewmodel.ApiMethodView;
import com.google.api.codegen.viewmodel.ImportSectionView;
import com.google.api.codegen.viewmodel.InitCodeView;
import com.google.api.codegen.viewmodel.OptionalArrayMethodView;
import com.google.api.codegen.viewmodel.ViewModel;
import com.google.api.tools.framework.model.Interface;
import com.google.api.tools.framework.model.Method;
import com.google.api.tools.framework.model.Model;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
Expand All @@ -30,21 +47,26 @@

/** Responsible for producing package metadata related views for NodeJS */
public class NodeJSPackageMetadataTransformer implements ModelToViewTransformer {
private static final List<String> TOP_LEVEL_FILES =
ImmutableList.of("nodejs/package.json.snip", "nodejs/README.md.snip");
private static final String README_FILE = "nodejs/README.md.snip";
private static final String README_OUTPUT_FILE = "README.md";
private static final List<String> TOP_LEVEL_FILES = ImmutableList.of("nodejs/package.json.snip");

private static String NODE_PREFIX = "nodejs/";

private final FileHeaderTransformer fileHeaderTransformer =
new FileHeaderTransformer(new NodeJSImportSectionTransformer());
private final PackageMetadataConfig packageConfig;
private final PackageMetadataTransformer metadataTransformer = new PackageMetadataTransformer();
private final ValueProducer valueProducer = new StandardValueProducer();
private final TestCaseTransformer testCaseTransformer = new TestCaseTransformer(valueProducer);

public NodeJSPackageMetadataTransformer(PackageMetadataConfig packageConfig) {
this.packageConfig = packageConfig;
}

@Override
public List<String> getTemplateFileNames() {
return TOP_LEVEL_FILES;
return ImmutableList.<String>builder().addAll(TOP_LEVEL_FILES).add(README_FILE).build();
}

@Override
Expand All @@ -54,9 +76,66 @@ public List<ViewModel> transform(Model model, GapicProductConfig productConfig)
new NodeJSPackageMetadataNamer(
productConfig.getPackageName(), productConfig.getDomainLayerLocation());
models.addAll(generateMetadataViews(model, namer));
models.add(generateReadmeView(model, productConfig, namer));
return models;
}

private ViewModel generateReadmeView(
Model model, GapicProductConfig productConfig, NodeJSPackageMetadataNamer namer) {
List<ApiMethodView> exampleMethods = generateExampleMethods(model, productConfig);
return metadataTransformer
.generateMetadataView(
packageConfig, model, README_FILE, README_OUTPUT_FILE, TargetLanguage.RUBY)
.identifier(namer.getMetadataIdentifier())
.fileHeader(
fileHeaderTransformer.generateFileHeader(
productConfig,
ImportSectionView.newBuilder().build(),
new NodeJSSurfaceNamer(
productConfig.getPackageName(), NodeJSUtils.isGcloud(productConfig))))
.developmentStatus(
namer.getReleaseAnnotation(packageConfig.releaseLevel(TargetLanguage.RUBY)))
.exampleMethods(exampleMethods)
.build();
}

// Generates methods used as examples for the README.md file.
// Note: This is based on sample gen method calls. In the future, the example
// methods may be configured separately.
private List<ApiMethodView> generateExampleMethods(
Model model, GapicProductConfig productConfig) {
ImmutableList.Builder<ApiMethodView> exampleMethods = ImmutableList.builder();
for (Interface apiInterface : new InterfaceView().getElementIterable(model)) {
GapicInterfaceContext context = createContext(apiInterface, productConfig);
if (context.getInterfaceConfig().getSmokeTestConfig() != null) {
Method method = context.getInterfaceConfig().getSmokeTestConfig().getMethod();
FlatteningConfig flatteningGroup =
testCaseTransformer.getSmokeTestFlatteningGroup(
context.getMethodConfig(method), context.getInterfaceConfig().getSmokeTestConfig());
GapicMethodContext flattenedMethodContext =
context.asFlattenedMethodContext(method, flatteningGroup);
exampleMethods.add(createExampleApiMethodView(flattenedMethodContext));
}
}
return exampleMethods.build();
}

private OptionalArrayMethodView createExampleApiMethodView(GapicMethodContext context) {
OptionalArrayMethodView initialApiMethodView =
new DynamicLangApiMethodTransformer(new NodeJSApiMethodParamTransformer())
.generateMethod(context);

OptionalArrayMethodView.Builder apiMethodView = initialApiMethodView.toBuilder();

InitCodeTransformer initCodeTransformer = new InitCodeTransformer();
InitCodeView initCodeView =
initCodeTransformer.generateInitCode(
context, testCaseTransformer.createSmokeTestInitContext(context));
apiMethodView.initCode(initCodeView);

return apiMethodView.build();
}

private List<ViewModel> generateMetadataViews(Model model, NodeJSPackageMetadataNamer namer) {
ImmutableList.Builder<ViewModel> views = ImmutableList.builder();
for (String template : TOP_LEVEL_FILES) {
Expand All @@ -81,4 +160,16 @@ private ViewModel generateMetadataView(
.hasMultipleServices(hasMultipleServices)
.build();
}

private GapicInterfaceContext createContext(
Interface apiInterface, GapicProductConfig productConfig) {
return GapicInterfaceContext.create(
apiInterface,
productConfig,
new ModelTypeTable(
new JSTypeTable(productConfig.getPackageName()),
new NodeJSModelTypeNameConverter(productConfig.getPackageName())),
new NodeJSSurfaceNamer(productConfig.getPackageName(), NodeJSUtils.isGcloud(productConfig)),
new NodeJSFeatureConfig());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
import com.google.api.codegen.config.GrpcStreamingConfig.GrpcStreamingType;
import com.google.api.codegen.config.SingleResourceNameConfig;
import com.google.api.codegen.config.VisibilityConfig;
import com.google.api.codegen.metacode.InitFieldConfig;
import com.google.api.codegen.transformer.FeatureConfig;
import com.google.api.codegen.transformer.GapicInterfaceContext;
import com.google.api.codegen.transformer.ModelTypeFormatterImpl;
import com.google.api.codegen.transformer.ModelTypeTable;
import com.google.api.codegen.transformer.SurfaceNamer;
import com.google.api.codegen.transformer.Synchronicity;
import com.google.api.codegen.util.CommonRenderingUtil;
import com.google.api.codegen.util.Name;
import com.google.api.codegen.util.NamePath;
import com.google.api.codegen.util.js.JSCommentReformatter;
Expand All @@ -40,9 +42,11 @@
import com.google.api.tools.framework.model.Method;
import com.google.api.tools.framework.model.ProtoFile;
import com.google.api.tools.framework.model.TypeRef;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -465,4 +469,25 @@ public String getStreamTypeName(GrpcStreamingType type) {
"SurfaceNamer.getStreamTypeName(GrpcStreamingType." + type.toString() + ")");
}
}

@Override
public String injectRandomStringGeneratorCode(String randomString) {
String delimiter = ",";
String[] split =
CommonRenderingUtil.stripQuotes(randomString)
.replace(
InitFieldConfig.RANDOM_TOKEN, delimiter + InitFieldConfig.RANDOM_TOKEN + delimiter)
.split(delimiter);
ArrayList<String> stringParts = new ArrayList<>();
for (String token : split) {
if (token.length() > 0) {
if (token.equals(InitFieldConfig.RANDOM_TOKEN)) {
stringParts.add("Date.now().toString()");
} else {
stringParts.add("\"" + token + "\"");
}
}
}
return Joiner.on(" + ").join(stringParts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.codegen.config.VersionBound;
import com.google.api.codegen.grpcmetadatagen.GenerationLayer;
import com.google.api.codegen.grpcmetadatagen.PackageType;
import com.google.api.codegen.viewmodel.ApiMethodView;
import com.google.api.codegen.viewmodel.FileHeaderView;
import com.google.api.codegen.viewmodel.ViewModel;
import com.google.auto.value.AutoValue;
Expand Down Expand Up @@ -99,6 +100,9 @@ public String resourceRoot() {

public abstract boolean hasSmokeTests();

@Nullable
public abstract List<ApiMethodView> exampleMethods();

// TODO(landrito) Currently only Ruby supports using fileHeaderView. Switch all metadata gen to
// use this field.
@Nullable
Expand Down Expand Up @@ -202,6 +206,9 @@ public abstract static class Builder {
/** File header information such as copyright lines and license lines */
public abstract Builder fileHeader(FileHeaderView val);

/** Methods to show smoke test examples for in the readme * */
public abstract Builder exampleMethods(List<ApiMethodView> vals);

public abstract PackageMetadataView build();
}
}
76 changes: 47 additions & 29 deletions src/main/resources/com/google/api/codegen/nodejs/README.md.snip
Original file line number Diff line number Diff line change
@@ -1,45 +1,63 @@
@snippet generate(metadata)
{@metadata.fullName} for NodeJS
=================================================

{@metadata.identifier} contains the IDL-generated [grpc][] library for the service: [{@metadata.fullName}] ({@metadata.majorVersion}) defined in the [googleapis][] git repository.

[googleapis]:https://github.com/googleapis/googleapis/tree/master/{@metadata.protoPath}
[grpc]:http://www.grpc.io/docs/tutorials/basic/node.html
[{@metadata.fullName}]: https://developers.google.com/apis-explorer/?hl=en_US#p/{@metadata.discoveryApiName}/{@metadata.majorVersion}/
@extends "nodejs/method_sample.snip"

Getting started
---------------

{@metadata.identifier} will allow you to connect to the [{@metadata.fullName}][] and access all its methods.
@snippet generate(metadata)
{@title(metadata)}

In order to achieve so, you need to set up authentication, as well as install the library locally.
{@quickStart(metadata)}

{@commonLinkDeclarations(metadata)}
@end

Setup Authentication
--------------------
@private title(metadata)
@# NodeJS Client for {@metadata.fullName} ({@metadata.developmentStatus})

To authenticate all of your API calls, first install and setup the [Google Cloud SDK][].
Once done, you can then run the following command in your terminal:
Idiomatic NodeJS client for [{@metadata.fullName}][Product Documentation]

$ gcloud beta auth application-default login
- [Client Library Documentation][]
- [Product Documentation][]
@end

or
@private quickStart(metadata)
@## Quick Start
In order to use this library, you first need to go through the following steps:

$ gcloud auth login
- [Select or create a Cloud Platform project.](https://console.cloud.google.com/project)
- [Enable the {@metadata.shortName} api.](https://console.cloud.google.com/apis/api/{@metadata.shortName})
- [Setup Authentication.](https://googlecloudplatform.github.io/google-cloud-node/@#/docs/google-cloud/master/guides/authentication)

Please see the [gcloud beta auth application-default login][] to find documentation showing the difference between these commands.
@### Installation
```
$ npm install {@metadata.identifier}
```

[Google Cloud SDK]: https://cloud.google.com/sdk/
[gcloud beta auth application-default login]: https://cloud.google.com/sdk/gcloud/reference/beta/auth/application-default/login
@if metadata.exampleMethods
{@exampleMethods(metadata.exampleMethods, metadata)}

@end
@### Next Steps
- Read the [Client Library Documentation][] for {@metadata.fullName} to see other available methods on the client.
- Read the [{@metadata.fullName} Product documentation][Product Documentation] to learn more about the product and see How-to Guides.
- View this [repository's main README](https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/README.md) to see the full list of Cloud APIs that we cover.
@end

Installation
-------------------
@private exampleMethods(methods, metadata)
@if methods
@### Preview

Install this library using gem:
@end
@join method : methods on BREAK
@#### {@method.apiClassName}
```js
var {@method.apiModuleName} = require('@metadata.identifier').{@metadata.majorVersion}({
// optional auth parameters.
});
$ npm install {@metadata.identifier}
{@decorateSampleCode(method, sampleCode(method))}
```
@end
@end

At this point you are all set to continue.
@private commonLinkDeclarations(metadata)
[Client Library Documentation]: https://googlecloudplatform.github.io/google-cloud-node/@#/docs/{@metadata.shortName}/latest/{@metadata.shortName}
[Product Documentation]: https://cloud.google.com/{@metadata.shortName}
@end
Loading

0 comments on commit d284d84

Please sign in to comment.