@autorest/python 6.19.0 → 6.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/generator/pygen/black.py +2 -3
  2. package/generator/pygen/codegen/models/combined_type.py +1 -1
  3. package/generator/pygen/codegen/models/credential_types.py +7 -14
  4. package/generator/pygen/codegen/models/enum_type.py +1 -1
  5. package/generator/pygen/codegen/models/lro_operation.py +0 -1
  6. package/generator/pygen/codegen/models/lro_paging_operation.py +1 -1
  7. package/generator/pygen/codegen/models/model_type.py +5 -7
  8. package/generator/pygen/codegen/models/operation.py +13 -2
  9. package/generator/pygen/codegen/models/paging_operation.py +0 -1
  10. package/generator/pygen/codegen/models/parameter.py +5 -1
  11. package/generator/pygen/codegen/models/parameter_list.py +2 -5
  12. package/generator/pygen/codegen/models/primitive_types.py +11 -4
  13. package/generator/pygen/codegen/models/property.py +5 -1
  14. package/generator/pygen/codegen/serializers/__init__.py +1 -1
  15. package/generator/pygen/codegen/serializers/builder_serializer.py +22 -20
  16. package/generator/pygen/codegen/serializers/general_serializer.py +2 -1
  17. package/generator/pygen/codegen/serializers/model_serializer.py +3 -0
  18. package/generator/pygen/codegen/serializers/sample_serializer.py +1 -3
  19. package/generator/pygen/codegen/serializers/test_serializer.py +6 -0
  20. package/generator/pygen/codegen/templates/model_base.py.jinja2 +319 -67
  21. package/generator/pygen/codegen/templates/model_dpg.py.jinja2 +5 -0
  22. package/generator/pygen/codegen/templates/serialization.py.jinja2 +271 -162
  23. package/generator/pygen/codegen/templates/test.py.jinja2 +2 -2
  24. package/generator/pygen/m2r.py +1 -1
  25. package/generator/pygen/postprocess/__init__.py +2 -2
  26. package/generator/pygen/postprocess/venvtools.py +1 -3
  27. package/generator/pygen/preprocess/__init__.py +1 -1
  28. package/generator/pygen/utils.py +10 -3
  29. package/generator/setup.py +1 -1
  30. package/package.json +8 -5
  31. package/scripts/__pycache__/venvtools.cpython-310.pyc +0 -0
  32. package/scripts/copy-generator.ts +24 -0
  33. package/scripts/eng/format.ts +5 -0
  34. package/scripts/eng/lint.ts +75 -0
  35. package/scripts/eng/mypy.ini +38 -0
  36. package/scripts/eng/pylintrc +58 -0
  37. package/scripts/eng/pyrightconfig.json +6 -0
  38. package/scripts/eng/regenerate.ts +292 -0
  39. package/scripts/{run-tests.ts → eng/run-tests.ts} +27 -18
  40. package/scripts/eng/utils.ts +38 -0
  41. package/scripts/mypy.ini +38 -0
  42. package/scripts/run-python3.ts +25 -0
  43. package/scripts/system-requirements.ts +253 -0
  44. package/scripts/copy-generator.js +0 -19
@@ -29,9 +29,9 @@ class {{ test.test_class_name }}({{ test.base_test_class_name }}):
29
29
  {% endif %}
30
30
  @recorded_by_proxy{{ async_suffix }}
31
31
  {% if code_model.options["azure_arm"] %}
32
- {{ async }}def test_{{ testcase.operation.name }}(self, resource_group):
32
+ {{ async }}def test_{{ testcase.name }}(self, resource_group):
33
33
  {% else %}
34
- {{ async }}def test_{{ testcase.operation.name }}(self, {{ prefix_lower }}_endpoint):
34
+ {{ async }}def test_{{ testcase.name }}(self, {{ prefix_lower }}_endpoint):
35
35
  {{ client_var }} = self.{{ test.create_client_name }}(endpoint={{ prefix_lower }}_endpoint)
