问题描述
我在 Xamarin
中有一个多平台项目.每个 .csproj
项目(iOS、Android、Mac、Windows)的版本号从 .sln
解决方案选项中设置版本号.
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.
我知道有一些解决方案可以从 iOS 中的 NSBundle
和其他平台中的类似中获取版本号,但是我不能使用这些.
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.
这是我的解决方案的结构,称为ETCS
(git branch develop).在那里,我有一个项目 ETCS
,它是核心功能和项目 ETCS.***
,每个平台一个.
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.
版本号是在解决方案中设置的,项目从中获取该编号.见下图.我需要的是一种在核心功能项目中设置constant
的方法.
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.
推荐答案
一个解决方案 (.sln
) 是一个纯文本文件,没有 xml、json 等,所以你可以解析它但是你想要.
A solution (.sln
) is a plain text file, no xml, json, etc.. so you can parse it however you want.
如果你通过 Visual Studio for Mac/MonoDevelop/Xamarin Studio 添加一个版本,你最终会在 .sln
中得到一个 GlobalSection
:
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
注意:通过 MSBuildEmitSolution=1
生成的解决方案 metaproj
不包含此信息,因此您无法使用内置的 MSBuild 功能对其进行解析.
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.
解析版本(此处使用 bash,但使用您自己选择的工具:
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).
如果您有一个看起来像这样的 .cs
文件 (Global.Template
):
If you have a .cs
file (Global.Template
) that looks like this:
namespace MovieMadness
{
public static class Globals
{
public const string SolutionVersion = "SushiHangover";
}
}
您可以将 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
结果:
namespace MovieMadness
{
public static class Globals
{
public const string SolutionVersion = "3.14";
}
}
这篇关于从 Xamarin 解决方案访问版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!