Remove remaining references to Assembly.Location

This commit is contained in:
Caelan Sayler
2021-08-13 10:58:42 +01:00
parent 223ec60bb0
commit 6a9449712b
8 changed files with 47 additions and 40 deletions

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
namespace Squirrel
{
internal static class AssemblyRuntimeInfo
{
public static string EntryExePath { get; }
public static string BaseDirectory { get; }
public static AssemblyName ExecutingAssemblyName => Assembly.GetExecutingAssembly().GetName();
static AssemblyRuntimeInfo()
{
EntryExePath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
BaseDirectory = AppContext.BaseDirectory;
}
}
}

View File

@@ -5,5 +5,4 @@ using System.Runtime.InteropServices;
[assembly: ComVisible(false)]
[assembly: InternalsVisibleTo("Squirrel.Tests")]
[assembly: InternalsVisibleTo("Update")]
[assembly: InternalsVisibleTo("Update-Mono")]
[assembly: InternalsVisibleTo("SyncReleases")]

View File

@@ -48,13 +48,10 @@ namespace Squirrel.SimpleSplat
"XDESPROC.EXE",
};
var entry = Assembly.GetEntryAssembly();
if (entry != null) {
var exeName = (new FileInfo(entry.Location)).Name.ToUpperInvariant();
var exeName = (new FileInfo(AssemblyRuntimeInfo.EntryExePath)).Name.ToUpperInvariant();
if (designEnvironments.Any(x => x.Contains(exeName))) {
return true;
}
if (designEnvironments.Any(x => x.Contains(exeName))) {
return true;
}
return false;

View File

@@ -385,11 +385,11 @@ namespace Squirrel
// If we're running in the context of Update.exe, we can't
// update ourselves. Instead, ask the new Update.exe to do it
// once we exit
var us = Assembly.GetEntryAssembly();
if (us != null && Path.GetFileName(us.Location).Equals("update.exe", StringComparison.OrdinalIgnoreCase)) {
var ourLocation = AssemblyRuntimeInfo.EntryExePath;
if (ourLocation != null && Path.GetFileName(ourLocation).Equals("update.exe", StringComparison.OrdinalIgnoreCase)) {
var appName = targetDir.Parent.Name;
Process.Start(newSquirrel, "--updateSelf=" + us.Location);
Process.Start(newSquirrel, "--updateSelf=" + ourLocation);
return;
}

View File

@@ -35,7 +35,7 @@ namespace Squirrel
string accessToken = null)
{
var repoUri = new Uri(repoUrl);
var userAgent = new ProductInfoHeaderValue("Squirrel", Assembly.GetExecutingAssembly().GetName().Version.ToString());
var userAgent = new ProductInfoHeaderValue("Squirrel", AssemblyRuntimeInfo.ExecutingAssemblyName.Version.ToString());
if (repoUri.Segments.Length != 3) {
throw new Exception("Repo URL must be to the root URL of the repo e.g. https://github.com/myuser/myrepo");

View File

@@ -105,8 +105,7 @@ namespace Squirrel
public void KillAllProcessesBelongingToPackage()
{
var ourExe = Assembly.GetEntryAssembly();
var ourExePath = ourExe != null ? ourExe.Location : null;
var ourExePath = AssemblyRuntimeInfo.EntryExePath;
UnsafeUtility.EnumerateProcesses()
.Where(x => {

View File

@@ -236,13 +236,12 @@ namespace Squirrel
// * We're Update.exe, running on initial install from SquirrelTemp
// * We're a C# EXE with Squirrel linked in
var assembly = Assembly.GetEntryAssembly();
if (assemblyLocation == null && assembly == null) {
if (assemblyLocation == null && AssemblyRuntimeInfo.EntryExePath == null) {
// dunno lol
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
assemblyLocation = assemblyLocation ?? assembly.Location;
assemblyLocation = assemblyLocation ?? AssemblyRuntimeInfo.EntryExePath;
if (Path.GetFileName(assemblyLocation).Equals("update.exe", StringComparison.OrdinalIgnoreCase)) {
// NB: Both the "SquirrelTemp" case and the "App's folder" case
@@ -316,19 +315,17 @@ namespace Squirrel
static string getUpdateExe()
{
var assembly = Assembly.GetEntryAssembly();
var ourPath = AssemblyRuntimeInfo.EntryExePath;
// Are we update.exe?
if (assembly != null &&
Path.GetFileName(assembly.Location).Equals("update.exe", StringComparison.OrdinalIgnoreCase) &&
assembly.Location.IndexOf("app-", StringComparison.OrdinalIgnoreCase) == -1 &&
assembly.Location.IndexOf("SquirrelTemp", StringComparison.OrdinalIgnoreCase) == -1) {
return Path.GetFullPath(assembly.Location);
if (ourPath != null &&
Path.GetFileName(ourPath).Equals("update.exe", StringComparison.OrdinalIgnoreCase) &&
ourPath.IndexOf("app-", StringComparison.OrdinalIgnoreCase) == -1 &&
ourPath.IndexOf("SquirrelTemp", StringComparison.OrdinalIgnoreCase) == -1) {
return Path.GetFullPath(ourPath);
}
assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
var updateDotExe = Path.Combine(Path.GetDirectoryName(assembly.Location), "..\\Update.exe");
var updateDotExe = Path.Combine(AssemblyRuntimeInfo.BaseDirectory, "..\\Update.exe");
var target = new FileInfo(updateDotExe);
if (!target.Exists) throw new Exception("Update.exe not found, not a Squirrel-installed app?");

View File

@@ -25,9 +25,6 @@ namespace Squirrel.Update
{
static StartupOption opt;
public static string MyBaseDirectory => AppContext.BaseDirectory;
public static string MyEntryExePath => Process.GetCurrentProcess().MainModule.FileName;
public static int Main(string[] args)
{
var pg = new Program();
@@ -126,7 +123,7 @@ namespace Squirrel.Update
public async Task Install(bool silentInstall, ProgressSource progressSource, string sourceDirectory = null)
{
sourceDirectory = sourceDirectory ?? MyBaseDirectory;
sourceDirectory = sourceDirectory ?? AssemblyRuntimeInfo.BaseDirectory;
var releasesPath = Path.Combine(sourceDirectory, "RELEASES");
this.Log().Info("Starting install, writing to {0}", sourceDirectory);
@@ -161,7 +158,7 @@ namespace Squirrel.Update
Directory.CreateDirectory(mgr.RootAppDirectory);
var updateTarget = Path.Combine(mgr.RootAppDirectory, "Update.exe");
this.ErrorIfThrows(() => File.Copy(MyEntryExePath, updateTarget, true),
this.ErrorIfThrows(() => File.Copy(AssemblyRuntimeInfo.EntryExePath, updateTarget, true),
"Failed to copy Update.exe to " + updateTarget);
await mgr.FullInstall(silentInstall, progressSource.Raise);
@@ -213,7 +210,7 @@ namespace Squirrel.Update
public async Task UpdateSelf()
{
waitForParentToExit();
var src = MyEntryExePath;
var src = AssemblyRuntimeInfo.EntryExePath;
var updateDotExeForOurPackage = Path.Combine(
Path.GetDirectoryName(src),
"..", "Update.exe");
@@ -304,7 +301,7 @@ namespace Squirrel.Update
if (!File.Exists(bootstrapperExe)) {
bootstrapperExe = Path.Combine(
MyBaseDirectory,
AssemblyRuntimeInfo.BaseDirectory,
"Setup.exe");
}
@@ -451,7 +448,7 @@ namespace Squirrel.Update
}
// Find the latest installed version's app dir
var appDir = MyBaseDirectory;
var appDir = AssemblyRuntimeInfo.BaseDirectory;
var releases = ReleaseEntry.ParseReleaseFile(
File.ReadAllText(Utility.LocalReleaseFileForAppDir(appDir), Encoding.UTF8));
@@ -526,7 +523,7 @@ namespace Squirrel.Update
this.Log().Info("Building embedded zip file for Setup.exe");
using (Utility.WithTempDirectory(out tempPath, null)) {
this.ErrorIfThrows(() => {
File.Copy(MyEntryExePath, Path.Combine(tempPath, "Update.exe"));
File.Copy(AssemblyRuntimeInfo.EntryExePath, Path.Combine(tempPath, "Update.exe"));
File.Copy(fullPackage, Path.Combine(tempPath, Path.GetFileName(fullPackage)));
}, "Failed to write package files to temp dir: " + tempPath);
@@ -573,9 +570,7 @@ namespace Squirrel.Update
// Try to find SignTool.exe
var exe = @".\signtool.exe";
if (!File.Exists(exe)) {
exe = Path.Combine(
MyBaseDirectory,
"signtool.exe");
exe = Path.Combine( AssemblyRuntimeInfo.BaseDirectory, "signtool.exe");
// Run down PATH and hope for the best
if (!File.Exists(exe)) exe = "signtool.exe";
@@ -725,7 +720,7 @@ namespace Squirrel.Update
static string pathToWixTools()
{
var ourPath = MyBaseDirectory;
var ourPath = AssemblyRuntimeInfo.BaseDirectory;
// Same Directory? (i.e. released)
if (File.Exists(Path.Combine(ourPath, "candle.exe"))) {
@@ -743,7 +738,7 @@ namespace Squirrel.Update
static string getAppNameFromDirectory(string path = null)
{
path = path ?? MyBaseDirectory;
path = path ?? AssemblyRuntimeInfo.BaseDirectory;
return (new DirectoryInfo(path)).Name;
}
@@ -806,7 +801,7 @@ namespace Squirrel.Update
try {
var dir = saveInTemp ?
Path.GetTempPath() :
Program.MyBaseDirectory;
AssemblyRuntimeInfo.BaseDirectory;
var fileName = commandSuffix == null ? String.Format($"Squirrel.{i}.log", i) : String.Format($"Squirrel-{commandSuffix}.{i}.log", i);
var file = Path.Combine(dir, fileName.Replace(".0.log", ".log"));
var str = File.Open(file, FileMode.Append, FileAccess.Write, FileShare.Read);