|
3 | 3 | // https://github.com/nuke-build/nuke/blob/master/LICENSE |
4 | 4 |
|
5 | 5 | using System; |
| 6 | +using System.IO; |
6 | 7 | using System.Linq; |
7 | 8 | using System.Reflection; |
8 | 9 | using JetBrains.Annotations; |
@@ -49,25 +50,37 @@ public SolutionAttribute(string relativePath) |
49 | 50 |
|
50 | 51 | public override object GetValue(MemberInfo member, object instance) |
51 | 52 | { |
| 53 | + var solutionFile = TryGetSolutionFileFromNukeFile() ?? |
| 54 | + GetSolutionFileFromParametersFile(member); |
52 | 55 | var deserializer = typeof(SolutionSerializer).GetMethod(nameof(SolutionSerializer.DeserializeFromFile)).NotNull() |
53 | 56 | .MakeGenericMethod(member.GetMemberType()); |
54 | | - return deserializer.Invoke(obj: null, new object[] { GetSolutionFile(member) }); |
| 57 | + return deserializer.Invoke(obj: null, new object[] { solutionFile }); |
55 | 58 | } |
56 | 59 |
|
57 | 60 | // TODO: allow wildcard matching? [Solution("nuke-*.sln")] -- no globbing? |
58 | 61 | // TODO: for just [Solution] without parameter being passed, do wildcard search? |
59 | | - private string GetSolutionFile(MemberInfo member) |
| 62 | + private string GetSolutionFileFromParametersFile(MemberInfo member) |
60 | 63 | { |
61 | | - if (_relativePath != null) |
62 | | - return PathConstruction.Combine(NukeBuild.RootDirectory, _relativePath); |
| 64 | + return _relativePath != null |
| 65 | + ? PathConstruction.Combine(NukeBuild.RootDirectory, _relativePath) |
| 66 | + : EnvironmentInfo.GetParameter<AbsolutePath>(member).NotNull($"No solution file defined for '{member.Name}'."); |
| 67 | + } |
| 68 | + |
| 69 | + private string TryGetSolutionFileFromNukeFile() |
| 70 | + { |
| 71 | + var nukeFile = Path.Combine(NukeBuild.RootDirectory, Constants.NukeFileName); |
| 72 | + if (!File.Exists(nukeFile)) |
| 73 | + return null; |
| 74 | + |
| 75 | + var solutionFileRelative = File.ReadAllLines(nukeFile).ElementAtOrDefault(0); |
| 76 | + ControlFlow.Assert(solutionFileRelative != null && !solutionFileRelative.Contains(value: '\\'), |
| 77 | + $"First line of {Constants.NukeFileName} must provide solution path using UNIX separators"); |
| 78 | + |
| 79 | + var solutionFile = Path.GetFullPath(Path.Combine(NukeBuild.RootDirectory, solutionFileRelative)); |
| 80 | + ControlFlow.Assert(File.Exists(solutionFile), |
| 81 | + $"Solution file '{solutionFile}' provided via {Constants.NukeFileName} does not exist."); |
63 | 82 |
|
64 | | - var solutionFile = EnvironmentInfo.GetParameter<AbsolutePath>(member); |
65 | | - return solutionFile.NotNull( |
66 | | - new[] |
67 | | - { |
68 | | - $"No solution file defined for '{member.Name}'.", |
69 | | - $"Invoke: nuke --save-profile --{ParameterService.GetParameterDashedName(member)} <value>" |
70 | | - }.JoinNewLine()); |
| 83 | + return (AbsolutePath) solutionFile; |
71 | 84 | } |
72 | 85 | } |
73 | 86 | } |
0 commit comments