Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic Sample generation in README #11

Merged
merged 7 commits into from
Oct 21, 2017
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ autorest --typescript --output-folder=<path-to-the-output-folder(usually upto li
```
autorest --typescript --output-folder=<path-to-the-output-folder(usually upto lib or src folder of your project)> --license-header=MICROSOFT_MIT_NO_VERSION <path-to-readme.md> -package-name=<your-package-name> --package-version<your-package-version>
```
- If you want to generate metadata files (package.json, .npmignore, webpack.config.js, tsconfig.json), then provide `--generate-metadata=true`:
- If you want to generate metadata files **__(package.json, .npmignore, webpack.config.js, tsconfig.json, README.md(with a sample))__**, then provide `--generate-metadata=true`:

**NOTE: This will generate all the _metadata_ files one level above the output-folder.**
**NOTE: This will generate all the __metadata__ files one level above the output-folder.**
```
autorest --typescript --output-folder=<path-to-the-output-folder(usually upto lib or src folder of your project)> --license-header=MICROSOFT_MIT_NO_VERSION --input-file=<path-to-swagger-spec> --package-name=<your-package-name> --package-version<your-package-version> --generate-metadata=true
```
Expand Down
4 changes: 4 additions & 0 deletions src/azure/CodeGeneratorTSa.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public override async Task Generate(CodeModel cm)
// .npmignore
var npmIgnore = new NpmIgnore { Model = codeModel };
await Write(npmIgnore, Path.Combine("../", ".npmignore"));

//README.md
var readme = new AzureReadmeTemplate { Model = codeModel };
await Write(readme, Path.Combine("../", "README.md"));
}
}
}
Expand Down
74 changes: 74 additions & 0 deletions src/azure/Templates/AzureReadmeTemplate.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@using AutoRest.Core.Utilities
@using AutoRest.TypeScript.Azure.Model
@inherits AutoRest.Core.Template<CodeModelTSa>

# Microsoft Azure SDK for isomorphic javascript - @(Model.Name)
This project provides an isomorphic javascript package for accessing Azure. Right now it supports:
- node.js version 6.x.x or higher
- browser javascript
@EmptyLine
## How to Install
@EmptyLine
- nodejs
```
npm install @(Model.PackageName)
```
- browser
```html
@{
@:<script type="text/javascript" src="https://raw.githubusercontent.com/Azure/azure-sdk-for-js/master/lib/services/@(Model.PackageName)/@(Model.Name.ToCamelCase())Bundle.js"></script>
}
```
@EmptyLine
## How to use
@EmptyLine
### nodejs - Authentication, client creation and @(Model.GetSampleMethod()?.Name) @(Model.GetSampleMethodGroupName()) as an example written in TypeScript.
@EmptyLine
```javascript
import * as msRest from "ms-rest-js";
import * as msRestAzure from "ms-rest-azure-js";
import * as msRestNodeAuth from "ms-rest-nodeauth";
import { @(Model.Name), @(Model.ClientPrefix)Models, @(Model.ClientPrefix)Mappers } from "@(Model.PackageName)";
const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];
@EmptyLine
msRestNodeAuth.interactiveLogin().then((creds) => {
const client = new @(Model.Name)(creds, subscriptionId);
@(Model.GenerateSampleMethod())
}).catch((err) => {
console.log('An error ocurred:');
console.dir(err, {depth: null, colors: true});
});
```
@EmptyLine
### browser - Authentication, client creation and @(Model.GetSampleMethod().Name) @(Model.GetSampleMethodGroupName()) as an example written in javascript.
@EmptyLine
- index.html
```html
@{
@:<!DOCTYPE html>
@:<html lang="en">
@: <head>
@: <title>My Todos</title>
@: <script type="text/javascript" src="https://raw.githubusercontent.com/Azure/ms-rest-js/master/msRestBundle.js"></script>
@: <script type="text/javascript" src="https://raw.githubusercontent.com/Azure/ms-rest-js/master/msRestAzureBundle.js"></script>
@: <script type="text/javascript" src="https://raw.githubusercontent.com/Azure/azure-sdk-for-js/master/lib/services/@(Model.PackageName)/@(Model.Name.ToCamelCase())Bundle.js"></script>
@: <script type="text/javascript">
@: document.write('hello world');
@: const subscriptionId = "<Subscription_Id>";
@: const token = "<access_token>";
@: const creds = new msRest.TokenCredentials(token);
@: const client = new @(Model.Name)(creds, undefined, subscriptionId);
@: @(Model.GenerateSampleMethod(true)).catch((err) => {
@: console.log('An error ocurred:');
@: console.dir(err, { depth: null, colors: true});
@: });
@: </script>
@: </head>
@: <body>
@: </body>
@:</html>
}
```
@EmptyLine
# Related projects
- [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)
3 changes: 2 additions & 1 deletion src/azure/Templates/AzureServiceClientTemplate.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
@using AutoRest.Core.Utilities
@using AutoRest.TypeScript.vanilla.Templates
@using AutoRest.TypeScript
@inherits AutoRest.Core.Template<AutoRest.TypeScript.Azure.Model.CodeModelTSa>
@using AutoRest.TypeScript.Azure.Model
@inherits AutoRest.Core.Template<CodeModelTSa>
/*
@Header(" * ")
*/
Expand Down
145 changes: 144 additions & 1 deletion src/vanilla/ClientModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public static string TSType(this IModelType type, bool inModelsModule) {
{
var enumName = enumType.Name.ToPascalCase();
tsType = "Models." + enumName;
if (inModelsModule || enumName.Contains('.'))
if (inModelsModule || enumName.Contains('.'))
{
tsType = enumName;
}
Expand Down Expand Up @@ -959,5 +959,148 @@ public static bool IsCompositeOrEnumType(this IModelType type)
return false;
}
}

