Msbuild 如何从assemblyInfo.cs读取assemblyversion?

Msbuild 如何从assemblyInfo.cs读取assemblyversion?,msbuild,installation,windows-installer,Msbuild,Installation,Windows Installer,嗨, 还有很多人已经发布了很多关于这个的问题,但是这里的情况不同 我需要从版本号中提取前三位数字,即。$(主要)。$(次要)。$(构建)。 我该怎么做???。我尝试了AssemblyInfoTask..但该任务只是为了覆盖版本号。而不是提取版本号 我需要提取前三个数字,并将它们分配给某些属性。以供进一步使用 嗯,我可以使用FileUpdatetask覆盖它们。比如: <FileUpdate Files="@(AssemblyFile)" Regex='(\d+)\.(\

嗨, 还有很多人已经发布了很多关于这个的问题,但是这里的情况不同

我需要从版本号中提取前三位数字,即。
$(主要)。$(次要)。$(构建)
。 我该怎么做???。我尝试了
AssemblyInfo
Task..但该任务只是为了覆盖版本号。而不是提取版本号

我需要提取前三个数字,并将它们分配给某些属性。以供进一步使用

嗯,我可以使用
FileUpdate
task覆盖它们。比如:

<FileUpdate 
    Files="@(AssemblyFile)" 
    Regex='(\d+)\.(\d+)\.(\d+)\.(\d+)' 
    ReplacementText='$1.$2.$3.$(Revision)'>
</FileUpdate>

现在,我如何使用它们的值(即,
$1、$2、$3
)来分配给属性


Thanx。

唯一的解决方案是编写自定义生成任务,并在代码中手动解析版本号。

您可以使用从AssemblyInfo.cs访问AssemblyVersion和AssemblyFileVersion

实际上,此任务只能用于设置版本


可能有用。

我使用MSBuild.Community.Tasks中的RegexMatch任务


您可以将匹配的输出写入一个项目组,尽管您希望将其读入3个属性,如上所述,然后会首选自定义任务。

我刚刚在google上找到了这个,可能会有帮助:

特别是:

//For AssemblyFileVersion
Assembly asm = Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
string version = fvi.FileVersion
//For AssemblyVersion
string revision = Assembly.GetExecutingAssembly().GetName().Version.Revision;

您可以从文件中读取行,使用正则表达式获取字符串,并根据需要进行更改。如果您使用的是MSBuild 4.0,则可以使用属性函数,这使您能够访问.NET API。 此示例应给出AssemblyVersion的前三个数字

<Target Name="ReadAssemblyVersion">

    <ReadLinesFromFile File="$(VersionFile)">
        <Output TaskParameter="Lines"
                ItemName="ItemsFromFile"/>
    </ReadLinesFromFile>

    <PropertyGroup>
        <Pattern>\[assembly: AssemblyVersion\(.(\d+)\.(\d+)\.(\d+)</Pattern>
        <In>@(ItemsFromFile)</In>
        <Out>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern)))</Out>
    </PropertyGroup>

    <Message Text="Output : $(Out.Remove(0, 28))"/>

</Target>

