You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
abp/samples/DashboardDemo/test/DashboardDemo.Web.Tests/WebContentDirectoryFinder.cs

48 lines
1.7 KiB

using System;
using System.IO;
using System.Linq;
namespace DashboardDemo
{
/// <summary>
/// This class is used to find root path of the web project. Used for;
/// 1. unit tests (to find views).
/// 2. entity framework core command line commands (to find the conn string).
/// </summary>
public static class WebContentDirectoryFinder
{
public static string CalculateContentRootFolder()
{
var domainAssemblyDirectoryPath = Path.GetDirectoryName(typeof(DashboardDemoDomainModule).Assembly.Location);
if (domainAssemblyDirectoryPath == null)
{
throw new Exception($"Could not find location of {typeof(DashboardDemoDomainModule).Assembly.FullName} assembly!");
}
var directoryInfo = new DirectoryInfo(domainAssemblyDirectoryPath);
while (!DirectoryContains(directoryInfo.FullName, "DashboardDemo.sln"))
{
if (directoryInfo.Parent == null)
{
throw new Exception("Could not find content root folder!");
}
directoryInfo = directoryInfo.Parent;
}
var webFolder = Path.Combine(directoryInfo.FullName, $"src{Path.DirectorySeparatorChar}DashboardDemo.Web");
if (Directory.Exists(webFolder))
{
return webFolder;
}
throw new Exception("Could not find root folder of the web project!");
}
private static bool DirectoryContains(string directory, string fileName)
{
return Directory.GetFiles(directory).Any(filePath => string.Equals(Path.GetFileName(filePath), fileName));
}
}
}