@azure-tools/typespec-python 0.29.0 → 0.31.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 (88) hide show
  1. package/dist/scripts/eng/format.d.ts +2 -0
  2. package/dist/scripts/eng/format.d.ts.map +1 -0
  3. package/dist/scripts/eng/format.js +4 -0
  4. package/dist/scripts/eng/format.js.map +1 -0
  5. package/dist/scripts/eng/lint.d.ts +5 -0
  6. package/dist/scripts/eng/lint.d.ts.map +1 -0
  7. package/dist/scripts/eng/lint.js +66 -0
  8. package/dist/scripts/eng/lint.js.map +1 -0
  9. package/dist/scripts/{regenerate.d.ts.map → eng/regenerate.d.ts.map} +1 -1
  10. package/dist/scripts/{regenerate.js → eng/regenerate.js} +27 -29
  11. package/dist/scripts/eng/regenerate.js.map +1 -0
  12. package/dist/scripts/{run-tests.d.ts.map → eng/run-tests.d.ts.map} +1 -1
  13. package/dist/scripts/eng/run-tests.js +80 -0
  14. package/dist/scripts/eng/run-tests.js.map +1 -0
  15. package/dist/scripts/eng/utils.d.ts +3 -0
  16. package/dist/scripts/eng/utils.d.ts.map +1 -0
  17. package/dist/scripts/eng/utils.js +39 -0
  18. package/dist/scripts/eng/utils.js.map +1 -0
  19. package/dist/scripts/system-requirements.d.ts.map +1 -1
  20. package/dist/scripts/system-requirements.js.map +1 -1
  21. package/dist/src/code-model.d.ts.map +1 -1
  22. package/dist/src/code-model.js +4 -1
  23. package/dist/src/code-model.js.map +1 -1
  24. package/dist/src/emitter.d.ts.map +1 -1
  25. package/dist/src/emitter.js +17 -4
  26. package/dist/src/emitter.js.map +1 -1
  27. package/dist/src/external-process.d.ts +0 -1
  28. package/dist/src/external-process.d.ts.map +1 -1
  29. package/dist/src/http.js +14 -8
  30. package/dist/src/http.js.map +1 -1
  31. package/dist/src/lib.d.ts +1 -0
  32. package/dist/src/lib.d.ts.map +1 -1
  33. package/dist/src/lib.js +1 -0
  34. package/dist/src/lib.js.map +1 -1
  35. package/dist/src/types.d.ts +1 -1
  36. package/dist/src/types.d.ts.map +1 -1
  37. package/dist/src/types.js +82 -9
  38. package/dist/src/types.js.map +1 -1
  39. package/dist/src/utils.d.ts +2 -2
  40. package/dist/src/utils.d.ts.map +1 -1
  41. package/dist/src/utils.js +65 -10
  42. package/dist/src/utils.js.map +1 -1
  43. package/generator/pygen/black.py +2 -3
  44. package/generator/pygen/codegen/models/combined_type.py +1 -1
  45. package/generator/pygen/codegen/models/credential_types.py +7 -14
  46. package/generator/pygen/codegen/models/enum_type.py +1 -1
  47. package/generator/pygen/codegen/models/lro_operation.py +0 -1
  48. package/generator/pygen/codegen/models/lro_paging_operation.py +1 -1
  49. package/generator/pygen/codegen/models/model_type.py +5 -7
  50. package/generator/pygen/codegen/models/operation.py +13 -2
  51. package/generator/pygen/codegen/models/paging_operation.py +0 -1
  52. package/generator/pygen/codegen/models/parameter.py +5 -1
  53. package/generator/pygen/codegen/models/parameter_list.py +2 -5
  54. package/generator/pygen/codegen/models/primitive_types.py +11 -4
  55. package/generator/pygen/codegen/models/property.py +5 -1
  56. package/generator/pygen/codegen/serializers/__init__.py +1 -1
  57. package/generator/pygen/codegen/serializers/builder_serializer.py +22 -20
  58. package/generator/pygen/codegen/serializers/general_serializer.py +2 -1
  59. package/generator/pygen/codegen/serializers/model_serializer.py +3 -0
  60. package/generator/pygen/codegen/serializers/sample_serializer.py +1 -3
  61. package/generator/pygen/codegen/serializers/test_serializer.py +6 -0
  62. package/generator/pygen/codegen/templates/model_base.py.jinja2 +319 -67
  63. package/generator/pygen/codegen/templates/model_dpg.py.jinja2 +5 -0
  64. package/generator/pygen/codegen/templates/serialization.py.jinja2 +271 -162
  65. package/generator/pygen/codegen/templates/test.py.jinja2 +2 -2
  66. package/generator/pygen/m2r.py +1 -1
  67. package/generator/pygen/postprocess/__init__.py +2 -2
  68. package/generator/pygen/postprocess/venvtools.py +1 -3
  69. package/generator/pygen/preprocess/__init__.py +1 -1
  70. package/generator/pygen/utils.py +10 -3
  71. package/generator/setup.py +1 -1
  72. package/package.json +13 -10
  73. package/scripts/__pycache__/venvtools.cpython-310.pyc +0 -0
  74. package/scripts/eng/format.ts +5 -0
  75. package/scripts/eng/lint.ts +75 -0
  76. package/scripts/eng/mypy.ini +38 -0
  77. package/scripts/eng/pylintrc +58 -0
  78. package/scripts/eng/pyrightconfig.json +6 -0
  79. package/scripts/{regenerate.ts → eng/regenerate.ts} +40 -33
  80. package/scripts/eng/run-tests.ts +90 -0
  81. package/scripts/eng/utils.ts +38 -0
  82. package/scripts/run_tsp.py +7 -5
  83. package/dist/scripts/regenerate.js.map +0 -1
  84. package/dist/scripts/run-tests.js +0 -50
  85. package/dist/scripts/run-tests.js.map +0 -1
  86. package/scripts/run-tests.ts +0 -57
  87. /package/dist/scripts/{regenerate.d.ts → eng/regenerate.d.ts} +0 -0
  88. /package/dist/scripts/{run-tests.d.ts → eng/run-tests.d.ts} +0 -0