\[程序集:程序集版本\(.(\d+)\(\d+)\(\d+)
@(ItemsFromFile)
$([System.Text.RegularExpressions.Regex]::匹配($(In),$(Pattern)))

基于Alex的答案,我使用正则表达式读取AssemblyVersion(和其他信息),并在我的WiX/MSI文件名和版本字符串中使用它。希望我的答案不会太吵

这是我的.wixproj文件的顶部。感兴趣的点是第一个属性组输出名称,以及定义常量

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\..\MyApplication\Properties\AssemblyInfoCommon.cs'))</In>
        <Pattern>^\s*\[assembly: AssemblyVersion\(\D*(\d+)\.(\d+)\.(\d+)</Pattern>
        <AssemblyVersionMajor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</AssemblyVersionMajor>
        <AssemblyVersionMinor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[2].Value)</AssemblyVersionMinor>
        <AssemblyVersionBuild>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[3].Value)</AssemblyVersionBuild>
        <Pattern>^\s*\[assembly: AssemblyDescription\(\s*"([^"]+)"</Pattern>
        <AssemblyDescription>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</AssemblyDescription>
        <Pattern>^\s*\[assembly: AssemblyProduct\(\s*"([^"]+)"</Pattern>
        <AssemblyProduct>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</AssemblyProduct>
        <Pattern>^\s*\[assembly: AssemblyCompany\(\s*"([^"]+)"</Pattern>
        <AssemblyCompany>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups[1].Value)</AssemblyCompany>
    </PropertyGroup>
    <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">x86</Platform>
        <ProductVersion>3.7</ProductVersion>
        <ProjectGuid>MYGUID00-840B-4055-8251-F2B83BC5DBB9</ProjectGuid>
        <SchemaVersion>2.0</SchemaVersion>
        <OutputName>$(AssemblyProduct)-$(AssemblyVersionMajor).$(AssemblyVersionMinor).$(AssemblyVersionBuild)</OutputName>
        <OutputType>Package</OutputType>
        <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' AND '$(MSBuildExtensionsPath32)' != '' ">$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
        <WixTargetsPath Condition=" '$(WixTargetsPath)' == '' ">$(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\Wix.targets</WixTargetsPath>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
        <OutputPath>bin\$(Configuration)\</OutputPath>
        <IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
        <DefineConstants>Debug;AssemblyVersionMajor=$(AssemblyVersionMajor);AssemblyVersionMinor=$(AssemblyVersionMinor);AssemblyVersionBuild=$(AssemblyVersionBuild);AssemblyDescription=$(AssemblyDescription);AssemblyProduct=$(AssemblyProduct);AssemblyCompany=$(AssemblyCompany)</DefineConstants>
        <SuppressValidation>False</SuppressValidation>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
        <OutputPath>bin\$(Configuration)\</OutputPath>
        <IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
        <DefineConstants>AssemblyVersionMajor=$(AssemblyVersionMajor);AssemblyVersionMinor=$(AssemblyVersionMinor);AssemblyVersionBuild=$(AssemblyVersionBuild);AssemblyDescription=$(AssemblyDescription);AssemblyProduct=$(AssemblyProduct);AssemblyCompany=$(AssemblyCompany)</DefineConstants>
    </PropertyGroup>

$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\..\MyApplication\Properties\AssemblyInfoCommon.cs'))
^\s*\[程序集:程序集版本\(\D*(\D+)\(\D+)\(\D+)
$([System.Text.RegularExpressions.Regex]::匹配($(In),$(模式),System.Text.RegularExpressions.RegexOptions.Multiline)。组[1]。值)
$([System.Text.RegularExpressions.Regex]::匹配($(In),$(模式),System.Text.RegularExpressions.RegexOptions.Multiline)。组[2]。值)
$([System.Text.RegularExpressions.Regex]::匹配($(In),$(模式),System.Text.RegularExpressions.RegexOptions.Multiline)。组[3]。值)
^\s*\[assembly:AssemblyDescription\(\s*”([^“]+)”
$([System.Text.RegularExpressions.Regex]::匹配($(In),$(模式),System.Text.RegularExpressions.RegexOptions.Multiline)。组[1]。值)
^\s*\[assembly:AssemblyProduct\(\s*”([^“]+)”
$([System.Text.RegularExpressions.Regex]::匹配($(In),$(模式),System.Text.RegularExpressions.RegexOptions.Multiline)。组[1]。值)
^\s*\[assembly:AssemblyCompany\(\s*”([^“]+)”
$([System.Text.RegularExpressions.Regex]::匹配($(In),$(模式),System.Text.RegularExpressions.RegexOptions.Multiline)。组[1]。值)
调试
x86
3.7
MYGUID00-840B-4055-8251-F2B83BC5DBB9
2
$(AssemblyProduct)-$(AssemblyVersionMajor)。$(AssemblyVersionMinor)。$(AssemblyVersionBuild)
包裹
$(MSBuildExtensionsPath32)\Microsoft\WiX\v3.x\WiX.targets
$(MSBuildExtensionsPath)\Microsoft\WiX\v3.x\WiX.targets
bin\$(配置)\
obj\$(配置)\
调试;AssemblyVersionMajor=$(AssemblyVersionMajor);AssemblyVersionMinor=$(AssemblyVersionMinor);AssemblyVersionBuild=$(AssemblyVersionBuild);AssemblyDescription=$(AssemblyDescription);AssemblyProduct=$(AssemblyProduct);AssemblyCompany=$(AssemblyCompany)
假的
bin\$(配置)\
obj\$(配置)\
AssemblyVersionMajor=$(AssemblyVersionMajor);AssemblyVersionMinor=$(AssemblyVersionMinor);AssemblyVersionBuild=$(AssemblyVersionBuild);AssemblyDescription=$(AssemblyDescription);AssemblyProduct=$(AssemblyProduct);AssemblyCompany=$(AssemblyCompany)
然后在一个.wxi文件中我有:

<?define MajorVersion="$(var.AssemblyVersionMajor)" ?>
<?define MinorVersion="$(var.AssemblyVersionMinor)" ?>
<?define BuildVersion="$(var.AssemblyVersionBuild)" ?>
<?define VersionNumber="$(var.MajorVersion).$(var.MinorVersion).$(var.BuildVersion)" ?>

最后在my Product.wxs中:

<?include Definitions.wxi ?>
<Product Id="$(var.GuidProduct)" Name="$(var.AssemblyProduct) $(var.VersionNumber)" Language="!(loc.LANG)"
         Version="$(var.VersionNumber)" Manufacturer="$(var.AssemblyCompany)" UpgradeCode="$(var.GuidUpgrade)">
    <Package Id="$(var.GuidPackage)" InstallerVersion="301" Compressed="yes" InstallScope="perMachine"
             Keywords="!(loc.Keywords)" Description="$(var.AssemblyProduct)" Comments="$(var.AssemblyDescription)" />

Awesome thread,在Alex和Robbol的工作基础上,我能够定义扩展的msbuild属性,这是受semver.org(Major、Minor、Patch、PreRelease)启发的。我选择解析AssemblyInformalVersion,因为它是唯一与semver兼容的属性。以下是我的示例:

<PropertyGroup>
    <In>$([System.IO.File]::ReadAllText('$(MSBuildProjectDirectory)\Properties\AssemblyInfo.cs'))</In>
    <Pattern>\[assembly: AssemblyInformationalVersion\("(?&lt;Major&gt;\d+)\.(?&lt;Minor&gt;\d+)\.(?&lt;Patch&gt;[\d]+)(?&lt;PreReleaseInfo&gt;[0-9A-Za-z-.]+)?</Pattern>
    <AssemblyVersionMajor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["Major"].Value)</AssemblyVersionMajor>
    <AssemblyVersionMinor>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["Minor"].Value)</AssemblyVersionMinor>
    <AssemblyVersionPatch>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["Patch"].Value)</AssemblyVersionPatch>
    <AssemblyVersionPreRelease>$([System.Text.RegularExpressions.Regex]::Match($(In), $(Pattern), System.Text.RegularExpressions.RegexOptions.Multiline).Groups["PreReleaseInfo"].Value)</AssemblyVersionPreRelease>
</PropertyGroup>

将输出:Major:'0',Minor:'9',Patch:'1',PreRelease:'-beta'

如果您希望能够100%准确地处理AssemblyInfo文件,可以使用C#task+Roslyn

public类ReadAssemblyInfo:任务{
[必需]
公共字符串AssemblyInfoFilePath{get;set;}
[输出]
公共TaskItem AssemblyVersion{get;set;}
[输出]
公共任务项AssemblyInformationalVersion{get;set;}
[输出]
公共任务项AssemblyFileVersion{get;set;}
公共重写bool Execute(){
使用(var reader=newstreamreader(AssemblyInfoFilePath)){
var text=reader.ReadToEnd();
var-tree=CSharpSyntaxTree.ParseText(文本);
var root=(CompilationUnitSyntax)tree.GetRoot();
var attributeList=root.degenantNodes(),of type();
foreach(属性列表中的var p){
foreach(p.Attributes中的var属性){
var identifier=attribute.Name作为IdentifierNameSyntax;
if(标识符!=null){
var值=ParseAt
  <Target Name="AfterBuild">
    <Message Text="$(AssemblyVersionMajor)"></Message>
    <Message Text="$(AssemblyVersionMinor)"></Message>
    <Message Text="$(AssemblyVersionPatch)"></Message>
    <Message Text="$(AssemblyVersionPreRelease)"></Message>
</Target>
[assembly: AssemblyInformationalVersion("0.9.1-beta")]