Skip to content

Commit 286eda9

Browse files
authored
Merge pull request #168 from benmerms/user/benmer/NewGetBuildVersionParameterForGitProjectRelativePath
Add optional GitRelativeProjectPath parameter to GetBuildVersion for …
2 parents 75e01a3 + 6ec5af9 commit 286eda9

File tree

4 files changed

+70
-10
lines changed

4 files changed

+70
-10
lines changed

src/NerdBank.GitVersioning.Tests/VersionOracleTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,41 @@ public void CanSetSemVer2ForNuGetPackageVersionNonPublicRelease()
242242
oracle.PublicRelease = false;
243243
Assert.Equal($"7.8.9-foo.25.g{this.CommitIdShort}", oracle.NuGetPackageVersion);
244244
}
245+
246+
[Fact]
247+
public void CanUseGitProjectRelativePathWithGitRepoRoot()
248+
{
249+
VersionOptions rootVersion = new VersionOptions
250+
{
251+
Version = SemanticVersion.Parse("1.1"),
252+
};
253+
254+
VersionOptions projectVersion = new VersionOptions
255+
{
256+
Version = SemanticVersion.Parse("2.2"),
257+
};
258+
259+
string childProjectRelativeDir = "ChildProject1";
260+
string childProjectAbsoluteDir = Path.Combine(this.RepoPath, childProjectRelativeDir);
261+
this.WriteVersionFile(rootVersion);
262+
this.WriteVersionFile(projectVersion, childProjectRelativeDir);
263+
264+
this.InitializeSourceControl();
265+
266+
// Check Root Version. Root version will be used
267+
var oracle = VersionOracle.Create(this.RepoPath, this.RepoPath, null, null);
268+
Assert.Equal("1.1", oracle.MajorMinorVersion.ToString());
269+
270+
// Check ChildProject with projectRelativeDir, with version file. Child project version will be used.
271+
oracle = VersionOracle.Create(childProjectAbsoluteDir, this.RepoPath, null, null, childProjectRelativeDir);
272+
Assert.Equal("2.2", oracle.MajorMinorVersion.ToString());
273+
274+
// Check ChildProject withOUT projectRelativeDir, with Version file. Child project version will be used.
275+
oracle = VersionOracle.Create(childProjectAbsoluteDir, this.RepoPath);
276+
Assert.Equal("2.2", oracle.MajorMinorVersion.ToString());
277+
278+
// Check ChildProject withOUT Version file. Root version will be used.
279+
oracle = VersionOracle.Create(Path.Combine(this.RepoPath, "otherChildProject"), this.RepoPath, null, null, "otherChildProject");
280+
Assert.Equal("1.1", oracle.MajorMinorVersion.ToString());
281+
}
245282
}

src/NerdBank.GitVersioning/VersionOracle.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +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-
/// <returns></returns>
35-
public static VersionOracle Create(string projectDirectory, string gitRepoDirectory = null, ICloudBuild cloudBuild = null, int? overrideBuildNumberOffset = null)
30+
public static VersionOracle Create(string projectDirectory, string gitRepoDirectory = null, ICloudBuild cloudBuild = null, int? overrideBuildNumberOffset = null, string projectPathRelativeToGitRepoRoot = null)
3631
{
3732
Requires.NotNull(projectDirectory, nameof(projectDirectory));
3833
if (string.IsNullOrEmpty(gitRepoDirectory))
@@ -42,18 +37,20 @@ public static VersionOracle Create(string projectDirectory, string gitRepoDirect
4237

4338
using (var git = OpenGitRepo(gitRepoDirectory))
4439
{
45-
return new VersionOracle(projectDirectory, git, cloudBuild, overrideBuildNumberOffset);
40+
return new VersionOracle(projectDirectory, git, cloudBuild, overrideBuildNumberOffset, projectPathRelativeToGitRepoRoot);
4641
}
4742
}
4843

4944
/// <summary>
5045
/// Initializes a new instance of the <see cref="VersionOracle"/> class.
5146
/// </summary>
52-
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null)
47+
public VersionOracle(string projectDirectory, LibGit2Sharp.Repository repo, ICloudBuild cloudBuild, int? overrideBuildNumberOffset = null, string projectPathRelativeToGitRepoRoot = null)
5348
{
5449
var repoRoot = repo?.Info?.WorkingDirectory?.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
5550
var relativeRepoProjectDirectory = !string.IsNullOrWhiteSpace(repoRoot)
56-
? 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))
5754
: null;
5855

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
BuildMetadata="@(BuildMetadata)"
6666
DefaultPublicRelease="$(PublicRelease)"
6767
GitRepoRoot="$(GitRepoRoot)"
68+
ProjectPathRelativeToGitRepoRoot="$(ProjectPathRelativeToGitRepoRoot)"
6869
OverrideBuildNumberOffset="$(OverrideBuildNumberOffset)"
6970
TargetsPath="$(MSBuildThisFileDirectory)">
7071

src/Nerdbank.GitVersioning.Tasks/GetBuildVersion.cs

Lines changed: 26 additions & 1 deletion
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
{
@@ -40,6 +41,20 @@ public GetBuildVersion()
4041
/// </summary>
4142
public string GitRepoRoot { get; set; }
4243

44+
/// <summary>
45+
/// Gets or sets the relative path from the <see cref="GitRepoRoot"/> to the directory under it that contains the project being built.
46+
/// </summary>
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; }
57+
4358
/// <summary>
4459
/// Gets or sets the optional override build number offset.
4560
/// </summary>
@@ -160,9 +175,19 @@ protected override bool ExecuteInner()
160175
{
161176
try
162177
{
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+
163188
var cloudBuild = CloudBuild.Active;
164189
var overrideBuildNumberOffset = (this.OverrideBuildNumberOffset == int.MaxValue) ? (int?)null : this.OverrideBuildNumberOffset;
165-
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild, overrideBuildNumberOffset);
190+
var oracle = VersionOracle.Create(Directory.GetCurrentDirectory(), this.GitRepoRoot, cloudBuild, overrideBuildNumberOffset, this.ProjectPathRelativeToGitRepoRoot);
166191
if (!string.IsNullOrEmpty(this.DefaultPublicRelease))
167192
{
168193
oracle.PublicRelease = string.Equals(this.DefaultPublicRelease, "true", StringComparison.OrdinalIgnoreCase);

0 commit comments

Comments
 (0)