in src/Microsoft.VisualStudio.SlnGen/SlnHierarchy.cs [171:206]
private static SlnFolder GetRootFolder(IEnumerable<SlnProject> projects)
{
List<string> paths = projects.Where(i => !i.IsMainProject).Select(i => Directory.GetParent(i.FullPath).FullName).ToList();
if (!paths.Any())
{
throw new InvalidOperationException();
}
// TODO: Unit tests, optimize
string commonPath = string.Empty;
List<string> separatedPath = paths
.First(str => str.Length == paths.Max(st2 => st2.Length))
.Split(new[] { Path.DirectorySeparatorChar })
.ToList();
string nextPath = null;
foreach (string pathSegment in separatedPath.AsEnumerable())
{
if (commonPath.Length == 0 && paths.All(str => str.StartsWith(pathSegment, StringComparison.OrdinalIgnoreCase)))
{
commonPath = pathSegment + Path.DirectorySeparatorChar;
}
else if (paths.All(str => str.StartsWith(nextPath = $"{commonPath}{pathSegment}{Path.DirectorySeparatorChar}", StringComparison.OrdinalIgnoreCase)))
{
commonPath = nextPath;
}
else
{
break;
}
}
return new SlnFolder(commonPath.TrimEnd(Path.DirectorySeparatorChar));
}