| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
using System; |
| 5 |
using System.Diagnostics; |
| 6 |
using System.IO; |
| 7 |
|
| 8 |
namespace Microsoft.NuGet.Build.Tasks.Tests.Helpers |
| 9 |
{ |
| 10 |
public class TempDirectory |
| 11 |
{ |
| 12 |
private readonly string _path; |
| 13 |
private readonly TempRoot _root; |
| 14 |
|
| 15 |
protected TempDirectory(TempRoot root) |
| 16 |
: this(root.Root, root) |
| 17 |
{ |
| 18 |
} |
| 19 |
|
| 20 |
private TempDirectory(string path, TempRoot root) |
| 21 |
{ |
| 22 |
Debug.Assert(path != null); |
| 23 |
Debug.Assert(root != null); |
| 24 |
|
| 25 |
_path = path; |
| 26 |
_root = root; |
| 27 |
} |
| 28 |
|
| 29 |
private static string CreateUniqueDirectory(string basePath) |
| 30 |
{ |
| 31 |
while (true) |
| 32 |
{ |
| 33 |
string dir = System.IO.Path.Combine(basePath, Guid.NewGuid().ToString()); |
| 34 |
try |
| 35 |
{ |
| 36 |
Directory.CreateDirectory(dir); |
| 37 |
return dir; |
| 38 |
} |
| 39 |
catch (IOException) |
| 40 |
{ |
| 41 |
|
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public string Path |
| 47 |
{ |
| 48 |
get { return _path; } |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public TempFile CreateFile(string name) |
| 56 |
{ |
| 57 |
string filePath = System.IO.Path.Combine(_path, name); |
| 58 |
TempRoot.CreateStream(filePath); |
| 59 |
return _root.AddFile(new DisposableFile(filePath)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public TempFile CopyFile(string originalPath) |
| 66 |
{ |
| 67 |
string name = System.IO.Path.GetFileName(originalPath); |
| 68 |
string filePath = System.IO.Path.Combine(_path, name); |
| 69 |
File.Copy(originalPath, filePath); |
| 70 |
return _root.AddFile(new DisposableFile(filePath)); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public TempDirectory CreateDirectory(string name) |
| 78 |
{ |
| 79 |
string dirPath = System.IO.Path.Combine(_path, name); |
| 80 |
Directory.CreateDirectory(dirPath); |
| 81 |
return new TempDirectory(dirPath, _root); |
| 82 |
} |
| 83 |
|
| 84 |
public override string ToString() |
| 85 |
{ |
| 86 |
return _path; |
| 87 |
} |
| 88 |
} |
| 89 |
} |