Skip to content

Commit bd36ed4

Browse files
committed
Refactor the usages of System.IO.
1 parent b59425a commit bd36ed4

File tree

58 files changed

+299
-203
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+299
-203
lines changed

Rubberduck.CodeAnalysis/Inspections/Logistics/Inspector.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Globalization;
5-
using System.IO;
5+
using Path = System.IO.Path;
66
using System.Linq;
77
using System.Reflection;
88
using System.Threading;

Rubberduck.Core/AddRemoveReferences/ReferenceModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.ComponentModel;
3-
using System.IO;
3+
using Path = System.IO.Path;
44
using System.Runtime.CompilerServices;
55
using System.Runtime.InteropServices;
66
using System.Runtime.InteropServices.ComTypes;

Rubberduck.Core/AddRemoveReferences/ReferenceReconciler.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using System.IO;
2+
using Path = System.IO.Path;
33
using System.Linq;
44
using System.Runtime.InteropServices;
55
using Rubberduck.Interaction;

Rubberduck.Core/AddRemoveReferences/RegisteredLibraryInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Globalization;
4-
using System.IO;
4+
using Path = System.IO.Path;
55
using System.Runtime.InteropServices.ComTypes;
66

77
namespace Rubberduck.AddRemoveReferences

Rubberduck.Core/App.cs

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
using System.Collections.Generic;
2-
using System.IO;
3-
using Infralution.Localization.Wpf;
1+
using Infralution.Localization.Wpf;
42
using NLog;
53
using Rubberduck.Common;
64
using Rubberduck.Interaction;
75
using Rubberduck.Settings;
86
using Rubberduck.UI.Command.MenuItems;
97
using System;
8+
using System.Collections.Generic;
109
using System.Diagnostics;
1110
using System.Globalization;
1211
using System.Linq;
1312
using Rubberduck.Parsing.UIContext;
1413
using Rubberduck.Resources;
15-
using Rubberduck.Runtime;
1614
using Rubberduck.UI.Command;
1715
using Rubberduck.VBEditor.Utility;
1816
using Rubberduck.VersionCheck;
1917
using Application = System.Windows.Forms.Application;
2018
using Rubberduck.SettingsProvider;
19+
using System.IO.Abstractions;
2120

2221
namespace Rubberduck
2322
{
@@ -33,13 +32,15 @@ public sealed class App : IDisposable
3332
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
3433

3534
private Configuration _config;
35+
private IFileSystem _filesystem;
3636

3737
public App(IMessageBox messageBox,
3838
IConfigurationService<Configuration> configService,
3939
IAppMenu appMenus,
4040
IRubberduckHooks hooks,
4141
IVersionCheck version,
42-
CommandBase checkVersionCommand)
42+
CommandBase checkVersionCommand,
43+
IFileSystem filesystem)
4344
{
4445
_messageBox = messageBox;
4546
_configService = configService;
@@ -49,6 +50,7 @@ public App(IMessageBox messageBox,
4950
_checkVersionCommand = checkVersionCommand;
5051

5152
_configService.SettingsChanged += _configService_SettingsChanged;
53+
_filesystem = filesystem;
5254

5355
UiContextProvider.Initialize();
5456
}
@@ -65,13 +67,13 @@ private void _configService_SettingsChanged(object sender, ConfigurationChangedE
6567
}
6668
}
6769

68-
private static void EnsureLogFolderPathExists()
70+
private void EnsureLogFolderPathExists()
6971
{
7072
try
7173
{
72-
if (!Directory.Exists(ApplicationConstants.LOG_FOLDER_PATH))
74+
if (!_filesystem.Directory.Exists(ApplicationConstants.LOG_FOLDER_PATH))
7375
{
74-
Directory.CreateDirectory(ApplicationConstants.LOG_FOLDER_PATH);
76+
_filesystem.Directory.CreateDirectory(ApplicationConstants.LOG_FOLDER_PATH);
7577
}
7678
}
7779
catch
@@ -80,15 +82,15 @@ private static void EnsureLogFolderPathExists()
8082
}
8183
}
8284

