Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an overload for the WithDirectoryPath method #569

Conversation

haridasnykiel
Copy link

This overload will aloow the user to select the commondirectory path and add the relative path to the common path selected.

Copy link
Collaborator

@HofmeisterAn HofmeisterAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Detecting "common" paths among various projects seams a bit trick. There is probably no reliable (100%) implementation. We need to guess sometimes.

What do you think about this:

/// <summary>
/// Resolves common directory paths.
/// </summary>
[PublicAPI]
public readonly struct CommonDirectoryPath
{
  /// <summary>
  /// Initializes a new instance of the <see cref="CommonDirectoryPath" /> struct.
  /// </summary>
  /// <param name="directoryPath">The directory path.</param>
  [PublicAPI]
  public CommonDirectoryPath(string directoryPath)
  {
    this.DirectoryPath = directoryPath;
  }

  /// <summary>
  /// Gets the directory path.
  /// </summary>
  [PublicAPI]
  public string DirectoryPath { get; }

  /// <summary>
  /// Resolves the first Git directory upwards the directory tree.
  /// </summary>
  /// <remarks>
  /// Start node is the caller file path directory. End node is the root directory.
  /// </remarks>
  /// <param name="filePath">The caller file path.</param>
  /// <returns>The first Git directory upwards the directory tree.</returns>
  /// <exception cref="DirectoryNotFoundException">Thrown when the Git directory was not found upwards the directory tree.</exception>
  [PublicAPI]
  public static CommonDirectoryPath GetGitDirectory([CallerFilePath, NotNull] string filePath = "")
  {
    return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), ".git"));
  }

  /// <summary>
  /// Resolves the first Visual Studio solution file upwards the directory tree.
  /// </summary>
  /// <remarks>
  /// Start node is the caller file path directory. End node is the root directory.
  /// </remarks>
  /// <param name="filePath">The caller file path.</param>
  /// <returns>The first Visual Studio solution file upwards the directory tree.</returns>
  /// <exception cref="DirectoryNotFoundException">Thrown when the Visual Studio solution file was not found upwards the directory tree.</exception>
  [PublicAPI]
  public static CommonDirectoryPath GetSolutionDirectory([CallerFilePath, NotNull] string filePath = "")
  {
    return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), "*.sln"));
  }

  /// <summary>
  /// Resolves the first CSharp project file upwards the directory tree.
  /// </summary>
  /// <remarks>
  /// Start node is the caller file path directory. End node is the root directory.
  /// </remarks>
  /// <param name="filePath">The caller file path.</param>
  /// <returns>The first CSharp project file upwards the directory tree.</returns>
  /// <exception cref="DirectoryNotFoundException">Thrown when the CSharp project file was not found upwards the directory tree.</exception>
  [PublicAPI]
  public static CommonDirectoryPath GetProjectDirectory([CallerFilePath, NotNull] string filePath = "")
  {
    return new CommonDirectoryPath(GetDirectoryPath(Path.GetDirectoryName(filePath), "*.csproj"));
  }

  /// <summary>
  /// Resolves the caller file path directory.
  /// </summary>
  /// <param name="filePath">The caller file path.</param>
  /// <returns>The caller file path directory.</returns>
  [PublicAPI]
  public static CommonDirectoryPath GetCallerFileDirectory([CallerFilePath, NotNull] string filePath = "")
  {
    return new CommonDirectoryPath(Path.GetDirectoryName(filePath));
  }

  private static string GetDirectoryPath(string path, string searchPattern)
  {
    return GetDirectoryPath(Directory.Exists(path) ? new DirectoryInfo(path) : null, searchPattern);
  }

  private static string GetDirectoryPath(DirectoryInfo path, string searchPattern)
  {
    if (path != null)
    {
      return path.EnumerateFileSystemInfos(searchPattern, SearchOption.TopDirectoryOnly).Any() ? path.FullName : GetDirectoryPath(path.Parent, searchPattern);
    }

    var message = $"Cannot find '{searchPattern}' and resolve the base directory in the directory tree.";
    throw new DirectoryNotFoundException(message);
  }
}
public sealed class GitHub
{
  [Fact]
  public void Issue569()
  {
    Assert.True(Directory.Exists(CommonDirectoryPath.GetGitDirectory().DirectoryPath));
    Assert.True(Directory.Exists(CommonDirectoryPath.GetSolutionDirectory().DirectoryPath));
    Assert.True(Directory.Exists(CommonDirectoryPath.GetProjectDirectory().DirectoryPath));
    Assert.True(Directory.Exists(CommonDirectoryPath.GetCallerFileDirectory().DirectoryPath));
    Assert.Throws<DirectoryNotFoundException>(() => CommonDirectoryPath.GetGitDirectory(Path.GetPathRoot(Directory.GetCurrentDirectory())));
  }
}

{
var rootDir = Path.GetDirectoryName(System.Reflection.Assembly.
GetExecutingAssembly().CodeBase);
var pattern = "(?<!fil)[A-Za-z]:\\\\+[\\S\\s]*?(?=\\\\+bin)";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pattern does not match on macOS.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will look into this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. MSDN recommends the location property.
  2. You don't need a regular expression. Just use string.Split. Something like that will work: locationBasePath.Split("bin", StringSplitOptions.RemoveEmptyEntries).First()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill try it, thanks, ill be sure to try it on mac for next time.

public IImageFromDockerfileBuilder WithDockerfileDirectory(CommonDirectoryPath commonDirectoryPath, string dockerfileDirectory)
{
var path = Path.Combine(commonDirectoryPath.DirectoryPath, dockerfileDirectory);
return this.MergeNewConfiguration(new ImageFromDockerfileConfiguration(dockerfileDirectory: path));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call overloaded method.

HofmeisterAn added a commit that referenced this pull request Sep 4, 2022
@HofmeisterAn
Copy link
Collaborator

fcbd4df, closes #558.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants