From de7366c04694c2b69cfd8ad4060c9be7fdd8dfd2 Mon Sep 17 00:00:00 2001 From: Yabo Hu Date: Wed, 5 Aug 2026 19:46:35 +1000 Subject: [PATCH 1/2] support csharp transform --- powershell/utils/tsp-host.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/powershell/utils/tsp-host.ts b/powershell/utils/tsp-host.ts index 5ff5c76c652..55fd21220cd 100644 --- a/powershell/utils/tsp-host.ts +++ b/powershell/utils/tsp-host.ts @@ -1,5 +1,5 @@ import { Message } from '@autorest/extension-base'; -import { comment } from '@azure-tools/codegen'; +import { comment, safeEval } from '@azure-tools/codegen'; import { writeFileSync, existsSync, mkdirSync } from 'fs'; import { dirname } from 'path'; export interface TspWriteFileOptions { @@ -66,6 +66,8 @@ export class TspHostImpl implements TspHost { if (artifactType === 'binary-file') { writeFileSync(filename, Buffer.from(content, 'base64')); } else { + // Apply `transform` directives here since the AutoRest text-transform pipeline step is not run in the TypeSpec flow. + content = this.applyTransformDirectives(content, filename, artifactType); if (artifactType === 'source-file-csharp' && filename.endsWith('.cs')) { const header = comment('Copyright (c) Microsoft Corporation. All rights reserved.\nLicensed under the MIT License. See License.txt in the project root for license information.\nChanges may cause incorrect behavior and will be lost if the code is regenerated.', '//'); content = header + '\n' + content; @@ -73,6 +75,28 @@ export class TspHostImpl implements TspHost { writeFileSync(filename, content); } } + + private applyTransformDirectives(content: string, filename: string, artifactType?: string): string { + if (!artifactType) { + return content; + } + const directives = this.configurations['directive']; + if (!directives) { + return content; + } + for (const directive of (Array.isArray(directives) ? directives : [directives])) { + if (!directive || !directive.transform) { + continue; + } + const from = directive.from; + const froms = from === undefined ? [] : (Array.isArray(from) ? from : [from]); + if (!froms.includes(artifactType)) { + continue; + } + content = safeEval(`(() => { ${directive.transform} })()`, { $: content, $documentPath: filename }); + } + return content; + } UpdateConfigurationFile(filename: string, content: string): void { // Shall not be called throw new Error('Method UpdateConfigurationFile not implemented'); From 6266fe205c773dac97891a9f6dedebdb7682452d Mon Sep 17 00:00:00 2001 From: Yabo Hu Date: Sat, 8 Aug 2026 15:39:29 +1000 Subject: [PATCH 2/2] support non-returned transform --- powershell/utils/tsp-host.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/powershell/utils/tsp-host.ts b/powershell/utils/tsp-host.ts index 55fd21220cd..4fcdce1e55c 100644 --- a/powershell/utils/tsp-host.ts +++ b/powershell/utils/tsp-host.ts @@ -66,7 +66,7 @@ export class TspHostImpl implements TspHost { if (artifactType === 'binary-file') { writeFileSync(filename, Buffer.from(content, 'base64')); } else { - // Apply `transform` directives here since the AutoRest text-transform pipeline step is not run in the TypeSpec flow. + // Apply `transform` directives. content = this.applyTransformDirectives(content, filename, artifactType); if (artifactType === 'source-file-csharp' && filename.endsWith('.cs')) { const header = comment('Copyright (c) Microsoft Corporation. All rights reserved.\nLicensed under the MIT License. See License.txt in the project root for license information.\nChanges may cause incorrect behavior and will be lost if the code is regenerated.', '//'); @@ -93,7 +93,12 @@ export class TspHostImpl implements TspHost { if (!froms.includes(artifactType)) { continue; } - content = safeEval(`(() => { ${directive.transform} })()`, { $: content, $documentPath: filename }); + // A transform may either `return` a new value or reassign `$` in place; fall back to `$` when nothing is returned. + const code = `(() => { let $ = __tsp_content__; const __tsp_ret__ = (() => { ${directive.transform}\n })(); return __tsp_ret__ === undefined ? $ : __tsp_ret__; })()`; + const result = safeEval(code, { __tsp_content__: content, $documentPath: filename }); + if (typeof result === 'string') { + content = result; + } } return content; }