Skip to content

Commit fcf3771

Browse files
committed
diff_report.mdに実行コンピュータ名を出力するように改修
1 parent 702a3e1 commit fcf3771

4 files changed

Lines changed: 88 additions & 1 deletion

File tree

Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,11 @@ class Program
149149
/// </summary>
150150
private static string _thisAppVersion;
151151

152+
/// <summary>
153+
/// このアプリを実行したコンピュータ名
154+
/// </summary>
155+
private static string _computerName;
156+
152157
/// <summary>
153158
/// 処理時間(文字列)
154159
/// </summary>
@@ -172,6 +177,8 @@ static async Task Main(string[] args)
172177
_thisAppVersion = Utility.GetAppVersion(typeof(Program));
173178
LoggerService.LogMessage(LoggerService.LogLevel.Info, string.Format(APPLICATION_VERSION, _thisAppVersion), shouldOutputMessageToConsole: true);
174179

180+
_computerName = Utility.GetComputerName();
181+
175182
LoggerService.LogMessage(LoggerService.LogLevel.Info, VALIDATING_ARGS, shouldOutputMessageToConsole: true);
176183

177184
#region コマンドライン引数の過不足およびnull, 空文字, 空白文字チェック
@@ -283,6 +290,7 @@ static async Task Main(string[] args)
283290
_reportsFolderAbsolutePath,
284291
_thisAppVersion,
285292
_elapsedTimeString,
293+
_computerName,
286294
_config);
287295

288296
// 正常終了メッセージ出力

Services/ReportGenerateService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public sealed class ReportGenerateService
4444
/// </summary>
4545
private const string REPORT_HEADER_APP_VERSION = "- App Version: " + Constants.APP_NAME + " {0}";
4646

47+
/// <summary>
48+
/// レポートヘッダ: コンピュータ名
49+
/// </summary>
50+
private const string REPORT_HEADER_COMPUTER = "- Computer: {0}";
51+
4752
/// <summary>
4853
/// レポートヘッダ: 旧フォルダパス
4954
/// </summary>
@@ -337,6 +342,7 @@ public sealed class ReportGenerateService
337342
/// <param name="reportsFolderAbsolutePath">出力先レポートフォルダの絶対パス</param>
338343
/// <param name="appVersion">アプリケーションバージョン</param>
339344
/// <param name="elapsedTimeString">経過時間文字列 (null 可)</param>
345+
/// <param name="computerName">実行コンピュータ名</param>
340346
/// <param name="config">設定オブジェクト</param>
341347
/// <exception cref="Exception">入出力エラーなど予期しない例外</exception>
342348
public void GenerateDiffReport(
@@ -345,6 +351,7 @@ public void GenerateDiffReport(
345351
string reportsFolderAbsolutePath,
346352
string appVersion,
347353
string elapsedTimeString,
354+
string computerName,
348355
ConfigSettings config)
349356
{
350357
string diffReportAbsolutePath = Path.Combine(reportsFolderAbsolutePath, DIFF_REPORT_FILE_NAME);
@@ -365,6 +372,7 @@ public void GenerateDiffReport(
365372
// ヘッダ
366373
streamWriter.WriteLine(REPORT_TITLE);
367374
streamWriter.WriteLine(string.Format(REPORT_HEADER_APP_VERSION, appVersion));
375+
streamWriter.WriteLine(string.Format(REPORT_HEADER_COMPUTER, computerName));
368376
streamWriter.WriteLine(string.Format(REPORT_HEADER_OLD, oldFolderAbsolutePath));
369377
streamWriter.WriteLine(string.Format(REPORT_HEADER_NEW, newFolderAbsolutePath));
370378
streamWriter.WriteLine(string.Format(REPORT_HEADER_IGNORED_EXTENSIONS, string.Join(REPORT_LIST_SEPARATOR, config.IgnoredExtensions)));

Utils/Utility.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Diagnostics;
44
using System.IO;
55
using System.Linq;
6+
using System.Net;
67
using System.Runtime.InteropServices;
78
using System.Security.Cryptography;
89
using System.Text;
@@ -279,6 +280,11 @@ public static class Utility
279280
/// <see cref="ToSafeFileName"/> の既定最大長。
280281
/// </summary>
281282
private const int SAFE_FILENAME_DEFAULT_MAX_LENGTH = 180;
283+
284+
/// <summary>
285+
/// コンピュータ名が取得できなかった場合のフォールバック文字列。
286+
/// </summary>
287+
private const string UNKNOWN_COMPUTER_NAME = "Unknown Computer";
282288
#endregion
283289

284290
#region private interop (macOS)
@@ -374,6 +380,39 @@ private static bool TryGetFileSystemInfoOnMac(string path, out string fsType, ou
374380
#endregion
375381

376382
#region public methods
383+
/// <summary>
384+
/// 実行中のコンピュータ名をベストエフォートで取得します。
385+
/// </summary>
386+
/// <returns>取得できたコンピュータ名。取得できない場合は <see cref="UNKNOWN_COMPUTER_NAME"/>。</returns>
387+
public static string GetComputerName()
388+
{
389+
var machineName = TryGetEnvironmentMachineName();
390+
if (!string.IsNullOrWhiteSpace(machineName))
391+
{
392+
return machineName;
393+
}
394+
395+
var hostName = TryGetDnsHostName();
396+
if (!string.IsNullOrWhiteSpace(hostName))
397+
{
398+
return hostName;
399+
}
400+
401+
var envHost = Environment.GetEnvironmentVariable("HOSTNAME");
402+
if (!string.IsNullOrWhiteSpace(envHost))
403+
{
404+
return envHost;
405+
}
406+
407+
var envComputer = Environment.GetEnvironmentVariable("COMPUTERNAME");
408+
if (!string.IsNullOrWhiteSpace(envComputer))
409+
{
410+
return envComputer;
411+
}
412+
413+
return UNKNOWN_COMPUTER_NAME;
414+
}
415+
377416
/// <summary>
378417
/// 指定パスがネットワーク共有(UNC/NFS/CIFS/SSHFS など)の可能性が高いかを判定します。
379418
/// - Windows: UNC (\\\\ / \\?\UNC\) とネットワークドライブを検出。
@@ -1065,5 +1104,37 @@ public static string BuildBaseLabel(string command, string[] args)
10651104
/// <exception cref="Exception">例外は発生せず、常に結合済み文字列を返します。</exception>
10661105
public static string GetUsedArgs(string[] args) => string.Join(" ", args.Select(x => x.Contains(' ') ? $"\"{x}\"" : x));
10671106
#endregion
1107+
1108+
#region private helper methods
1109+
/// <summary>
1110+
/// <see cref="Environment.MachineName"/> にアクセスし、取得できない場合は null を返します。
1111+
/// </summary>
1112+
private static string TryGetEnvironmentMachineName()
1113+
{
1114+
try
1115+
{
1116+
return Environment.MachineName;
1117+
}
1118+
catch
1119+
{
1120+
return null;
1121+
}
1122+
}
1123+
1124+
/// <summary>
1125+
/// <see cref="Dns.GetHostName"/> にアクセスし、取得できない場合は null を返します。
1126+
/// </summary>
1127+
private static string TryGetDnsHostName()
1128+
{
1129+
try
1130+
{
1131+
return Dns.GetHostName();
1132+
}
1133+
catch
1134+
{
1135+
return null;
1136+
}
1137+
}
1138+
#endregion
10681139
}
10691140
}

version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"publicReleaseRefSpec": [
55
"^refs/heads/main$",
66
"^refs/tags/v\\d+\\.\\d+(?:\\.\\d+)?$"

0 commit comments

Comments
 (0)