Update Lint tool invoke script : separate rules to data-plane and arm (#29)

* Update Lint tool invoke script : separate rules to data-plane and arm

* improve npm test script

* add npm test to pipeline
This commit is contained in:
JianyeXi 2020-02-27 14:13:19 +08:00 коммит произвёл GitHub
Родитель 6f1b458146
Коммит bce4854295
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 293 добавлений и 7 удалений

Просмотреть файл

@ -6,6 +6,19 @@ jobs:
pool:
vmImage: 'Ubuntu 16.04'
steps:
- task: Npm@1
displayName: 'npm install'
inputs:
verbose: false
- task: Npm@1
displayName: 'npm test'
inputs:
command: custom
verbose: false
customCommand: test
- task: Npm@1
displayName: 'npm pack'
inputs:

Просмотреть файл

@ -6,11 +6,12 @@
"main": "dist/index.js",
"files": [
"dist/",
"src/"
"src/",
"tests/"
],
"scripts": {
"tsc": "tsc",
"test": "tsc",
"test": "tsc && mocha dist/tests/*.js",
"prepack": "npm install && tsc"
},
"repository": {
@ -30,7 +31,11 @@
"@types/jsonpath": "^0.2.0",
"@types/node": "^11.13.6",
"@types/request": "^2.48.1",
"typescript": "^3.4.4"
"typescript": "^3.4.4",
"mocha": "^7.0.1",
"mocha-typescript": "^1.0.22",
"@types/mocha": "^5.2.6",
"@types/commonmark": "^0.27.0"
},
"dependencies": {
"@octokit/rest": "^16.42.0",
@ -41,6 +46,9 @@
"glob": "^7.1.3",
"js-yaml": "^3.13.1",
"oav": "0.20.9",
"request": "^2.88.0"
"request": "^2.88.0",
"commonmark": "0.27.0",
"mocha-cli": "^1.0.1",
"mocha-typescript": "1.0.22"
}
}

Просмотреть файл

