21 public static AbsolutePath
AsPath(
this string input) => AbsolutePath.Create(input);
26 public static AbsolutePath?
TryAsPath(
this string? input)
27 => !
string.IsNullOrWhiteSpace(input) && Path.IsPathRooted(input)
28 ? AbsolutePath.Create(input)
35 public static AbsolutePath?
TryAsRelativePath(
this string? input, AbsolutePath? root =
null)
36 =>
string.IsNullOrWhiteSpace(input)
38 : Path.IsPathRooted(input)
39 ? AbsolutePath.Create(input)
40 : (root ?? NukeBuild.RootDirectory) / input;
45 public static void Delete(
this AbsolutePath path)
47 if (path.FileExists()) path.DeleteFile();
48 if (path.DirectoryExists()) path.DeleteDirectory();
54 public static FileSystemInfo?
AsInfo(
this AbsolutePath path)
56 if (path.FileExists())
57 return new FileInfo(path);
58 if (path.DirectoryExists())
59 return new DirectoryInfo(path);
68 public static FileSystemInfo?
AsLinkInfo(
this AbsolutePath link)
70 var info = link.AsInfo();
71 return info?.LinkTarget ==
null ? null : info;
74 private static FileSystemInfo LinkBoilerplate(
75 AbsolutePath link, AbsolutePath real,
76 Action assertRealExists,
77 Func<string, string, FileSystemInfo> createLink
79 var info = link.AsLinkInfo();
80 var linkTarget = info?.ResolveLinkTarget(
true)?.FullName;
81 if (linkTarget == real)
83 Log.Information(
"Link already exists {1} <- {0}", link, real);
86 if (linkTarget !=
null)
88 Log.Warning(
"Existing link points to somewhere else {1} <- {0}", link, linkTarget);
89 Log.Warning(
" Replacing target with {0}", real);
92 else Log.Information(
"Linking {1} <- {0}", link, real);
95 Assert.False(link.FileExists() || link.DirectoryExists());
97 if (!link.Parent.DirectoryExists())
98 link.Parent.CreateDirectory();
100 var realRelative = link.Parent.GetRelativePathTo(real);
102 return createLink(link, realRelative);
110 public static FileSystemInfo
LinksFile(
this AbsolutePath link, AbsolutePath real)
113 () => Assert.FileExists(real),
114 File.CreateSymbolicLink
122 public static FileSystemInfo
LinkedByFile(
this AbsolutePath real, AbsolutePath link)
130 public static FileSystemInfo
LinksDirectory(
this AbsolutePath link, AbsolutePath real)
133 () => Assert.DirectoryExists(real),
134 Directory.CreateSymbolicLink
151 public static FileSystemInfo
Links(
this AbsolutePath link, AbsolutePath real)
153 Assert.True(real.FileExists() || real.DirectoryExists());
154 if (real.FileExists())
return link.LinksFile(real);
155 return link.LinksDirectory(real);
163 public static FileSystemInfo
LinkedBy(
this AbsolutePath real, AbsolutePath link)
164 =>
Links(link, real);
175 .Else(() => real.Copy(link, policy));
177 public static IEnumerable<AbsolutePath> SubTree(
this AbsolutePath origin, Func<AbsolutePath, bool>? filter =
null)
178 => origin.DescendantsAndSelf(d =>
179 from sd in d.GlobDirectories(
"*")
180 where filter?.Invoke(sd) ??
true
188 public static AbsolutePath
GetRoot(
this AbsolutePath path) => Path.GetPathRoot(path)!.AsPath();
190 public static bool LookAroundFor(Func<string, bool> predicate, out AbsolutePath? result, Func<AbsolutePath, bool>? directoryFilter =
null, AbsolutePath? rootDirectory =
null)
193 rootDirectory ??= NukeBuild.RootDirectory;
195 var parents = rootDirectory
196 .DescendantsAndSelf(d => d.Parent, d => Path.GetPathRoot(d) != d );
198 foreach(var p
in parents)
199 foreach(var f
in Directory.EnumerateFiles(p))
203 result = (AbsolutePath) f;
208 foreach(var p
in rootDirectory.SubTree(directoryFilter))
209 foreach(var f
in Directory.EnumerateFiles(p))
213 result = (AbsolutePath) f;
220 public static AbsolutePath? GetVersionSubfolder(
this AbsolutePath root,
string version)
222 if (Path.IsPathRooted(version))
224 return (AbsolutePath) version;
226 if ((root / version).DirectoryExists())
228 return root / version;
230 version = version.Contains(
'.') ? version : version +
".0";
231 if (Version.TryParse(version, out var semVersion))
233 var majorMinorPatch = $
"{semVersion.Major}.{semVersion.Minor}.{semVersion.Build}".Replace(
"-1",
"0");
234 var majorMinor = $
"{semVersion.Major}.{semVersion.Minor}".Replace(
"-1",
"0");
236 root.GlobDirectories(
"*").FirstOrDefault(d => d.Name.Contains(majorMinorPatch))
237 ?? root.GlobDirectories(
"*").FirstOrDefault(d => d.Name.Contains(majorMinor))
238 ?? root.GlobDirectories(
"*").FirstOrDefault(d => d.Name.Contains(semVersion.Major.ToString()));