René's URL Explorer Experiment


Title: Support pure C# DSL keywords in PowerShell · Issue #3243 · PowerShell/PowerShell · GitHub

Open Graph Title: Support pure C# DSL keywords in PowerShell · Issue #3243 · PowerShell/PowerShell

X Title: Support pure C# DSL keywords in PowerShell · Issue #3243 · PowerShell/PowerShell

Description: This is slightly premature, given that it assumes #3169 , but I want to lay out a possible roadmap for improving the DSL/DynamicKeyword functionality in that PR. #3169 provides C# syntax for specifying a dynamic keyword syntax, but achie...

Open Graph Description: This is slightly premature, given that it assumes #3169 , but I want to lay out a possible roadmap for improving the DSL/DynamicKeyword functionality in that PR. #3169 provides C# syntax for specif...

X Description: This is slightly premature, given that it assumes #3169 , but I want to lay out a possible roadmap for improving the DSL/DynamicKeyword functionality in that PR. #3169 provides C# syntax for specif...

Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3243

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support pure C# DSL keywords in PowerShell","articleBody":"This is slightly premature, given that it assumes #3169 , but I want to lay out a possible roadmap for improving the DSL/DynamicKeyword functionality in that PR.\r\n\r\n#3169 provides C# syntax for specifying a dynamic keyword syntax, but achieves the actual keyword functionality by invoking PowerShell functions of the form `DslModuleName\\DslKeywordName`. This works by rewriting the AST at compile time so that what the user has written as keyword invocations get turned into function calls, where the \"bodies\" of keywords (scriptblocks or hashtables) get passed in as arguments to the function (essentially DSLs are syntactic sugar for nested function calls). This works well, but some users may prefer to write everything within the same file, just like Cmdlets. There are a few steps to make here.\r\n\r\nWork out just how much we can change the public, but relatively unused, `DynamicKeyword` API\r\n---\r\nAt the moment, this looks like:\r\n```powershell\r\n$kw = [System.Management.Automation.DynamicKeyword]::new()\r\n$kw.Keyword = \"MyKeyword\"\r\n$kw.PostParse = { ... }\r\n...\r\n[System.Management.Automation.DynamicKeyword]::AddKeyword($kw)\r\n```\r\nThis API is currently available through and stored by a thread-static variable (as seen above). However, because keywords are specific to a parser session, they are likely better stored as parser-local. This would be a breaking change, but with a good cause -- especially since PowerShell currently keeps the parser and the REPL in different threads. While this API might always have a use, it might be nice to prefer a more fluent way to define DSL keywords within PowerShell (such as a keyword-defining DSL).\r\n\r\nUpdate [DynamicKeywordStatementRule](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/parser/Parser.cs#L3379)\r\n---\r\nCurrently this is relatively specific for DSC keywords, and in particular has special logic for names in keywords, and does not allow keywords to use parameters -- parameters passed to a scriptblock- or hashtable-bodied keyword will be ignored. For a suggestion on how an update to this might look, see [a rough prototype I wrote](https://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/Parser.cs#L3753).\r\n\r\nSeparate DSC keywords from `DynamicKeyword`s\r\n---\r\nThis class is currently relatively specific to DSC keywords, and might be better as a base type that DSC keywords can inherit from. It contains assumptions such as `ResourceName`, as well as an extension class (`DynamicKeywordExtension`) which would be a good candidate for features on a `DscKeyword` class.\r\n\r\nBreak up the `DynamicKeyword` class\r\n---\r\n Currently `DynamicKeyword` holds information like keyword name and parameter type constraints, that can be read in without loading an assembly, as well as delegates like `SemanticCheck` that do require an assembly load. Moving to a pure C# implementation without changing `DynamicKeyword` might result in it becoming a catch-all monolith. Instead, breaking it up might pay some dividends:\r\n- A `DynamicKeywordMetadata` class could hold any information that can be obtained without loading the keyword's containing assembly. Ideally this class would be immutable after construction like other elements in the AST. This could inherit from `CommandMetadata`, containing things like `ParameterMetadata`.\r\n- A `DynamicKeywordInfo` class, just like `CmdletInfo` would contain information evaluated from loading the user-defined type that defines a keyword, and would inherit from `CommandInfo`. This would similarly be immutable, but more aimed at storing parse-time delegates and being used at compile-time. For a rough prototype, see [here](https://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/DslSpecification.cs#L29).\r\n- `DynamicKeyword` itself would perhaps be better repurposed in the namespace in the same way as `Cmdlet`, so as the abstract base class that user-defined keywords inherit from, with virtual methods to provide useful abstractions over the complexity of the PowerShell internals. A benefit here is that DSC keywords could be defined by inheriting from `DscKeyword`. Again, see [here](https://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/DslSpecification.cs#L313).\r\n- Finally, the state managed in the static part of `DynamicKeyword` should be taken out and managed with structures like `DynamicKeywordScope` and `DynamicKeywordRuntimeState` and moved to be parser-local. This is already in #3169 [here](https://github.com/rjmholt/PowerShell/blob/dynamic-keyword-metadata-reader/src/System.Management.Automation/engine/parser/DynamicKeyword.cs#L200) and [here](https://github.com/rjmholt/PowerShell/blob/dynamic-keyword-metadata-reader/src/System.Management.Automation/engine/parser/DynamicKeyword.cs#L389),\r\n\r\n\r\nCompiling the C# keyword\r\n---\r\nThis seemed originally to be a hard problem (and may still be), since users must be able to specify when the expressions they pass to their keywords get evaluated -- in case they are side-effectful. Meaning that arbitrary PowerShell expressions that may reference runtime state (like `$y + Get-Result -Param ($z+1)`) must be passed into the keyword functions written in C# without executing them, and then executed at a time when the C# code prescribes.\r\n\r\n(Consider in Pester: I can put a `BeforeAll` block after all other keywords, but if the keywords above it are executed before it, they might not have the state set up that they need.)\r\n\r\nThis is solved in the current implementation by passing the [body as an expression](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/parser/ast.cs#L7220) to the function that implements the keyword -- essentially canning the keyword body like a [thunk](https://en.wikipedia.org/wiki/Thunk) so that the implementing user can choose when (of if) to evaluate it.\r\n\r\nI think a similar method of creating a C# `ScriptBlock` object for keywords to take in as an argument might work well, which would effectively manage the problem of how state set up by outer keywords is communicated to inner keywords. But it certainly bodes discussion.\r\n\r\nPassing parameters to keywords\r\n---\r\nThis is the actual hard problem. In order to use the parameter binding algorithm to set parameters on keywords implemented as C#/.NET objects, we need to use the parameter binder logic to make sure that parameters passed to keywords work and are resolved in exactly the same way as parameters passed to any other PowerShell command -- that users' expectations are met.\r\n\r\nFrom what I've read in the code so far, much of the existing logic is spread across `ParameterBinderBase`, `ParameterBinderController`, `CmdletParameterBinderController` and several others. My understanding is that there are two parameter binders in use. `ReflectionParameterBinder` and `ScriptParameterBinder` do most of the heavy lifting with some common code in the base class, and `PseudoParameterBinder` uses a combination of the two internally, while `NativeCommandParameterBinder` does its best to just pass arguments through as strings. The difficulty arises when `CommandProcess` [assumes that the command invoking it could only be a ScriptBlock or a Cmdlet](https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/CommandProcessor.cs#L114). However, this might be easier to fix than I think, or more likely I am wrong about how this all works.\r\n\r\nThis is complicated by the fact that the parameters on keywords (not their values) are known at compile time -- so it's less complicated to perform parameter and parameter type checks than for Cmdlets.\r\n\r\nFor what it's worth, I have a [small start on a C#-keyword parameter binder that doesn't work](https://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/DslSpecification.cs#L673), but similarly doesn't crash PowerShell (I consider this an achievement...). It seems like the alternatives are:\r\n- Rewrite some of the current parameter binder logic so that base classes make less assumptions about subclasses and also provide more of the parameter binding logic in a generic way.\r\n- Partially re-implement the parameter binding algorithm in a new subclass of `ParameterBinderBase` and only touch what needs to be changed in the existing parts of code like `CommandProcessor`.\r\n\r\nThis is as far as I've gotten with moving dynamic keywords to a pure C# implementation -- and I wanted to record it for discussion.","author":{"url":"https://github.com/rjmholt","@type":"Person","name":"rjmholt"},"datePublished":"2017-03-03T00:44:38Z","url":"https://github.com/PowerShell/PowerShell/issues/3243"}

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322
route-pattern/_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format)
route-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:9248cc28-8da0-3f94-a2ab-a124c4db542d
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idAA4A:3C5A36:1B5933:274426:6AFDD045
html-safe-noncee1826992deea73c091883852d4a588eb2c143c545b8e24fc229b9a98811ffba2
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTRBOjNDNUEzNjoxQjU5MzM6Mjc0NDI2OjZBRkREMDQ1IiwidmlzaXRvcl9pZCI6IjE1Njg2NzEyOTEyMjc4ODU2MzciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacb75859d4ee758cf80b9733e320343ba277d4069999ea914dbed59394f0f343fa
hovercard-subject-tagissue:211563093
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/3243/issue_layout
twitter:imagehttps://opengraph.githubassets.com/ee60af2cbbddf60e4b4fb191e38dc68b82b1e2931fb846cd77f812618aaea2a9/PowerShell/PowerShell/issues/3243
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/ee60af2cbbddf60e4b4fb191e38dc68b82b1e2931fb846cd77f812618aaea2a9/PowerShell/PowerShell/issues/3243
og:image:altThis is slightly premature, given that it assumes #3169 , but I want to lay out a possible roadmap for improving the DSL/DynamicKeyword functionality in that PR. #3169 provides C# syntax for specif...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamerjmholt
hostnamegithub.com
expected-hostnamegithub.com
None26886a40b582b821a7a76351e0a2f42b6d6160854b6e2520db24f31fd7be5da7
turbo-cache-controlno-preview
go-importgithub.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git
octolytics-dimension-user_id11524380
octolytics-dimension-user_loginPowerShell
octolytics-dimension-repository_id49609581
octolytics-dimension-repository_nwoPowerShell/PowerShell
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id49609581
octolytics-dimension-repository_network_root_nwoPowerShell/PowerShell
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release68ebaa631a1d248d69ebde4b9c5defd3a19aeb2f
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/PowerShell/PowerShell/issues/3243#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F3243
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2FPowerShell%2FPowerShell%2Fissues%2F3243
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=PowerShell%2FPowerShell
Reloadhttps://github.com/PowerShell/PowerShell/issues/3243
Reloadhttps://github.com/PowerShell/PowerShell/issues/3243
Reloadhttps://github.com/PowerShell/PowerShell/issues/3243
PowerShellhttps://github.com/PowerShell
PowerShellhttps://github.com/PowerShell/PowerShell
Codehttps://github.com/PowerShell/PowerShell
Issues1.3k (1.3k)https://github.com/PowerShell/PowerShell/issues
Pull requests366 (366)https://github.com/PowerShell/PowerShell/pulls
Discussionshttps://github.com/PowerShell/PowerShell/discussions
Actionshttps://github.com/PowerShell/PowerShell/actions
Projectshttps://github.com/PowerShell/PowerShell/projects
Security and quality3 (3)https://github.com/PowerShell/PowerShell/security
Insightshttps://github.com/PowerShell/PowerShell/insights
https://github.com/PowerShell/PowerShell
New issuehttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/3243
New issuehttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/3243
Support pure C# DSL keywords in PowerShellhttps://github.com/PowerShell/PowerShell/issues/3243#top
https://github.com/daxian-dbw
Issue-Enhancementthe issue is more of a feature request than a bughttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Enhancement%22
WG-Languageparser, language semanticshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Language%22
6.2.0-Considerhttps://github.com/PowerShell/PowerShell/milestone/42
https://github.com/rjmholt
Rob Holt (rjmholt)https://github.com/rjmholt
on Mar 3, 2017https://github.com/PowerShell/PowerShell/issues/3243#issue-211563093
#3169https://github.com/PowerShell/PowerShell/pull/3169
#3169https://github.com/PowerShell/PowerShell/pull/3169
DynamicKeywordStatementRulehttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/parser/Parser.cs#L3379
a rough prototype I wrotehttps://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/Parser.cs#L3753
herehttps://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/DslSpecification.cs#L29
herehttps://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/DslSpecification.cs#L313
Dynamic keyword metadata reader #3169https://github.com/PowerShell/PowerShell/pull/3169
herehttps://github.com/rjmholt/PowerShell/blob/dynamic-keyword-metadata-reader/src/System.Management.Automation/engine/parser/DynamicKeyword.cs#L200
herehttps://github.com/rjmholt/PowerShell/blob/dynamic-keyword-metadata-reader/src/System.Management.Automation/engine/parser/DynamicKeyword.cs#L389
body as an expressionhttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/parser/ast.cs#L7220
thunkhttps://en.wikipedia.org/wiki/Thunk
assumes that the command invoking it could only be a ScriptBlock or a Cmdlethttps://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/CommandProcessor.cs#L114
small start on a C#-keyword parameter binder that doesn't workhttps://github.com/rjmholt/PowerShell/blob/dsl-csharp-prototype/src/System.Management.Automation/engine/parser/DslSpecification.cs#L673
https://github.com/rjmholt
Rob Holt (rjmholt)https://github.com/rjmholt
on Mar 3, 2017https://github.com/PowerShell/PowerShell/issues/3243#issuecomment-283830352
Steve Lee (SteveL-MSFT)https://github.com/SteveL-MSFT
WG-Languageparser, language semanticshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Language%22
Issue-Enhancementthe issue is more of a feature request than a bughttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Enhancement%22
on Mar 4, 2017https://github.com/PowerShell/PowerShell/issues/3243#event-986369755
Steve Lee (SteveL-MSFT)https://github.com/SteveL-MSFT
on Mar 4, 2017https://github.com/PowerShell/PowerShell/issues/3243#event-986369890
Steve Lee (SteveL-MSFT)https://github.com/SteveL-MSFT
Dongbo Wang (daxian-dbw)https://github.com/daxian-dbw
on Jun 6, 2017https://github.com/PowerShell/PowerShell/issues/3243#event-1112477691
Steve Lee (SteveL-MSFT)https://github.com/SteveL-MSFT
6.1.0-Considerhttps://github.com/PowerShell/PowerShell/milestone/26
6.2.0-Considerhttps://github.com/PowerShell/PowerShell/milestone/42
on Jan 31, 2018https://github.com/PowerShell/PowerShell/issues/3243#event-1450039059
https://github.com/rjmholt
Rob Holt (rjmholt)https://github.com/rjmholt
on Apr 13, 2018https://github.com/PowerShell/PowerShell/issues/3243#issuecomment-381277953
Rob Holt (rjmholt)https://github.com/rjmholt
completedhttps://github.com/PowerShell/PowerShell/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Apr 13, 2018https://github.com/PowerShell/PowerShell/issues/3243#event-1574263299
Sign up for freehttps://github.com/signup?return_to=https://github.com/PowerShell/PowerShell/issues/3243
Sign in to commenthttps://github.com/login?return_to=https://github.com/PowerShell/PowerShell/issues/3243
Dongbo Wang (daxian-dbw)https://github.com/daxian-dbw
Issue-Enhancementthe issue is more of a feature request than a bughttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22Issue-Enhancement%22
WG-Languageparser, language semanticshttps://github.com/PowerShell/PowerShell/issues?q=state%3Aopen%20label%3A%22WG-Language%22
6.2.0-Considerhttps://github.com/PowerShell/PowerShell/milestone/42
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.