Skip to content

Commit 6ec5af9

Browse files
committed
Style and validation touch-ups
1 parent c6836f2 commit 6ec5af9

File tree

4 files changed

+34
-21
lines changed

4 files changed

+34
-21
lines changed

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public void CanUseGitProjectRelativePathWithGitRepoRoot()
257257
};
258258

259259
string childProjectRelativeDir = "ChildProject1";
260-
string childProjectAbsoluteDir = string.Format(@"{0}\{1}", this.RepoPath, childProjectRelativeDir);
260+
string childProjectAbsoluteDir = Path.Combine(this.RepoPath, childProjectRelativeDir);
261261
this.WriteVersionFile(rootVersion);
262262
this.WriteVersionFile(projectVersion, childProjectRelativeDir);
263263

@@ -267,16 +267,16 @@ public void CanUseGitProjectRelativePathWithGitRepoRoot()
267267
var oracle = VersionOracle.Create(this.RepoPath, this.RepoPath, null, null);
268268
Assert.Equal("1.1", oracle.MajorMinorVersion.ToString());
269269

270-
// Check ChildProject with projectRelativeDir , with Version file. Child Project version will be used.
270+
// Check ChildProject with projectRelativeDir, with version file. Child project version will be used.
271271
oracle = VersionOracle.Create(childProjectAbsoluteDir, this.RepoPath, null, null, childProjectRelativeDir);
272272
Assert.Equal("2.2", oracle.MajorMinorVersion.ToString());
273273

274-
// Check ChildProject withOUT projectRelativeDir , with Version file. Child Project version will be used.
274+
// Check ChildProject withOUT projectRelativeDir, with Version file. Child project version will be used.
275275
oracle = VersionOracle.Create(childProjectAbsoluteDir, this.RepoPath);
276276
Assert.Equal("2.2", oracle.MajorMinorVersion.ToString());
277277

278278
// Check ChildProject withOUT Version file. Root version will be used.
279-
oracle = VersionOracle.Create(string.Format(@"{0}\{1}", this.RepoPath, "otherChildProject"), this.RepoPath, null, null, "otherChildProject");
279+
oracle = VersionOracle.Create(Path.Combine(this.RepoPath, "otherChildProject"), this.RepoPath, null, null, "otherChildProject");
280280
Assert.Equal("1.1", oracle.MajorMinorVersion.ToString());
281281
}
282282
}

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ public class VersionOracle
2727
/// <summary>
2828
/// Initializes a new instance of the <see cref="VersionOracle"/> class.
2929
/// </summary>
30-
/// <param name="projectDirectory"></param>
31-
/// <param name="gitRepoDirectory"></param>
32-
/// <param name="cloudBuild"></param>
33-
/// <param name="overrideBuildNumberOffset"></param>
34-
/// <param name="gitProjectRelativePath"></param>
35-
/// <returns></returns>
36-
public static VersionOracle Create(string projectDirectory, string gitRepoDirectory = null, ICloudBuild cloudBuild = null, int? overrideBuildNumberOffset = null, string gitProjectRelativePath = null)
30+
public static VersionOracle Create(string projectDirectory, string gitRepoDirectory = null, ICloudBuild cloudBuild = null, int? overrideBuildNumberOffset = null, string projectPathRelativeToGitRepoRoot = null)
3731
{
3832
Requires.NotNull(projectDirectory, nameof(projectDirectory));
3933
if (string.IsNullOrEmpty(gitRepoDirectory))
@@ -43,20 +37,20 @@ public static VersionOracle Create(string projectDirectory, string gitRepoDirect
4337

4438
using (var git = OpenGitRepo(gitRepoDirectory))
4539
{
46-
return new VersionOracle(projectDirectory, git, cloudBuild, overrideBuildNumberOffset, gitProjectRelativePath);
40+
return new VersionOracle(projectDirectory, git, cloudBuild, overrideBuildNumberOffset, projectPathRelativeToGitRepoRoot);
4741
}
4842
}
4943

5044
/// <summary>
5145
/// Initializes a new instance of the <see cref="VersionOracle"/> class.
5246
/// </summary>
53-
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null, string gitProjectRelativePath = null)
47+
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null, string projectPathRelativeToGitRepoRoot = null)
5448
{
5549
var repoRoot = repo?.Info?.WorkingDirectory?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
5650
var relativeRepoProjectDirectory = !string.IsNullOrWhiteSpace(repoRoot)
57-
? ( !string.IsNullOrEmpty(gitProjectRelativePath)
58-
? gitProjectRelativePath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
59-
: projectDirectory.Substring(repoRoot.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) )
51+
? (!string.IsNullOrEmpty(projectPathRelativeToGitRepoRoot)
52+
? projectPathRelativeToGitRepoRoot
53+
: projectDirectory.Substring(repoRoot.Length).TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar))
6054
: null;
6155

