Skip to content

Commit ecc5f4b

Browse files
gfraiteurclaude
andcommitted
Extract dependency path resolution to TeamCityHelper.GetRestoredDependencyVersionFile
Move the source-dependencies directory detection logic to a shared method and use it from both DependenciesConfigurationFile and DependencySource. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ae9bec7 commit ecc5f4b

File tree

3 files changed

+621
-606
lines changed

3 files changed

+621
-606
lines changed

src/PostSharp.Engineering.BuildTools/Build/Files/DependenciesConfigurationFile.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using PostSharp.Engineering.BuildTools.Build.MSBuild;
44
using PostSharp.Engineering.BuildTools.Dependencies;
55
using PostSharp.Engineering.BuildTools.Dependencies.Model;
6+
using PostSharp.Engineering.BuildTools.Tools.TeamCity;
67
using PostSharp.Engineering.BuildTools.Utilities;
78
using Spectre.Console;
89
using System;
@@ -228,23 +229,7 @@ bool TryGetBuildId( out string? versionFile1, out ICiBuildSpec? ciBuildSpec )
228229

229230
var dependencySource = DependencySource.CreateRestoredDependency( (CiBuildId) buildSpec, origin );
230231

231-
// On TeamCity, the repo may be checked out under source-dependencies/<repo>.
232-
// In that case, dependencies/ is at the work directory root (parent of source-dependencies).
233-
var dependenciesBaseDir = context.RepoDirectory;
234-
var parentDir = Path.GetDirectoryName( context.RepoDirectory );
235-
236-
if ( parentDir != null &&
237-
string.Equals( Path.GetFileName( parentDir ), "source-dependencies", StringComparison.OrdinalIgnoreCase ) )
238-
{
239-
dependenciesBaseDir = Path.GetDirectoryName( parentDir )!;
240-
}
241-
242-
dependencySource.VersionFile = Path.GetFullPath(
243-
Path.Combine(
244-
dependenciesBaseDir,
245-
"dependencies",
246-
name,
247-
name + ".version.props" ) );
232+
dependencySource.VersionFile = TeamCityHelper.GetRestoredDependencyVersionFile( context.RepoDirectory, name );
248233