@@ -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": "@azure-tools/typespec-python",
3
- "version": "0.29.0",
3
+ "version": "0.31.0",
4
4
  "author": "Microsoft Corporation",
5
5
  "description": "TypeSpec emitter for Python SDKs",
6
6
  "homepage": "https://github.com/Azure/autorest.python",
@@ -36,7 +36,7 @@
36
36
  "@azure-tools/typespec-azure-core": ">=0.45.0 <1.0.0",
37
37
  "@azure-tools/typespec-azure-resource-manager": ">=0.45.0 <1.0.0",
38
38
  "@azure-tools/typespec-autorest": ">=0.45.0 <1.0.0",
39
- "@azure-tools/typespec-client-generator-core": ">=0.45.1 <1.0.0",
39
+ "@azure-tools/typespec-client-generator-core": ">=0.45.4 <1.0.0",
40
40
  "@azure-tools/typespec-azure-rulesets": ">=0.45.0 <3.0.0",
41
41
  "@typespec/compiler": ">=0.59.1 <1.0.0",
42
42
  "@typespec/http": ">=0.59.0 <1.0.0",
@@ -53,8 +53,8 @@
53
53
  "devDependencies": {
54
54
  "@azure-tools/typespec-azure-resource-manager": "~0.45.0",
55
55
  "@azure-tools/typespec-autorest": "~0.45.0",
56
- "@azure-tools/cadl-ranch-expect": "~0.15.1",
57
- "@azure-tools/cadl-ranch-specs": "~0.35.4",
56
+ "@azure-tools/cadl-ranch-expect": "~0.15.3",
57
+ "@azure-tools/cadl-ranch-specs": "~0.37.1",
58
58
  "@types/js-yaml": "~4.0.5",
59
59
  "@types/node": "^18.16.3",
60
60
  "@types/yargs": "17.0.32",
@@ -62,25 +62,28 @@
62
62
  "@typespec/eslint-config-typespec": "~0.55.0",
63
63
  "@typespec/openapi": "~0.59.0",
64
64
  "c8": "~7.13.0",
65
+ "vitest": "^2.0.4",
65
66
  "rimraf": "~5.0.0",
66
- "typescript": "~5.1.3",
67
+ "typescript": "~5.5.4",
67
68
  "@azure-tools/typespec-azure-core": "~0.45.0",
68
- "@azure-tools/typespec-client-generator-core": "0.45.1",
69
+ "@azure-tools/typespec-client-generator-core": "0.45.4",
69
70
  "@typespec/compiler": "~0.59.1",
70
71
  "@typespec/http": "~0.59.0",
71
72
  "@typespec/rest": "~0.59.0",
72
73
  "@typespec/versioning": "~0.59.0",
73
74
  "@azure-tools/typespec-azure-rulesets": "0.45.0",
74
- "yargs": "~17.2.1"
75
+ "yargs": "~17.2.1",
76
+ "chalk": "5.3.0"
75
77
  },