6256
var commit = repo?.Head.Commits.FirstOrDefault();

src/Nerdbank.GitVersioning.NuGet/build/Nerdbank.GitVersioning.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
BuildMetadata="@(BuildMetadata)"
6666
DefaultPublicRelease="$(PublicRelease)"
6767
GitRepoRoot="$(GitRepoRoot)"
68-
GitProjectRelativePath="$(GitProjectRelativePath)"
68+
ProjectPathRelativeToGitRepoRoot="$(ProjectPathRelativeToGitRepoRoot)"
6969
OverrideBuildNumberOffset="$(OverrideBuildNumberOffset)"
7070
TargetsPath="$(MSBuildThisFileDirectory)">
7171

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.Build.Framework;
99
using Microsoft.Build.Utilities;
1010
using MSBuildExtensionTask;
11+
using Validation;
1112

1213
public class GetBuildVersion : ContextAwareTask
1314
{
@@ -41,10 +42,18 @@ public GetBuildVersion()
4142
public string GitRepoRoot { get; set; }
4243

4344
/// <summary>
44-
/// Gets or sets the project path relative to Git Root. This is the case where GitRepoRoot might be different from where project folder is getting built
45-
/// If null or empty, default behavior will be to use Environment.CurrentDirectory and substring GitRepoRoot to search the GIT Tree for version.json
45+
/// Gets or sets the relative path from the <see cref="GitRepoRoot"/> to the directory under it that contains the project being built.
4646
/// </summary>
47-
public string GitProjectRelativePath { get; set; }
47+
/// <value>
48+
/// If not supplied, the directories from <see cref="GitRepoRoot"/> to <see cref="Environment.CurrentDirectory"/>
49+
/// will be searched for version.json.
50+
/// If supplied, the value <em>must</em> fall beneath the <see cref="GitRepoRoot"/> (i.e. this value should not contain "..\").
51+
/// </value>
52+
/// <remarks>
53+
/// This property is useful when the project that MSBuild is building is not found under <see cref="GitRepoRoot"/> such that the
54+
/// relative path can be calculated automatically.
55+
/// </remarks>
56+
public string ProjectPathRelativeToGitRepoRoot { get; set; }
4857

4958
/// <summary>
5059
/// Gets or sets the optional override build number offset.
@@ -166,9 +175,19 @@ protected override bool ExecuteInner()
166175
{
167176
try
168177
{
178+
if (!string.IsNullOrEmpty(this.ProjectPathRelativeToGitRepoRoot))
179+
{
180+
Requires.Argument(Path.IsPathRooted(this.ProjectPathRelativeToGitRepoRoot), nameof(this.ProjectPathRelativeToGitRepoRoot), "Path must be relative.");
181+
Requires.Argument(!(
182+
this.ProjectPathRelativeToGitRepoRoot.Contains(".." + Path.DirectorySeparatorChar) ||
183+
this.ProjectPathRelativeToGitRepoRoot.Contains(".." + Path.AltDirectorySeparatorChar)),
184+
nameof(this.ProjectPathRelativeToGitRepoRoot),
185+
"Path must not use ..\\");
186+
}
187+
169188
var cloudBuild = CloudBuild.Active;
170189
var overrideBuildNumberOffset = (this.OverrideBuildNumberOffset == int.MaxValue) ? (int?)null : this.OverrideBuildNumberOffset;
171-
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild, overrideBuildNumberOffset, this.GitProjectRelativePath);
190+
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild, overrideBuildNumberOffset, this.ProjectPathRelativeToGitRepoRoot);
172191
if (!string.IsNullOrEmpty(this.DefaultPublicRelease))
173192
{
174193
oracle.PublicRelease = string.Equals(this.DefaultPublicRelease, "true", StringComparison.OrdinalIgnoreCase);

0 commit comments

Comments
 (0)