Skip to content

Commit 353abb4

Browse files
committed
完善应用版本号的获取逻辑。 📨
支持在应用程序根目录的 .version 版本文件来表示应用程序的版本号。
1 parent 154ff77 commit 353abb4

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

Zongsoft.Core/src/Services/ApplicationContext.cs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected ApplicationContext(IServiceProvider services)
105105

106106
public Version Version
107107
{
108-
get => _version ??= Assembly.GetEntryAssembly()?.GetName().Version ?? Assembly.GetExecutingAssembly().GetName().Version;
108+
get => _version ??= this.GetVersion();
109109
set => _version = value;
110110
}
111111

@@ -190,9 +190,46 @@ public string EnsureDirectory(string relativePath)
190190
#endregion
191191

192192
#region 虚拟方法
193-
protected virtual IApplicationEnvironment CreateEnvironment(IHostEnvironment environment, IDictionary<object, object> properties)
193+
protected virtual IApplicationEnvironment CreateEnvironment(IHostEnvironment environment, IDictionary<object, object> properties) => new ApplicationEnvironment(environment, properties);
194+
protected virtual Version GetVersion()
194195
{
195-
return new ApplicationEnvironment(environment, properties);
196+
var version = GetVersionFromFile(Path.Combine(this.ApplicationPath, ".version"));
197+
if(version != null)
198+
return version;
199+
200+
var assembly = Assembly.GetEntryAssembly();
201+
if(assembly != null)
202+
return assembly.GetName().Version;
203+
204+
assembly = Assembly.GetExecutingAssembly();
205+
if(assembly != null)
206+
return assembly.GetName().Version;
207+
208+
return null;
209+
210+
static Version GetVersionFromFile(string filePath)
211+
{
212+
if(filePath == null || !File.Exists(filePath))
213+
return null;
214+
215+
string text;
216+
using var reader = File.OpenText(filePath);
217+
218+
while((text = reader.ReadLine()) != null)
219+
{
220+
if(string.IsNullOrEmpty(text))
221+
continue;
222+
223+
var index = text.IndexOf('@');
224+
225+
if(index < 0)
226+
return Version.TryParse(text, out var version) ? version : null;
227+
else
228+
return Version.TryParse(text.AsSpan()[(index + 1)..], out var version) ? version : null;
229+
}
230+
231+
return null;
232+
}
196233
}
197234
#endregion
198235

0 commit comments

Comments
 (0)