Skip to content

Commit 4b6a811

Browse files
authored
Cadl refresh sdk script (Azure#28801)
* init * test package * update * update: * udpate readme * update: * update: * update * version * version * fix * fix * delete annotation code * Update emitter-package.json
1 parent b550bdf commit 4b6a811

File tree

8 files changed

+102
-24
lines changed

8 files changed

+102
-24
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,7 @@ sdk/cosmos/azure-cosmos/test/test_config.py
144144
.pre-commit-config.yaml
145145

146146
# api stub files
147-
*_python.json
147+
*_python.json
148+
149+
# temporary folder to refresh SDK with cadl
150+
TempCadlFiles/

cadl_to_sdk_config.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

eng/emitter-package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"main": "dist/src/index.js",
3+
"dependencies": {
4+
"@azure-tools/cadl-python": "0.4.25",
5+
"@autorest/python": "6.4.0",
6+
"@azure-tools/cadl-autorest": "0.26.0",
7+
"@azure-tools/cadl-azure-core": "0.26.0",
8+
"@azure-tools/cadl-dpg": "0.26.0",
9+
"@azure-tools/cadl-azure-resource-manager": "0.26.0",
10+
"@cadl-lang/compiler": "0.40.0",
11+
"@cadl-lang/eslint-config-cadl": "0.5.0",
12+
"@cadl-lang/openapi": "0.40.0",
13+
"@cadl-lang/rest": "0.40.0",
14+
"@cadl-lang/versioning": "0.40.0"
15+
}
16+
}

eng/scripts/Language-Settings.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,3 +589,11 @@ function Validate-Python-DocMsPackages ($PackageInfo, $PackageInfos, $PackageSou
589589
-PackageSourceOverride $PackageSourceOverride -DocValidationImageId $DocValidationImageId
590590
}
591591
}
592+
593+
function Get-python-EmitterName() {
594+
return "@azure-tools/cadl-python"
595+
}
596+
597+
function Get-python-EmitterAdditionalOptions([string]$projectDirectory) {
598+
return "--option @azure-tools/cadl-python.emitter-output-dir=$projectDirectory/"
599+
}

scripts/cadl_refresh_sdk/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Overview
2+
The script is to refresh SDK code
3+
4+
# Prerequisites
5+
1. Install [Powershell 7+](https://learn.microsoft.com/en-us/shows/it-ops-talk/how-to-install-powershell-7)
6+
2. Install [Python 3.7+](https://www.python.org/downloads/release/python-370/)
7+
3. Install [Nodejs](https://nodejs.org/en/download/)
8+
9+
# Usage
10+
Step into root folder of this repo and run cmd with sdk folder:
11+
```powershell
12+
D:\azure-sdk-for-python> python .\scripts\cadl_refresh_sdk\main.py .\sdk\contosowidgetmanager\azure-contosowidgetmanager
13+
```

scripts/cadl_refresh_sdk/main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
import sys
3+
from pathlib import Path
4+
from subprocess import check_call
5+
6+
7+
def main(sdk_folder: str):
8+
# install package.json
9+
script = Path("eng/common/scripts/Cadl-Project-Sync.ps1")
10+
check_call(f"pwsh {script} {Path(sdk_folder)}", shell=True)
11+
12+
# generate SDK
13+
cmd = Path("eng/common/scripts/Cadl-Project-Generate.ps1")
14+
check_call(f"pwsh {cmd} {Path(sdk_folder)}", shell=True)
15+
16+
if __name__ == '__main__':
17+
if len(sys.argv[1:]) != 1:
18+
print("Please input sdk folder like: sdk/datadog/azure-mgmt-datadog")
19+
else:
20+
main(sys.argv[1])

tools/azure-sdk-tools/packaging_tools/generate_utils.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@ def update_servicemetadata(sdk_folder, data, config, folder_name, package_name,
127127
f.write("".join(includes))
128128

129129

130+
def update_cadl_location(sdk_folder, data, config, folder_name, package_name, input_readme):
131+
if "meta" in config:
132+
return
133+
134+
metadata = {
135+
"directory": input_readme,
136+
"commit": data["headSha"],
137+
"repo": data["repoHttpsUrl"].split("github.com/")[-1],
138+
"cleanup": False,
139+
}
140+
141+
_LOGGER.info("cadl-location:\n {}".format(json.dumps(metadata, indent=2)))
142+
143+
package_folder = Path(sdk_folder) / folder_name / package_name
144+
if not package_folder.exists():
145+
_LOGGER.info(f"Package folder doesn't exist: {package_folder}")
146+
return
147+
148+
metadata_file_path = package_folder / "cadl-location.yaml"
149+
with open(metadata_file_path, "w") as writer:
150+
yaml.safe_dump(metadata, writer)
151+
_LOGGER.info(f"Saved metadata to {metadata_file_path}")
152+
153+
130154
def judge_tag_preview(path: str) -> bool:
131155
files = [i for i in Path(path).glob("**/*.py")]
132156
default_api_version = "" # for multi-api
@@ -365,17 +389,11 @@ def gen_cadl(cadl_relative_path: str, spec_folder: str) -> Dict[str, Any]:
365389

366390
# npm install tool
367391
origin_path = os.getcwd()
368-
with open("cadl_to_sdk_config.json", "r") as file_in:
392+
with open(Path("eng/emitter-package.json"), "r") as file_in:
369393
cadl_python_dep = json.load(file_in)
370394
os.chdir(Path(spec_folder) / cadl_relative_path)
371-
if Path("package.json").exists():
372-
with open("package.json", "r") as file_in:
373-
cadl_tools = json.load(file_in)
374-
else:
375-
cadl_tools = {"dependencies": dict()}
376-
cadl_tools["dependencies"].update(cadl_python_dep["dependencies"])
377395
with open("package.json", "w") as file_out:
378-
json.dump(cadl_tools, file_out)
396+
json.dump(cadl_python_dep, file_out)
379397
check_call("npm install", shell=True)
380398

381399
# generate code

tools/azure-sdk-tools/packaging_tools/sdk_generator.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
format_samples,
1919
gen_dpg,
2020
dpg_relative_folder,
21-
gen_cadl
21+
gen_cadl,
22+
update_cadl_location,
2223
)
2324

2425
_LOGGER = logging.getLogger(__name__)
@@ -147,6 +148,19 @@ def main(generate_input, generate_output):
147148
)
148149
except Exception as e:
149150
_LOGGER.info(f"fail to update meta: {str(e)}")
151+
152+
# update cadl-location.yaml
153+
try:
154+
update_cadl_location(
155+
sdk_folder,
156+
data,
157+
config,
158+
folder_name,
159+
package_name,
160+
input_readme,
161+
)
162+
except Exception as e:
163+
_LOGGER.info(f"fail to update cadl-location: {str(e)}")
150164

151165
# Setup package locally
152166
check_call(

0 commit comments

Comments
 (0)