Skip to content

Commit

Permalink
Include version in macOS app bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
cwensley committed Mar 14, 2023
1 parent 8b126ab commit aec584f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 46 deletions.
13 changes: 11 additions & 2 deletions Source/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@

<PropertyGroup>
<Company>Picoe Software Solutions</Company>
<Copyright>(c) 2006-2022 by Curtis Wensley aka Eto</Copyright>
<Version Condition="$(Version) == ''">3.3.11-dev</Version>
<Copyright>(c) 2006-2023 by Curtis Wensley aka Eto</Copyright>

<DevVersion>3.3.12</DevVersion>

<!-- set version from tag -->
<Version Condition="$(Version) == '' AND $(BuildBranch.StartsWith('refs/tags/'))">$(BuildBranch.Substring(10))</Version>
<!-- set version for CI build -->
<Version Condition="$(Version) == '' AND $(BuildVersion) != ''">$(DevVersion)-ci-$([System.DateTime]::Now.ToString('yyyyMMdd')).$(BuildVersion)</Version>
<!-- set version for development -->
<Version Condition="$(Version) == ''">$(DevVersion)-dev</Version>


<BasePath>$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..\'))</BasePath>
<ArtifactsDir>$(BasePath)artifacts\</ArtifactsDir>
Expand Down
46 changes: 2 additions & 44 deletions Source/Directory.Build.targets
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,49 +1,7 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- <Import Project="EmbedReferences.targets" /> -->
<Import Project="UpdatePList.targets" />

<UsingTask TaskName="_ParseVersion" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<Version ParameterType="System.String" Required="true" />
<AssemblyVersion ParameterType="System.String" Output="true" />
<AssemblyFileVersion ParameterType="System.String" Output="true" />
<AssemblyInformationalVersion ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System"/>
<Using Namespace="System.IO"/>
<Using Namespace="System.Text.RegularExpressions"/>
<Code Type="Fragment" Language="cs"><![CDATA[
var regex = new Regex(@"(?<ver>(?<maj>\d+)[.](?<min>\d+)([.](?<build>\d+)([.](?<rev>\d+))?)?)([-](?<info>.+))?");
if (string.IsNullOrEmpty(AssemblyVersion)) AssemblyVersion = regex.Replace(Version, "${ver}");
if (string.IsNullOrEmpty(AssemblyFileVersion)) AssemblyFileVersion = AssemblyFileVersion ?? regex.Replace(Version, "${ver}");
if (string.IsNullOrEmpty(AssemblyInformationalVersion)) AssemblyInformationalVersion = Version;
]]></Code>
</Task>
</UsingTask>

<Target Name="_ParseAssemblyVersion" BeforeTargets="BeforeBuild">

<_ParseVersion Version="$(Version)">
<Output TaskParameter="AssemblyVersion" PropertyName="AssemblyVersion"/>
<Output TaskParameter="AssemblyFileVersion" PropertyName="AssemblyFileVersion"/>
<Output TaskParameter="AssemblyInformationalVersion" PropertyName="AssemblyInformationalVersion"/>
</_ParseVersion>
</Target>
<!-- <Import Project="EmbedReferences.targets" /> -->

<Target Name="_WriteVersion" BeforeTargets="CoreCompile" Condition="$(GenerateAssemblyVersion) == 'True'"
Inputs="$(MSBuildAllProjects);@(Compile);@(_CoreCompileResourceInputs);$(ApplicationIcon);$(AssemblyOriginatorKeyFile);@(ReferencePathWithRefAssemblies);@(CompiledLicenseFile);@(LinkResource);@(EmbeddedDocumentation);$(Win32Resource);$(Win32Manifest);@(CustomAdditionalCompileInputs);$(ResolvedCodeAnalysisRuleSet);@(AdditionalFiles);@(EmbeddedFiles)"
Outputs="$(IntermediateOutputPath)VersionInfo.cs">