public static string InitializeType(this IModelType paramType, string paramName, bool isBrowser = false)
{
if (paramType is EnumType)
{
return paramType.InitializeEnumType(isBrowser);
}
else if (paramType is PrimaryType)
{
return paramType.InitializePrimaryType(paramName, isBrowser);
}
else if (paramType is SequenceType)
{
return paramType.InitializeSequenceType(paramName, isBrowser);
}
else if (paramType is DictionaryType)
{
return paramType.InitializeDictionaryType(paramName, isBrowser);
}
else if (paramType is CompositeType)
{
return paramType.InitializeCompositeType(paramName, isBrowser);
}

return null;
}

public static string InitializeCompositeType(this IModelType paramType, string paramName, bool isBrowser = false)
{
var builder = new IndentedStringBuilder(" ");
var composite = (CompositeType)paramType;
if (composite.ComposedProperties.Any())
{
builder.AppendLine("{").Indent();
var composedPropertyList = new List<Property>(composite.ComposedProperties.Where(p => /**p.IsRequired &&**/ !p.IsReadOnly));
for (var i = 0; i < composedPropertyList.Count; i++)
{
var prop = composedPropertyList[i];
if (i != composedPropertyList.Count - 1)
{
builder.AppendLine("{0}: {1},", prop.Name, prop.ModelType.InitializeType(prop.Name, isBrowser));
}
else
{
builder.AppendLine("{0}: {1}", prop.Name, prop.ModelType.InitializeType(prop.Name, isBrowser));
}
}
builder.Outdent().Append("}");
}
else
{
builder.AppendLine("{}");
}
return builder.ToString();
}

public static string InitializeDictionaryType(this IModelType paramType, string paramName, bool isBrowser = false)
{
var dictionary = (DictionaryType)paramType;
var paramValue = $"{{ \"key1\": {dictionary.ValueType.InitializeType(paramName, isBrowser)} }}";
return paramValue;
}

public static string InitializeSequenceType(this IModelType paramType, string paramName, bool isBrowser = false)
{
var sequence = (SequenceType)paramType;
var paramValue = $"[{sequence.ElementType.InitializeType(paramName, isBrowser)}]";
return paramValue;
}

public static string InitializeEnumType(this IModelType paramType, bool isBrowser = false)
{
var paramValue = "\"\"";
var enumValue = ((EnumType)paramType).Values[0].SerializedName;
paramValue = $"\"{enumValue}\"";
// TODO: Till we support setting UnderlyingType in autorest.modeler we wil default to string type as the enum value
// if ( ((EnumType)paramType).UnderlyingType.IsPrimaryType(KnownPrimaryType.String))
// {
// paramValue = $"\"{enumValue}\"";
// }
return paramValue;
}

public static string InitializePrimaryType(this IModelType paramType, string paramName, bool isBrowser = false)
{
var paramValue = "\"\"";
if (paramType.IsPrimaryType(KnownPrimaryType.String))
{
if (paramName.EqualsIgnoreCase("location"))
{
paramValue = "\"westus\"";
}
else
{
paramValue = $"\"test{paramName.ToCamelCase()}\"";
}
}
else if (paramType.IsPrimaryType(KnownPrimaryType.Date))
{
paramValue = "new Date().toISOString().substring(0, 10)";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.DateTime))
{
paramValue = "new Date().toISOString()";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.DateTimeRfc1123))
{
paramValue = "new Date().toUTCString()";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.UnixTime))
{
paramValue = "new Date()";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.TimeSpan))
{
paramValue = "\"P1Y2M3DT4H5M6S\"";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.Boolean))
{
paramValue = "true";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.Int) || paramType.IsPrimaryType(KnownPrimaryType.Long))
{
paramValue = "1";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.Decimal) || paramType.IsPrimaryType(KnownPrimaryType.Double))
{
paramValue = "1.01";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.Object))
{
paramValue = "{}";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.Uuid))
{
paramValue = "ec7b1657-199d-4d8a-bbb2-89a11a42e02a";
}
else if (paramType.IsPrimaryType(KnownPrimaryType.Stream))
{
paramValue = isBrowser ? "new ReadableStream()" : "new require(\"stream\").Readable()";
}
return paramValue;
}
}
}
5 changes: 4 additions & 1 deletion src/vanilla/CodeGeneratorTS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//

using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -86,6 +85,10 @@ public override async Task Generate(CodeModel cm)
// .npmignore
var npmIgnore = new NpmIgnore { Model = codeModel };
await Write(npmIgnore, Path.Combine("../", ".npmignore"));

//README.md
var readme = new ReadmeTemplate { Model = codeModel };
await Write(readme, Path.Combine("../", "README.md"));
}
}
}
Expand Down
Loading