36
36
  {% endif %}
37
37
  {{testcase.response }}{{ client_var }}{{ testcase.operation_group_prefix }}.{{ testcase.operation.name }}(
@@ -27,7 +27,7 @@ class GeneratorRenderer(m2r2.RestRenderer):
27
27
  return f":code:`{html}`"
28
28
 
29
29
 
30
- class M2R(YamlUpdatePlugin): # pylint: disable=abstract-method
30
+ class M2R(YamlUpdatePlugin):
31
31
  """A plugin to convert any description and summary from MD to RST."""
32
32
 
33
33
  def update_yaml(self, yaml_data: Dict[str, Any]) -> None:
@@ -3,7 +3,7 @@
3
3
  # Licensed under the MIT License. See License.txt in the project root for
4
4
  # license information.
5
5
  # --------------------------------------------------------------------------
6
- from typing import Tuple, Any, Dict
6
+ from typing import Tuple, Any
7
7
  from pathlib import Path
8
8
  import os
9
9
  import shutil
@@ -28,7 +28,7 @@ def format_file(file: Path, file_content: str) -> str:
28
28
  return file_content
29
29
 
30
30
 
31
- class PostProcessPlugin(Plugin): # pylint: disable=abstract-method
31
+ class PostProcessPlugin(Plugin):
32
32
  def __init__(self, **kwargs: Any):
33
33
  super().__init__(**kwargs)
34
34
  output_folder_uri = self.options["outputFolderUri"]
@@ -52,9 +52,7 @@ def create(
52
52
  return builder.context
53
53
 
54
54
 
55
- def python_run( # pylint: disable=inconsistent-return-statements
56
- venv_context, module, command, directory=_ROOT_DIR
57
- ) -> Optional[str]:
55
+ def python_run(venv_context, module, command, directory=_ROOT_DIR) -> Optional[str]:
58
56
  try:
59
57
  cmd_line = [
60
58
  venv_context.env_exe,
@@ -163,7 +163,7 @@ def has_multi_part_content_type(yaml_data: Dict[str, Any]) -> bool:
163
163
  return any(ct for ct in yaml_data.get("contentTypes", []) if ct == "multipart/form-data")
164
164
 
165
165
 
166
- class PreProcessPlugin(YamlUpdatePlugin): # pylint: disable=abstract-method
166
+ class PreProcessPlugin(YamlUpdatePlugin):
167
167
  """Add Python naming information."""
168
168
 
169
169
  @property
@@ -82,9 +82,7 @@ def parse_args(
82
82
  return value
83
83
 
84
84
  unknown_args_ret = {
85
- ua.strip("--").split("=", maxsplit=1)[0]: _get_value( # pylint: disable=bad-str-strip-call
86
- ua.strip("--").split("=", maxsplit=1)[1] # pylint: disable=bad-str-strip-call
87
- )
85
+ ua.strip("--").split("=", maxsplit=1)[0]: _get_value(ua.strip("--").split("=", maxsplit=1)[1])
88
86
  for ua in unknown_args
89
87
  }
90
88
  return args, unknown_args_ret
@@ -107,6 +105,7 @@ KNOWN_TYPES: Dict[str, Dict[str, Any]] = {
107
105
  }
108
106
 
109
107
  JSON_REGEXP = re.compile(r"^(application|text)/(.+\+)?json$")
108
+ XML_REGEXP = re.compile(r"^(application|text)/(.+\+)?xml$")
110
109
 
111
110
 
112
111
  def build_policies(
@@ -151,3 +150,11 @@ def build_policies(
151
150
 
152
151
  def extract_original_name(name: str) -> str:
153
152
  return name[1 : -len("_initial")]
153
+
154
+
155
+ def json_serializable(content_type: str) -> bool:
156
+ return bool(JSON_REGEXP.match(content_type.split(";")[0].strip().lower()))
157
+
158
+
159
+ def xml_serializable(content_type: str) -> bool:
160
+ return bool(XML_REGEXP.match(content_type.split(";")[0].strip().lower()))
@@ -15,7 +15,7 @@ from setuptools import setup, find_packages
15
15
 
16
16
  # Version extraction inspired from 'requests'
17
17
  with open(os.path.join("pygen", "_version.py"), "r") as fd:
18
- version = re.search(r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE).group(1)
18
+ version = re.search(r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]', fd.read(), re.MULTILINE).group(1) # type: ignore
19
19
 
20
20
  if not version:
21
21
  raise RuntimeError("Cannot find version information")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autorest/python",
3
- "version": "6.19.0",
3
+ "version": "6.21.0",
4
4
  "description": "The Python extension for generators in AutoRest.",
5
5
  "main": "index.js",
6
6
  "repository": {
@@ -26,7 +26,8 @@
26
26
  "devDependencies": {
27
27
  "@microsoft.azure/autorest.testserver": "^3.3.46",
28
28
  "typescript": "~5.1.3",
29
- "@azure-tools/typespec-python": "^0.29.0"
29
+ "chalk": "5.3.0",
30
+ "@azure-tools/typespec-python": "^0.31.0"
30
31
  },
31
32
  "files": [
32
33
  "autorest/**/*.py",
@@ -38,9 +39,11 @@
38
39
  ],
39
40
  "scripts": {
40
41
  "start": "node ./scripts/run-python3.js ./scripts/start.py",
41
- "build": "node ./scripts/copy-generator.js --force",
42
- "install": "node ./scripts/copy-generator.js && node ./scripts/run-python3.js ./scripts/install.py",
42
+ "build": "tsx ./scripts/copy-generator.ts --force",
43
+ "install": "tsx ./scripts/copy-generator.ts && node ./scripts/run-python3.js ./scripts/install.py",
43
44
  "debug": "node ./scripts/run-python3.js ./scripts/start.py --debug",
44
- "test": "tsx ./scripts/run-tests.ts"
45
+ "test": "tsx ./scripts/eng/run-tests.ts --validFolders azure/legacy azure/version-tolerant vanilla/legacy vanilla/version-tolerant dpg/version-tolerant",
46
+ "lint": "tsx ./scripts/eng/lint.ts --folderName autorest --skipEslint true",
47
+ "format": "tsx ./scripts/eng/format.ts"
45
48
  }
46
49
  }
@@ -0,0 +1,24 @@
1
+ import { copyFileSync, readdirSync } from "fs";
2
+ import { existsSync, removeSync, copySync } from "fs-extra";
3
+ import { join } from "path";
4
+
5
+ const force: boolean = process.argv[2] === "--force";
6
+
7
+ function copyAndCreateDir(sourceDir: string, destDir: string) {
8
+ // Delete the destination directory if it exists
9
+ if (existsSync(destDir)) {
10
+ if (force) removeSync(destDir);
11
+ else process.exit(0);
12
+ }
13
+
14
+ // Copy the source directory to the destination directory
15
+ copySync(sourceDir, destDir);
16
+ }
17
+
18
+ const typespecModulePath: string = join(__dirname, "..", "node_modules", "@azure-tools", "typespec-python");
19
+
20
+ // Copy the generator directory
21
+ copyAndCreateDir(join(typespecModulePath, "generator"), join(__dirname, "..", "generator"));
22
+
23
+ // Copy the scripts directory
24
+ copyAndCreateDir(join(typespecModulePath, "scripts", "eng"), join(__dirname, "..", "scripts", "eng"));
@@ -0,0 +1,5 @@
1
+ /* eslint-disable no-console */
2
+
3
+ import { runCommand } from "./utils.js";
4
+
5
+ runCommand("black .", "black");
@@ -0,0 +1,75 @@
1
+ /* eslint-disable no-console */
2
+ import yargs from "yargs";
3
+ import { hideBin } from "yargs/helpers";
4
+ import { runCommand, executeCommand } from "./utils.js";
5
+
6
+ interface Arguments {
7
+ folderName: string;
8
+ command?: "pylint" | "mypy" | "pyright" | "eslint";
9
+ skipWarning?: boolean;
10
+ skipEslint?: boolean;
11
+ }
12
+
13
+ const validCommands = ["pylint", "mypy", "pyright", "eslint"];
14
+
15
+ // PARSE INPUT ARGUMENTS
16
+ const argv = yargs(hideBin(process.argv))
17
+ .option("folderName", {
18
+ type: "string",
19
+ choices: ["generator", "autorest"],
20
+ description: "Specify the flavor",
21
+ default: "generator",
22
+ })
23
+ .option("command", {
24
+ alias: "c",
25
+ type: "string",
26
+ choices: validCommands,
27
+ description: "Specify the command to run",
28
+ })
29
+ .option("skipWarning", {
30
+ alias: "s",
31
+ type: "boolean",
32
+ description: "Skip to check warnings",
33
+ })
34
+ .option("skipEslint", {
35
+ alias: "e",
36
+ type: "boolean",
37
+ description: "Skip to check eslint",
38
+ }).argv as Arguments;
39
+
40
+ export function pylint() {
41
+ runCommand(`pylint ${argv.folderName}/ --rcfile ./scripts/eng/pylintrc`, "pylint");
42
+ }
43
+
44
+ export function mypy() {
45
+ runCommand(`mypy ${argv.folderName}/ --config-file ./scripts/eng/mypy.ini`, "mypy");
46
+ }
47
+
48
+ export function pyright() {
49
+ runCommand(`pyright ${argv.folderName}/ -p ./scripts/eng/pyrightconfig.json`, "pyright");
50
+ }
51
+
52
+ export function eslint() {
53
+ // const checkWarning = argv.skipWarning ? "" : "--max-warnings=0";
54
+ const checkWarning = "";
55
+ executeCommand(`npx eslint . --ext .ts ${checkWarning} `, "eslint");
56
+ }
57
+
58
+ if (argv.command === "pylint") {
59
+ pylint();
60
+ } else if (argv.command === "mypy") {
61
+ mypy();
62
+ } else if (argv.command === "pyright") {
63
+ pyright();
64
+ } else if (argv.command === "eslint") {
65
+ if (!argv.skipEslint) {
66
+ eslint();
67
+ }
68
+ } else {
69
+ pylint();
70
+ mypy();
71
+ pyright();
72
+ if (!argv.skipEslint) {
73
+ eslint();
74
+ }
75
+ }
@@ -0,0 +1,38 @@
1
+ # global configurations
2
+ [mypy]
3
+ python_version = 3.8
4
+
5
+
6
+ # module level configuratiohns
7
+ [mypy-jsonrpc.*]
8
+ ignore_missing_imports = True
9
+
10
+ [mypy-ptvsd.*]
11
+ ignore_missing_imports = True
12
+
13
+ [mypy-debugpy.*]
14
+ ignore_missing_imports = True
15
+
16
+ [mypy-m2r2.*]
17
+ ignore_missing_imports = True
18
+
19
+ [mypy-autorest.common.utils.*]
20
+ ignore_missing_imports = True
21
+
22
+ [mypy-autorest.common.python_mappings.*]
23
+ ignore_missing_imports = True
24
+
25
+ [mypy-pygen.codegen.models.*]
26
+ ignore_missing_imports = True
27
+
28
+ [mypy-setuptools]
29
+ ignore_missing_imports = True
30
+
31
+ [mypy-*._patch]
32
+ ignore_missing_imports = True
33
+
34
+ [mypy-pygen.*]
35
+ ignore_missing_imports = True
36
+
37
+ [mypy-yaml.*]
38
+ ignore_missing_imports = True
@@ -0,0 +1,58 @@
1
+ [MASTER]
2
+ py-version=3.8
3
+ ignore-patterns=test_*,conftest,setup
4
+ reports=no
5
+
6
+ # PYLINT DIRECTORY BLACKLIST.
7
+ ignore=_generated,samples,examples,test,tests,doc,.tox,generated_samples
8
+
9
+ [MESSAGES CONTROL]
10
+
11
+ # Add enable for useless disables
12
+ enable=useless-suppression
13
+
14
+ # For all codes, run 'pylint --list-msgs' or go to 'https://pylint.readthedocs.io/en/latest/reference_guide/features.html'
15
+ # locally-disabled: Warning locally suppressed using disable-msg
16
+ # cyclic-import: because of https://github.com/PyCQA/pylint/issues/850
17
+ # too-many-arguments: Due to the nature of the CLI many commands have large arguments set which reflect in large arguments set in corresponding methods.
18
+ # Let's black deal with bad-continuation
19
+ disable=useless-object-inheritance,missing-docstring,locally-disabled,fixme,cyclic-import,too-many-arguments,invalid-name,duplicate-code,too-few-public-methods,consider-using-f-string,super-with-arguments,redefined-builtin,import-outside-toplevel,client-suffix-needed,unnecessary-dunder-call,unnecessary-ellipsis,disallowed-name,consider-using-max-builtin
20
+
21
+ [FORMAT]
22
+ max-line-length=120
23
+
24
+ [VARIABLES]
25
+ # Tells whether we should check for unused import in __init__ files.
26
+ init-import=yes
27
+
28
+ [DESIGN]
29
+ # Maximum number of locals for function / method body
30
+ max-locals=25
31
+ # Maximum number of branch for function / method body
32
+ max-branches=20
33
+ # Maximum number of instance attributes for class
34
+ max-attributes=10
35
+ # Maximum number of ancestors
36
+ max-parents=15
37
+
38
+ [SIMILARITIES]
39
+ min-similarity-lines=10
40
+
41
+ [BASIC]
42
+ # Naming hints based on PEP 8 (https://www.python.org/dev/peps/pep-0008/#naming-conventions).
43
+ # Consider these guidelines and not hard rules. Read PEP 8 for more details.
44
+
45
+ # The invalid-name checker must be **enabled** for these hints to be used.
46
+ include-naming-hint=yes
47
+
48
+ # keep short; underscores are discouraged
49
+ module-naming-style=snake_case
50
+ const-naming-style=UPPER_CASE
51
+ class-naming-style=PascalCase
52
+ class-attribute-naming-style=snake_case
53
+ attr-naming-style=snake_case
54
+ method-naming-style=snake_case
55
+ function-naming-style=snake_case
56
+ argument-naming-style=snake_case
57
+ variable-naming-style=snake_case
58
+ inlinevar-naming-style=snake_case
@@ -0,0 +1,6 @@
1
+ {
2
+ "reportUnnecessaryCast": "warning",
3
+ "reportTypeCommentUsage": true,
4
+ "reportMissingImports": false,
5
+ "pythonVersion": "3.8"
6
+ }
@@ -0,0 +1,292 @@
1
+ /* eslint-disable no-console */
2
+ import { exec as execCallback } from "child_process";
3
+ import { promisify } from "util";
4
+ import yargs from "yargs";
5
+ import { hideBin } from "yargs/helpers";
6
+ import { dirname, join, relative, resolve } from "path";
7
+ import { promises, rmSync } from "fs";
8
+ import { fileURLToPath } from "url";
9
+
10
+ // Promisify the exec function
11
+ const exec = promisify(execCallback);
12
+
13
+ // Get the directory of the current file
14
+ const PLUGIN_DIR = resolve(fileURLToPath(import.meta.url), "../../../");
15
+ const CADL_RANCH_DIR = resolve(PLUGIN_DIR, "node_modules/@azure-tools/cadl-ranch-specs/http");
16
+ interface TspCommand {
17
+ outputDir: string;
18
+ command: string;
19
+ }
20
+
21
+ const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<string, string>[]> = {
22
+ "resiliency/srv-driven/old.tsp": {
23
+ "package-name": "resiliency-srv-driven1",
24
+ "package-mode": "azure-dataplane",
25
+ "package-pprint-name": "ResiliencySrvDriven1",
26
+ },
27
+ "resiliency/srv-driven": {
28
+ "package-name": "resiliency-srv-driven2",
29
+ "package-mode": "azure-dataplane",
30
+ "package-pprint-name": "ResiliencySrvDriven2",
31
+ },
32
+ "authentication/http/custom": {
33
+ "package-name": "authentication-http-custom",
34
+ },
35
+ "authentication/union": {
36
+ "package-name": "authentication-union",
37
+ },
38
+ "type/array": {
39
+ "package-name": "typetest-array",
40
+ },
41
+ "type/dictionary": {
42
+ "package-name": "typetest-dictionary",
43
+ },
44
+ "type/enum/extensible": {
45
+ "package-name": "typetest-enum-extensible",
46
+ },
47
+ "type/enum/fixed": {
48
+ "package-name": "typetest-enum-fixed",
49
+ },
50
+ "type/model/empty": {
51
+ "package-name": "typetest-model-empty",
52
+ },
53
+ "type/model/inheritance/enum-discriminator": {
54
+ "package-name": "typetest-model-enumdiscriminator",
55
+ },
56
+ "type/model/inheritance/nested-discriminator": {
57
+ "package-name": "typetest-model-nesteddiscriminator",
58
+ },
59
+ "type/model/inheritance/not-discriminated": {
60
+ "package-name": "typetest-model-notdiscriminated",
61
+ },
62
+ "type/model/inheritance/single-discriminator": {
63
+ "package-name": "typetest-model-singlediscriminator",
64
+ },
65
+ "type/model/inheritance/recursive": {
66
+ "package-name": "typetest-model-recursive",
67
+ },
68
+ "type/model/usage": {
69
+ "package-name": "typetest-model-usage",
70
+ },
71
+ "type/model/visibility": [
72
+ { "package-name": "typetest-model-visibility" },
73
+ { "package-name": "headasbooleantrue", "head-as-boolean": "true" },
74
+ { "package-name": "headasbooleanfalse", "head-as-boolean": "false" },
75
+ ],
76
+ "type/property/nullable": {
77
+ "package-name": "typetest-property-nullable",
78
+ },
79
+ "type/property/optionality": {
80
+ "package-name": "typetest-property-optional",
81
+ },
82
+ "type/property/additional-properties": {
83
+ "package-name": "typetest-property-additionalproperties",
84
+ },
85
+ "type/scalar": {
86
+ "package-name": "typetest-scalar",
87
+ },
88
+ "type/property/value-types": {
89
+ "package-name": "typetest-property-valuetypes",
90
+ },
91
+ "type/union": {
92
+ "package-name": "typetest-union",
93
+ },
94
+ "azure/core/lro/rpc": {
95
+ "package-name": "azurecore-lro-rpc",
96
+ },
97
+ "client/structure/multi-client": {
98
+ "package-name": "client-structure-multiclient",
99
+ },
100
+ "client/structure/renamed-operation": {
101
+ "package-name": "client-structure-renamedoperation",
102
+ },
103
+ "client/structure/two-operation-group": {
104
+ "package-name": "client-structure-twooperationgroup",
105
+ },
106
+ "mgmt/sphere": [{ "package-name": "azure-mgmt-spheredpg" }],
107
+ };
108
+
109
+ function toPosix(dir: string): string {
110
+ return dir.replace(/\\/g, "/");
111
+ }
112
+
113
+ function getEmitterOption(spec: string): Record<string, string>[] {
114
+ const relativeSpec = toPosix(relative(CADL_RANCH_DIR, spec));
115
+ const key = relativeSpec.includes("resiliency/srv-driven/old.tsp") ? relativeSpec : dirname(relativeSpec);
116
+ const result = EMITTER_OPTIONS[key] || [{}];
117
+ return Array.isArray(result) ? result : [result];
118
+ }
119
+
120
+ // Function to execute CLI commands asynchronously
121
+ async function executeCommand(tspCommand: TspCommand): Promise<void> {
122
+ try {
123
+ rmSync(tspCommand.outputDir, { recursive: true, force: true });
124
+ } catch (error) {
125
+ console.error(`rm error: ${error}`);
126
+ }
127
+ try {
128
+ console.log(`exec: ${tspCommand.command}`);
129
+ const { stdout, stderr } = await exec(tspCommand.command);
130
+ if (stdout) console.log(`stdout: ${stdout}`);
131
+ if (stderr) console.error(`stderr: ${stderr}`);
132
+ } catch (error) {
133
+ console.error(`exec error: ${error}`);
134
+ throw error;
135
+ }
136
+ }
137
+
138
+ interface RegenerateFlagsInput {
139
+ flavor?: "azure" | "unbranded";
140
+ debug?: boolean;
141
+ name?: string;
142
+ }
143
+
144
+ interface RegenerateFlags {
145
+ flavor: "azure" | "unbranded";
146
+ debug: boolean;
147
+ name?: string;
148
+ }
149
+
150
+ const SpecialFlags: Record<string, Record<string, any>> = {
151
+ azure: {
152
+ "generate-test": true,
153
+ "generate-sample": true,
154
+ },
155
+ };
156
+
157
+ async function getSubdirectories(baseDir: string, flags: RegenerateFlags): Promise<string[]> {
158
+ const subdirectories: string[] = [];
159
+
160
+ async function searchDir(currentDir: string) {
161
+ const items = await promises.readdir(currentDir, { withFileTypes: true });
162
+
163
+ const promisesArray = items.map(async (item) => {
164
+ const subDirPath = join(currentDir, item.name);
165
+ if (item.isDirectory()) {
166
+ const mainTspPath = join(subDirPath, "main.tsp");
167
+ const clientTspPath = join(subDirPath, "client.tsp");
168
+
169
+ const mainTspRelativePath = toPosix(relative(baseDir, mainTspPath));
170
+ if (flags.flavor === "unbranded" && mainTspRelativePath.includes("azure")) return;
171
+
172
+ // after fix test generation for nested operation group, remove this check
173
+ if (mainTspRelativePath.includes("client-operation-group")) return;
174
+
175
+ const hasMainTsp = await promises
176
+ .access(mainTspPath)
177
+ .then(() => true)
178
+ .catch(() => false);
179
+ const hasClientTsp = await promises
180
+ .access(clientTspPath)
181
+ .then(() => true)
182
+ .catch(() => false);
183
+
184
+ if (mainTspRelativePath.toLowerCase().includes(flags.name || "")) {
185
+ if (mainTspRelativePath.includes("resiliency/srv-driven")) {
186
+ subdirectories.push(resolve(subDirPath, "old.tsp"));
187
+ }
188
+ if (hasClientTsp) {
189
+ subdirectories.push(resolve(subDirPath, "client.tsp"));
190
+ } else if (hasMainTsp) {
191
+ subdirectories.push(resolve(subDirPath, "main.tsp"));
192
+ }
193
+ }
194
+
195
+ // Recursively search in the subdirectory
196
+ await searchDir(subDirPath);
197
+ }
198
+ });
199
+
200
+ await Promise.all(promisesArray);
201
+ }
202
+
203
+ await searchDir(baseDir);
204
+ return subdirectories;
205
+ }
206
+
207
+ function defaultPackageName(spec: string): string {
208
+ return toPosix(relative(CADL_RANCH_DIR, dirname(spec)))
209
+ .replace(/\//g, "-")
210
+ .toLowerCase();
211
+ }
212
+
213
+ interface EmitterConfig {
214
+ optionsStr: string;
215
+ outputDir: string;
216
+ }
217
+
218
+ function addOptions(spec: string, generatedFolder: string, flags: RegenerateFlags): EmitterConfig[] {
219
+ const emitterConfigs: EmitterConfig[] = [];
220
+ for (const config of getEmitterOption(spec)) {
221
+ const options: Record<string, string> = { ...config };
222
+ options["flavor"] = flags.flavor;
223
+ for (const [k, v] of Object.entries(SpecialFlags[flags.flavor] ?? {})) {
224
+ options[k] = v;
225
+ }
226
+ if (options["emitter-output-dir"] === undefined) {
227
+ const packageName = options["package-name"] || defaultPackageName(spec);
228
+ options["emitter-output-dir"] = toPosix(`${generatedFolder}/test/${flags.flavor}/generated/${packageName}`);
229
+ }
230
+ if (flags.debug) {
231
+ options["debug"] = "true";
232
+ }
233
+ if (flags.flavor === "unbranded") {
234
+ options["company-name"] = "Unbranded";
235
+ }
236
+ options["examples-dir"] = toPosix(join(dirname(spec), "examples"));
237
+ const configs = Object.entries(options).flatMap(([k, v]) => {
238
+ return `--option @azure-tools/typespec-python.${k}=${v}`;
239
+ });
240
+ emitterConfigs.push({
241
+ optionsStr: configs.join(" "),
242
+ outputDir: options["emitter-output-dir"],
243
+ });
244
+ }
245
+ return emitterConfigs;
246
+ }
247
+ function _getCmdList(spec: string, flags: RegenerateFlags): TspCommand[] {
248
+ return addOptions(spec, PLUGIN_DIR, flags).map((option) => {
249
+ return {
250
+ outputDir: option.outputDir,
251
+ command: `tsp compile ${spec} --emit=${toPosix(PLUGIN_DIR)} ${option.optionsStr}`,
252
+ };
253
+ });
254
+ }
255
+
256
+ async function regenerate(flags: RegenerateFlagsInput): Promise<void> {
257
+ if (flags.flavor === undefined) {
258
+ await regenerate({ ...flags, flavor: "azure" });
259
+ await regenerate({ ...flags, flavor: "unbranded" });
260
+ } else {
261
+ const flagsResolved = { debug: false, flavor: flags.flavor, ...flags };
262
+ const CADL_RANCH_DIR = resolve(PLUGIN_DIR, "node_modules/@azure-tools/cadl-ranch-specs/http");
263
+ const subdirectories = await getSubdirectories(CADL_RANCH_DIR, flagsResolved);
264
+ const cmdList: TspCommand[] = subdirectories.flatMap((subdirectory) =>
265
+ _getCmdList(subdirectory, flagsResolved),
266
+ );
267
+ const PromiseCommands = cmdList.map((tspCommand) => executeCommand(tspCommand));
268
+ await Promise.all(PromiseCommands);
269
+ }
270
+ }
271
+
272
+ // PARSE INPUT ARGUMENTS
273
+ const argv = yargs(hideBin(process.argv))
274
+ .option("flavor", {
275
+ type: "string",
276
+ choices: ["azure", "unbranded"],
277
+ description: "Specify the flavor",
278
+ })
279
+ .option("debug", {
280
+ alias: "d",
281
+ type: "boolean",
282
+ description: "Debug mode",
283
+ })
284
+ .option("name", {
285
+ alias: "n",
286
+ type: "string",
287
+ description: "Specify filename if you only want to generate a subset",
288
+ }).argv;
289
+
290
+ regenerate(argv as RegenerateFlags)
291
+ .then(() => console.log("Regeneration successful"))
292
+ .catch((error) => console.error(`Regeneration failed: ${error.message}`));