If you have a C# project and want to manage the assembly version info during automation build process such as the msbuild with Jenkins, then two options are available:
- Specify the version number in AssemblyInfo.cs to with asterisk(*) – n.n.* (for example, ‘1.0.*’)
- Or you might want to manage it with a build script such as python or powershell.
Here is a python build script to manage the version string within AssemblyInfo.cs and Package.appxmanifest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_package_version(manifest_file, target_version): | |
from xml.etree import ElementTree as et | |
et.register_namespace("", "http://schemas.microsoft.com/appx/manifest/foundation/windows10") | |
et.register_namespace("mp","http://schemas.microsoft.com/appx/2014/phone/manifest") | |
et.register_namespace("uap", "http://schemas.microsoft.com/appx/manifest/uap/windows10") | |
tree = et.parse(manifest_file) | |
e = tree.find('{http://schemas.microsoft.com/appx/manifest/foundation/windows10}Identity') | |
e.attrib['Version'] = target_version | |
tree.write(manifest_file, xml_declaration=True, encoding='utf-8') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
def set_version(infocs, target_version): | |
if not infocs or not target_version: | |
raise Exception('invalid param') | |
return | |
with open(infocs, "r+") as f: | |
assemblyinfo_cs = f.read() | |
pattern_1 = re.compile(r'AssemblyVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', re.MULTILINE) | |
pattern_2 = re.compile(r'AssemblyFileVersion\("[0-9]+(\.([0-9]+|\*)){1,3}"\)', re.MULTILINE) | |
sub1 = r'AssemblyVersion("{}")'.format(target_version) | |
sub2 = r'AssemblyFileVersion("{}")'.format(target_version) | |
phase_1 = re.sub(pattern_1, sub1, assemblyinfo_cs) | |
phase_2 = re.sub(pattern_2, sub2, phase_1) | |
f.seek(0) | |
f.write(phase_2) | |
f.truncate() |
Thanks,
Heejune
Thanks – will use this my Team City build!