Skip to content

Commit

Permalink
[TypeSpec Validation] verify folder structure (#25712)
Browse files Browse the repository at this point in the history
* folder structure

* typespec folder depth limit

* main.tsp and client.tsp

* fix windows separator issue
  • Loading branch information
ckairen authored and jnlycklama committed Nov 8, 2023
1 parent d7a39b6 commit 4a3ffa6
Showing 1 changed file with 53 additions and 0 deletions.
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 {
}
});

// Verify top level folder is lower case
let folderStruct = folder.split("/");
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 '.'
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

0 comments on commit 4a3ffa6

Please sign in to comment.