83-
private static void EnsureTempPathExists()
85+
private void EnsureTempPathExists()
8486
{
8587
// This is required by the parser - allow this to throw.
86-
if (!Directory.Exists(ApplicationConstants.RUBBERDUCK_TEMP_PATH))
88+
if (!_filesystem.Directory.Exists(ApplicationConstants.RUBBERDUCK_TEMP_PATH))
8789
{
88-
Directory.CreateDirectory(ApplicationConstants.RUBBERDUCK_TEMP_PATH);
90+
_filesystem.Directory.CreateDirectory(ApplicationConstants.RUBBERDUCK_TEMP_PATH);
8991
}
9092
// The parser swallows the error if deletions fail - clean up any temp files on startup
91-
foreach (var file in new DirectoryInfo(ApplicationConstants.RUBBERDUCK_TEMP_PATH).GetFiles())
93+
foreach (var file in _filesystem.DirectoryInfo.FromDirectoryName(ApplicationConstants.RUBBERDUCK_TEMP_PATH).GetFiles())
9294
{
9395
try
9496
{
@@ -241,7 +243,7 @@ public void LogRubberduckStart()
241243
{
242244
$"\tHost Product: {Application.ProductName} {(Environment.Is64BitProcess ? "x64" : "x86")}",
243245
$"\tHost Version: {Application.ProductVersion}",
244-
$"\tHost Executable: {Path.GetFileName(Application.ExecutablePath).ToUpper()}", // .ToUpper() used to convert ExceL.EXE -> EXCEL.EXE
246+
$"\tHost Executable: {_filesystem.Path.GetFileName(Application.ExecutablePath).ToUpper()}", // .ToUpper() used to convert ExceL.EXE -> EXCEL.EXE
245247
});
246248
}
247249
catch

Rubberduck.Core/Common/ClipboardWriter.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using System.IO;
2-
using System.Windows;
3-
using System.Windows.Media.Imaging;
1+
using System;
42
using System.Collections.Generic;
3+
using MemoryStream = System.IO.MemoryStream;
54
using System.Linq;
6-
using System;
5+
using System.Windows;
6+
using System.Windows.Media.Imaging;
77

88
namespace Rubberduck.Common
99
{

Rubberduck.Core/Common/ExportFormatter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.IO;
2+
using MemoryStream = System.IO.MemoryStream;
33
using System.Net;
44
using System.Text;
55
using System.Xml;

Rubberduck.Core/Common/WindowsOperatingSystem.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
using System;
22
using System.Diagnostics;
3-
using System.IO;
3+
using System.IO.Abstractions;
44
using System.Management;
5-
using System.Runtime.CompilerServices;
65
using NLog;
76

87
namespace Rubberduck.Common
98
{
109
public sealed class WindowsOperatingSystem : IOperatingSystem
1110
{
12-
public static readonly ILogger _Logger = LogManager.GetCurrentClassLogger();
11+
private static readonly ILogger _Logger = LogManager.GetCurrentClassLogger();
12+
private readonly IFileSystem _filesystem;
13+
14+
public WindowsOperatingSystem(
15+
IFileSystem filesystem)
16+
{
17+
_filesystem = filesystem;
18+
}
1319

1420
public void ShowFolder(string folderPath)
1521
{
16-
if (!Directory.Exists(folderPath))
22+
if (!_filesystem.Directory.Exists(folderPath))
1723
{
18-
Directory.CreateDirectory(folderPath);
24+
_filesystem.Directory.CreateDirectory(folderPath);
1925
}
2026

2127
using (Process.Start(folderPath))

Rubberduck.Core/Navigation/CodeExplorer/CodeExplorerComponentViewModel.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1+
using System;
12
using System.Collections.Generic;
2-
using System.IO;
3+
using Path = System.IO.Path;
34
using System.Linq;
45
using Rubberduck.Parsing.Symbols;
56
using Rubberduck.Parsing.Annotations.Concrete;
67
using Rubberduck.VBEditor.SafeComWrappers;
78
using Rubberduck.Resources.CodeExplorer;
89
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
9-
using System;
1010

1111
namespace Rubberduck.Navigation.CodeExplorer
1212
{

Rubberduck.Core/Navigation/CodeExplorer/CodeExplorerReferenceViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System.Collections.Generic;
2-
using System.IO;
2+
using Path = System.IO.Path;
33
using System.Linq;
44
using Rubberduck.AddRemoveReferences;
55
using Rubberduck.Parsing.Symbols;

Rubberduck.Core/Rubberduck.Core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<PackageReference Include="NLog.Schema">
8585
<Version>4.5.10</Version>
8686
</PackageReference>
87+
<PackageReference Include="System.IO.Abstractions" Version="12.2.1" />
8788
<PackageReference Include="System.ValueTuple">
8889
<Version>4.5.0</Version>
8990
</PackageReference>

Rubberduck.Core/Settings/ReferenceConfigProvider.cs

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using System.IO;
2+
using System.IO.Abstractions;
33
using System.Reflection;
44
using System.Windows.Forms;
55
using Rubberduck.Resources.Registration;
@@ -13,19 +13,25 @@ namespace Rubberduck.Settings
1313
{
1414
public class ReferenceConfigProvider : ConfigurationServiceBase<ReferenceSettings>, IDisposable
1515
{
16-
private static readonly string HostApplication = Path.GetFileName(Application.ExecutablePath).ToUpperInvariant();
16+
private readonly string _hostApplication;
1717

1818
private readonly IEnvironmentProvider _environment;
1919
private readonly IVbeEvents _events;
20+
private readonly IFileSystem _filesystem;
2021
private bool _listening;
2122

22-
public ReferenceConfigProvider(IPersistenceService<ReferenceSettings> persister, IEnvironmentProvider environment, IVbeEvents events)
23+
public ReferenceConfigProvider(
24+
IPersistenceService<ReferenceSettings> persister,
25+
IEnvironmentProvider environment,
26+
IVbeEvents events,
27+
IFileSystem filesystem)
2328
: base(persister, new DefaultSettings<ReferenceSettings, Properties.Settings>())
2429
{
2530
_environment = environment;
2631
_events = events;
32+
_filesystem = filesystem;
33+
_hostApplication = _filesystem.Path.GetFileName(Application.ExecutablePath).ToUpperInvariant();
2734

28-
2935
var settings = Read();
3036
_listening = settings.AddToRecentOnReferenceEvents;
3137
if (_listening && _events != null)
@@ -54,14 +60,14 @@ public override ReferenceSettings ReadDefaults()
5460
var appdata = _environment?.GetFolderPath(Environment.SpecialFolder.ApplicationData);
5561
if (!string.IsNullOrEmpty(documents))
5662
{
57-
var addins = Path.Combine(appdata, "Microsoft", "AddIns");
58-
if (Directory.Exists(addins))
63+
var addins = _filesystem.Path.Combine(appdata, "Microsoft", "AddIns");
64+
if (_filesystem.Directory.Exists(addins))
5965
{
6066
defaults.ProjectPaths.Add(addins);
6167
}
6268

63-
var templates = Path.Combine(appdata, "Microsoft", "Templates");
64-
if (Directory.Exists(templates))
69+
var templates = _filesystem.Path.Combine(appdata, "Microsoft", "Templates");
70+
if (_filesystem.Directory.Exists(templates))
6571
{
6672
defaults.ProjectPaths.Add(templates);
6773
}
@@ -95,7 +101,7 @@ private void ReferenceAddedHandler(object sender, ReferenceEventArgs e)
95101
}
96102

97103
var settings = Read();
98-
settings.TrackUsage(e.Reference, e.Type == ReferenceKind.Project ? HostApplication : null);
104+
settings.TrackUsage(e.Reference, e.Type == ReferenceKind.Project ? _hostApplication : null);
99105
Save(settings);
100106
}
101107

Rubberduck.Core/Templates/TemplateFileHandler.cs

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.IO;
3+
using System.IO.Abstractions;
44
using System.Linq;
55
using Rubberduck.SettingsProvider;
66

@@ -15,30 +15,34 @@ public interface ITemplateFileHandlerProvider
1515
public class TemplateFileHandlerProvider : ITemplateFileHandlerProvider
1616
{
1717
private readonly string _rootPath;
18+
private readonly IFileSystem _filesystem;
1819

19-
public TemplateFileHandlerProvider(IPersistencePathProvider pathProvider)
20+
public TemplateFileHandlerProvider(
21+
IPersistencePathProvider pathProvider,
22+
IFileSystem fileSystem)
2023
{
2124
_rootPath = pathProvider.DataFolderPath("Templates");
25+
_filesystem = fileSystem;
2226
}
2327

2428
public ITemplateFileHandler CreateTemplateFileHandler(string templateName)
2529
{
26-
if (!Directory.Exists(_rootPath))
30+
if (!_filesystem.Directory.Exists(_rootPath))
2731
{
28-
Directory.CreateDirectory(_rootPath);
32+
_filesystem.Directory.CreateDirectory(_rootPath);
2933
}
3034

31-
var fullPath = Path.Combine(_rootPath, templateName);
32-
if (!Directory.Exists(Path.GetDirectoryName(fullPath)))
35+
var fullPath = _filesystem.Path.Combine(_rootPath, templateName);
36+
if (!_filesystem.Directory.Exists(_filesystem.Path.GetDirectoryName(fullPath)))
3337
{
3438
throw new InvalidOperationException("Cannot provide a path for where the parent directory do not exist");
3539
}
36-
return new TemplateFileHandler(fullPath);
40+
return new TemplateFileHandler(fullPath, _filesystem);
3741
}
3842

3943
public IEnumerable<string> GetTemplateNames()
4044
{
41-
var info = new DirectoryInfo(_rootPath);
45+
var info = _filesystem.DirectoryInfo.FromDirectoryName(_rootPath);
4246
return info.GetFiles().Select(file => file.Name).ToList();
4347
}
4448
}
@@ -53,22 +57,26 @@ public interface ITemplateFileHandler
5357
public class TemplateFileHandler : ITemplateFileHandler
5458
{
5559
private readonly string _fullPath;
60+
private readonly IFileSystem _filesystem;
5661

57-
public TemplateFileHandler(string fullPath)
62+
public TemplateFileHandler(
63+
string fullPath,
64+
IFileSystem fileSystem)
5865
{
5966
_fullPath = fullPath + (fullPath.EndsWith(".rdt") ? string.Empty : ".rdt");
67+
_filesystem = fileSystem;
6068
}
6169

62-
public bool Exists => File.Exists(_fullPath);
70+
public bool Exists => _filesystem.File.Exists(_fullPath);
6371

6472
public string Read()
6573
{
66-
return Exists ? File.ReadAllText(_fullPath) : null;
74+
return Exists ? _filesystem.File.ReadAllText(_fullPath) : null;
6775
}
6876

6977
public void Write(string content)
7078
{
71-
File.WriteAllText(_fullPath, content);
79+
_filesystem.File.WriteAllText(_fullPath, content);
7280
}
7381
}
7482
}

Rubberduck.Core/UI/About/AboutControlViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System;
22
using System.Diagnostics;
3+
using Path = System.IO.Path;
34
using NLog;
45
using NLog.Targets;
56
using Rubberduck.Resources.About;
67
using Rubberduck.UI.Command;
78
using Rubberduck.VersionCheck;
89
using Application = System.Windows.Forms.Application;
9-
using System.IO;
1010

1111
namespace Rubberduck.UI.About
1212
{

Rubberduck.Core/UI/AddRemoveReferences/AddRemoveReferencesModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
2-
using System.IO;
32
using System.Linq;
3+
using Path = System.IO.Path;
44
using System.Windows.Forms;
55
using Rubberduck.AddRemoveReferences;
66
using Rubberduck.Parsing.Symbols;

0 commit comments

Comments
 (0)