<ItemGroup>
<VersionInfo Include="[assembly: System.Reflection.AssemblyVersion(&quot;$(AssemblyVersion)&quot;)]" />
<VersionInfo Include="[assembly: System.Reflection.AssemblyFileVersion(&quot;$(AssemblyFileVersion)&quot;)]" />
<VersionInfo Include="[assembly: System.Reflection.AssemblyInformationalVersion(&quot;$(AssemblyInformationalVersion)&quot;)]" />
</ItemGroup>
<MakeDir Directories="$(IntermediateOutputPath)" />
<WriteLinesToFile File="$(IntermediateOutputPath)VersionInfo.cs" Overwrite="True" Lines="@(VersionInfo)" />
</Target>

</Project>
9 changes: 9 additions & 0 deletions Source/PabloDraw/PabloDraw.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,14 @@
<!-- Should go into Eto 2.7.2 -->
<MakeDir Directories="$(DmgOutputPath)" />
</Target>

<Target Name="_UpdateMacVersion" Condition="$(BuildTarget) == 'Mac'" BeforeTargets="MacFinishBundle">
<ItemGroup>
<PListEntry Include="CFBundleShortVersionString" Value="$(AssemblyVersion)" />
<PListEntry Include="CFBundleVersion" Value="$(AssemblyVersion)" />
</ItemGroup>

<UpdatePListEntries TargetFile="$(OutputPListFile)" Entries="@(PListEntry)" />
</Target>

</Project>
108 changes: 108 additions & 0 deletions Source/UpdatePList.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">


<UsingTask TaskName="UpdatePListEntries" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<TargetFile ParameterType="System.String" Required="true" />
<Entries ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<IconFile ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="netstandard" />
<Code Type="Class" Language="cs"><![CDATA[
using System;
using System.IO;
using System.Xml;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
public class UpdatePListEntries : Task
{
[Required]
public string TargetFile { get; set; }
[Required]
public Microsoft.Build.Framework.ITaskItem[] Entries { get; set; }
[Output]
public string IconFile { get; set; }
XmlDocument xml;
XmlNode dict;
class NullSubsetXmlTextWriter : XmlTextWriter
{
XmlWriterSettings _settings;
public NullSubsetXmlTextWriter(string inputFileName, Encoding encoding)
: base(inputFileName, encoding)
{
Formatting = Formatting.Indented;
IndentChar = ' ';
Indentation = 2;
_settings = new XmlWriterSettings();
_settings.Encoding = Encoding.UTF8;
_settings.Indent = true;
_settings.IndentChars = " ";
_settings.NewLineChars = "\n";
_settings.NewLineHandling = NewLineHandling.Entitize;
}
public override XmlWriterSettings Settings { get { return _settings; } }
public override void WriteDocType(string name, string pubid, string sysid, string subset)
{
// fix issue writing doctype
if (subset == string.Empty)
subset = null;
base.WriteDocType(name, pubid, sysid, subset);
}
}
string GetStringProperty(string name)
{
var location = dict.SelectSingleNode("key[.='" + name + "']/following-sibling::string[1]");
return location.InnerText;
}
void AddStringProperty(string name, string value, bool force = false)
{
XmlNode node;
var exists = dict.SelectSingleNode("key[text()='" + name + "']") != null;
if (exists && !force)
return;
dict.AppendChild(node = xml.CreateNode(XmlNodeType.Element, "key", null));
node.InnerText = name;
dict.AppendChild(node = xml.CreateNode(XmlNodeType.Element, "string", null));
node.InnerText = value;
}
public override bool Execute()
{
xml = new XmlDocument();
xml.Load(TargetFile);
dict = xml.SelectSingleNode("plist/dict") as XmlElement;
foreach (var entry in Entries)
{
if (!bool.TryParse(entry.GetMetadata("Force"), out var force))
force = false;
AddStringProperty(entry.ItemSpec, entry.GetMetadata("Value"), force);
}
IconFile = GetStringProperty("CFBundleIconFile");
using (var sw = new NullSubsetXmlTextWriter(TargetFile, Encoding.UTF8))
xml.Save(sw);
return true;
}
}
]]></Code>
</Task>
</UsingTask>


</Project>

0 comments on commit aec584f

Please sign in to comment.