我刚刚开始新的模块 psgraphplus. 如果你认识我,这就是我看看我是如何构建模块的时候。我用完了很多小虫 完整模块膏药模板 我花了一点时间在我身上工作 模块构建脚本 。我可以谈谈另一个帖子中的构建过程,但为了这个对话,它只是我运行的脚本,它在模块上执行多个操作。它运行测试,颠簸版本并发布到 Psgallery. .
今天,我将通过我的模块中的更改来浏览我如何碰到该版本。
指数
什么是语义版号码?
When it comes down to it, a version number is just a few numbers separated by periods (ex 2.13.87
). Semantic versioning is one popular way to manage the version number. If we call the parts of the version Major
, Minor
, Patch
in that order;
Major
在发生变化时更新Minor
在有功能添加时更新(没有破坏更改)Path
修复或更改某些内容时会更新
这是非常简化的,但会涵盖我们今天所需要的。退房 semver.org. 如果您想要更多信息。
更新模块清单的版本
我杠杆了 buildhelper. community module to help me update the version on my manifest. It has a lot of great little tools in there. We are going to work with Step-ModuleVersion
today.
如果打开模块清单,您将看到一个看起来像这样的版本属性:
# Version number of this module.
ModuleVersion = '2.13.87'
If we want to bump it from 2.13.87
to 2.13.88
, we can use Step-ModuleVersion
to do so.
$ManifestPath = '.\MyModule.psd1'
Step-ModuleVersion -Path $ManifestPath -By Patch
该命令每次都可以轻松击中版本,但我想要语义版本化。
检测模块的变化
我决定监视函数名称及其参数以检测破坏变化。我的理论是,如果我删除或重命名函数或参数,我应该考虑破坏的变化并更新主要版本号。我可以使用相同的逻辑来检测作为要素添加的功能或参数的添加。
指纹
首先,我们需要建立我们的指纹。这是我的计划:
- 导入模块
- 获取功能
- 枚举每个参数
- 创建像此“功能:参数”这样的指纹
这是代码那样。
Import-Module ".\$ModuleName"
$commandList = Get-Command -Module $ModuleName
Remove-Module $ModuleName
Write-Output 'Calculating fingerprint'
$fingerprint = foreach ( $command in $commandList )
{
foreach ( $parameter in $command.parameters.keys )
{
'{0}:{1}' -f $command.name, $command.parameters[$parameter].Name
$command.parameters[$parameter].aliases |
Foreach-Object { '{0}:{1}' -f $command.name, $_}
}
}
然后,我们将保存该指纹到项目中的文件。
检查指纹
每个构建我们都将加载现有的指纹,将其与当前的指纹进行比较并更新。比较两条指纹之间的每一行将告诉我们是否添加或删除了某些东西。
if ( Test-Path .\fingerprint )
{
$oldFingerprint = Get-Content .\fingerprint
}
$bumpVersionType = 'Patch'
'Detecting new features'
$fingerprint | Where {$_ -notin $oldFingerprint } |
ForEach-Object {$bumpVersionType = 'Minor'; " $_"}
'Detecting breaking changes'
$oldFingerprint | Where {$_ -notin $fingerprint } |
ForEach-Object {$bumpVersionType = 'Major'; " $_"}
Set-Content -Path .\fingerprint -Value $fingerprint
In this example, if there were no changes then we would bump the Patch
version. If I have something in the new fingerprint that is not in the old one, then we bump the Minor
version. Finally, we move onto detecting when a fingerprint that was there before but was removed in order to bump the Major
version.
Now that we know what to bump, we return to Step-ModuleVersion
to take care of it.
$ManifestPath = '.\MyModule.psd1'
Step-ModuleVersion -Path $ManifestPath -By $bumpVersionType
最终结果
与此作为模块构建脚本的一部分使我能够自动指示版本是否有破坏性。这只能这样做。如果存在其他更改,我仍然可以在需要时用手更新我的版本。
结束细节
此方法的一个问题是添加到项目中的额外文件。这意味着如果使用构建系统运行此操作,则必须在构建之后检查该文件。我目前在本地运行,因此指纹成为我登记的一部分。
I had an earlier version of this that would use the FunctionsToExport
in the module manifest to detect changes. It only handled new and removed functions.
在我去之前,我会看到它在这个项目上的工作原理如何添加到我的所有其他项目。如果您想在我的构建脚本中看到此内容,您可以在此处查看: module.build.ps1. .