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

[TypeSpec Validation] verify folder structure #25712

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions eng/tools/TypeSpecValidation/src/rules/folder-structure.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { globby } from "globby";
import path from "path";
import { Rule } from "../rule.js";
import { RuleResult } from "../rule-result.js";
import { checkFileExists } from "../utils.js";

export class FolderStructureRule implements Rule {
readonly name = "FolderStructure";
Expand All @@ -18,6 +20,57 @@ export class FolderStructureRule implements Rule {
}
mikeharder marked this conversation as resolved.
Show resolved Hide resolved
});

// Verify top level folder is lower case
let folderStruct = folder.split(path.sep);
if (folderStruct[1].match(/[A-Z]/g)) {
success = false;
errorOutput += `Invalid folder name. Folders under specification/ must be lower case.\n`;
}

let packageFolder = folderStruct[folderStruct.length - 1];

// Verify package folder is at most 3 levels deep
if (folderStruct.length > 4) {
success = false;
errorOutput += `Please limit TypeSpec folder depth to 3 levels or less`;
}

// Verify second level folder is capitalized after each '.'
ckairen marked this conversation as resolved.
Show resolved Hide resolved
if (
/(^|\. *)([a-z])/g.test(packageFolder) &&
!["data-plane", "resource-manager"].includes(packageFolder)
) {
success = false;
errorOutput += `Invalid folder name. Folders under specification/${folderStruct[1]} must be capitalized after each '.'\n`;
}

// Verify 'Shared' follows 'Management'
if (packageFolder.includes("Management") && packageFolder.includes("Shared")) {
if (!packageFolder.includes("Management.Shared")) {
success = false;
errorOutput += `Invalid folder name. For management libraries with a shared component, 'Shared' should follow 'Management'.`;
}
}

// Verify tspconfig, main.tsp, examples/
let containsMinStruct =
(await checkFileExists(path.join(folder, "main.tsp"))) ||
(await checkFileExists(path.join(folder, "client.tsp")));

if (await checkFileExists(path.join(folder, "main.tsp"))) {
containsMinStruct =
containsMinStruct && (await checkFileExists(path.join(folder, "examples")));
}

if (!packageFolder.includes("Shared")) {
containsMinStruct =
containsMinStruct && (await checkFileExists(path.join(folder, "tspconfig.yaml")));
}
if (!containsMinStruct) {
success = false;
errorOutput += `Invalid folder structure. Package must contain main.tsp or client.tsp, tspconfig.yaml, and examples folder if there's main.tsp.`;
}

return {
success: success,
stdOutput: stdOutput,
Expand Down
Loading