Skip to content

Instantly share code, notes, and snippets.

@chiguniiita
Last active Jan 31, 2020
Embed
What would you like to do?
TFVCのラベル名をファイルのバージョンに埋め込んで、Nugetリリースまでする(Build)
pool:
name: Azure Pipelines
steps:
- bash: 'env | sort'
displayName: 'Bash Script'
- powershell: |
$Src = @'
using System;
using System.IO;
using System.Text.RegularExpressions;
public static class ReleaseHelper
{
public static string GetNugetVersion(string buildSourceVersion)
{
var temp = buildSourceVersion.Substring(1).Split('.');
var nugetVersion = string.Join(".", temp[0], temp[1], temp[3]);
return nugetVersion;
}
public static string GetVersionEmbeddedCsProj(string csproj, string build_sourceversion)
{
var ret = csproj;
var jst = TimeZoneInfo.FindSystemTimeZoneById("Tokyo Standard Time");
var now_jst = TimeZoneInfo.ConvertTime(DateTime.Now, jst);
var today_jst = TimeZoneInfo.ConvertTime(DateTime.Today, jst);
var v1 = 1;
var v2 = 0;
var v3 = (today_jst - new DateTime(2000, 1, 1)).Days;
var v4 = (int)(now_jst - today_jst).TotalSeconds / 2;
var patchVersion = build_sourceversion.Substring(1);
var fileVersion = string.Join(".", v1, v2, v3, v4);
var productVersion = "1.0.0+" + patchVersion;
var regPropertyGroup = Regex.Match(csproj, "<PropertyGroup>(?<PropertyGroupValue>.*)</PropertyGroup>", RegexOptions.Singleline);
var newPropertyGroup = regPropertyGroup.Value;
Func<string, string, string, string> vFunc = (string content, string tagName, string value) =>
{
var newContent = content;
var reg = Regex.Match(newContent, "<" + tagName + ">.*</" + tagName + ">");
if (reg.Success)
{
newContent = newContent.Replace(reg.Value, "<" + tagName + ">" + value + "</" + tagName + ">");
}
else
{
var pgv = regPropertyGroup.Groups["PropertyGroupValue"].Value;
var newpgv = pgv + " " + "<" + tagName + ">" + value + "</" + tagName + ">" + Environment.NewLine + " ";
newContent = newContent.Replace(pgv, newpgv);
}
return newContent;
};
newPropertyGroup = vFunc(newPropertyGroup, "Version", productVersion);
newPropertyGroup = vFunc(newPropertyGroup, "FileVersion", fileVersion);
ret = ret.Replace(regPropertyGroup.Value, newPropertyGroup);
return ret;
}
}
'@
Add-Type -TypeDefinition $Src -Language CSharp
$NugetVersion = [ReleaseHelper]::GetNugetVersion($env:BUILD_SOURCEVERSION)
$csprojs = Get-ChildItem -Recurse -Force -Name -Include *.csproj -Path $PWD
foreach($CsProjPath in $csprojs)
{
$CsProjContent = Get-Content $CsProjPath -raw
$CsProjContent = [ReleaseHelper]::GetVersionEmbeddedCsProj($CsProjContent,$env:BUILD_SOURCEVERSION)
echo "##vso[task.setvariable variable=PackageVersion]$NugetVersion"
$CsProjContent | Out-File $CsProjPath
}
displayName: 'PowerShell Script'
- task: DotNetCoreCLI@2
displayName: 'dotnet build'
inputs:
projects: '**/*.csproj'
- task: DotNetCoreCLI@2
displayName: 'dotnet pack'
inputs:
command: pack
- task: PublishBuildArtifacts@1
displayName: 'Publish Artifact: drop'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment