-
Notifications
You must be signed in to change notification settings - Fork 58
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
Add templating processor for native apps #1503
Merged
sfc-gh-melnacouzi
merged 10 commits into
main
from
melnacouzi-add-templating-processors
Aug 28, 2024
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d85d426
Add templating processor for native apps
sfc-gh-melnacouzi 6dd67c4
Add release note and fix a bug
sfc-gh-melnacouzi f9f2dfe
Add subdirectories for integ tests
sfc-gh-melnacouzi 7c16268
Merge branch 'main' into melnacouzi-add-templating-processors
sfc-gh-melnacouzi 6377f7f
Fix failing test due to changing error message
sfc-gh-melnacouzi 46f9885
Fix failing test due to changing error message
sfc-gh-melnacouzi e8fa870
Remove output folder
sfc-gh-melnacouzi c412e27
Refactor tests
sfc-gh-melnacouzi 4e8a457
Rename templating to templates
sfc-gh-melnacouzi 72b5d5e
Merge branch 'main' into melnacouzi-add-templating-processors
sfc-gh-melnacouzi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/snowflake/cli/_plugins/nativeapp/codegen/templating/templating_processor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# Copyright (c) 2024 Snowflake Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Optional | ||
|
||
import jinja2 | ||
from snowflake.cli._plugins.nativeapp.artifacts import BundleMap | ||
from snowflake.cli._plugins.nativeapp.codegen.artifact_processor import ( | ||
ArtifactProcessor, | ||
) | ||
from snowflake.cli._plugins.nativeapp.exceptions import InvalidTemplateInFileError | ||
from snowflake.cli.api.cli_global_context import get_cli_context | ||
from snowflake.cli.api.console import cli_console as cc | ||
from snowflake.cli.api.project.schemas.native_app.path_mapping import ( | ||
PathMapping, | ||
ProcessorMapping, | ||
) | ||
from snowflake.cli.api.rendering.project_definition_templates import ( | ||
get_client_side_jinja_env, | ||
) | ||
from snowflake.cli.api.rendering.sql_templates import ( | ||
choose_sql_jinja_env_based_on_template_syntax, | ||
) | ||
|
||
|
||
class TemplatingProcessor(ArtifactProcessor): | ||
""" | ||
Processor class to perform templating on all relevant artifacts (specified in the project definition file). | ||
""" | ||
|
||
def process( | ||
self, | ||
artifact_to_process: PathMapping, | ||
processor_mapping: Optional[ProcessorMapping], | ||
**kwargs, | ||
): | ||
""" | ||
Process the artifact by executing the templating logic on it. | ||
""" | ||
cc.step(f"Processing artifact {artifact_to_process} with templating processor") | ||
sfc-gh-bdufour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
bundle_map = BundleMap( | ||
project_root=self._bundle_ctx.project_root, | ||
deploy_root=self._bundle_ctx.deploy_root, | ||
) | ||
bundle_map.add(artifact_to_process) | ||
|
||
for src, dest in bundle_map.all_mappings( | ||
absolute=True, | ||
expand_directories=True, | ||
): | ||
if src.is_dir(): | ||
continue | ||
sfc-gh-bdufour marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with self.edit_file(dest) as f: | ||
file_name = src.relative_to(self._bundle_ctx.project_root) | ||
|
||
jinja_env = ( | ||
choose_sql_jinja_env_based_on_template_syntax( | ||
f.contents, reference_name=file_name | ||
) | ||
if dest.name.lower().endswith(".sql") | ||
else get_client_side_jinja_env() | ||
) | ||
|
||
try: | ||
expanded_template = jinja_env.from_string(f.contents).render( | ||
get_cli_context().template_context | ||
) | ||
|
||
# For now, we are printing the source file path in the error message | ||
# instead of the destination file path to make it easier for the user | ||
# to identify the file that has the error, and edit the correct file. | ||
except jinja2.TemplateSyntaxError as e: | ||
raise InvalidTemplateInFileError(file_name, e, e.lineno) from e | ||
|
||
except jinja2.UndefinedError as e: | ||
raise InvalidTemplateInFileError(file_name, e) from e | ||
|
||
if expanded_template != f.contents: | ||
f.edited_contents = expanded_template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
a little bikeshedding necessary for 'templating' here. How do we refer to the templates feature in public docs again?
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.
documentation is being updated to refer to these as
template
ortemplates
:Project definition file templates
instead ofProject definition file templating
.I think we can switch to
templates
for this. WDYT?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
template expansion
as another optionThere 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.
+1 for
templates
thenThere 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.
updated
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.
trivial but can you update
templating
totemplates
in the pr description as well :')