1+ // ---------------------------------------------------------------------------
2+ // Copyright (c) Hassan Habib & Shri Humrudha Jagathisun All rights reserved.
3+ // Licensed under the MIT License.
4+ // See License.txt in the project root for license information.
5+ // ---------------------------------------------------------------------------
6+
7+ using System . Collections . Generic ;
8+ using ADotNet . Models . Pipelines . GithubPipelines . DotNets ;
9+ using ADotNet . Models . Pipelines . GithubPipelines . DotNets . Tasks ;
10+ using ADotNet . Models . Pipelines . GithubPipelines . DotNets . Tasks . SetupDotNetTaskV1s ;
11+
12+ namespace ADotNet . Clients . Builders
13+ {
14+ /// <summary>
15+ /// A builder to create a job for a GitHub Actions workflow.
16+ /// </summary>
17+ public class JobBuilder
18+ {
19+ private readonly Job job ;
20+
21+ internal JobBuilder ( )
22+ {
23+ this . job = new Job
24+ {
25+ Steps = new List < GithubTask > ( ) ,
26+ EnvironmentVariables = null
27+ } ;
28+ }
29+
30+ /// <summary>
31+ /// Sets the name of the job.
32+ /// </summary>
33+ /// <param name="name">The name of the job.</param>
34+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
35+ public JobBuilder WithName ( string name )
36+ {
37+ this . job . Name = name ;
38+
39+ return this ;
40+ }
41+
42+ /// <summary>
43+ /// Specifies the machine on which the job will run.
44+ /// </summary>
45+ /// <param name="machine">The machine or environment to run the job on.</param>
46+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
47+ public JobBuilder RunsOn ( string machine )
48+ {
49+ this . job . RunsOn = machine ;
50+
51+ return this ;
52+ }
53+
54+ /// <summary>
55+ /// Adds an environment variable to the job.
56+ /// </summary>
57+ /// <param name="key">The key of the environment variable.</param>
58+ /// <param name="value">The value of the environment variable.</param>
59+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
60+ public JobBuilder AddEnvironmentVariable ( string key , string value )
61+ {
62+ this . job . EnvironmentVariables ??= new Dictionary < string , string > ( ) ;
63+
64+ this . job . EnvironmentVariables [ key ] = value ;
65+
66+ return this ;
67+ }
68+
69+ /// <summary>
70+ /// Adds multiple environment variables to the job.
71+ /// </summary>
72+ /// <param name="variables">A dictionary of environment variables to add.</param>
73+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
74+ public JobBuilder AddEnvironmentVariables ( Dictionary < string , string > variables )
75+ {
76+ this . job . EnvironmentVariables ??= new Dictionary < string , string > ( ) ;
77+
78+ foreach ( var variable in variables )
79+ {
80+ this . job . EnvironmentVariables [ variable . Key ] = variable . Value ;
81+ }
82+
83+ return this ;
84+ }
85+
86+ /// <summary>
87+ /// Adds a checkout step to the job.
88+ /// </summary>
89+ /// <param name="name">The name of the checkout step (default: "Check out").</param>
90+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
91+ public JobBuilder AddCheckoutStep ( string name = "Check out" )
92+ {
93+ this . job . Steps . Add ( new CheckoutTaskV2 { Name = name } ) ;
94+
95+ return this ;
96+ }
97+
98+ /// <summary>
99+ /// Adds a setup step for a specific .NET version to the job.
100+ /// </summary>
101+ /// <param name="version">The version of .NET to set up.</param>
102+ /// <param name="stepName">The name of the setup step (default: "Setup Dot Net Version").</param>
103+ /// <param name="includePrerelease">Specifies whether to include prerelease versions.</param>
104+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
105+ public JobBuilder AddSetupDotNetStep (
106+ string version ,
107+ string stepName = "Setup Dot Net Version" ,
108+ bool includePrerelease = false )
109+ {
110+ this . job . Steps . Add ( new SetupDotNetTaskV1
111+ {
112+ Name = stepName ,
113+ TargetDotNetVersion = new TargetDotNetVersion
114+ {
115+ DotNetVersion = version ,
116+ IncludePrerelease = includePrerelease
117+ }
118+ } ) ;
119+
120+ return this ;
121+ }
122+
123+ /// <summary>
124+ /// Adds a restore step to the job.
125+ /// </summary>
126+ /// <param name="name">The name of the restore step (default: "Restore").</param>
127+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
128+ public JobBuilder AddRestoreStep ( string name = "Restore" )
129+ {
130+ this . job . Steps . Add ( new RestoreTask { Name = name } ) ;
131+
132+ return this ;
133+ }
134+
135+ /// <summary>
136+ /// Adds a build step to the job.
137+ /// </summary>
138+ /// <param name="name">The name of the build step (default: "Build").</param>
139+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
140+ public JobBuilder AddBuildStep ( string name = "Build" )
141+ {
142+ this . job . Steps . Add ( new DotNetBuildTask { Name = name } ) ;
143+
144+ return this ;
145+ }
146+
147+ /// <summary>
148+ /// Adds a test step to the job.
149+ /// </summary>
150+ /// <param name="name">The name of the test step (default: "Test").</param>
151+ /// <param name="command">The command to execute the test
152+ /// (default: "dotnet test --no-build --verbosity normal").</param>
153+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
154+ public JobBuilder AddTestStep ( string name = "Test" , string command = null )
155+ {
156+ this . job . Steps . Add ( new TestTask
157+ {
158+ Name = name ,
159+ Run = command ?? "dotnet test --no-build --verbosity normal"
160+ } ) ;
161+
162+ return this ;
163+ }
164+
165+ /// <summary>
166+ /// Adds a generic step to the job with a custom command.
167+ /// </summary>
168+ /// <param name="name">The name of the step.</param>
169+ /// <param name="runCommand">The command to execute for this step.</param>
170+ /// <returns>The current instance of <see cref="JobBuilder"/>.</returns>
171+ public JobBuilder AddGenericStep ( string name , string runCommand )
172+ {
173+ this . job . Steps . Add ( new GithubTask
174+ {
175+ Name = name ,
176+ Run = runCommand
177+ } ) ;
178+
179+ return this ;
180+ }
181+
182+ /// <summary>
183+ /// Builds and returns the configured job.
184+ /// </summary>
185+ /// <returns>The configured <see cref="Job"/> instance.</returns>
186+ public Job Build ( ) => this . job ;
187+ }
188+ }
0 commit comments