PowerShellによるプロジェクトファイルのバージョン番号の一括変更

2063 ワード

一部のプロジェクトでは、ソリューションに多くのプロジェクトファイル(クラスライブラリ、アプリケーションなど)が含まれており、AssemblyInfo.csファイルのバージョン情報を一つ一つ変更するのは面倒です.
// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
 

        次のコードをps 1ファイルとして保存し、ソリューションと同じディレクトリに配置します.
#         
$SolutionPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
#     AssemblyInfo.cs  
$FileList = Get-ChildItem $SolutionPath -Recurse AssemblyInfo.cs
$oldVersion = "Version\(""\d{1,}.\d{1,}.\d{1,}.\d{1,}""\)"
$newVersion = "Version(""1.1.1.1"")"
Foreach($filename in $FileList)
{
    $filename = $filename.FullName 
    (Get-Content $filename) |  
    Foreach-Object { $_ -replace $oldVersion, $newVersion } |  
    Set-Content $filename 
}