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