Skip to content

Commit

Permalink
(refactor) IFileSystem.get_current_assembly_path
Browse files Browse the repository at this point in the history
Move all calls to `Assembly.GetExecutingAssembly().CodeBase` path to
IFileSystem instead. The code to get the curent assembly path was in
quite a few locations. This moves it down to one location.
  • Loading branch information
ferventcoder committed Jun 4, 2015
1 parent 58d0a39 commit f489545
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/chocolatey.console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private static void remove_old_chocolatey_exe(IFileSystem fileSystem)
{
try
{
fileSystem.delete_file(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty) + ".old");
fileSystem.delete_file(fileSystem.get_current_assembly_path() + ".old");
fileSystem.delete_file(fileSystem.combine_paths(AppDomain.CurrentDomain.BaseDirectory, "choco.exe.old"));
}
catch (Exception ex)
Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey.tests.integration/NUnitSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static void fix_application_parameter_variables(Container container)
{
var fileSystem = container.GetInstance<IFileSystem>();

var applicationLocation = fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty));
var applicationLocation = fileSystem.get_directory_name(fileSystem.get_current_assembly_path());

var field = typeof (ApplicationParameters).GetField("InstallLocation");
field.SetValue(null, applicationLocation);
Expand Down
2 changes: 1 addition & 1 deletion src/chocolatey.tests.integration/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Scenario

public static string get_top_level()
{
return _fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty));
return _fileSystem.get_directory_name(_fileSystem.get_current_assembly_path());
}

public static string get_package_install_path()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override void Context()
{
FileSystem = new DotNetFileSystem();
Provider = new CrytpoHashProvider(FileSystem,CryptoHashProviderType.Md5);
ContextDirectory = FileSystem.combine_paths(FileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)), "context");
ContextDirectory = FileSystem.combine_paths(FileSystem.get_directory_name(FileSystem.get_current_assembly_path()), "context");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public static class ApplicationParameters
public static readonly string ChocolateyInstallEnvironmentVariableName = "ChocolateyInstall";
public static readonly string Name = "Chocolatey";
#if DEBUG
public static readonly string InstallLocation = _fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty));
public static readonly string InstallLocation = _fileSystem.get_directory_name(_fileSystem.get_current_assembly_path());
#else
public static readonly string InstallLocation = Environment.GetEnvironmentVariable(ChocolateyInstallEnvironmentVariableName) ?? _fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty));
public static readonly string InstallLocation = System.Environment.GetEnvironmentVariable(ChocolateyInstallEnvironmentVariableName) ?? _fileSystem.get_directory_name(_fileSystem.get_current_assembly_path());
#endif

public static readonly string CommonAppDataChocolatey = _fileSystem.combine_paths(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), Name);
public static readonly string CommonAppDataChocolatey = _fileSystem.combine_paths(System.Environment.GetFolderPath(System.Environment.SpecialFolder.CommonApplicationData), Name);
public static readonly string LoggingLocation = _fileSystem.combine_paths(InstallLocation, "logs");
public static readonly string LoggingFile = @"chocolatey.log";
public static readonly string Log4NetConfigurationAssembly = @"chocolatey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void run(ChocolateyConfiguration configuration)
AssemblyFileExtractor.extract_all_resources_to_relative_directory(
_fileSystem,
assembly,
_fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)),
_fileSystem.get_directory_name(_fileSystem.get_current_assembly_path()),
folders,
ApplicationParameters.ChocolateyFileResources,
overwriteExisting: configuration.Force,
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure/commands/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static void initialize_with(Lazy<IFileSystem> file_system, Func<IProcess>

public int execute(string process, string arguments, int waitForExitInSeconds)
{
return execute(process, arguments, waitForExitInSeconds, file_system.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)));
return execute(process, arguments, waitForExitInSeconds, file_system.get_directory_name(file_system.get_current_assembly_path()));
}

public int execute(
Expand All @@ -64,7 +64,7 @@ public int execute(
return execute(process,
arguments,
waitForExitInSeconds,
file_system.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)),
file_system.get_directory_name(file_system.get_current_assembly_path()),
stdOutAction,
stdErrAction,
updateProcessPath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Action<object, DataReceivedEventArgs> stdErrAction
_powershell,
arguments,
waitForExitSeconds,
workingDirectory: fileSystem.get_directory_name(Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty)),
workingDirectory: fileSystem.get_directory_name(fileSystem.get_current_assembly_path()),
stdOutAction: stdOutAction,
stdErrAction: stdErrAction,
updateProcessPath: true
Expand Down
5 changes: 5 additions & 0 deletions src/chocolatey/infrastructure/filesystem/DotNetFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public char get_path_directory_separator_char()
return Path.DirectorySeparatorChar;
}

public string get_current_assembly_path()
{
return Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", string.Empty);
}

#endregion

#region File
Expand Down
6 changes: 6 additions & 0 deletions src/chocolatey/infrastructure/filesystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public interface IFileSystem
/// <returns></returns>
char get_path_directory_separator_char();

/// <summary>
/// Gets the location of the executing assembly
/// </summary>
/// <returns>The path to the executing assembly</returns>
string get_current_assembly_path();

#endregion

#region File
Expand Down

0 comments on commit f489545

Please sign in to comment.