76
78
  "scripts": {
77
79
  "clean": "rimraf ./dist ./temp ./venv ./node_modules",
78
80
  "build": "tsc -p .",
79
81
  "watch": "tsc -p . --watch",
80
- "lint": "eslint . --ext .ts --max-warnings=0",
82
+ "lint": "tsx ./scripts/eng/lint.ts",
81
83
  "lint:fix": "eslint . --fix --ext .ts",
84
+ "format": "npx prettier **/*.ts --write && tsx ./scripts/eng/format.ts",
82
85
  "install": "tsx ./scripts/run-python3.ts ./scripts/install.py",
83
- "regenerate": "tsx ./scripts/regenerate.ts",
84
- "test": "tsx ./scripts/run-tests.ts"
86
+ "regenerate": "tsx ./scripts/eng/regenerate.ts",
87
+ "test": "npx vitest run ./test && tsx ./scripts/eng/run-tests.ts"
85
88
  }
86
89
  }
@@ -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
+ }
@@ -4,15 +4,19 @@ import { promisify } from "util";
4
4
  import yargs from "yargs";
5
5
  import { hideBin } from "yargs/helpers";
6
6
  import { dirname, join, relative, resolve } from "path";
7
- import { promises } from "fs";
7
+ import { promises, rmSync } from "fs";
8
8
  import { fileURLToPath } from "url";
9
9
 
10
10
  // Promisify the exec function
11
11
  const exec = promisify(execCallback);
12
12
 
13
13
  // Get the directory of the current file
14
- const PLUGIN_DIR = resolve(fileURLToPath(import.meta.url), "../../");
14
+ const PLUGIN_DIR = resolve(fileURLToPath(import.meta.url), "../../../");
15
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
+ }
16
20
 
