dotnet-installer/test/Microsoft.DotNet.Tools.Tests.Utilities/TempFileSystem/TempRoot.cs
2015-12-14 17:39:29 -08:00

72 lines
2 KiB
C#

// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public sealed class TempRoot : IDisposable
{
private readonly List<IDisposable> _temps = new List<IDisposable>();
public static readonly string Root;
static TempRoot()
{
Root = Path.Combine(Path.GetTempPath(), "DotnetCLITests");
Directory.CreateDirectory(Root);
}
public void Dispose()
{
if (_temps != null)
{
DisposeAll(_temps);
_temps.Clear();
}
}
private static void DisposeAll(IEnumerable<IDisposable> temps)
{
foreach (var temp in temps)
{
try
{
if (temp != null)
{
temp.Dispose();
}
}
catch
{
// ignore
}
}
}
public TempDirectory CreateDirectory()
{
var dir = new DisposableDirectory(this);
_temps.Add(dir);
return dir;
}
public TempFile CreateFile(string prefix = null, string extension = null, string directory = null, [CallerFilePath]string callerSourcePath = null, [CallerLineNumber]int callerLineNumber = 0)
{
return AddFile(new DisposableFile(prefix, extension, directory, callerSourcePath, callerLineNumber));
}
public DisposableFile AddFile(DisposableFile file)
{
_temps.Add(file);
return file;
}
internal static void CreateStream(string fullPath)
{
using (var file = new FileStream(fullPath, FileMode.CreateNew)) { }
}
}
}