Skip to content

Commit

Permalink
[Pipeline][typespec] use common script (Azure#30646)
Browse files Browse the repository at this point in the history
* use common script

* update for automation

* update

* update

* remove unnecessary dependency

* update python emitter

* Update emitter-package.json
  • Loading branch information
msyyc authored Jun 14, 2023
1 parent 1fd5c0a commit 2ac597f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 69 deletions.
2 changes: 1 addition & 1 deletion eng/emitter-package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"main": "dist/src/index.js",
"dependencies": {
"@azure-tools/typespec-python": "0.10.0"
"@azure-tools/typespec-python": "0.11.0"
}
}
59 changes: 6 additions & 53 deletions tools/azure-sdk-tools/packaging_tools/generate_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,30 +145,6 @@ def update_servicemetadata(sdk_folder, data, config, folder_name, package_name,
f.write("".join(includes))


def update_typespec_location(sdk_folder, data, config, folder_name, package_name, input_readme):
if "meta" in config:
return

metadata = {
"directory": input_readme,
"commit": data["headSha"],
"repo": data["repoHttpsUrl"].split("github.com/")[-1],
"cleanup": False,
}

_LOGGER.info("tsp-location:\n {}".format(json.dumps(metadata, indent=2)))

package_folder = Path(sdk_folder) / folder_name / package_name
if not package_folder.exists():
_LOGGER.info(f"Package folder doesn't exist: {package_folder}")
return

metadata_file_path = package_folder / "tsp-location.yaml"
with open(metadata_file_path, "w") as writer:
yaml.safe_dump(metadata, writer)
_LOGGER.info(f"Saved metadata to {metadata_file_path}")


def judge_tag_preview(path: str) -> bool:
files = [i for i in Path(path).glob("**/*.py")]
default_api_version = "" # for multi-api
Expand Down Expand Up @@ -371,17 +347,6 @@ def format_samples(sdk_code_path) -> None:

_LOGGER.info(f"format generated_samples successfully")

def get_npm_package_version(package: str) -> Dict[any, any]:
temp_file = "python_temp.json"
check_call(f"npm list {package} -json > {temp_file}", shell=True)
with open(temp_file, "r") as file_in:
data = json.load(file_in)
if "dependencies" not in data:
_LOGGER.info(f"can not find {package}: {data}")
return {}

return data["dependencies"]

def generate_ci(template_path: Path, folder_path: Path, package_name: str) -> None:
ci = Path(folder_path, "ci.yml")
service_name = folder_path.name
Expand All @@ -401,27 +366,15 @@ def generate_ci(template_path: Path, folder_path: Path, package_name: str) -> No
with open(ci, "w") as file_out:
file_out.writelines(content)

def gen_typespec(typespec_relative_path: str, spec_folder: str) -> Dict[str, Any]:
def gen_typespec(typespec_relative_path: str, spec_folder: str, head_sha: str, rest_repo_url: str) -> Dict[str, Any]:
typespec_python = "@azure-tools/typespec-python"
autorest_python = "@autorest/python"

# npm install tool
origin_path = os.getcwd()
with open(Path("eng/emitter-package.json"), "r") as file_in:
typespec_python_dep = json.load(file_in)
os.chdir(Path(spec_folder) / typespec_relative_path)
with open("package.json", "w") as file_out:
json.dump(typespec_python_dep, file_out)
check_call("npm install", shell=True)

# generate code
typespec_file = "client.tsp" if Path("client.tsp").exists() else "."
check_call(f"npx tsp compile {typespec_file} --emit {typespec_python} --arg \"python-sdk-folder={origin_path}\" ", shell=True)
# call scirpt to generate sdk
check_call(f'pwsh {Path("eng/common/scripts/TypeSpec-Project-Process.ps1")} {(Path(spec_folder) / typespec_relative_path).resolve()} {head_sha} {rest_repo_url}', shell=True)

# get version of codegen used in generation
npm_package_verstion = get_npm_package_version(autorest_python)

# return to original folder
os.chdir(origin_path)
with open(Path("eng/emitter-package.json"), "r") as file_in:
data = json.load(file_in)
npm_package_verstion = {typespec_python: data["dependencies"][typespec_python]}

return npm_package_verstion
16 changes: 1 addition & 15 deletions tools/azure-sdk-tools/packaging_tools/sdk_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
gen_dpg,
dpg_relative_folder,
gen_typespec,
update_typespec_location,
return_origin_path,
check_api_version_in_subfolder,
)
Expand Down Expand Up @@ -121,7 +120,7 @@ def main(generate_input, generate_output):
elif "data-plane" in input_readme:
config = gen_dpg(input_readme, data.get("autorestConfig", ""), dpg_relative_folder(spec_folder))
else:
config = gen_typespec(input_readme, spec_folder)
config = gen_typespec(input_readme, spec_folder, data["headSha"], data["repoHttpsUrl"])
is_typespec = True
package_names = get_package_names(sdk_folder)
_LOGGER.info(f"[CODEGEN]({input_readme})codegen end. [(packages:{str(package_names)})]")
Expand Down Expand Up @@ -162,19 +161,6 @@ def main(generate_input, generate_output):
except Exception as e:
_LOGGER.info(f"fail to update meta: {str(e)}")

# update tsp-location.yaml
try:
update_typespec_location(
sdk_folder,
data,
config,
folder_name,
package_name,
input_readme,
)
except Exception as e:
_LOGGER.info(f"fail to update tsp-location: {str(e)}")

# Setup package locally
check_call(
f"pip install --ignore-requires-python -e {sdk_code_path}",
Expand Down

0 comments on commit 2ac597f

Please sign in to comment.