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

Commit 3e4a2b0

Browse files
committed
Make the repo dir -> project dir mapping in IO operations more transparent
Every time we do an IO operation, we need to convert any relative paths we have to be based off of the real working directory. That's easier to do with an NPath object than with a string. This introduces a wrapper object that does the conversion and passes the call onto IFileSystem, so that NPath itself doesn't know about it, so it's easier to audit all calls and not miss any conversions. Context: NPath/FileSystem are working under the assumption that the working directory of the process is the repo root. This is a lie - Unity sets the working dir to the project path when it loads a project. We lie so it's easier to spawn our processes from the root of the git repository and to show all changes, even though they're outside the Unity project path.
1 parent 4c29fc1 commit 3e4a2b0

File tree

4 files changed

+153
-67
lines changed

4 files changed

+153
-67
lines changed

src/GitHub.Api/IO/FileSystem.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ public interface IFileSystem
2323
IEnumerable<string> GetDirectories(string path);
2424
IEnumerable<string> GetDirectories(string path, string pattern);
2525
IEnumerable<string> GetDirectories(string path, string pattern, SearchOption searchOption);
26-
string GetDirectoryName(string path);
2726
string GetFileNameWithoutExtension(string fileName);
2827
IEnumerable<string> GetFiles(string path);
2928
IEnumerable<string> GetFiles(string path, string pattern);
3029
IEnumerable<string> GetFiles(string path, string pattern, SearchOption searchOption);
3130
string GetFullPath(string path);
32-
string GetParentDirectory(string path);
3331
string GetRandomFileName();
3432
string GetTempPath();
3533
Stream OpenRead(string path);
@@ -53,7 +51,6 @@ public interface IFileSystem
5351
public class FileSystem : IFileSystem
5452
{
5553
private string currentDirectory;
56-
private string processDirectory;
5754

5855
public FileSystem()
5956
{ }
@@ -64,7 +61,7 @@ public FileSystem()
6461
/// <param name="directory">Current directory</param>
6562
public FileSystem(string directory)
6663
{
67-
processDirectory = currentDirectory = directory;
64+
currentDirectory = directory;
6865
}
6966

7067
public void SetCurrentDirectory(string directory)
@@ -104,11 +101,6 @@ public string GetFullPath(string path)
104101
return Path.GetFullPath(path);
105102
}
106103

107-
public string GetDirectoryName(string path)
108-
{
109-
return Path.GetDirectoryName(path);
110-
}
111-
112104
public bool DirectoryExists(string path)
113105
{
114106
return Directory.Exists(path);
@@ -120,11 +112,6 @@ public bool ExistingPathIsDirectory(string path)
120112
return (attr & FileAttributes.Directory) == FileAttributes.Directory;
121113
}
122114

123-
public string GetParentDirectory(string path)
124-
{
125-
return Directory.GetParent(path).FullName;
126-
}
127-
128115
public IEnumerable<string> GetDirectories(string path, string pattern)
129116
{
130117
return Directory.GetDirectories(path, pattern);

src/GitHub.Api/IO/NiceIO.cs

Lines changed: 145 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public bool Exists(NPath append)
308308
public bool DirectoryExists()
309309
{
310310
ThrowIfNotInitialized();
311-
return FileSystem.DirectoryExists(ToProcessDirectory().ToString());
311+
return FSWrapper.DirectoryExists(this);
312312
}
313313

314314
public bool DirectoryExists(string append)
@@ -324,13 +324,13 @@ public bool DirectoryExists(NPath append)
324324
ThrowIfNotInitialized();
325325
if (!append.IsInitialized)
326326
return DirectoryExists();
327-
return FileSystem.DirectoryExists(Combine(append).ToProcessDirectory().ToString());
327+
return FSWrapper.DirectoryExists(Combine(append));
328328
}
329329

330330
public bool FileExists()
331331
{
332332
ThrowIfNotInitialized();
333-
return FileSystem.FileExists(ToProcessDirectory().ToString());
333+
return FSWrapper.FileExists(this);
334334
}
335335

336336
public bool FileExists(string append)
@@ -346,7 +346,7 @@ public bool FileExists(NPath append)
346346
ThrowIfNotInitialized();
347347
if (!append.IsInitialized)
348348
return FileExists();
349-
return FileSystem.FileExists(Combine(append).ToProcessDirectory().ToString());
349+
return FSWrapper.FileExists(Combine(append));
350350
}
351351

352352
public string ExtensionWithDot
@@ -535,7 +535,7 @@ public bool IsRoot
535535

536536
public IEnumerable<NPath> Files(string filter, bool recurse = false)
537537
{
538-
return FileSystem.GetFiles(ToProcessDirectory().ToString(), filter, recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Select(s => new NPath(s));
538+
return FSWrapper.GetFiles(this, filter, recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Select(s => new NPath(s));
539539
}
540540

541541
public IEnumerable<NPath> Files(bool recurse = false)
@@ -555,7 +555,7 @@ public IEnumerable<NPath> Contents(bool recurse = false)
555555

556556
public IEnumerable<NPath> Directories(string filter, bool recurse = false)
557557
{
558-
return FileSystem.GetDirectories(ToProcessDirectory().ToString(), filter, recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Select(s => new NPath(s));
558+
return FSWrapper.GetDirectories(this, filter, recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Select(s => new NPath(s));
559559
}
560560

561561
public IEnumerable<NPath> Directories(bool recurse = false)
@@ -572,7 +572,7 @@ public NPath CreateFile()
572572
ThrowIfRelative();
573573
ThrowIfRoot();
574574
EnsureParentDirectoryExists();
575-
FileSystem.WriteAllBytes(ToProcessDirectory().ToString(), new byte[0]);
575+
FSWrapper.WriteAllBytes(this, new byte[0]);
576576
return this;
577577
}
578578

@@ -597,7 +597,7 @@ public NPath CreateDirectory()
597597
if (IsRoot)
598598
throw new NotSupportedException("CreateDirectory is not supported on a root level directory because it would be dangerous:" + ToString());
599599

600-
FileSystem.DirectoryCreate(ToProcessDirectory().ToString());
600+
FSWrapper.DirectoryCreate(this);
601601
return this;
602602
}
603603

@@ -666,7 +666,7 @@ NPath CopyWithDeterminedDestination(NPath absoluteDestination, Func<NPath, bool>
666666

667667
absoluteDestination.EnsureParentDirectoryExists();
668668

669-
FileSystem.FileCopy(ToProcessDirectory().ToString(), absoluteDestination.ToString(), true);
669+
FSWrapper.FileCopy(this, absoluteDestination, true);
670670
return absoluteDestination;
671671
}
672672

@@ -698,11 +698,11 @@ public void Delete(DeleteMode deleteMode = DeleteMode.Normal)
698698
{
699699
if (isFile)
700700
{
701-
FileSystem.FileDelete(ToProcessDirectory().ToString());
701+
FSWrapper.FileDelete(this);
702702
}
703703
else
704704
{
705-
FileSystem.DirectoryDelete(ToProcessDirectory().ToString(), true);
705+
FSWrapper.DirectoryDelete(this, true);
706706
}
707707
}
708708
catch (IOException)
@@ -797,13 +797,13 @@ public NPath Move(NPath dest)
797797
{
798798
dest.DeleteIfExists();
799799
dest.EnsureParentDirectoryExists();
800-
FileSystem.FileMove(ToProcessDirectory().ToString(), dest.ToProcessDirectory().ToString());
800+
FSWrapper.FileMove(this, dest);
801801
return dest;
802802
}
803803

804804
if (DirectoryExists())
805805
{
806-
FileSystem.DirectoryMove(ToProcessDirectory().ToString(), dest.ToProcessDirectory().ToString());
806+
FSWrapper.DirectoryMove(this, dest);
807807
return dest;
808808
}
809809

@@ -814,64 +814,64 @@ public NPath WriteAllText(string contents)
814814
{
815815
ThrowIfNotInitialized();
816816
EnsureParentDirectoryExists();
817-
FileSystem.WriteAllText(ToProcessDirectory().ToString(), contents);
817+
FSWrapper.WriteAllText(this, contents);
818818
return this;
819819
}
820820

821821
public string ReadAllText()
822822
{
823823
ThrowIfNotInitialized();
824-
return FileSystem.ReadAllText(ToProcessDirectory().ToString());
824+
return FSWrapper.ReadAllText(this);
825825
}
826826

827827
public NPath WriteAllText(string contents, Encoding encoding)
828828
{
829829
ThrowIfNotInitialized();
830830
EnsureParentDirectoryExists();
831-
FileSystem.WriteAllText(ToProcessDirectory().ToString(), contents, encoding);
831+
FSWrapper.WriteAllText(this, contents, encoding);
832832
return this;
833833
}
834834

835835
public string ReadAllText(Encoding encoding)
836836
{
837837
ThrowIfNotInitialized();
838-
return FileSystem.ReadAllText(ToProcessDirectory().ToString(), encoding);
838+
return FSWrapper.ReadAllText(this, encoding);
839839
}
840840

841841
public NPath WriteLines(string[] contents)
842842
{
843843
ThrowIfNotInitialized();
844844
EnsureParentDirectoryExists();
845-
FileSystem.WriteLines(ToProcessDirectory().ToString(), contents);
845+
FSWrapper.WriteLines(this, contents);
846846
return this;
847847
}
848848

849849
public NPath WriteAllLines(string[] contents)
850850
{
851851
ThrowIfNotInitialized();
852852
EnsureParentDirectoryExists();
853-
FileSystem.WriteAllLines(ToProcessDirectory().ToString(), contents);
853+
FSWrapper.WriteAllLines(this, contents);
854854
return this;
855855
}
856856

857857
public string[] ReadAllLines()
858858
{
859859
ThrowIfNotInitialized();
860-
return FileSystem.ReadAllLines(ToProcessDirectory().ToString());
860+
return FSWrapper.ReadAllLines(this);
861861
}
862862

863863
public NPath WriteAllBytes(byte[] contents)
864864
{
865865
ThrowIfNotInitialized();
866866
EnsureParentDirectoryExists();
867-
FileSystem.WriteAllBytes(ToProcessDirectory().ToString(), contents);
867+
FSWrapper.WriteAllBytes(this, contents);
868868
return this;
869869
}
870870

871871
public byte[] ReadAllBytes()
872872
{
873873
ThrowIfNotInitialized();
874-
return FileSystem.ReadAllBytes(ToProcessDirectory().ToString());
874+
return FSWrapper.ReadAllBytes(this);
875875
}
876876

877877

@@ -1100,18 +1100,21 @@ public static IFileSystem FileSystem
11001100
{
11011101
if (_fileSystem == null)
11021102
#if UNITY_4 || UNITY_5 || UNITY_5_3_OR_NEWER
1103-
_fileSystem = new FileSystem(UnityEngine.Application.dataPath);
1103+
FileSystem = new FileSystem(UnityEngine.Application.dataPath);
11041104
#else
1105-
_fileSystem = new FileSystem(Directory.GetCurrentDirectory());
1105+
FileSystem = new FileSystem(Directory.GetCurrentDirectory());
11061106
#endif
11071107
return _fileSystem;
11081108
}
11091109
set
11101110
{
11111111
_fileSystem = value;
1112+
FSWrapper = new FSWrapper(value);
11121113
}
11131114
}
11141115

1116+
private static FSWrapper FSWrapper { get; set; }
1117+
11151118
private static bool? _isUnix;
11161119
internal static bool IsUnix
11171120
{
@@ -1228,4 +1231,122 @@ public enum DeleteMode
12281231
Normal,
12291232
Soft
12301233
}
1234+
1235+
1236+
class FSWrapper
1237+
{
1238+
private readonly IFileSystem fileSystem;
1239+
1240+
public FSWrapper(IFileSystem fileSystem)
1241+
{
1242+
this.fileSystem = fileSystem;
1243+
}
1244+
1245+
public void DirectoryCreate(NPath path)
1246+
{
1247+
fileSystem.DirectoryCreate(path.ToProcessDirectory().ToString());
1248+
}
1249+
1250+
public void DirectoryDelete(NPath path, bool recursive)
1251+
{
1252+
fileSystem.DirectoryDelete(path.ToProcessDirectory().ToString(), recursive);
1253+
}
1254+
1255+
public bool DirectoryExists(NPath path)
1256+
{
1257+
return fileSystem.DirectoryExists(path.ToProcessDirectory().ToString());
1258+
}
1259+
public void DirectoryMove(NPath from, NPath to)
1260+
{
1261+
fileSystem.DirectoryMove(from.ToProcessDirectory().ToString(), to.ToProcessDirectory().ToString());
1262+
}
1263+
public bool ExistingPathIsDirectory(NPath path)
1264+
{
1265+
return fileSystem.ExistingPathIsDirectory(path.ToProcessDirectory().ToString());
1266+
}
1267+
public void FileCopy(NPath from, NPath to, bool overwrite)
1268+
{
1269+
fileSystem.FileCopy(from.ToProcessDirectory().ToString(), to.ToProcessDirectory().ToString(), overwrite);
1270+
}
1271+
public void FileDelete(NPath path)
1272+
{
1273+
fileSystem.FileDelete(path.ToProcessDirectory().ToString());
1274+
}
1275+
public bool FileExists(NPath path)
1276+
{
1277+
return fileSystem.FileExists(path.ToProcessDirectory().ToString());
1278+
}
1279+
public void FileMove(NPath from, NPath to)
1280+
{
1281+
fileSystem.FileMove(from.ToProcessDirectory().ToString(), to.ToProcessDirectory().ToString());
1282+
}
1283+
public IEnumerable<string> GetDirectories(NPath path)
1284+
{
1285+
return fileSystem.GetDirectories(path.ToProcessDirectory().ToString());
1286+
}
1287+
public IEnumerable<string> GetDirectories(NPath path, string pattern)
1288+
{
1289+
return fileSystem.GetDirectories(path.ToProcessDirectory().ToString(), pattern);
1290+
}
1291+
public IEnumerable<string> GetDirectories(NPath path, string pattern, SearchOption searchOption)
1292+
{
1293+
return fileSystem.GetDirectories(path.ToProcessDirectory().ToString(), pattern, searchOption);
1294+
}
1295+
public IEnumerable<string> GetFiles(NPath path)
1296+
{
1297+
return fileSystem.GetFiles(path.ToProcessDirectory().ToString());
1298+
}
1299+
public IEnumerable<string> GetFiles(NPath path, string pattern)
1300+
{
1301+
return fileSystem.GetFiles(path.ToProcessDirectory().ToString(), pattern);
1302+
}
1303+
public IEnumerable<string> GetFiles(NPath path, string pattern, SearchOption searchOption)
1304+
{
1305+
return fileSystem.GetFiles(path.ToProcessDirectory().ToString(), pattern, searchOption);
1306+
}
1307+
public Stream OpenRead(NPath path)
1308+
{
1309+
return fileSystem.OpenRead(path.ToProcessDirectory().ToString());
1310+
}
1311+
public Stream OpenWrite(NPath path, FileMode mode)
1312+
{
1313+
return fileSystem.OpenWrite(path.ToProcessDirectory().ToString(), mode);
1314+
}
1315+
public byte[] ReadAllBytes(NPath path)
1316+
{
1317+
return fileSystem.ReadAllBytes(path.ToProcessDirectory().ToString());
1318+
}
1319+
public string[] ReadAllLines(NPath path)
1320+
{
1321+
return fileSystem.ReadAllLines(path.ToProcessDirectory().ToString());
1322+
}
1323+
public string ReadAllText(NPath path)
1324+
{
1325+
return fileSystem.ReadAllText(path.ToProcessDirectory().ToString());
1326+
}
1327+
public string ReadAllText(NPath path, Encoding encoding)
1328+
{
1329+
return fileSystem.ReadAllText(path.ToProcessDirectory().ToString(), encoding);
1330+
}
1331+
public void WriteAllBytes(NPath path, byte[] bytes)
1332+
{
1333+
fileSystem.WriteAllBytes(path.ToProcessDirectory().ToString(), bytes);
1334+
}
1335+
public void WriteAllLines(NPath path, string[] contents)
1336+
{
1337+
fileSystem.WriteAllLines(path.ToProcessDirectory().ToString(), contents);
1338+
}
1339+
public void WriteAllText(NPath path, string contents)
1340+
{
1341+
fileSystem.WriteAllText(path.ToProcessDirectory().ToString(), contents);
1342+
}
1343+
public void WriteAllText(NPath path, string contents, Encoding encoding)
1344+
{
1345+
fileSystem.WriteAllText(path.ToProcessDirectory().ToString(), contents, encoding);
1346+
}
1347+
public void WriteLines(NPath path, string[] contents)
1348+
{
1349+
fileSystem.WriteLines(path.ToProcessDirectory().ToString(), contents);
1350+
}
1351+
}
12311352
}

src/tests/IntegrationTests/CachingClasses.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,12 @@ public void UpdateData(IRepositoryInfoCacheData data)
397397
isUpdated = true;
398398
}
399399

400+
if (currentHead != data.CurrentHead)
401+
{
402+
currentHead = data.CurrentHead;
403+
isUpdated = true;
404+
}
405+
400406
SaveData(now, isUpdated);
401407
}
402408

0 commit comments

Comments
 (0)