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 JetBrains . Annotations ;
4- using System ;
5- using System . Collections . Generic ;
6- using System . IO ;
7- using System . Linq ;
8-
9- namespace PostSharp . Engineering . BuildTools . Docker ;
10-
11- [ PublicAPI ]
12- public sealed class DotNetComponent : ContainerComponent
13- {
14- public string Version { get ; }
15-
16- public Version ? ParsedVersion { get ; }
17-
18- public DotNetComponentKind DotNetComponentKind { get ; }
19-
20- public DotNetComponent ( string version , DotNetComponentKind dotNetComponentKind )
21- {
22- this . Version = version ;
23- this . DotNetComponentKind = dotNetComponentKind ;
24-
25- var v = this . Version . Split ( "-" ) [ 0 ] ;
26-
27- if ( System . Version . TryParse ( v , out var parsedVersion ) )
28- {
29- this . ParsedVersion = parsedVersion ;
30- }
31- }
32-
33- public override string Name => $ "Install .NET { this . DotNetComponentKind } { this . Version } ";
34-
35- public override string Key => $ "{ nameof ( DotNetComponent ) } :{ this . DotNetComponentKind } :{ this . Version } ";
36-
37- public override ContainerComponentKind Kind => ContainerComponentKind . DotNet ;
38-
39- public override void AddRequirements ( IReadOnlyList < ContainerComponent > components , Action < ContainerComponent > add )
40- {
41- if ( ! components . OfType < DotNetInstallerComponent > ( ) . Any ( ) )
42- {
43- add ( new DotNetInstallerComponent ( ) ) ;
44- }
45- }
46-
47- public override void WriteDockerfile ( TextWriter writer , ContainerOperatingSystem operatingSystem )
48- {
49- // Run script directly since we're already in a PowerShell shell
50- if ( this . DotNetComponentKind == DotNetComponentKind . Sdk )
51- {
52- writer . WriteLine (
53- $ """
54- RUN & .\dotnet-install.ps1 -Version { this . Version } -InstallDir 'C:\Program Files\dotnet'
55- """ ) ;
56- }
57- else
58- {
59- var runtime = this . DotNetComponentKind switch
60- {
61- DotNetComponentKind . DotNetRuntime => "dotnet" ,
62- DotNetComponentKind . WindowsDesktopRuntime => "windowsdesktop" ,
63- DotNetComponentKind . AspNetCoreRuntime => "aspnetcore" ,
64- _ => throw new InvalidOperationException ( )
65- } ;
66-
67- writer . WriteLine (
68- $ """
69- RUN & .\dotnet-install.ps1 -Version { this . Version } -Runtime { runtime } -InstallDir 'C:\Program Files\dotnet'
70- """ ) ;
71- }
72- }
73-
74- public override string ToString ( ) => $ "{ this . Kind } { this . DotNetComponentKind } { this . Version } ";
75-
76- public override int CompareTo ( ContainerComponent ? other )
77- {
78- var compareBase = base . CompareTo ( other ) ;
79-
80- if ( compareBase != 0 )
81- {
82- return compareBase ;
83- }
84-
85- var otherDotNetComponent = ( DotNetComponent ) other ! ;
86-
87- // Compare the version number.
88- if ( this . ParsedVersion != null && otherDotNetComponent . ParsedVersion != null )
89- {
90- var compareParsedVersion = this . ParsedVersion . CompareTo ( otherDotNetComponent . ParsedVersion ) ;
91-
92- if ( compareParsedVersion != 0 )
93- {
94- return compareParsedVersion ;
95- }
96- }
97-
98- // Compare the string part of the version number.
99- return - string . Compare ( this . Version , otherDotNetComponent . Version , StringComparison . Ordinal ) ;
100- }
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 JetBrains . Annotations ;
4+ using System ;
5+ using System . Collections . Generic ;
6+ using System . IO ;
7+ using System . Linq ;
8+
9+ namespace PostSharp . Engineering . BuildTools . Docker ;
10+
11+ [ PublicAPI ]
12+ public sealed class DotNetComponent : ContainerComponent
13+ {
14+ public string Version { get ; }
15+
16+ public Version ? ParsedVersion { get ; }
17+
18+ public DotNetComponentKind DotNetComponentKind { get ; }
19+
20+ public DotNetComponent ( string version , DotNetComponentKind dotNetComponentKind )
21+ {
22+ this . Version = version ;
23+ this . DotNetComponentKind = dotNetComponentKind ;
24+
25+ var v = this . Version . Split ( "-" ) [ 0 ] ;
26+
27+ if ( System . Version . TryParse ( v , out var parsedVersion ) )
28+ {
29+ this . ParsedVersion = parsedVersion ;
30+ }
31+ }
32+
33+ public override string Name => $ "Install .NET { this . DotNetComponentKind } { this . Version } ";
34+
35+ public override string Key => $ "{ nameof ( DotNetComponent ) } :{ this . DotNetComponentKind } :{ this . Version } ";
36+
37+ public override ContainerComponentKind Kind => ContainerComponentKind . DotNet ;
38+
39+ public override void AddRequirements ( IReadOnlyList < ContainerComponent > components , Action < ContainerComponent > add )
40+ {
41+ if ( ! components . OfType < DotNetInstallerComponent > ( ) . Any ( ) )
42+ {
43+ add ( new DotNetInstallerComponent ( ) ) ;
44+ }
45+
46+ if ( this . DotNetComponentKind == DotNetComponentKind . Sdk && ! components . OfType < DotNetDumpComponent > ( ) . Any ( ) )
47+ {
48+ add ( new DotNetDumpComponent ( ) ) ;
49+ }
50+ }
51+
52+ public override void WriteDockerfile ( TextWriter writer , ContainerOperatingSystem operatingSystem )
53+ {
54+ // Run script directly since we're already in a PowerShell shell
55+ if ( this . DotNetComponentKind == DotNetComponentKind . Sdk )
56+ {
57+ writer . WriteLine (
58+ $ """
59+ RUN & .\dotnet-install.ps1 -Version { this . Version } -InstallDir 'C:\Program Files\dotnet'
60+ """ ) ;
61+ }
62+ else
63+ {
64+ var runtime = this . DotNetComponentKind switch
65+ {
66+ DotNetComponentKind . DotNetRuntime => "dotnet" ,
67+ DotNetComponentKind . WindowsDesktopRuntime => "windowsdesktop" ,
68+ DotNetComponentKind . AspNetCoreRuntime => "aspnetcore" ,
69+ _ => throw new InvalidOperationException ( )
70+ } ;
71+
72+ writer . WriteLine (
73+ $ """
74+ RUN & .\dotnet-install.ps1 -Version { this . Version } -Runtime { runtime } -InstallDir 'C:\Program Files\dotnet'
75+ """ ) ;
76+ }
77+ }
78+
79+ public override string ToString ( ) => $ "{ this . Kind } { this . DotNetComponentKind } { this . Version } ";
80+
81+ public override int CompareTo ( ContainerComponent ? other )
82+ {
83+ var compareBase = base . CompareTo ( other ) ;
84+
85+ if ( compareBase != 0 )
86+ {
87+ return compareBase ;
88+ }
89+
90+ var otherDotNetComponent = ( DotNetComponent ) other ! ;
91+
92+ // Compare the version number.
93+ if ( this . ParsedVersion != null && otherDotNetComponent . ParsedVersion != null )
94+ {
95+ var compareParsedVersion = this . ParsedVersion . CompareTo ( otherDotNetComponent . ParsedVersion ) ;
96+
97+ if ( compareParsedVersion != 0 )
98+ {
99+ return compareParsedVersion ;
100+ }
101+ }
102+
103+ // Compare the string part of the version number.
104+ return - string . Compare ( this . Version , otherDotNetComponent . Version , StringComparison . Ordinal ) ;
105+ }
101106}
0 commit comments