Skip to content

Run optional post-emitter.ps1 script after SDK generation#47456

Open
msyyc wants to merge 10 commits into
Azure:mainfrom
msyyc:feature/post-emitter-script
Open

Run optional post-emitter.ps1 script after SDK generation#47456
msyyc wants to merge 10 commits into
Azure:mainfrom
msyyc:feature/post-emitter-script

Conversation

@msyyc

@msyyc msyyc commented Jun 11, 2026

Copy link
Copy Markdown
Member

for microsoft/typespec#10230

Summary

Adds support for an optional, hard-coded post-emitter PowerShell script that runs after SDK code generation. When a script named _post_emitter.ps1 exists directly inside a generated package folder (sdk/<service>/azure-*), the SDK generation pipeline (sdk_generator.py) executes it. This lets service teams (e.g. Foundry) run custom post-processing on the generated SDK.

The leading underscore in _post_emitter.ps1 signals that the script is private to the package.

This is intentionally not language-specific in spirit; each language repo owns its own pipeline, so the same one-line hook can be replicated to other language repos once validated for Python.

Behavior

  • Looks for _post_emitter.ps1 in the package folder after code generation / metadata update and before ApiView + build, so downstream steps pick up any changes.
  • Runs only if the script is present; otherwise it is a no-op.

Safety / robustness

  • Non-interactive invocation: pwsh -NonInteractive -NoProfile -ExecutionPolicy Bypass -File.
  • Output logged: stdout/stderr are captured and written to the pipeline log (stdout as INFO, stderr as WARNING).
  • Timeout: 600s, so a hung script cannot stall the pipeline.
  • Failure isolation: all failures (non-zero exit, timeout, exceptions) are logged as warnings and never fail the overall generation.
  • Graceful degradation: if no PowerShell (pwsh/powershell) executable is available, it logs a warning and skips.

Note: PowerShell has no built-in way to confine a script's file writes to a folder. If stronger isolation is needed, it must come from the OS layer (filesystem ACLs / dedicated low-privilege user, or a container/sandbox mounting only the package folder read-write). A git-diff post-check could also be added to detect writes outside the package folder.

Testing

Validated locally with a temp package folder containing a _post_emitter.ps1: script executes, stdout/stderr are logged, and the no-script case is a no-op.

Add support for a hard-coded, optional post-emitter PowerShell script
(post-emitter.ps1) located in the generated package folder
(sdk/<service>/azure-*). When present, it is executed after code
generation so service teams (e.g. Foundry) can run custom
post-processing on the generated SDK.

Safety/robustness:
- Only runs a script located directly inside the package folder
  (guards against path traversal / symlinks).
- Invoked non-interactively (-NonInteractive -NoProfile).
- Captures and logs stdout/stderr so output is visible in the pipeline.
- Wrapped with a 600s timeout.
- Failures are logged but never fail the overall generation.
- Gracefully skips when no PowerShell (pwsh) executable is available.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 11, 2026 07:07
@msyyc msyyc requested review from danieljurek and scbedd as code owners June 11, 2026 07:07
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR extends the eng/tools/azure-sdk-tools SDK generation pipeline by adding a post-generation hook: if a post-emitter.ps1 script exists directly in a generated package folder, sdk_generator.py will execute it after codegen/metadata updates and before ApiView/build steps, enabling service-specific post-processing.

Changes:

  • Added run_post_emitter_script() to detect and execute an optional post-emitter.ps1 in the generated package directory with logging, timeout, and failure isolation.
  • Wired the new hook into main() immediately after sdk_update_metadata(...), ensuring downstream steps see any script-produced changes.

Comment thread eng/tools/azure-sdk-tools/packaging_tools/sdk_generator.py
Comment thread eng/tools/azure-sdk-tools/packaging_tools/sdk_generator.py
msyyc and others added 3 commits June 11, 2026 15:13
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@msyyc msyyc requested a review from iscai-msft June 11, 2026 07:36
Comment thread eng/tools/azure-sdk-tools/packaging_tools/sdk_generator.py Outdated
@iscai-msft

Copy link
Copy Markdown
Contributor

@copilot change the POST_EMITTER_SCRIPT_NAME variable value from _post_emitter.ps1 to PostEmitter.ps1

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread eng/tools/azure-sdk-tools/packaging_tools/sdk_generator.py
Comment thread eng/tools/azure-sdk-tools/packaging_tools/sdk_generator.py Outdated
msyyc and others added 4 commits June 12, 2026 10:15
Helps debugging when a service team adds the script but does not see it
run. Also fix a stale docstring reference to the script name.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
… filename

Avoids the docstring drifting out of sync with the constant (the
previous literal was stale). The constant is now the single source of
truth for the script name.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@msyyc msyyc requested a review from dargilco June 12, 2026 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants