MSBuildタスクを使用して、アプリケーションのプロパティによって返される文字列にビルド時間を代入する方法があります。このアプローチは、Xamarin.Forms、Xamarin.Android、およびXamarin.iOSプロジェクトを持つアプリでうまく使用しています。
msbuildを使用している場合はMSBuildインラインタスク、xbuildを使用しているMacではMono用にコンパイルされたMSBuildカスタムタスクが必要です。
EDIT:ビルドタスクにロジックのすべてを移動し、代わりに単純な文字列のRegex
を使用することによって簡素化
ファイルが「リセット」することなく、各ビルドによって変更することができるように交換してください。
MSBuildのインラインタスク定義(SetBuildDate.targetsに保存されているが、この例ではXamarin.Formsプロジェクトへのローカルファイル):
<Project xmlns='http://schemas.microsoft.com/developer/msbuild/2003' ToolsVersion="12.0">
<UsingTask TaskName="SetBuildDate" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup>
<FilePath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs"><![CDATA[
DateTime now = DateTime.UtcNow;
string buildDate = now.ToString("F");
string replacement = string.Format("BuildDate => \"{0}\"", buildDate);
string pattern = @"BuildDate => ""([^""]*)""";
string content = File.ReadAllText(FilePath);
System.Text.RegularExpressions.Regex rgx = new System.Text.RegularExpressions.Regex(pattern);
content = rgx.Replace(content, replacement);
File.WriteAllText(FilePath, content);
File.SetLastWriteTimeUtc(FilePath, now);
]]></Code>
</Task>
</UsingTask>
</Project>
EDIT:
追加のMSBuild Execのステップへreadonly属性を削除します。 TFSが大好きです。
ターゲットBeforeBuildにファイルcsproj Xamarin.FormsにMSBuildのタスク(インラインまたはコンパイルされた、インラインアプローチはxbuildためにコメントされている)を呼び出す:
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets. -->
<!--<Import Project="SetBuildDate.targets" />-->
<UsingTask AssemblyFile="$(MSBuildProjectDirectory)\BI.Framework.BuildExtensions.dll" TaskName="Some.Framework.BuildExtensions.BuildDateTask" />
<Target Name="BeforeBuild">
<Exec Command="attrib $(MSBuildProjectDirectory)\BuildMetadata.cs -r" />
<!--<SetBuildDate FilePath="$(MSBuildProjectDirectory)\BuildMetadata.cs" />-->
<BuildDateTask FilePath="$(MSBuildProjectDirectory)\BuildMetadata.cs" />
</Target>
FilePath
プロパティはXamarinでBuildMetadata.cs
ファイルに設定されていますビルド時間が置換されるに文字列プロパティBuildDate
、と単純なクラスが含まれています.Formsプロジェクト:
public class BuildMetadata
{
public static string BuildDate => "This can be any arbitrary string";
}
プロジェクトにこのファイル
BuildMetadata.cs
を追加します。これはすべてのビルドによって変更されますが、繰り返しビルド(繰り返しの置換)が可能な方法で行われるため、必要に応じてソースコントロールに含めるか省略することができます。
を追加しました:ここ
は、Mac上でxbuild
を構築するときのためのMSBuildインラインタスクを置き換えるために、カスタムの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
}
}
をコピーし、xbuild経由リリースのためにこのカスタムタスクを構築出力カスタムタスクdllは、ビルド日付を設定するプロジェクトのプロジェクトディレクトリに移動します。