@ -19,7 +19,10 @@ async function getLinterResult(swaggerPath: string|null|undefined) {
if (!fs.existsSync(swaggerPath)) {
return [];
}
let cmd = "npx autorest --reset && " + linterCmd + swaggerPath;
let openapiType = await utils.getOpenapiType(swaggerPath);
let openapiTypeCmd = ' --openapi-type=' + openapiType + ' ';
let cmd = "npx autorest --reset && " + linterCmd + openapiTypeCmd + swaggerPath;
console.log(`Executing: ${cmd}`);
const { err, stdout, stderr } = await new Promise(res => exec(cmd, { encoding: 'utf8', maxBuffer: 1024 * 1024 * 64 },
(err: unknown, stdout: unknown, stderr: unknown) => res({ err: err, stdout: stdout, stderr: stderr })));

Просмотреть файл

@ -13,6 +13,7 @@ import * as util from 'util'
import { execSync } from 'child_process'
import { devOps } from '@azure/avocado'
import * as childProcess from 'child_process'
import * as commonmark from "commonmark";
export const exec = util.promisify(childProcess.exec)
@ -324,3 +325,65 @@ export const initializeValidator = async function() {
context.validator = validator;
return context;
};
/**
* Get Openapi Type From readme.md ,If failed then from the path
* @returns {string} arm | data-plane | default
*/
export const getOpenapiType = async function(configFile: string):Promise<string> {
try {
let rawMarkdown = fs.readFileSync(configFile, 'utf8');
for (const codeBlock of ParseCodeblocks(rawMarkdown)) {
if (codeBlock.info?.trim().toLocaleLowerCase() !== "yaml") {
continue;
}
let lines = codeBlock.literal?.trim().split("\n")
if (lines === undefined) {
continue;
}
for (let line of lines) {
if (line?.trim().startsWith("openapi-type:")) {
let openapiType = line?.trim().split(":")[1].trim();
return new Promise((resolve) => {
resolve(openapiType);
})
}
}
}
}
catch(err) {
console.log("parse failed with msg:" + err);
}
if ( configFile.match(/.+\/specification\/.*\/resource-manager\/.*readme.md$/g)) {
return new Promise((resolve) => {
resolve("arm");
})
}
else if (configFile.match(/.+\/specification\/.*\/data-plane\/.*readme.md$/g)) {
return new Promise((resolve) => {
resolve("data-plane");
})
}
else {
return new Promise((resolve) => {
resolve("default");
})
}
function ParseCommonmark(markdown: string): commonmark.Node {
return new commonmark.Parser().parse(markdown);
}
function* ParseCodeblocks(markdown: string): Iterable<commonmark.Node> {
const parsed = ParseCommonmark(markdown);
const walker = parsed.walker();
let event;
while ((event = walker.next())) {
let node = event.node;
if (event.entering && node.type === "code_block") {
yield node;
}
}
}
}

Просмотреть файл

@ -0,0 +1,71 @@
# Services
> see https://aka.ms/autorest
This is the AutoRest configuration file.
# Notice
---
## Getting Started
To build the SDK for Services, simply [Install AutoRest](https://aka.ms/autorest/install) and in this folder, run:
> `autorest`
To see additional help and options, run:
> `autorest --help`
---
## Configuration
### Basic Information
These are the global settings
``` yaml
openapi-type: arm
tag: package-2017-04
```
### Tag: package-2017-04
These settings apply only when `--tag=package-2017-04` is specified on the command line.
``` yaml $(tag) == 'package-2017-04'
input-file:
- Microsoft.Services/stable/2017-04-18/services.json
```
### Tag: package-2016-02-preview
These settings apply only when `--tag=package-2016-02-preview` is specified on the command line.
``` yaml $(tag) == 'package-2016-02-preview'
input-file:
- Microsoft.Services/preview/2016-02-01-preview/services.json
```
---
# Code Generation
## Swagger to SDK
This section describes what SDK should be generated by the automatic system.
This is not used by Autorest itself.
``` yaml $(swagger-to-sdk)
swagger-to-sdk:
- repo: azure-sdk-for-net
- repo: azure-sdk-for-python
- repo: azure-sdk-for-java
- repo: azure-sdk-for-go
- repo: azure-sdk-for-js
- repo: azure-sdk-for-node
- repo: azure-sdk-for-ruby
after_scripts:
- bundle install && rake arm:regen_all_profiles['azure_mgmt__services']
```

Просмотреть файл

@ -0,0 +1,95 @@
# Services Face SDK
> see https://aka.ms/autorest
Configuration for generating Face SDK.
The current release is `release_1_0`.
``` yaml
tag: release_1_0
add-credentials: true
openapi-type: data-plane
```
# Releases
### Release 1.0
These settings apply only when `--tag=release_1_0` is specified on the command line.
``` yaml $(tag) == 'release_1_0'
input-file: stable/v1.0/Face.json
```
## Swagger to SDK
This section describes what SDK should be generated by the automatic system.
This is not used by Autorest itself.
``` yaml $(swagger-to-sdk)
swagger-to-sdk:
- repo: azure-sdk-for-python
- repo: azure-sdk-for-java
- repo: azure-sdk-for-go
- repo: azure-sdk-for-js
- repo: azure-sdk-for-node
- repo: azure-sdk-for-ruby
```
## CSharp Settings
These settings apply only when `--csharp` is specified on the command line.
``` yaml $(csharp)
csharp:
sync-methods: None
license-header: MICROSOFT_MIT_NO_VERSION
azure-arm: false
namespace: Microsoft.Azure.Services.Vision.Face
output-folder: $(csharp-sdks-folder)/services/Vision.Face/src/Generated
clear-output-folder: true
```
## Go
See configuration in [readme.go.md](./readme.go.md)
## Java
These settings apply only when `--java` is specified on the command line.
Please also specify `--azure-libraries-for-java-folder=<path to the root directory of your azure-libraries-for-java clone>`.
``` yaml $(java)
java:
azure-arm: true
namespace: com.microsoft.azure.services.vision.faceapi
license-header: MICROSOFT_MIT_NO_CODEGEN
payload-flattening-threshold: 1
output-folder: $(azure-libraries-for-java-folder)/services/data-plane/vision/faceapi
with-optional-parameters: true
with-single-async-method: true
```
## Multi-API/Profile support for AutoRest v3 generators
AutoRest V3 generators require the use of `--tag=all-api-versions` to select api files.
This block is updated by an automatic script. Edits may be lost!
``` yaml $(tag) == 'all-api-versions' /* autogenerated */
# include the azure profile definitions from the standard location
require: $(this-folder)/../../../../profiles/readme.md
# all the input files across all versions
input-file:
- $(this-folder)/stable/v1.0/Face.json
```
If there are files that should not be in the `all-api-versions` set,
uncomment the `exclude-file` section below and add the file paths.
``` yaml $(tag) == 'all-api-versions'
#exclude-file:
# - $(this-folder)/Microsoft.Example/stable/2010-01-01/somefile.json
```

33
tests/utilsTest.ts Normal file
Просмотреть файл

@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License in the project root for license information.
import { suite, test, slow, timeout, skip, only } from "mocha-typescript";
import * as assert from "assert";
import {utils as utils} from "../src/index"
@suite class UtilsTest {
@test async "TestGetOpenapiTypeDataplane" () {
let openapiType = await utils.getOpenapiType("./tests/Resource/openapi-type-data-plane-readme.md")
assert.equal(openapiType,"data-plane")
}
@test async "TestGetOpenapiTypeDataplanArm" () {
let openapiType = await utils.getOpenapiType("./tests/Resource/openapi-type-arm-readme.md")
assert.equal(openapiType,"arm")
}
@test async "TestGetOpenapiTypeNoExistFile" () {
let openapiType = await utils.getOpenapiType("C:/code/data-plane/test/readme.md")
assert.equal(openapiType,"default")
}
@test async "TestGetOpenapiTypeFromPathWithArm" () {
let openapiType = await utils.getOpenapiType("C:/specification/test/resource-manager/test/readme.md")
assert.equal(openapiType,"arm")
}
@test async "TestGetOpenapiTypeFromPathWithDataPlane" () {
let openapiType = await utils.getOpenapiType("/home/work/1/spec/specification/test/data-plane/test/readme.md")
assert.equal(openapiType,"data-plane")
}
}

Просмотреть файл

@ -46,7 +46,7 @@
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
@ -56,7 +56,7 @@
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
}
}