Skip to content
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

TemplateProvider for python v2 templates #3799

Merged
merged 29 commits into from
Sep 11, 2023
Merged

TemplateProvider for python v2 templates #3799

merged 29 commits into from
Sep 11, 2023

Conversation

nturinski
Copy link
Member

Combines the feedback from both #3729 and #3742

This is a massive PR, but hopefully shouldn't touch any existing logic with our old code paths. I'll delete the readme file before I merge, but I left it in here for some context as to what/why I'm doing for parsing the new templates.

@nturinski nturinski requested a review from a team as a code owner August 10, 2023 21:38
src/How to parse v2 Templates.md Outdated Show resolved Hide resolved
import { ParsedAction } from "../../../templates/script/parseScriptTemplatesV2";
import { FunctionV2WizardContext } from "../FunctionV2WizardContext";

export abstract class ActionSchemaBaseStep<T extends FunctionV2WizardContext> extends AzureWizardExecuteStep<T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Base should always be a postfix. So ActionSchemaStepBase.

Also I think you could remove schema from the name. Maybe add in a V2 somewhere ActionV2StepBase?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. I was thinking of it as the base for the step, but it's actually the step's base.

English 💯

const filePath = nonNullProp(this.action, 'filePath');
let source: string | undefined = context.functionTemplateV2?.files[filePath];
if (!source) {
throw new Error(`Could not find file "${filePath}" in template`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

localize

Comment on lines +19 to +22
const assignToTokens = Object.keys(context).filter(k => k.startsWith('$('));
for (const token of assignToTokens) {
source = source.replace(new RegExp(this.escapeRegExp(token), 'g'), context[token] as string);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is duplicated in ReplaceTokensInTextExecuteStep.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I wasn't sure what to do. Technically, this step is supposed to exist, but I don't actually see it in any of the jobs. Ideally, I could remove this code and it would be done in ReplaceTokenInTextExecuteStep, but since it never seems to be a step, I'll just delete this with a comment in GetTemplateFileContentExecuteStep`.

export class ShowMarkdownPreviewExecuteStep<T extends FunctionV2WizardContext> extends ActionSchemaBaseStep<T> {
public async executeAction(context: T): Promise<void> {
const filename = nonNullProp(this.action, 'filePath');
const content = context.functionTemplateV2?.files[filename] || '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason using || over ?? here? Just curious.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an old habit 😅

}

public shouldPrompt(context: T): boolean {
return !context[this.input.assignTo];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the input type is boolean, and the user inputs false and context[this.input.assignTo] === false then shouldPrompt() will return true when it should return false since the user has already completed this prompt.

Certainly an edge case, but I could see it causing issues.

We probably should just check if the value is not null or undefined.

src/constants.ts Outdated Show resolved Hide resolved
switch (templateFilter) {
case TemplateFilter.All:
return templates.functionTemplatesV2;
// TODO: Verify if schema V2 templates have categories
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

status on this todo?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They do not. We could still have All vs. Verified (once we add tests) but I'm not sure if there's much value in that. I may just remove the filter for V2

protected _language: string;

public async getLatestTemplates(context: IActionContext, latestTemplateVersion: string): Promise<ITemplates> {
// just use backup templates until template feed deploys v2 templates
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update this comment

for (const job of templateV2.jobs) {
const parsedInputs: ParsedInput[] = [];
job.inputs.forEach(input => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this still needed?

const userPrompts: RawUserPrompt[] = parseUserPrompts(rawBindings, resources);
const templates: FunctionV2Template[] = [];
for (const templateV2 of rawTemplates) {
// look into jobs-- each job can be an Azure Wizard. Name is the title
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update/remote


export interface ITemplatesReleaseV2 extends ITemplatesReleaseBase {
userPrompts: string;
// it is not supposed to exist in the v2 schema, but sometimes userPrompts accidentally gets replaced with bindings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to a GitHub issue for this?

const bundleTemplateTypes: string[] = ['durable', 'signalr'];
// I don't think v2 templates are ever a bundle template? Will have to verify this
Copy link
Member

@alexweininger alexweininger Aug 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the verdict 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nah

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gif caught my attention 😅

Yeah they should be bundle templates. For example, we should have a different template for a Cosmos DB trigger depending if it's a newer bundle or old bundle. Here are examples of how the code is different:

If this isn't the case, we can have a chat with Naren and I'm sure he'll sort us out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, what I meant is, I assume that they are all "true" and since it wouldn't be included in the v1 runtime, we don't ever call it in the first place.

So nah, we don't need this.

@nturinski nturinski force-pushed the nat/v2TemplateSchema branch from 3e46c10 to 1878157 Compare September 8, 2023 21:48
alexweininger
alexweininger previously approved these changes Sep 11, 2023
alexweininger
alexweininger previously approved these changes Sep 11, 2023
@nturinski nturinski merged commit 53db433 into main Sep 11, 2023
@nturinski nturinski deleted the nat/v2TemplateSchema branch September 11, 2023 22:36
@microsoft microsoft locked and limited conversation to collaborators Oct 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants