1- using GitHub . Unity ;
1+ using System ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
43using System . IO ;
54using System . Text ;
65
76namespace 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+ }
0 commit comments