Skip to content

Commit

Permalink
build: automate @angular/cli schema.json generation
Browse files Browse the repository at this point in the history
With this change we automate the generation of `@angular/cli/lib/config/schema.json`. While on paper we could use quicktype for this. Quicktype doesn't handle `patternProperties` and `oneOf` that well.

How does this works?
Relative `$ref` will be resolved and inlined as part of the root schema definitions.

Example
```json
"@schematics/angular:enum": {
    "$ref": "../../../../schematics/angular/enum/schema.json"
},
```

Will be parsed and transformed to
```json
"@schematics/angular:enum": {
  "$ref": "#/definitions/SchematicsAngularEnumSchema"
},
"definitions: {
  "SchematicsAngularEnumSchema": {
    "title": "Angular Enum Options Schema",
    "type": "object",
    "description": "Generates a new, generic enum definition for the given or default project.",
    "properties": {...}
   }
}
```
  • Loading branch information
alan-agius4 committed Mar 11, 2021
1 parent d254d05 commit 4b0223b
Show file tree
Hide file tree
Showing 12 changed files with 782 additions and 2,189 deletions.
47 changes: 44 additions & 3 deletions packages/angular/cli/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

load("@npm//@bazel/jasmine:index.bzl", "jasmine_node_test")
load("//tools:ts_json_schema.bzl", "ts_json_schema")
load("//tools:ng_cli_schema_generator.bzl", "cli_json_schema")
load("//tools:defaults.bzl", "ts_library")

# @external_begin
Expand All @@ -28,7 +29,7 @@ ts_library(
) + [
# @external_begin
# These files are generated from the JSON schema
"//packages/angular/cli:lib/config/schema.ts",
"//packages/angular/cli:lib/config/workspace-schema.ts",
"//packages/angular/cli:commands/analytics.ts",
"//packages/angular/cli:commands/add.ts",
"//packages/angular/cli:commands/build.ts",
Expand Down Expand Up @@ -58,8 +59,11 @@ ts_library(
exclude = [
# NB: we need to exclude the nested node_modules that is laid out by yarn workspaces
"node_modules/**",
"cli/lib/config/workspace-schema.json",
],
),
) + [
"//packages/angular/cli:lib/config/schema.json",
],
module_name = "@angular/cli",
# strict_checks = False,
deps = [
Expand All @@ -84,9 +88,46 @@ ts_library(
],
)

CLI_SCHEMA_DATA = [
"//packages/angular_devkit/build_angular:src/app-shell/schema.json",
"//packages/angular_devkit/build_angular:src/browser/schema.json",
"//packages/angular_devkit/build_angular:src/dev-server/schema.json",
"//packages/angular_devkit/build_angular:src/extract-i18n/schema.json",
"//packages/angular_devkit/build_angular:src/karma/schema.json",
"//packages/angular_devkit/build_angular:src/ng-packagr/schema.json",
"//packages/angular_devkit/build_angular:src/protractor/schema.json",
"//packages/angular_devkit/build_angular:src/server/schema.json",
"//packages/angular_devkit/build_angular:src/tslint/schema.json",
"//packages/schematics/angular:app-shell/schema.json",
"//packages/schematics/angular:application/schema.json",
"//packages/schematics/angular:class/schema.json",
"//packages/schematics/angular:component/schema.json",
"//packages/schematics/angular:directive/schema.json",
"//packages/schematics/angular:enum/schema.json",
"//packages/schematics/angular:guard/schema.json",
"//packages/schematics/angular:interceptor/schema.json",
"//packages/schematics/angular:interface/schema.json",
"//packages/schematics/angular:library/schema.json",
"//packages/schematics/angular:module/schema.json",
"//packages/schematics/angular:ng-new/schema.json",
"//packages/schematics/angular:pipe/schema.json",
"//packages/schematics/angular:resolver/schema.json",
"//packages/schematics/angular:service/schema.json",
"//packages/schematics/angular:service-worker/schema.json",
"//packages/schematics/angular:web-worker/schema.json",
]

cli_json_schema(
name = "cli_config_schema",
src = "lib/config/workspace-schema.json",
out = "lib/config/schema.json",
data = CLI_SCHEMA_DATA,
)

ts_json_schema(
name = "cli_schema",
src = "lib/config/schema.json",
src = "lib/config/workspace-schema.json",
data = CLI_SCHEMA_DATA,
)

ts_json_schema(
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/commands/add-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { analytics, tags } from '@angular-devkit/core';
import { NodePackageDoesNotSupportSchematics } from '@angular-devkit/schematics/tools';
import { dirname, join } from 'path';
import { intersects, prerelease, rcompare, satisfies, valid, validRange } from 'semver';
import { PackageManager } from '../lib/config/schema';
import { PackageManager } from '../lib/config/workspace-schema';
import { isPackageNameSafeForAnalytics } from '../models/analytics';
import { Arguments } from '../models/interface';
import { RunSchematicOptions, SchematicCommand } from '../models/schematic-command';
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/cli/commands/update-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { execSync } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as semver from 'semver';
import { PackageManager } from '../lib/config/schema';
import { PackageManager } from '../lib/config/workspace-schema';
import { Command } from '../models/command';
import { Arguments } from '../models/interface';
import { SchematicEngineHost } from '../models/schematic-engine-host';
Expand Down
Loading

0 comments on commit 4b0223b

Please sign in to comment.