249234
file._dependencies[name] = dependencySource;
250235
}
@@ -372,7 +357,10 @@ void AddImport( string file, bool required = true, string? label = null, bool ad
372357
// Transform file path for WSL if needed (for XML content)
373358
var transformedFile = TransformPath( file );
374359

375-
var element = new XElement( "Import", new XAttribute( "Project", transformedFile ), new XAttribute( "Condition", $"Exists( '{transformedFile}' )" ) );
360+
var element = new XElement(
361+
"Import",
362+
new XAttribute( "Project", transformedFile ),
363+
new XAttribute( "Condition", $"Exists( '{transformedFile}' )" ) );
376364

377365
if ( label != null )
378366
{
@@ -454,7 +442,8 @@ void WriteBuildServerSource()
454442

455443
if ( versionFile == null )
456444
{
457-
throw new InvalidOperationException( $"'{this.FilePath}': The VersionFile property of dependency '{dependency.Key}' is not set." );
445+
throw new InvalidOperationException(
446+
$"'{this.FilePath}': The VersionFile property of dependency '{dependency.Key}' is not set." );
458447
}
459448

460449
WriteBuildServerSource();
Lines changed: 107 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,108 @@
1-
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2-
3-
using PostSharp.Engineering.BuildTools.Build;
4-
using System;
5-
using System.Globalization;
6-
using System.IO;
7-
using System.Xml.Linq;
8-
using System.Xml.XPath;
9-
10-
namespace PostSharp.Engineering.BuildTools.Dependencies.Model
11-
{
12-
public sealed class DependencySource
13-
{
14-
/// <summary>
15-
/// Gets the NuGet version of the dependency, if <see cref="SourceKind"/> is set to <see cref="DependencySourceKind.Feed"/>.
16-
/// </summary>
17-
public string? Version { get; private init; }
18-
19-
public ICiBuildSpec? BuildServerSource { get; internal set; }
20-
21-
internal string? VersionFile { get; set; }
22-
23-
public DependencySourceKind SourceKind { get; private init; }
24-
25-
public DependencyConfigurationOrigin Origin { get; private init; }
26-
27-
public string? LocalPath { get; private init; }
28-
29-
public string GetResolvedLocalPath( BuildContext context, string dependencyKey )
30-
{
31-
if ( this.SourceKind != DependencySourceKind.Local )
32-
{
33-
throw new InvalidOperationException( "The dependency source must be local." );
34-
}
35-
36-
var localPath = this.LocalPath == null
37-
? Path.Combine(
38-
context.RepoDirectory,
39-
"..",
40-
dependencyKey )
41-
: Path.Combine( context.RepoDirectory, this.LocalPath );
42-
43-
return Path.GetFullPath( localPath );
44-
}
45-
46-
public static DependencySource CreateLocalDependency( DependencyConfigurationOrigin origin, string? path )
47-
=> new() { Origin = origin, SourceKind = DependencySourceKind.Local, LocalPath = path };
48-
49-
/// <summary>
50-
/// Creates a <see cref="DependencySource"/> that represents a build server artifact dependency that has been restored,
51-
/// and that exists under the 'dependencies' directory.
52-
/// </summary>
53-
public static DependencySource CreateRestoredDependency(
54-
BuildContext context,
55-
DependencyDefinition dependencyDefinition,
56-
DependencyConfigurationOrigin origin )
57-
{
58-
var path = Path.Combine( context.RepoDirectory, "dependencies", dependencyDefinition.Name, $"{dependencyDefinition.Name}.version.props" );
59-
var document = XDocument.Load( path );
60-
61-
var buildNumber = document.Root!.XPathSelectElement( $"/Project/PropertyGroup/{dependencyDefinition.NameWithoutDot}BuildNumber" )?.Value;
62-
var buildType = document.Root!.XPathSelectElement( $"/Project/PropertyGroup/{dependencyDefinition.NameWithoutDot}BuildType" )?.Value;
63-
64-
CiBuildId? buildId;
65-
66-
if ( string.IsNullOrEmpty( buildNumber ) || string.IsNullOrEmpty( buildType ) )
67-
{
68-
buildId = null;
69-
}
70-
else
71-
{
72-
buildId = new CiBuildId( int.Parse( buildNumber, CultureInfo.InvariantCulture ), buildType );
73-
}
74-
75-
return CreateRestoredDependency( buildId, origin );
76-
}
77-
78-
public static DependencySource CreateRestoredDependency( CiBuildId? buildId, DependencyConfigurationOrigin origin )
79-
=> new() { Origin = origin, SourceKind = DependencySourceKind.RestoredDependency, BuildServerSource = buildId };
80-
81-
public static DependencySource CreateFeed( string? version, DependencyConfigurationOrigin origin )
82-
=> new() { Origin = origin, SourceKind = DependencySourceKind.Feed, Version = version };
83-
84-
public static DependencySource CreateBuildServerSource( ICiBuildSpec source, DependencyConfigurationOrigin origin )
85-
=> new() { Origin = origin, SourceKind = DependencySourceKind.BuildServer, BuildServerSource = source };
86-
87-
public override string ToString()
88-
{
89-
switch ( this.SourceKind )
90-
{
91-
case DependencySourceKind.BuildServer or DependencySourceKind.RestoredDependency:
92-
return $"{this.SourceKind}, {this.BuildServerSource}, Origin={this.Origin}";
93-
94-
case DependencySourceKind.Local:
95-
{
96-
return $"{this.SourceKind}, Origin={this.Origin}";
97-
}
98-
99-
case DependencySourceKind.Feed:
100-
return $"{this.SourceKind}, {this.Version}, Origin={this.Origin}";
101-
102-
default:
103-
return "<Error>";
104-
}
105-
}
106-
}
1+
// Copyright (c) SharpCrafters s.r.o. See the LICENSE.md file in the root directory of this repository root for details.
2+
3+
using PostSharp.Engineering.BuildTools.Build;
4+
using PostSharp.Engineering.BuildTools.Tools.TeamCity;
5+
using System;
6+
using System.Globalization;
7+
using System.IO;
8+
using System.Xml.Linq;
9+
using System.Xml.XPath;
10+
11+
namespace PostSharp.Engineering.BuildTools.Dependencies.Model
12+
{
13+
public sealed class DependencySource
14+
{
15+
/// <summary>
16+
/// Gets the NuGet version of the dependency, if <see cref="SourceKind"/> is set to <see cref="DependencySourceKind.Feed"/>.
17+
/// </summary>
18+
public string? Version { get; private init; }
19+
20+
public ICiBuildSpec? BuildServerSource { get; internal set; }
21+
22+
internal string? VersionFile { get; set; }
23+
24+
public DependencySourceKind SourceKind { get; private init; }
25+
26+
public DependencyConfigurationOrigin Origin { get; private init; }
27+
28+
public string? LocalPath { get; private init; }
29+
30+
public string GetResolvedLocalPath( BuildContext context, string dependencyKey )
31+
{
32+
if ( this.SourceKind != DependencySourceKind.Local )
33+
{
34+
throw new InvalidOperationException( "The dependency source must be local." );
35+
}
36+
37+
var localPath = this.LocalPath == null
38+
? Path.Combine(
39+
context.RepoDirectory,
40+
"..",
41+
dependencyKey )
42+
: Path.Combine( context.RepoDirectory, this.LocalPath );
43+
44+
return Path.GetFullPath( localPath );
45+
}
46+
47+
public static DependencySource CreateLocalDependency( DependencyConfigurationOrigin origin, string? path )
48+
=> new() { Origin = origin, SourceKind = DependencySourceKind.Local, LocalPath = path };
49+
50+
/// <summary>
51+
/// Creates a <see cref="DependencySource"/> that represents a build server artifact dependency that has been restored,
52+
/// and that exists under the 'dependencies' directory.
53+
/// </summary>
54+
public static DependencySource CreateRestoredDependency(
55+
BuildContext context,
56+
DependencyDefinition dependencyDefinition,
57+
DependencyConfigurationOrigin origin )
58+
{
59+
var path = TeamCityHelper.GetRestoredDependencyVersionFile( context.RepoDirectory, dependencyDefinition.Name );
60+
var document = XDocument.Load( path );
61+
62+
var buildNumber = document.Root!.XPathSelectElement( $"/Project/PropertyGroup/{dependencyDefinition.NameWithoutDot}BuildNumber" )?.Value;
63+
var buildType = document.Root!.XPathSelectElement( $"/Project/PropertyGroup/{dependencyDefinition.NameWithoutDot}BuildType" )?.Value;
64+
65+
CiBuildId? buildId;
66+
67+
if ( string.IsNullOrEmpty( buildNumber ) || string.IsNullOrEmpty( buildType ) )
68+
{
69+
buildId = null;
70+
}
71+
else
72+
{
73+
buildId = new CiBuildId( int.Parse( buildNumber, CultureInfo.InvariantCulture ), buildType );
74+
}
75+
76+
return CreateRestoredDependency( buildId, origin );
77+
}
78+
79+
public static DependencySource CreateRestoredDependency( CiBuildId? buildId, DependencyConfigurationOrigin origin )
80+
=> new() { Origin = origin, SourceKind = DependencySourceKind.RestoredDependency, BuildServerSource = buildId };
81+
82+
public static DependencySource CreateFeed( string? version, DependencyConfigurationOrigin origin )
83+
=> new() { Origin = origin, SourceKind = DependencySourceKind.Feed, Version = version };
84+
85+
public static DependencySource CreateBuildServerSource( ICiBuildSpec source, DependencyConfigurationOrigin origin )
86+
=> new() { Origin = origin, SourceKind = DependencySourceKind.BuildServer, BuildServerSource = source };
87+
88+
public override string ToString()
89+
{
90+
switch ( this.SourceKind )
91+
{
92+
case DependencySourceKind.BuildServer or DependencySourceKind.RestoredDependency:
93+
return $"{this.SourceKind}, {this.BuildServerSource}, Origin={this.Origin}";
94+
95+
case DependencySourceKind.Local:
96+
{
97+
return $"{this.SourceKind}, Origin={this.Origin}";
98+
}
99+
100+
case DependencySourceKind.Feed:
101+
return $"{this.SourceKind}, {this.Version}, Origin={this.Origin}";
102+
103+
default:
104+
return "<Error>";
105+
}
106+
}
107+
}
107108
}

0 commit comments

Comments
 (0)