-
Notifications
You must be signed in to change notification settings - Fork 3.3k
{Release} Hotfix: TS Multiline String Support for Template Inputs #15760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
9d1dbe0
b9edbd8
5c5b27c
85c876f
11f8d85
bd1ac9f
31522c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,9 +3,10 @@ | |
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
| import os | ||
| import re | ||
| import json | ||
| from knack.util import CLIError | ||
| from azure.cli.core.util import read_file_content | ||
| from azure.cli.core.util import read_file_content, shell_safe_json_parse | ||
| from azure.cli.command_modules.resource.custom import _remove_comments_from_json | ||
| from azure.cli.core.profiles import ResourceType, get_sdk | ||
|
|
||
|
|
@@ -23,6 +24,28 @@ def __init__(self, root_template_directory): | |
| self.Artifact = [] | ||
|
|
||
|
|
||
| # pylint: disable=redefined-outer-name | ||
| def process_template(template, preserve_order=True, file_path=None): | ||
| from jsmin import jsmin | ||
|
|
||
| # When commenting at the bottom of all elements in a JSON object, jsmin has a bug that will wrap lines. | ||
| # It will affect the subsequent multi-line processing logic, so deal with this situation in advance here. | ||
| template = re.sub(r'(^[\t ]*//[\s\S]*?\n)|(^[\t ]*/\*{1,2}[\s\S]*?\*/)', '', template, flags=re.M) | ||
| minified = jsmin(template) | ||
|
|
||
| # Remove extra spaces, compress multiline string(s) | ||
| result = re.sub(r'\s\s+', ' ', minified, flags=re.DOTALL) | ||
|
|
||
| try: | ||
| return shell_safe_json_parse(result, preserve_order) | ||
| except CLIError: | ||
| # Because the processing of removing comments and compression will lead to misplacement of error location, | ||
| # so the error message should be wrapped. | ||
| if file_path: | ||
| raise CLIError("Failed to parse '{}', please check whether it is a valid JSON format".format(file_path)) | ||
| raise CLIError("Failed to parse the JSON data, please check whether it is a valid JSON format") | ||
|
|
||
|
|
||
| def pack(cmd, template_file): | ||
| """ | ||
| Packs the specified template and its referenced artifacts for use in a Template Spec. | ||
|
|
@@ -32,8 +55,7 @@ def pack(cmd, template_file): | |
| root_template_file_path = os.path.abspath(template_file) | ||
| context = PackingContext(os.path.dirname(root_template_file_path)) | ||
| template_content = read_file_content(template_file) | ||
| sanitized_template = _remove_comments_from_json(template_content) | ||
| template_json = json.loads(json.dumps(sanitized_template)) | ||
| template_json = json.loads(json.dumps(process_template(template_content))) | ||
| _pack_artifacts(cmd, root_template_file_path, context) | ||
| return PackagedTemplate(template_json, getattr(context, 'Artifact')) | ||
|
|
||
|
|
@@ -54,8 +76,7 @@ def _pack_artifacts(cmd, template_abs_file_path, context): | |
| try: | ||
| context.CurrentDirectory = os.path.dirname(template_abs_file_path) | ||
| template_content = read_file_content(template_abs_file_path) | ||
| artifactable_template_obj = sanitized_template = _remove_comments_from_json(template_content) | ||
| template_json = json.loads(json.dumps(sanitized_template)) | ||
| artifactable_template_obj = _remove_comments_from_json(template_content) | ||
| template_link_to_artifact_objs = _get_template_links_to_artifacts(cmd, artifactable_template_obj, | ||
| includeNested=True) | ||
|
|
||
|
|
@@ -94,8 +115,7 @@ def _pack_artifacts(cmd, template_abs_file_path, context): | |
| TemplateSpecTemplateArtifact = get_sdk(cmd.cli_ctx, ResourceType.MGMT_RESOURCE_TEMPLATESPECS, | ||
| 'TemplateSpecTemplateArtifact', mod='models') | ||
| template_content = read_file_content(abs_local_path) | ||
| sanitized_template = _remove_comments_from_json(template_content) | ||
| template_json = json.loads(json.dumps(sanitized_template)) | ||
| template_json = json.loads(json.dumps(process_template(template_content))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why serialize and then deserialize the object from
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. data returned by process_template is a ordered dictionary due to shell_safe_json_parse, the template_json needs to be a json object.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
| artifact = TemplateSpecTemplateArtifact(path=as_relative_path, template=template_json) | ||
| context.Artifact.append(artifact) | ||
| finally: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could revert this file as this PR #15804 has fixed the build issue
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or merge from
releaseto get a clear branch history.