我是模块的忠实粉丝,作为包装和分发PowerShell功能的方式。我一直在创建模块,我倾向于使用相当强大的构建和发布脚本。最近,我想要将个体高级功能释放为模块,但我觉得我现有的过程有点很多。所以我开始建立微模块。

指数

什么是微模块?

微模块的范围非常小,并且通常具有单个功能。构建一个微型模块是关于回到基础知识并保持尽可能简单的一切。

有很多良好的建议,如何构建模块。这条指导是有助于您,因为您的模块大小增长。如果我们知道我们的模块不会成长,我们不会添加任何功能,我们也可以采取不同的方法,尽管它可能无法完全符合社区最佳实践。

单一功能

The whole idea behind the micro module is that there is only one function. Because there is one function, we place it directly into the .psm1 file in source. This is important because when you have multiple functions, you should have them in their own files.

I love having multiple files for my dev work but that requires that I add a build script to combine them into the .psm1 for publishing. By having the single function in the .psm1 file already, I can skip that build step. I can just publish the module as it sits in source control.

仔细看看

Let’s take a look at the structure of my Watch-Command module. Watch-Command is a micro module that I published last week. I see 7 files in this project.

Watch-Command
│   azure-pipelines.yml
│   publish.ps1
│   readme.md
│
├───.vscode
│       settings.json
│
└───Watch-Command
        LICENSE
        Watch-Command.psd1
        Watch-Command.psm1

模块文件

The Watch-Command folder is the actual module with 3 files. The .psm1 file has 90 lines of PowerShell for the 1 function (named the same as my module). The .psd1 is still important and is required to publish to the PSGallery. I also have a LICENSE file in this folder so it gets delivered with the module.

连续交货

即使它是一个简单的模块,我仍然利用连续的递送管道发布模块。

publish.ps1.

At the heart of the pipeline is the publish.ps1. that will publish my module to the PSGallery. Here is a look at the publish.ps1. file in the project.

$publishModuleSplat = @{
    Path        = ".\Watch-Command"
    NuGetApiKey = $ENV: 尼八塔奇 
    Verbose     = $true
    Force       = $true
    Repository  = "PSGallery"
    ErrorAction = 'Stop'
}

"Files in module output:"
Get-ChildItem $Destination -Recurse -File |
    Select-Object -Expand FullName

"Publishing [$Destination] to [$PSRepository]"

Publish-Module @ 发布 ModuleSplat

It is basically a call to Publish-Module with a little verbosity. The important detail here is that the 尼八塔奇 is pulled from an environment variable.

Azure-pipelines.yml.

I am using azure devops pipelines to manage the deployment. I define the whole build in the Azure-pipelines.yml. file. This allows me to just point the pipeline at my source and the build will just work.

trigger:
  batch: true
  branches:
    include:
      - master

pool:
  vmImage: 'windows-2019'

steps:
- script: pwsh -Command {Install-Module PowerShellGet -Force}
  displayName: 'Update powershellget'
- script: pwsh -File publish.ps1
  displayName: 'Build and Publish  模块 '
  env:
     尼八塔奇 : $(nugetapikey)

The first thing I do is update 电源外壳 Get in its own build step. This way when the next PowerShell step executes, I know that it is running with a current verison of 电源外壳 Get.

For the second step, I call the publish.ps1. script to publish the module. I also need to map the environment 尼八塔奇 to the build step or it will be null in my script for the publish.

设置管道

I set up a single DevOps Pipeline for all my micro modules, but each one will get a unique build in that pipeline. When you create the build, you will have to point it at your source repository. Then specify the Azure-pipelines.yml. for the build definition.

尼八塔奇

我生成一个新的psgallery api键每个项目,并将其添加到构建中作为环境可行。确保单击小锁以保护该值。

发布

此时,我能够合并到Master,模块将发布。

结束评论

If you get a chance, you should check out my Watch-Command module. It lets you specify a command to be ran every 15 seconds. It will then clear the screen and show you the results of that command (over and over until you kill it).

这是它显示由CPU时间排序的本地进程列表的示例。

Watch-Command {Get-Process | Sort cpu -desc}

This micro module pattern was fast to set up. I was able to write my Watch-Command module and have the pipeline publishing it the same day. This allowed me to focus on my ideas and quickly get it out the door.