Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit d416f7e

Browse files
Merge pull request #581 from github-for-unity/niceio-latest
Switch NPath to a struct and move (I)FileSystem.cs into it. Fix some bugs
2 parents 01d142b + 89fbd02 commit d416f7e

31 files changed

+460
-452
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ public void Run(bool firstRun)
4848
Logger.Trace("Run - CurrentDirectory {0}", NPath.CurrentDirectory);
4949

5050
var gitExecutablePath = SystemSettings.Get(Constants.GitInstallPathKey)?.ToNPath();
51-
if (gitExecutablePath != null && gitExecutablePath.FileExists()) // we have a git path
51+
if (gitExecutablePath != null && gitExecutablePath.Value.FileExists()) // we have a git path
5252
{
5353
Logger.Trace("Using git install path from settings: {0}", gitExecutablePath);
54-
InitializeEnvironment(gitExecutablePath);
54+
InitializeEnvironment(gitExecutablePath.Value);
5555
}
5656
else // we need to go find git
5757
{
5858
Logger.Trace("No git path found in settings");
5959

60-
var initEnvironmentTask = new ActionTask<NPath>(CancellationToken, (b, path) => InitializeEnvironment(path)) { Affinity = TaskAffinity.UI };
60+
var initEnvironmentTask = new ActionTask<NPath>(CancellationToken, (_, path) => InitializeEnvironment(path)) { Affinity = TaskAffinity.UI };
6161
var findExecTask = new FindExecTask("git", CancellationToken)
6262
.FinallyInUI((b, ex, path) => {
6363
if (b && path != null)
6464
{
6565
Logger.Trace("FindExecTask Success: {0}", path);
66-
InitializeEnvironment(gitExecutablePath);
66+
InitializeEnvironment(path);
6767
}
6868
else
6969
{
@@ -189,7 +189,7 @@ private void InitializeEnvironment(NPath gitExecutablePath)
189189
}
190190
else
191191
{
192-
Logger.Warning("No Windows CredentialHeloper found: Setting to wincred");
192+
Logger.Warning("No Windows CredentialHelper found: Setting to wincred");
193193

194194
GitClient.SetConfig("credential.helper", "wincred", GitConfigSource.Global)
195195
.Then(afterGitSetup)

src/GitHub.Api/Events/RepositoryWatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private int ProcessEvents(Event[] fileEvents)
169169
var eventDirectory = new NPath(fileEvent.Directory);
170170
var fileA = eventDirectory.Combine(fileEvent.FileA);
171171

172-
NPath fileB = null;
172+
NPath fileB;
173173
if (fileEvent.FileB != null)
174174
{
175175
fileB = eventDirectory.Combine(fileEvent.FileB);

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<RunCodeAnalysis>false</RunCodeAnalysis>
5252
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
5353
<CodeAnalysisIgnoreGeneratedCode>true</CodeAnalysisIgnoreGeneratedCode>
54+
<CodeAnalysisRuleSet>..\..\common\codeanalysis-debug.ruleset</CodeAnalysisRuleSet>
5455
</PropertyGroup>
5556
<PropertyGroup>
5657
<BuildConfig Condition=" '$(BuildConfig)' == '' ">Debug</BuildConfig>
@@ -118,6 +119,7 @@
118119
<Compile Include="Helpers\Validation.cs" />
119120
<Compile Include="Installer\GitInstaller.cs" />
120121
<Compile Include="Installer\UnzipTask.cs" />
122+
<Compile Include="IO\FileSystem.cs" />
121123
<Compile Include="OutputProcessors\GitAheadBehindStatusOutputProcessor.cs" />
122124
<Compile Include="OutputProcessors\LfsVersionOutputProcessor.cs" />
123125
<Compile Include="OutputProcessors\VersionOutputProcessor.cs" />
@@ -169,7 +171,6 @@
169171
<Compile Include="OutputProcessors\StatusOutputProcessor.cs" />
170172
<Compile Include="Git\GitConfig.cs" />
171173
<Compile Include="OutputProcessors\LineProcessor.cs" />
172-
<Compile Include="IO\FileSystem.cs" />
173174
<Compile Include="Git\GitRemote.cs" />
174175
<Compile Include="OutputProcessors\IProcessManager.cs" />
175176
<Compile Include="IO\NiceIO.cs" />
@@ -182,7 +183,6 @@
182183
<DependentUpon>Localization.resx</DependentUpon>
183184
</Compile>
184185
<Compile Include="Platform\ProcessEnvironment.cs" />
185-
<Compile Include="IO\IFileSystem.cs" />
186186
<Compile Include="Platform\IProcessEnvironment.cs" />
187187
<Compile Include="Properties\AssemblyInfo.cs" />
188188
<Compile Include="Git\Repository.cs" />

src/GitHub.Api/IO/FileSystem.cs

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,82 @@
1-
using GitHub.Unity;
1+
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.IO;
54
using System.Text;
65

76
namespace GitHub.Unity
87
{
9-
class FileSystem : IFileSystem
8+
public interface IFileSystem
9+
{
10+
string ChangeExtension(string path, string extension);
11+
string Combine(string path1, string path2);
12+
string Combine(string path1, string path2, string path3);
13+
void DirectoryCreate(string path);
14+
void DirectoryDelete(string path, bool recursive);
15+
bool DirectoryExists(string path);
16+
void DirectoryMove(string toString, string s);
17+
bool ExistingPathIsDirectory(string path);
18+
void FileCopy(string sourceFileName, string destFileName, bool overwrite);
19+
void FileDelete(string path);
20+
bool FileExists(string path);
21+
void FileMove(string sourceFileName, string s);
22+
string GetCurrentDirectory();
23+
IEnumerable<string> GetDirectories(string path);
24+
IEnumerable<string> GetDirectories(string path, string pattern);
25+
IEnumerable<string> GetDirectories(string path, string pattern, SearchOption searchOption);
26+
string GetDirectoryName(string path);
27+
string GetFileNameWithoutExtension(string fileName);
28+
IEnumerable<string> GetFiles(string path);
29+
IEnumerable<string> GetFiles(string path, string pattern);
30+
IEnumerable<string> GetFiles(string path, string pattern, SearchOption searchOption);
31+
string GetFullPath(string path);
32+
string GetParentDirectory(string path);
33+
string GetRandomFileName();
34+
string GetTempPath();
35+
Stream OpenRead(string path);
36+
Stream OpenWrite(string path, FileMode mode);
37+
byte[] ReadAllBytes(string path);
38+
string[] ReadAllLines(string path);
39+
string ReadAllText(string path);
40+
string ReadAllText(string path, Encoding encoding);
41+
void SetCurrentDirectory(string currentDirectory);
42+
void WriteAllBytes(string path, byte[] bytes);
43+
void WriteAllLines(string path, string[] contents);
44+
void WriteAllText(string path, string contents);
45+
void WriteAllText(string path, string contents, Encoding encoding);
46+
void WriteLines(string path, string[] contents);
47+
48+
char DirectorySeparatorChar { get; }
49+
}
50+
51+
52+
public class FileSystem : IFileSystem
1053
{
1154
private string currentDirectory;
1255

1356
public FileSystem()
14-
{
15-
}
57+
{ }
1658

1759
/// <summary>
1860
/// Initialize the filesystem object with the path passed in set as the current directory
1961
/// </summary>
2062
/// <param name="directory">Current directory</param>
2163
public FileSystem(string directory)
2264
{
23-
this.currentDirectory = directory;
65+
currentDirectory = directory;
2466
}
2567

2668
public void SetCurrentDirectory(string directory)
2769
{
28-
Debug.Assert(Path.IsPathRooted(directory));
29-
this.currentDirectory = directory;
70+
if (!Path.IsPathRooted(directory))
71+
throw new ArgumentException("SetCurrentDirectory requires a rooted path", "directory");
72+
currentDirectory = directory;
3073
}
3174

3275
public bool FileExists(string filename)
3376
{
3477
return File.Exists(filename);
3578
}
3679

37-
public long FileLength(string path)
38-
{
39-
var fileInfo = new FileInfo(path);
40-
return fileInfo.Length;
41-
}
42-
4380
public IEnumerable<string> GetDirectories(string path)
4481
{
4582
return Directory.GetDirectories(path);
@@ -121,12 +158,17 @@ public IEnumerable<string> GetFiles(string path, string pattern, SearchOption se
121158
return Directory.GetFiles(path, pattern, searchOption);
122159
}
123160

161+
public byte[] ReadAllBytes(string path)
162+
{
163+
return File.ReadAllBytes(path);
164+
}
165+
124166
public void WriteAllBytes(string path, byte[] bytes)
125167
{
126168
File.WriteAllBytes(path, bytes);
127169
}
128170

129-
public void CreateDirectory(string toString)
171+
public void DirectoryCreate(string toString)
130172
{
131173
Directory.CreateDirectory(toString);
132174
}
@@ -173,11 +215,6 @@ public void WriteAllText(string path, string contents, Encoding encoding)
173215
File.WriteAllText(path, contents, encoding);
174216
}
175217

176-
public byte[] ReadAllBytes(string path)
177-
{
178-
return File.ReadAllBytes(path);
179-
}
180-
181218
public string ReadAllText(string path)
182219
{
183220
return File.ReadAllText(path);
@@ -198,9 +235,13 @@ public string[] ReadAllLines(string path)
198235
return File.ReadAllLines(path);
199236
}
200237

201-
public char DirectorySeparatorChar
238+
public void WriteLines(string path, string[] contents)
202239
{
203-
get { return Path.DirectorySeparatorChar; }
240+
using (var fs = File.AppendText(path))
241+
{
242+
foreach (var line in contents)
243+
fs.WriteLine(line);
244+
}
204245
}
205246

206247
public string GetRandomFileName()
@@ -217,5 +258,10 @@ public Stream OpenWrite(string path, FileMode mode)
217258
{
218259
return new FileStream(path, mode);
219260
}
261+
262+
public char DirectorySeparatorChar
263+
{
264+
get { return Path.DirectorySeparatorChar; }
265+
}
220266
}
221-
}
267+
}

src/GitHub.Api/IO/FileSystemHelpers.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,19 @@ static class FileSystemHelpers
77
{
88
public static string FindCommonPath(IEnumerable<string> paths)
99
{
10-
var parentPaths = paths.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.ToNPath().Parent);
11-
if (!parentPaths.Any())
10+
var parentPaths = paths.Where(s => !string.IsNullOrEmpty(s)).Select(s => s.ToNPath().Parent).ToArray();
11+
if (parentPaths.Count() <= 1)
1212
return null;
1313

14-
var parentsArray = parentPaths.ToArray();
15-
var maxDepth = parentsArray.Max(path => path.Depth);
16-
var deepestPath = parentsArray.First(path => path.Depth == maxDepth);
14+
var maxDepth = parentPaths.Max(path => path.Depth);
15+
var deepestPath = parentPaths.First(path => path.Depth == maxDepth);
1716

1817
var commonParent = deepestPath;
19-
foreach (var path in parentsArray)
18+
foreach (var path in parentPaths)
2019
{
21-
var cp = path.Elements.Any() ? commonParent.GetCommonParent(path) : null;
22-
if (cp != null)
23-
commonParent = cp;
24-
else
25-
{
26-
commonParent = null;
20+
commonParent = path.Elements.Any() ? commonParent.GetCommonParent(path) : NPath.Default;
21+
if (!commonParent.IsInitialized)
2722
break;
28-
}
2923
}
3024
return commonParent;
3125
}

src/GitHub.Api/IO/IFileSystem.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)