17
21
  const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<string, string>[]> = {
18
22
  "resiliency/srv-driven/old.tsp": {
@@ -46,9 +50,6 @@ const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<string, st
46
50
  "type/model/empty": {
47
51
  "package-name": "typetest-model-empty",
48
52
  },
49
- "type/model/flatten": {
50
- "package-name": "typetest-model-flatten",
51
- },
52
53
  "type/model/inheritance/enum-discriminator": {
53
54
  "package-name": "typetest-model-enumdiscriminator",
54
55
  },
@@ -117,9 +118,15 @@ function getEmitterOption(spec: string): Record<string, string>[] {
117
118
  }
118
119
 
119
120
  // Function to execute CLI commands asynchronously
120
- async function executeCommand(command: string): Promise<void> {
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
+ }
121
127
  try {
122
- const { stdout, stderr } = await exec(command);
128
+ console.log(`exec: ${tspCommand.command}`);
129
+ const { stdout, stderr } = await exec(tspCommand.command);
123
130
  if (stdout) console.log(`stdout: ${stdout}`);
124
131
  if (stderr) console.error(`stderr: ${stderr}`);
125
132
  } catch (error) {
@@ -162,9 +169,6 @@ async function getSubdirectories(baseDir: string, flags: RegenerateFlags): Promi
162
169
  const mainTspRelativePath = toPosix(relative(baseDir, mainTspPath));
163
170
  if (flags.flavor === "unbranded" && mainTspRelativePath.includes("azure")) return;
164
171
 
165
- // after xml support, remove this check
166
- if (mainTspRelativePath.includes("xml")) return;
167
-
168
172
  // after fix test generation for nested operation group, remove this check
169
173
  if (mainTspRelativePath.includes("client-operation-group")) return;
170
174
 
@@ -206,8 +210,13 @@ function defaultPackageName(spec: string): string {
206
210
  .toLowerCase();
207
211
  }
208
212
 
209
- function addOptions(spec: string, generatedFolder: string, flags: RegenerateFlags): string[] {
210
- const emitterConfigs: string[] = [];
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[] = [];
211
220
  for (const config of getEmitterOption(spec)) {
212
221
  const options: Record<string, string> = { ...config };
213
222
  options["flavor"] = flags.flavor;
@@ -224,41 +233,39 @@ function addOptions(spec: string, generatedFolder: string, flags: RegenerateFlag
224
233
  if (flags.flavor === "unbranded") {
225
234
  options["company-name"] = "Unbranded";
226
235
  }
227
- options["examples-directory"] = toPosix(join(dirname(spec), "examples"));
236
+ options["examples-dir"] = toPosix(join(dirname(spec), "examples"));
228
237
  const configs = Object.entries(options).flatMap(([k, v]) => {
229
238
  return `--option @azure-tools/typespec-python.${k}=${v}`;
230
239
  });
231
- emitterConfigs.push(configs.join(" "));
240
+ emitterConfigs.push({
241
+ optionsStr: configs.join(" "),
242
+ outputDir: options["emitter-output-dir"],
243
+ });
232
244
  }
233
245
  return emitterConfigs;
234
246
  }
235
-
236
- async function _regenerateSingle(spec: string, flags: RegenerateFlags): Promise<void> {
237
- // Perform some asynchronous operation here
238
- const options = addOptions(spec, PLUGIN_DIR, flags);
239
- const commandPromises = options.map((option) => {
240
- const command = `tsp compile ${spec} --emit=${toPosix(PLUGIN_DIR)} ${option}`;
241
- console.log(command);
242
- return executeCommand(command);
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
+ };
243
253
  });
244
- await Promise.all(commandPromises);
245
254
  }
246
255
 
247
- async function regenerate(flags: RegenerateFlagsInput): Promise<boolean> {
256
+ async function regenerate(flags: RegenerateFlagsInput): Promise<void> {
248
257
  if (flags.flavor === undefined) {
249
- const azureGeneration = await regenerate({ ...flags, flavor: "azure" });
250
- const unbrandedGeneration = await regenerate({ ...flags, flavor: "unbranded" });
251
- return azureGeneration && unbrandedGeneration;
258
+ await regenerate({ ...flags, flavor: "azure" });
259
+ await regenerate({ ...flags, flavor: "unbranded" });
252
260
  } else {
253
261
  const flagsResolved = { debug: false, flavor: flags.flavor, ...flags };
254
262
  const CADL_RANCH_DIR = resolve(PLUGIN_DIR, "node_modules/@azure-tools/cadl-ranch-specs/http");
255
263
  const subdirectories = await getSubdirectories(CADL_RANCH_DIR, flagsResolved);
256
- const promises = subdirectories.map(async (subdirectory) => {
257
- // Perform additional asynchronous operations on each subdirectory here
258
- await _regenerateSingle(subdirectory, flagsResolved);
259
- });
260
- await Promise.all(promises);
261
- return true;
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);
262
269
  }
263
270
  }
264
271
 
@@ -0,0 +1,90 @@
1
+ /* eslint-disable no-console */
2
+ import { execSync } from "child_process";
3
+ import { readFileSync } from "fs";
4
+ import { join } from "path";
5
+ import yargs from "yargs";
6
+ import { hideBin } from "yargs/helpers";
7
+ import { fileURLToPath } from "url";
8
+
9
+ interface Arguments {
10
+ validFolders: string[];
11
+ folder?: string;
12
+ command?: string;
13
+ name?: string;
14
+ }
15
+
16
+ const validCommands = ["ci", "lint", "mypy", "pyright", "apiview"];
17
+
18
+ // Parse command-line arguments using yargs
19
+ const argv = yargs(hideBin(process.argv))
20
+ .option("validFolders", {
21
+ alias: "vf",
22
+ describe: "Specify the valid folders",
23
+ type: "array",
24
+ default: ["azure", "unbranded"],
25
+ })
26
+ .option("folder", {
27
+ alias: "f",
28
+ describe: "Specify the folder to use",
29
+ type: "string",
30
+ })
31
+ .option("command", {
32
+ alias: "c",
33
+ describe: "Specify the command to run",
34
+ choices: validCommands,
35
+ type: "string",
36
+ })
37
+ .option("name", {
38
+ alias: "n",
39
+ describe: "Specify the name of the test",
40
+ type: "string",
41
+ }).argv as Arguments;
42
+
43
+ const foldersToProcess = argv.folder ? [argv.folder] : argv.validFolders;
44
+
45
+ const commandToRun = argv.command || "all";
46
+
47
+ function getCommand(command: string, folder: string, name?: string): string {
48
+ if (!validCommands.includes(command)) throw new Error(`Unknown command '${command}'.`);
49
+ const retval = `FOLDER=${folder} tox -c ./test/${folder}/tox.ini -e ${command}`;
50
+ if (name) {
51
+ return `${retval} -- -f ${name}`;
52
+ }
53
+ return retval;
54
+ }
55
+
56
+ function sectionExistsInToxIni(command: string, folder: string): boolean {
57
+ const toxIniPath = join(fileURLToPath(import.meta.url), `../../../test/${folder}/tox.ini`);
58
+ const toxIniContent = readFileSync(toxIniPath, "utf-8");
59
+ const sectionHeader = `[testenv:${command}]`;
60
+ return toxIniContent.includes(sectionHeader);
61
+ }
62
+
63
+ function myExecSync(command: string, folder: string, name?: string): void {
64
+ if (!sectionExistsInToxIni(command, folder)) {
65
+ console.log(`No section for ${command} in tox.ini for folder ${folder}. Skipping...`);
66
+ return;
67
+ }
68
+ execSync(getCommand(command, folder, name), { stdio: "inherit" });
69
+ }
70
+
71
+ foldersToProcess.forEach((folder) => {
72
+ try {
73
+ if (commandToRun === "all") {
74
+ for (const key of validCommands) {
75
+ console.log(`Running ${key} for folder ${folder}...`);
76
+ myExecSync(key, folder, argv.name);
77
+ }
78
+ } else if (getCommand(commandToRun, folder, argv.name)) {
79
+ console.log(`Running ${commandToRun} for folder ${folder}...`);
80
+ myExecSync(commandToRun, folder, argv.name);
81
+ } else {
82
+ console.error(`Error: Unknown command '${commandToRun}'.`);
83
+ process.exit(1);
84
+ }
85
+ } catch (error) {
86
+ console.error((error as Error).message);
87
+ console.error(`Error executing command for folder ${folder}: ${(error as Error).message}`);
88
+ process.exit(1);
89
+ }
90
+ });
@@ -0,0 +1,38 @@
1
+ /* eslint-disable no-console */
2
+ import { exec } from "child_process";
3
+ import process from "process";
4
+ import { existsSync } from "fs";
5
+ import { dirname, join } from "path";
6
+ import { fileURLToPath } from "url";
7
+ import chalk from "chalk";
8
+
9
+ // execute the command
10
+ export function executeCommand(command: string, prettyName: string) {
11
+ exec(command, (error, stdout, stderr) => {
12
+ if (error) {
13
+ console.error(chalk.red(`Error executing ${command}(stdout): ${stdout}`));
14
+ console.error(chalk.red(`Error executing ${command}{stderr}: ${stderr}`));
15
+ process.exit(1);
16
+ }
17
+ if (stderr) {
18
+ // Process stderr output
19
+ console.log(chalk.yellow(`${command}:\n${stderr}`));
20
+ return;
21
+ }
22
+ console.log(chalk.green(`${prettyName} passed`));
23
+ });
24
+ }
25
+
26
+ // Function to run a command and log the output
27
+ export function runCommand(command: string, prettyName: string) {
28
+ let pythonPath = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "venv/");
29
+ if (existsSync(join(pythonPath, "bin"))) {
30
+ pythonPath = join(pythonPath, "bin", "python");
31
+ } else if (existsSync(join(pythonPath, "Scripts"))) {
32
+ pythonPath = join(pythonPath, "Scripts", "python");
33
+ } else {
34
+ throw new Error(pythonPath);
35
+ }
36
+ command = `${pythonPath} -m ${command}`;
37
+ executeCommand(command, prettyName);
38
+ }
@@ -7,7 +7,8 @@ import sys
7
7
  import venv
8
8
  import logging
9
9
  from pathlib import Path
10
- from venvtools import python_run
10
+ from pygen import m2r, preprocess, codegen, black
11
+ from pygen.utils import parse_args
11
12
 
12
13
  _ROOT_DIR = Path(__file__).parent.parent
13
14
 
@@ -34,7 +35,8 @@ if __name__ == "__main__":
34
35
  breakpoint() # pylint: disable=undefined-variable
35
36
 
36
37
  # run m2r
37
- python_run(venv_context, "generator.pygen.m2r", command=sys.argv[1:])
38
- python_run(venv_context, "generator.pygen.preprocess.__init__", command=sys.argv[1:])
39
- python_run(venv_context, "generator.pygen.codegen.__init__", command=sys.argv[1:])
40
- python_run(venv_context, "generator.pygen.black", command=sys.argv[1:])
38
+ args, unknown_args = parse_args()
39
+ m2r.M2R(output_folder=args.output_folder, cadl_file=args.cadl_file, **unknown_args).process()
40
+ preprocess.PreProcessPlugin(output_folder=args.output_folder, cadl_file=args.cadl_file, **unknown_args).process()
41
+ codegen.CodeGenerator(output_folder=args.output_folder, cadl_file=args.cadl_file, **unknown_args).process()
42
+ black.BlackScriptPlugin(output_folder=args.output_folder, **unknown_args).process()