从AssemblyInfo后编译中读取AssemblyFileVersion

时间:2009-08-09 19:13:23

标签: .net msbuild assemblies versioning

如何读取 AssemblyFileVersion 或其组件 AssemblyFileMajorVersion AssemblyFileMinorVersion AssemblyFileBuildNumber 在编译之后的.csproj中的AssemblyFileRevision

我尝试过以下方法,从构建的程序集中提取信息:

<Target Name="AfterCompile">
    <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
         <Output
             TaskParameter="Assemblies"
             ItemName="MyAssemblyIdentities"/>
    </GetAssemblyIdentity>
    <Message Text="AssemblyVersion = %(MyAssemblyIdentities.Version)" />
</Target>

但是它检索 AssemblyVersion 而不是 AssemblyFileVersion 。对于后者,似乎没有记录的元数据条目。我也尝试过:

<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\MSBuild.ExtensionPack.tasks" />
<Target Name="AfterCompile">
    <MSBuild.ExtensionPack.Framework.Assembly TaskAction="GetInfo" NetAssembly="$(TargetPath)">
        <Output TaskParameter="OutputItems" ItemName="Info" />
    </MSBuild.ExtensionPack.Framework.Assembly>
    <Message Text="AssemblyFileVersion = %(Info.FileVersion)" />
</Target>

不幸的是,虽然这会检索正确的值,但它也会锁定程序集,直到VS2008关闭。

坦率地说,我不想要什么,因为我宁愿直接从AssemblyInfo.cs读取信息。但是,我无法弄清楚如何做到这一点。我假设MSBuild Extensions中的AssemblyInfo是一种方式,但它似乎专注于写入AssemblyInfo而不是从中检索值。

我怎样才能最好地完成这项工作?

3 个答案:

答案 0 :(得分:11)

我设法使用自定义任务解决了这个问题。类库DLL也是如此(为简洁起见,调整/删除了一些代码):

using System;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;

namespace GetAssemblyFileVersion
{
    public class GetAssemblyFileVersion : ITask
    {
        [Required]
        public string strFilePathAssemblyInfo { get; set; }
        [Output]
        public string strAssemblyFileVersion { get; set; }
        public bool Execute()
        {
            StreamReader streamreaderAssemblyInfo = null;
            Match matchVersion;
            Group groupVersion;
            string strLine;
            strAssemblyFileVersion = String.Empty;
            try
            {
                streamreaderAssemblyInfo = new StreamReader(strFilePathAssemblyInfo);
                while ((strLine = streamreaderAssemblyInfo.ReadLine()) != null)
                {
                    matchVersion = Regex.Match(strLine, @"(?:AssemblyFileVersion\("")(?<ver>(\d*)\.(\d*)(\.(\d*)(\.(\d*))?)?)(?:""\))", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline | RegexOptions.ExplicitCapture);
                    if (matchVersion.Success)
                    {
                        groupVersion = matchVersion.Groups["ver"];
                        if ((groupVersion.Success) && (!String.IsNullOrEmpty(groupVersion.Value)))
                        {
                            strAssemblyFileVersion = groupVersion.Value;
                            break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                BuildMessageEventArgs args = new BuildMessageEventArgs(e.Message, string.Empty, "GetAssemblyFileVersion", MessageImportance.High);
                BuildEngine.LogMessageEvent(args);
            }
            finally { if (streamreaderAssemblyInfo != null) streamreaderAssemblyInfo.Close(); } 
            return (true);
        }
        public IBuildEngine BuildEngine { get; set; }
        public ITaskHost HostObject { get; set; }
    }
}

在项目文件中:

<UsingTask AssemblyFile="GetAssemblyFileVersion.dll" TaskName="GetAssemblyFileVersion.GetAssemblyFileVersion" />
<Target Name="AfterCompile">
    <GetAssemblyFileVersion strFilePathAssemblyInfo="$(SolutionDir)\AssemblyInfo.cs">
        <Output TaskParameter="strAssemblyFileVersion" PropertyName="strAssemblyFileVersion" />
    </GetAssemblyFileVersion>
    <Message Text="AssemblyFileVersion = $(strAssemblyFileVersion)" />
</Target>

我已对此进行了测试,如果您使用 MSBuild.ExtensionPack.VersionNumber.targets 进行自动版本控制,它将会读取更新版本。

显然,这可以很容易地扩展,以便将正则表达式从项目文件传递到更通用的自定义任务,以便在任何文件中获得任何匹配。

<小时/> 更新2009/09/03:

必须进行一项额外更改才能对每个版本进行ApplicationVersion更新。必须将InitialTargets="AfterCompile"添加到<Project...。这是solved by Chao Kuo

答案 1 :(得分:1)

如果使任务继承自AppDomainIsolatedTask,则不需要从流加载程序集。您只需使用AppDomain.LoadFrom(文件)。

答案 2 :(得分:0)

如何复制assmelby然后在副本上运行Assembly任务?

Sayed Ibrahim Hashimi