Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix computation of app path when published as a "single file" #298

Merged
merged 1 commit into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/shared/Git-Credential-Manager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Atlassian.Bitbucket;
using GitHub;
using Microsoft.AzureRepos;
Expand Down Expand Up @@ -36,16 +37,19 @@ public static void Main(string[] args)

private static string GetApplicationPath()
{
Assembly entryAssembly = Assembly.GetExecutingAssembly();
if (entryAssembly is null)
{
throw new InvalidOperationException();
}
// Assembly::Location always returns an empty string if the application was published as a single file
#pragma warning disable IL3000
bool isSingleFile = string.IsNullOrEmpty(Assembly.GetEntryAssembly()?.Location);
#pragma warning restore IL3000

string candidatePath = entryAssembly.Location;
// Use "argv[0]" to get the full path to the entry executable - this is consistent across
// .NET Framework and .NET >= 5 when published as a single file.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻

string[] args = Environment.GetCommandLineArgs();
string candidatePath = args[0];

// Strip the .dll from assembly name on Mac and Linux
if (!PlatformUtils.IsWindows() && Path.HasExtension(candidatePath))
// If we have not been published as a single file on .NET 5 then we must strip the ".dll" file extension
// to get the default AppHost/SuperHost name.
if (!isSingleFile && Path.HasExtension(candidatePath))
{
return Path.ChangeExtension(candidatePath, null);
}
Expand Down
9 changes: 7 additions & 2 deletions src/shared/Microsoft.Git.CredentialManager/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ protected override async Task<int> RunInternalAsync(string[] args)
rootCommand.AddCommand(providerCommand);
}

// Trace the current version and program arguments
Context.Trace.WriteLine($"{Constants.GetProgramHeader()} '{string.Join(" ", args)}'");
// Trace the current version, OS, runtime, and program arguments
PlatformInformation info = PlatformUtils.GetPlatformInformation();
Context.Trace.WriteLine($"Version: {Constants.GcmVersion}");
Context.Trace.WriteLine($"Runtime: {info.ClrVersion}");
Context.Trace.WriteLine($"Platform: {info.OperatingSystemType} ({info.CpuArchitecture})");
Context.Trace.WriteLine($"AppPath: {_appPath}");
Context.Trace.WriteLine($"Arguments: {string.Join(" ", args)}");

try
{
Expand Down
36 changes: 18 additions & 18 deletions src/shared/Microsoft.Git.CredentialManager/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System.Diagnostics;
using System;
using System.Reflection;

namespace Microsoft.Git.CredentialManager
Expand Down Expand Up @@ -105,35 +105,35 @@ public static class HelpUrls
public const string GcmLinuxCredStores = "https://aka.ms/gcmcore-linuxcredstores";
}

private static string _gcmVersion;
private static Version _gcmVersion;

/// <summary>
/// The current version of Git Credential Manager.
/// </summary>
public static string GcmVersion
public static Version GcmVersion
{
get
{
if (_gcmVersion is null)
{
_gcmVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
var attr = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>();
if (attr is null)
{
_gcmVersion = assembly.GetName().Version;
}
else if (Version.TryParse(attr.Version, out Version asmVersion))
{
_gcmVersion = asmVersion;
}
else
{
// Unknown version!
_gcmVersion = new Version(0, 0);
}
}

return _gcmVersion;
}
}

/// <summary>
/// Get standard program header title for Git Credential Manager, including the current version and OS information.
/// </summary>
/// <returns>Standard program header.</returns>
public static string GetProgramHeader()
{
PlatformInformation info = PlatformUtils.GetPlatformInformation();

return $"Git Credential Manager version {GcmVersion} ({info.OperatingSystemType}, {info.ClrVersion})";
}

/// <summary>
/// Get the HTTP user-agent for Git Credential Manager.
/// </summary>
Expand Down