Access Version from Xamarin Solution

2019-08-20 07:15发布

问题:

I have a multi plattform project in Xamarin. The Version Number for each .csproj Project (iOS, Android, Mac, Windows) The Version number is set from the .sln Solution options.

My problem now is to retrieve the version number from the solution file without having access to the individual Projects.

I know that there are solutions to get the Version number from like NSBundle in iOS and similar in other platforms, however I can't use those.

EDIT: This is the structure of my Solution called ETCS (git branch develop). There I have a Project ETCS which is the core funtions and the Projects ETCS.***, one for each platform.

The version number is set in the solution from where the projects get that number. See image below. What I need is a way to set a constant in the core function project.

回答1:

A solution (.sln) is a plain text file, no xml, json, etc.. so you can parse it however you want.

If you add a version via Visual Studio for Mac/MonoDevelop/Xamarin Studio you end up with a GlobalSection in the .sln of:

GlobalSection(MonoDevelopProperties) = preSolution
        description = Sushi's Most Amazing App
        version = 3.14
    EndGlobalSection
EndGlobal

Note: The solution metaproj produced via MSBuildEmitSolution=1 does not contain this information, so you can not use built-in MSBuild features to parse it.

Parse the version out (using bash here, but use your own choice to tool(s):

export slnVersion=`grep "version =" SomeSolutionFile.sln | cut -f 2 -d "=" | sed -e 's/^[ \t]*//' | tr -cd "[:print:]"`

Now you have the version (in an env. variable in this example).

If you have a .cs file (Global.Template) that looks like this:

namespace MovieMadness
{
    public static class Globals
    {
        public const string SolutionVersion = "SushiHangover";
    }
}

You can replace SushiHangover with the solution based version:

export slnVersion=`grep "version =" SomeSolutionFile.sln | cut -f 2 -d "=" | sed -e 's/^[ \t]*//' | tr -cd "[:print:]"`
sed -e 's/SushiHangover/'"$slnVersion"'/g' Global.template > Global.cs
cat Global.cs

Results in:

namespace MovieMadness
{
    public static class Globals
    {
        public const string SolutionVersion = "3.14";
    }
}