Allow case-insensitive tenant names in ShellHost, Lombiq PowerShell Analyzers - This week in Orchard (19/08/2022)
HttpBackgroundJob helper component, allow case-insensitive tenant names in
ShellHost, and a demo about the Lombiq PowerShell Analyzers! Let's get started!
Orchard Core updates
HttpBackgroundJob helper component
HttpBackgroundJob component allows executing a background job in an isolated
ShellScope after the current HTTP request is completed. It allows a controller (or a Razor Page) action to return without waiting for a job to complete, and then, if the job updates some state data, also allows to render the job state.
Allow case-insensitive tenant names in ShellHost
Let's say you created a page where a user can log in to a specific tenant by providing the tenant's name, username, and password. To identify which tenant the user is trying to access you can use
_shellHost.TryGetSettings(tenantName, out settings), which looks up the tenant by name. The issue here is if the user does not provide a case-sensitive tenant name, the
ShellHost does not find anything.
The solution here is to define case-insensitive dictionaries. Then we would not need any more in some places to do an explicit case insensitive lookup. Check out the
_shellContexts and the
_shellSettings ConcurrentDictionaries on the screen below.
Lombiq PowerShell Analyzers
The Lombiq PowerShell Analyzers provide PowerShell static code analysis via PSScriptAnalyzer and Lombiq's recommended configuration for it. Use it from the CLI, in GitHub Actions, or integrated into MSBuild builds.
To use the PowerShell Analyzers:
- You must have PowerShell 7 or greater installed.
- The PSScriptAnalyzer module must be installed. The script will attempt to auto-install it, however, if this fails (e.g., on Windows PowerShell, you have to be an admin to install modules), follow the steps here.
If you have the PSScriptAnalyzer module installed, you can use Lombiq PowerShell Analyzers to have a static analysis of your PowerShell scripts.
As you may know, Lombiq's Open-Source Orchard Core Extensions is an Orchard Core CMS Visual Studio solution that contains most of Lombiq's open-source Orchard modules and themes, as well as related utilities and libraries. Please keep in mind that only those extensions included that use the latest released version of Orchard (i.e., the very cutting-edge ones depending on a nightly build are not yet here). This solution contains the Lombiq PowerShell Analyzers as well, so we will use this one for the demo.
If the Lombiq PowerShell Analyzers project is included via a submodule, edit the csproj file of your primary project(s) and add the following:
<Import Project="path\to\Lombiq.Analyzers.PowerShell\Lombiq.Analyzers.PowerShell.targets" />
This way you associate the analyzer with a .NET project, and MSBuild automatically invokes analysis before building. If the analysis passes, it creates a timestamp and won't perform the analysis again until a new script file has been added or an existing one modified.
As you can see, the Lombiq.OSOCE.Web.csproj contains this line by default.
You don't need to
<ProjectReference> Lombiq.Analyzers.PowerShell.csproj. If you include the project as a NuGet package, it will work as-is.
Just clone this repository and try to add some bad PowerShell scripts to it. Our goal here is to have some build errors when building our solution using Visual Studio. Now, it's time to write some bad code! Here we added a Violate-Analyzers.ps1 file to the Lombiq.OSOCE.Web project and built the solution. As you can see, the build failed with errors that say we have some issues in the Violate-Analyzers.ps1 file.
And that's just a quick demonstration about how to use the PowerShell Analyzers with MSBuild. You can use it with Visual Studio Code, PowerShell CI, and GitHub Actions as well. If you are interested in how to do that, check out the Readme file of the project or head to YouTube for a recording!
News from the community
Orchard Dojo Newsletter
Lombiq's Orchard Dojo Newsletter has 331 subscribers! We have started this newsletter to inform the community around Orchard of the latest news about the platform. By subscribing to this newsletter, you will get an e-mail whenever a new post is published to Orchard Dojo, including This week in Orchard of course.
Do you know of other Orchard enthusiasts who you think would like to read our weekly articles? Tell them to subscribe here!