首页 文章

从AssemblyInfo后编译中读取AssemblyFileVersion

提问于
浏览
12

如何在编译后读取.csproj中的AssemblyFileVersion或其组件AssemblyFileMajorVersion,AssemblyFileMinorVersion,AssemblyFileBuildNumber,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

    我设法使用自定义任务解决了这个问题 . 类库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进行自动版本控制,它将会读取更新版本 .

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


    Update 2009/09/03:

    必须进行一项额外更改才能在每次构建时进行 ApplicationVersion 更新 . 必须将 InitialTargets="AfterCompile" 添加到 <Project... . 这是solved by Chao Kuo .

  • 1

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

  • 11

    那么复制assmelby然后在副本上运行Assembly任务呢?

    Sayed Ibrahim Hashimi

相关问题