Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取iOS、Android和OSX上的Xamarin程序集构建日期_Xamarin_Build_Linker_Timestamp - Fatal编程技术网

获取iOS、Android和OSX上的Xamarin程序集构建日期

获取iOS、Android和OSX上的Xamarin程序集构建日期,xamarin,build,linker,timestamp,Xamarin,Build,Linker,Timestamp,在我的代码中,我需要一个Xamarin程序集构建日期。在Windows上我可以使用。但是,在iOS上,这不起作用。我想它在OSX上也不起作用,因为可移植可执行文件头是特定于Windows的 还有一个选项可以嵌入带有日期的资源,但是我希望避免在这个特定项目中使用资源 有没有办法找到在iOS、Android和OS X上运行的Xamarin程序集生成日期?一种方法是使用MSBuild任务将生成时间替换为应用程序上属性返回的字符串。我们在一个包含Xamarin.Forms、Xamarin.Android

在我的代码中,我需要一个Xamarin程序集构建日期。在Windows上我可以使用。但是,在iOS上,这不起作用。我想它在OSX上也不起作用,因为可移植可执行文件头是特定于Windows的

还有一个选项可以嵌入带有日期的资源,但是我希望避免在这个特定项目中使用资源


有没有办法找到在iOS、Android和OS X上运行的Xamarin程序集生成日期?

一种方法是使用MSBuild任务将生成时间替换为应用程序上属性返回的字符串。我们在一个包含Xamarin.Forms、Xamarin.Android和Xamarin.iOS项目的应用程序中成功地使用了这种方法

如果使用msbuild,则这可以是msbuild内联任务,而在Mac上使用xbuild,则需要是为Mono编译的msbuild自定义任务

编辑:

通过将所有逻辑移到构建任务中,并使用
Regex
而不是简单的字符串替换来简化,这样每个构建都可以修改文件,而无需“重置”

MSBuild内联任务定义(保存在本例的Xamarin.Forms项目本地的SetBuildDate.targets文件中):



编辑:

添加了MSBuild Exec步骤以删除只读属性。一定要爱TFS

调用target BeforeBuild中Xamarin.Forms csproj文件中的MSBuild任务(内联或已编译,内联方法对xbuild进行注释):


FilePath
属性设置为Xamarin.Forms项目中的
BuildMetadata.cs
文件,该文件包含一个简单类,该类具有字符串属性
BuildDate
,生成时间将被替换为:

公共类构建元数据
{
公共静态字符串BuildDate=>“这可以是任意字符串”;
}
将此文件
BuildMetadata.cs
添加到项目中。它将被每个构建修改,但是以允许重复构建(重复替换)的方式修改,因此您可以根据需要在源代码管理中包含或省略它

添加:

以下是一个自定义MSBuild任务,用于在Mac上使用
xbuild
进行构建时替换MSBuild内联任务:

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

namespace Some.Framework.BuildExtensions
{
    public class BuildDateTask : Task
    {
        #region Methods

        /// <summary>
        /// Called automatically when the task is run.
        /// </summary>
        /// <returns><c>true</c>for task success, <c>false</c> otherwise.</returns>
        public override bool Execute()
        {
            const string pattern = @"BuildDate => ""([^""]*)""";
            var now = DateTime.UtcNow;
            var buildDate = now.ToString("F");
            var replacement = $"BuildDate => \"{buildDate}\"";
            var content = File.ReadAllText(FilePath);
            var rgx = new Regex(pattern);
            content = rgx.Replace(content, replacement);
            File.WriteAllText(FilePath, content);
            File.SetLastWriteTimeUtc(FilePath, now);
            return true;
        }

        #endregion Methods

        #region Properties

        [Required]
        public string FilePath { get; set; }

        #endregion Properties
    }
}
使用系统;
使用System.IO;
使用System.Text.RegularExpressions;
使用Microsoft.Build.Framework;
使用Microsoft.Build.Utilities;
命名空间Some.Framework.BuildExtensions
{
公共类BuildDateTask:任务
{
#区域方法
/// 
///任务运行时自动调用。
/// 
///True表示任务成功,否则为false。
公共重写bool Execute()
{
常量字符串模式=@“BuildDate=>”([^“]*)”;
var now=DateTime.UtcNow;
var buildDate=now.ToString(“F”);
变量替换=$“BuildDate=>\”{BuildDate}\”;
var content=File.ReadAllText(文件路径);
var rgx=新正则表达式(模式);
content=rgx.Replace(content,replacement);
writealText(文件路径、内容);
SetLastWriteTimeUtc(文件路径,现在);
返回true;
}
#端域法
#区域属性
[必需]
公共字符串文件路径{get;set;}
#端域属性
}
}
通过xbuild生成此自定义任务以供发布,然后将输出自定义任务dll复制到要为其设置生成日期的项目的项目目录