-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): fern check now confirms IR version is compatible between g…
…enerator and cli (#4629)
- Loading branch information
1 parent
3332469
commit 7bd1fb9
Showing
23 changed files
with
442 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
packages/cli/fern-definition/validator/src/ast/visitGeneratorsYamlAst.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,29 @@ | ||
import { generatorsYml } from "@fern-api/configuration"; | ||
import { GeneratorsYmlFileAstVisitor } from "./GeneratorsYmlAstVisitor"; | ||
import { noop, visitObject } from "@fern-api/core-utils"; | ||
import { visitGeneratorGroups } from "./visitors/visitGeneratorGroups"; | ||
|
||
export async function visitGeneratorsYamlAst( | ||
contents: generatorsYml.GeneratorsConfigurationSchema, | ||
cliVersion: string, | ||
visitor: Partial<GeneratorsYmlFileAstVisitor> | ||
): Promise<void> { | ||
await visitor.file?.(contents, []); | ||
await visitObject(contents, { | ||
"auth-schemes": noop, | ||
api: noop, | ||
whitelabel: noop, | ||
metadata: noop, | ||
readme: noop, | ||
"default-group": noop, | ||
reviewers: noop, | ||
openapi: noop, | ||
"openapi-overrides": noop, | ||
"spec-origin": noop, | ||
"async-api": noop, | ||
"api-settings": noop, | ||
groups: async (groups) => { | ||
await visitGeneratorGroups({ groups, visitor, nodePath: ["groups"], cliVersion }); | ||
} | ||
}); | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/cli/fern-definition/validator/src/ast/visitors/visitGeneratorGroups.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { generatorsYml } from "@fern-api/configuration"; | ||
import { NodePath } from "@fern-api/fern-definition-schema"; | ||
import { GeneratorsYmlFileAstVisitor } from "../GeneratorsYmlAstVisitor"; | ||
|
||
export async function visitGeneratorGroups({ | ||
groups, | ||
visitor, | ||
nodePath, | ||
cliVersion | ||
}: { | ||
groups: Record<string, generatorsYml.GeneratorGroupSchema> | undefined; | ||
visitor: Partial<GeneratorsYmlFileAstVisitor>; | ||
nodePath: NodePath; | ||
cliVersion: string; | ||
}): Promise<void> { | ||
if (groups == null) { | ||
return; | ||
} | ||
for (const [groupName, group] of Object.entries(groups)) { | ||
await visitGeneratorGroup({ group, visitor, nodePath: [...nodePath, groupName], cliVersion }); | ||
} | ||
} | ||
|
||
async function visitGeneratorGroup({ | ||
group, | ||
visitor, | ||
nodePath, | ||
cliVersion | ||
}: { | ||
group: generatorsYml.GeneratorGroupSchema; | ||
visitor: Partial<GeneratorsYmlFileAstVisitor>; | ||
nodePath: NodePath; | ||
cliVersion: string; | ||
}): Promise<void> { | ||
await Promise.all( | ||
group.generators.map( | ||
async (generator, idx) => | ||
await visitor.generatorInvocation?.({ invocation: generator, cliVersion }, [ | ||
...nodePath, | ||
"generators", | ||
idx.toString(), | ||
generator.name | ||
]) | ||
) | ||
); | ||
} |
50 changes: 50 additions & 0 deletions
50
packages/cli/fern-definition/validator/src/createGeneratorsYmlAstVisitorForRules.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { NodePath } from "@fern-api/fern-definition-schema"; | ||
import { GeneratorsYmlFileAstNodeTypes, GeneratorsYmlFileAstNodeVisitor } from "./ast"; | ||
import { RuleVisitors } from "./Rule"; | ||
import { ValidationViolation } from "./ValidationViolation"; | ||
import { generatorsYml } from "@fern-api/configuration"; | ||
import { GeneratorsYmlFileAstVisitor } from "./ast/GeneratorsYmlAstVisitor"; | ||
|
||
export function createGeneratorsYmlAstVisitorForRules({ | ||
relativeFilepath, | ||
contents, | ||
allRuleVisitors, | ||
addViolations | ||
}: { | ||
relativeFilepath: RelativeFilePath; | ||
contents: generatorsYml.GeneratorsConfigurationSchema; | ||
allRuleVisitors: RuleVisitors[]; | ||
addViolations: (newViolations: ValidationViolation[]) => void; | ||
}): GeneratorsYmlFileAstVisitor { | ||
function createAstNodeVisitor<K extends keyof GeneratorsYmlFileAstNodeTypes>( | ||
nodeType: K | ||
): Record<K, GeneratorsYmlFileAstNodeVisitor<K>> { | ||
const visit: GeneratorsYmlFileAstNodeVisitor<K> = async ( | ||
node: GeneratorsYmlFileAstNodeTypes[K], | ||
nodePath: NodePath | ||
) => { | ||
for (const ruleVisitors of allRuleVisitors) { | ||
const visitFromRule = ruleVisitors.generatorsYml?.[nodeType]; | ||
if (visitFromRule != null) { | ||
const ruleViolations = await visitFromRule(node, { relativeFilepath, contents }); | ||
addViolations( | ||
ruleViolations.map((violation) => ({ | ||
severity: violation.severity, | ||
relativeFilepath, | ||
nodePath, | ||
message: violation.message | ||
})) | ||
); | ||
} | ||
} | ||
}; | ||
|
||
return { [nodeType]: visit } as Record<K, GeneratorsYmlFileAstNodeVisitor<K>>; | ||
} | ||
|
||
return { | ||
...createAstNodeVisitor("file"), | ||
...createAstNodeVisitor("generatorInvocation") | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...nition/validator/src/rules/compatible-ir-versions/__test__/compatible-ir-versions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils"; | ||
import { getViolationsForRule } from "../../../testing-utils/getViolationsForRule"; | ||
import { ValidationViolation } from "../../../ValidationViolation"; | ||
import { CompatibleIrVersionsRule } from "../compatible-ir-versions"; | ||
|
||
describe("compatible-ir-versions", () => { | ||
it("simple failure", async () => { | ||
process.env.DEFAULT_FDR_ORIGIN = "https://registry-dev2.buildwithfern.com"; | ||
const violations = await getViolationsForRule({ | ||
rule: CompatibleIrVersionsRule, | ||
absolutePathToWorkspace: join( | ||
AbsoluteFilePath.of(__dirname), | ||
RelativeFilePath.of("fixtures"), | ||
RelativeFilePath.of("simple") | ||
), | ||
cliVersion: "0.1.3-rc0" | ||
}); | ||
|
||
const expectedViolations: ValidationViolation[] = [ | ||
{ | ||
severity: "error", | ||
relativeFilepath: RelativeFilePath.of("generators.yml"), | ||
nodePath: ["groups", "python-sdk", "generators", "0", "fernapi/fern-python-sdk"], | ||
message: | ||
"The generator fernapi/fern-python-sdk requires CLI version 0.23.0-rc4 or later (current version: 0.1.3-rc0). Please run `fern upgrade` to upgrade your CLI version and use this generator." | ||
} | ||
]; | ||
|
||
expect(violations).toEqual(expectedViolations); | ||
}); | ||
|
||
it("simple success", async () => { | ||
process.env.DEFAULT_FDR_ORIGIN = "https://registry-dev2.buildwithfern.com"; | ||
const violations = await getViolationsForRule({ | ||
rule: CompatibleIrVersionsRule, | ||
absolutePathToWorkspace: join( | ||
AbsoluteFilePath.of(__dirname), | ||
RelativeFilePath.of("fixtures"), | ||
RelativeFilePath.of("simple") | ||
), | ||
// Latest CLI at the time of writing, so should definitely work | ||
cliVersion: "0.41.10" | ||
}); | ||
|
||
const expectedViolations: ValidationViolation[] = []; | ||
|
||
expect(violations).toEqual(expectedViolations); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
...on/validator/src/rules/compatible-ir-versions/__test__/fixtures/simple/definition/api.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
name: simple-api |
23 changes: 23 additions & 0 deletions
23
...validator/src/rules/compatible-ir-versions/__test__/fixtures/simple/definition/simple.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
types: | ||
_InvalidType: | ||
properties: | ||
a: string | ||
|
||
ValidType: | ||
properties: | ||
b: string | ||
|
||
service: | ||
base-path: / | ||
auth: false | ||
endpoints: | ||
exampleEndpoint: | ||
path: "" | ||
method: POST | ||
request: | ||
name: ExampleEndpointRequest | ||
query-parameters: | ||
some-query-param: optional<string> | ||
body: | ||
properties: | ||
foo: integer |
16 changes: 16 additions & 0 deletions
16
...nition/validator/src/rules/compatible-ir-versions/__test__/fixtures/simple/generators.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
groups: | ||
python-sdk: | ||
audiences: | ||
- external | ||
generators: | ||
- name: fernapi/fern-python-sdk | ||
version: 2.0.0 | ||
output: | ||
location: pypi | ||
package-name: fern-api | ||
token: ${PYPI_TOKEN} | ||
github: | ||
repository: fern-api/python-sdk | ||
license: MIT | ||
config: | ||
client_class_name: Fern |
Oops, something went wrong.