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

(#81) Fix global tool execution when trying to access a directory without permissions. #84

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions source/Nuke.Common/Git/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public static GitRepository FromLocalDirectory(string directory, string branch =
.Skip(count: 1)
.TakeWhile(x => !x.StartsWith("["))
.SingleOrDefault(x => x.StartsWithOrdinalIgnoreCase("url = "))
?.Split('=')[1];
?.Split('=')[1]
.Trim();
ControlFlow.Assert(url != null, $"Could not parse remote URL for '{remote}'.");

var (endpoint, identifier) = ParseUrl(url);
Expand All @@ -63,7 +64,7 @@ private static (string endpoint, string identifier) ParseUrl(string url)
var match = new[]
{
@"git@(?<endpoint>[^:/]+?)(:|/)(?<identifier>.+?)/?(\.git)?$",
@"^https://([^:]+:[^:@]+@)?(?<endpoint>[^/]+?)/(?<identifier>.+?)/?(\.git)?$"
@"^https?://([^:]+:[^:@]+@)?(?<endpoint>[^/]+?)/(?<identifier>.+?)/?(\.git)?$"
}
.Select(x => Regex.Match(url.Trim(), x))
.FirstOrDefault(x => x.Success);
Expand Down
42 changes: 42 additions & 0 deletions source/Nuke.Common/Utilities/DirectoryInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright Matthias Koch, Sebastian Karasek 2018.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Nuke.Common.Utilities
{
public static class DirectoryInfoExtensions
{
/// <summary>
/// Returns an enumerable collection of file information that matches a search pattern in all directories of a given
/// depth.
/// <para>System and hidden directories are skipped.</para>
/// </summary>
/// <param name="directoryInfo">The root directory to start the search.</param>
/// <param name="searchPattern">
/// The search string to match against the names of files. This parameter can contain a combination of valid literal
/// path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions.
/// The default pattern is "*", which returns all files.
/// </param>
/// <param name="maxDepth">The maximum depth to descend into directories to look for files.</param>
/// <returns>An enumerable collection of files that matches <paramref name="searchPattern" />.</returns>
/// <exception cref="ArgumentNullException"> <paramref name="searchPattern" /> is <see langword="null" />. </exception>
/// <exception cref="DirectoryNotFoundException">
/// The path encapsulated in the <see cref="DirectoryInfo" />
/// object is invalid, (for example, it is on an unmapped drive).
/// </exception>
public static IEnumerable<FileInfo> EnumerateFiles(this DirectoryInfo directoryInfo, string searchPattern, int maxDepth)
{
var searchDirectories = directoryInfo.EnumerateDirectories()
.Where(x => (x.Attributes & (FileAttributes.Hidden | FileAttributes.System)) == 0);

return searchDirectories
.SelectMany(x =>
x.EnumerateFiles(searchPattern).Concat(maxDepth == 0 ? new FileInfo[0] : x.EnumerateFiles(searchPattern, maxDepth - 1)));
}
}
}
4 changes: 3 additions & 1 deletion source/Nuke.GlobalTool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ private static void Handle(string[] args)

var currentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
var rootDirectory = FileSystemTasks.FindParentDirectory(currentDirectory, x => x.GetFiles(NukeBuild.ConfigurationFile).Any());
var buildScript = rootDirectory?.GetFiles($"build.{ScriptExtension}", SearchOption.AllDirectories).FirstOrDefault()?.FullName;
var buildScript = rootDirectory?
.EnumerateFiles($"build.{ScriptExtension}", maxDepth: 2)
.FirstOrDefault()?.FullName;

if (buildScript == null)
{
Expand Down