Skip to content

Commit

Permalink
feat(metrics): retrieve metrics for builders and schematics (AmadeusI…
Browse files Browse the repository at this point in the history
…TGroup#1183)

## Proposed change
AmadeusITGroup#1201
Retrieve metrics for builders and schematics
  • Loading branch information
matthieu-crouzet authored Jan 17, 2024
2 parents 32ebce1 + 42475fd commit 72c2ab0
Show file tree
Hide file tree
Showing 134 changed files with 1,627 additions and 350 deletions.
2 changes: 1 addition & 1 deletion packages/@ama-sdk/core/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Ng add @ama-sdk/core', () => {
const initialTree = Tree.empty();
initialTree.create('angular.json', readFileSync(join(__dirname, 'mocks', 'angular.mocks.json')));
initialTree.create('src/example.ts', readFileSync(join(__dirname, 'mocks', 'example.ts.mock')));
const context: any = { addTask: jest.fn() };
const context: any = { addTask: jest.fn(), logger: { debug: jest.fn() }, schematic: { description: { name: 'schematic', collection: { name: '@scope/test' }}}};
const tree = await firstValueFrom(callRule(ngAdd, initialTree, context));
const newContent = tree.readText('src/example.ts');
expect(newContent).not.toContain('@dapi/sdk-core');
Expand Down
9 changes: 8 additions & 1 deletion packages/@ama-sdk/core/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {chain, type Rule} from '@angular-devkit/schematics';
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import * as ts from 'typescript';
import * as path from 'node:path';

/**
* Rule to import all the necessary dependency to run an @ama-sdk based application
* Helps to migrate from previous versions with an import replacement
*/
export function ngAdd(): Rule {
function ngAddFn(): Rule {

const checkSchematicsDependency: Rule = async (_, context) => {
try {
Expand Down Expand Up @@ -79,3 +80,9 @@ export function ngAdd(): Rule {
addMandatoryPeerDeps
]);
}

/**
* Rule to import all the necessary dependency to run an @ama-sdk based application
* Helps to migrate from previous versions with an import replacement
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
3 changes: 3 additions & 0 deletions packages/@ama-sdk/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
"@angular-devkit/core": "~17.0.3",
"@angular-devkit/schematics": "~17.0.3",
"@angular-devkit/schematics-cli": "~17.0.3",
"@angular/cli": "~17.0.3",
"@o3r/schematics": "workspace:^",
"@openapitools/openapi-generator-cli": "~2.7.0",
"@schematics/angular": "~17.0.3",
"minimist": "^1.2.6",
"rxjs": "^7.8.1",
"typescript": "~5.2.2"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/@ama-sdk/create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const run = () => {
];

const errors = steps
.map((step) => spawnSync(process.execPath, step.args, { stdio: 'inherit', cwd: step.cwd || process.cwd() }))
.map((step) => spawnSync(process.execPath, step.args, { stdio: 'pipe', cwd: step.cwd || process.cwd() }))
.map(({error}) => error)
.filter((err) => !!err);

Expand Down
5 changes: 5 additions & 0 deletions packages/@ama-sdk/schematics/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
"@angular-devkit/schematics-cli": {
"optional": true
},
"@angular/cli": {
"optional": true
},
"@angular/compiler": {
"optional": true
},
Expand All @@ -46,6 +49,8 @@
"@angular-devkit/core": "~17.0.0",
"@angular-devkit/schematics": "~17.0.0",
"@angular-devkit/schematics-cli": "^17.0.1",
"@angular/cli": "^17.0.1",
"@o3r/schematics": "workspace:^",
"@openapitools/openapi-generator-cli": "~2.7.0",
"@schematics/angular": "~17.0.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
template, Tree,
url
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { NgGenerateApiExtensionSchematicsSchema } from './schema';

/**
* Generate a Extension of a API core definition
* @param options
*/
export function ngGenerateApiExtension(options: NgGenerateApiExtensionSchematicsSchema): Rule {
function ngGenerateApiExtensionFn(options: NgGenerateApiExtensionSchematicsSchema): Rule {

return (tree: Tree) => mergeWith(apply(url('./templates'), [
template({
Expand All @@ -26,3 +27,9 @@ export function ngGenerateApiExtension(options: NgGenerateApiExtensionSchematics
]), MergeStrategy.Overwrite);

}

/**
* Generate a Extension of a API core definition
* @param options
*/
export const ngGenerateApiExtension = createSchematicWithMetricsIfInstalled(ngGenerateApiExtensionFn);
13 changes: 11 additions & 2 deletions packages/@ama-sdk/schematics/schematics/java/client-core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
chain,
Rule,
SchematicContext,
Tree} from '@angular-devkit/schematics';
Tree
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import { promises as fs } from 'node:fs';
import * as path from 'node:path';
import { treeGlob } from '../../helpers/tree-glob';
Expand All @@ -16,7 +18,7 @@ const jarBasePath = path.resolve(__dirname, 'swagger-codegen-java-client', 'targ
* Generate a Java client SDK source code base on swagger specification
* @param options
*/
export function ngGenerateJavaClientCore(options: NgGenerateJavaClientCoreSchematicsSchema): Rule {
function ngGenerateJavaClientCoreFn(options: NgGenerateJavaClientCoreSchematicsSchema): Rule {

const specPath = path.resolve(process.cwd(), options.specPath);
/**
Expand Down Expand Up @@ -84,3 +86,10 @@ export function ngGenerateJavaClientCore(options: NgGenerateJavaClientCoreSchema
})
]);
}


/**
* Generate a Java client SDK source code base on swagger specification
* @param options
*/
export const ngGenerateJavaClientCore = createSchematicWithMetricsIfInstalled(ngGenerateJavaClientCoreFn);
9 changes: 7 additions & 2 deletions packages/@ama-sdk/schematics/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isJsonObject } from '@angular-devkit/core';
import { chain, externalSchematic, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import * as path from 'node:path';
import { readFileSync } from 'node:fs';
import { lastValueFrom } from 'rxjs';
Expand Down Expand Up @@ -159,9 +160,8 @@ const registerPackageSchematics = async (tree: Tree, context: SchematicContext)

/**
* Add Otter ama-sdk-schematics to a Project
* @param options
*/
export function ngAdd(): Rule {
function ngAddFn(): Rule {

return (tree, context) => chain([
registerPackageSchematics,
Expand All @@ -173,3 +173,8 @@ export function ngAdd(): Rule {
}
])(tree, context);
}

/**
* Add Otter ama-sdk-schematics to a Project
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
url
} from '@angular-devkit/schematics';
import type { Operation, PathObject } from '@ama-sdk/core';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import {existsSync, readFileSync} from 'node:fs';
import * as path from 'node:path';
import * as semver from 'semver';
Expand Down Expand Up @@ -39,7 +40,7 @@ const getPathObjectTemplate = (pathObj: PathObject) => {
* Generate a typescript SDK source code base on swagger specification
* @param options
*/
export function ngGenerateTypescriptSDK(options: NgGenerateTypescriptSDKCoreSchematicsSchema): Rule {
function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematicsSchema): Rule {

const specPath = path.resolve(process.cwd(), options.specPath);
const targetPath = options.directory || '';
Expand Down Expand Up @@ -150,3 +151,9 @@ export function ngGenerateTypescriptSDK(options: NgGenerateTypescriptSDKCoreSche
runGeneratorRule
]);
}

/**
* Generate a typescript SDK source code base on swagger specification
* @param options
*/
export const ngGenerateTypescriptSDK = createSchematicWithMetricsIfInstalled(ngGenerateTypescriptSDKFn);
10 changes: 8 additions & 2 deletions packages/@ama-sdk/schematics/schematics/typescript/mock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
Tree,
url
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import * as path from 'node:path';

import {NgGenerateMockSchematicsSchema} from './schema';


Expand Down Expand Up @@ -56,7 +56,7 @@ export function getDasherizeModelName(modelName: string): string {
* Add mock
* @param options
*/
export function ngGenerateMock(options: NgGenerateMockSchematicsSchema): Rule {
function ngGenerateMockFn(options: NgGenerateMockSchematicsSchema): Rule {
const dasherizeModelName = getDasherizeModelName(options.apiModel);

const generateRootBarrel: Rule = (tree: Tree) => {
Expand Down Expand Up @@ -97,3 +97,9 @@ export function ngGenerateMock(options: NgGenerateMockSchematicsSchema): Rule {

return chain(rules);
}

/**
* Add mock
* @param options
*/
export const ngGenerateMock = createSchematicWithMetricsIfInstalled(ngGenerateMockFn);
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('Typescript Shell Generator', () => {
skipInstall: true,
packageManager: 'npm'
}, Tree.empty());
}, 10000);
}, 15000);

it('should generate basic SDK package', () => {
expect(yarnTree.files.sort()).toEqual(baseFileList.sort());
Expand Down
13 changes: 11 additions & 2 deletions packages/@ama-sdk/schematics/schematics/typescript/shell/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import {
Tree,
url
} from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import {dump, load} from 'js-yaml';
import {isAbsolute, posix, relative} from 'node:path';
import {getPackageManagerName, NpmInstall} from '../../helpers/node-install';
import {readPackageJson} from '../../helpers/read-package';
import type {NgGenerateTypescriptSDKShellSchematicsSchema} from './schema';

/**
* Generate Typescript SDK shell
* @param options
*/
export function ngGenerateTypescriptSDK(options: NgGenerateTypescriptSDKShellSchematicsSchema): Rule {
function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKShellSchematicsSchema): Rule {

const installRule = (_tree: Tree, context: SchematicContext) => {
const workingDirectory = options.directory ? (isAbsolute(options.directory) ? relative(process.cwd(), options.directory) : options.directory) : '.';
Expand Down Expand Up @@ -49,7 +51,8 @@ export function ngGenerateTypescriptSDK(options: NgGenerateTypescriptSDKShellSch
'ts-jest': amaSdkSchematicsPackageJson.devDependencies!['ts-jest'],
'globby': amaSdkSchematicsPackageJson.devDependencies!.globby,
'typescript': amaSdkSchematicsPackageJson.devDependencies!.typescript,
'@openapitools/openapi-generator-cli': amaSdkSchematicsPackageJson.devDependencies!['@openapitools/openapi-generator-cli']
'@openapitools/openapi-generator-cli': amaSdkSchematicsPackageJson.devDependencies!['@openapitools/openapi-generator-cli'],
'rxjs': amaSdkSchematicsPackageJson.dependencies!.rxjs
};
const openApiSupportedVersion = typeof amaSdkSchematicsPackageJson.openApiSupportedVersion === 'string' &&
amaSdkSchematicsPackageJson.openApiSupportedVersion.replace(/\^|~/, '');
Expand Down Expand Up @@ -108,3 +111,9 @@ export function ngGenerateTypescriptSDK(options: NgGenerateTypescriptSDKShellSch
...(options.skipInstall ? [] : [installRule])
]);
}

/**
* Generate Typescript SDK shell
* @param options
*/
export const ngGenerateTypescriptSDK = createSchematicWithMetricsIfInstalled(ngGenerateTypescriptSDKFn);
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"@angular-eslint/eslint-plugin-template": "<%= angularEslintVersion %>",
"@angular-eslint/utils": "<%= angularEslintVersion %>",
"@angular/compiler": "<%= angularVersion %>",
"@angular/cli": "<%= angularVersion %>",
"@commitlint/cli": "^17.0.0",
"@schematics/angular": "<%= angularVersion %>",
"@commitlint/config-conventional": "^17.0.0",
Expand Down Expand Up @@ -102,6 +103,7 @@
"lint-staged": "^13.0.0",
"minimist": "^1.2.6",
"rimraf": "^5.0.1",
"rxjs": "<%= versions['rxjs'] %>",
"standard-version": "^9.0.0",
"ts-jest": "<%= versions['ts-jest'] %>",
"typedoc": "~0.25.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import * as path from 'node:path';
import * as fs from 'node:fs';
import { firstValueFrom } from 'rxjs';
import { ngAddAnalytics } from './index';
import { ngAddAnalyticsFn } from './index';

const collectionPath = path.join(__dirname, '..', '..', 'collection.json');
const o3rComponentPath = '/src/components/test/test.component.ts';
Expand Down Expand Up @@ -100,7 +100,7 @@ export class NgComponent {}
it('should throw if no Otter component', async () => {
const runner = new SchematicTestRunner('schematics', collectionPath);

await expect(firstValueFrom(runner.callRule(ngAddAnalytics({
await expect(firstValueFrom(runner.callRule(ngAddAnalyticsFn({
path: ngComponentPath,
skipLinter: false,
activateDummy: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
addInterfaceToClassTransformerFactory,
applyEsLintFix,
askConfirmationToConvertComponent,
createSchematicWithMetricsIfInstalled,
generateClassElementsFromString,
getO3rComponentInfoOrThrowIfNotFound,
isNgClassDecorator,
Expand Down Expand Up @@ -66,10 +67,9 @@ const checkAnalytics = (componentPath: string, tree: Tree, baseFileName: string)

/**
* Add analytics to an existing component
*
* @param options
*/
export function ngAddAnalytics(options: NgAddAnalyticsSchematicsSchema): Rule {
export function ngAddAnalyticsFn(options: NgAddAnalyticsSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const baseFileName = basename(options.path, '.component.ts');
Expand Down Expand Up @@ -236,11 +236,17 @@ export function ngAddAnalytics(options: NgAddAnalyticsSchematicsSchema): Rule {
externalSchematic('@o3r/core', 'convert-component', {
path: options.path
}),
ngAddAnalytics(options)
ngAddAnalyticsFn(options)
]);
}
}
throw e;
}
};
}

/**
* Add analytics to an existing component
* @param options
*/
export const ngAddAnalytics = createSchematicWithMetricsIfInstalled(ngAddAnalyticsFn);
10 changes: 7 additions & 3 deletions packages/@o3r/analytics/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { Rule } from '@angular-devkit/schematics';
import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';

/**
* Add Otter analytics to an Angular Project
*
* @param options
*/
export function ngAdd(): Rule {
function ngAddFn(): Rule {
/* ng add rules */
return () => {};
}

/**
* Add Otter analytics to an Angular Project
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
9 changes: 7 additions & 2 deletions packages/@o3r/apis-manager/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { chain, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';

import { createSchematicWithMetricsIfInstalled } from '@o3r/schematics';
import * as path from 'node:path';
import { NgAddSchematicsSchema } from './schema';

/**
* Add Otter apis manager to an Angular Project
* @param options
*/
export function ngAdd(options: NgAddSchematicsSchema): Rule {
function ngAddFn(options: NgAddSchematicsSchema): Rule {
return async (tree: Tree, context: SchematicContext) => {
try {
const { ngAddPackages, getO3rPeerDeps, applyEsLintFix, getWorkspaceConfig, getProjectNewDependenciesType } = await import('@o3r/schematics');
Expand Down Expand Up @@ -45,3 +45,8 @@ export function ngAdd(options: NgAddSchematicsSchema): Rule {

}

/**
* Add Otter apis manager to an Angular Project
* @param options
*/
export const ngAdd = createSchematicWithMetricsIfInstalled(ngAddFn);
Loading

0 comments on commit 72c2ab0

Please sign in to comment.