1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using LibGit2Sharp;
using VNLib.Tools.Build.Executor.Model;
using VNLib.Tools.Build.Executor.Constants;
using VNLib.Tools.Build.Executor.Extensions;
using static VNLib.Tools.Build.Executor.Constants.Config;
namespace VNLib.Tools.Build.Executor.Modules
{
/// <summary>
/// Represents a base class for all modules to inherit from
/// </summary>
internal abstract class ModuleBase : IArtifact, IBuildable, IModuleData, ITaskfileScope
{
protected readonly BuildConfig Config;
protected readonly TaskFile TaskFile;
///<inheritdoc/>
public TaskfileVars TaskVars { get; private set; }
///<inheritdoc/>
public DirectoryInfo WorkingDir { get; }
///<inheritdoc/>
public abstract string ModuleName { get; }
///<inheritdoc/>
public ICollection<IProject> Projects { get; } = new LinkedList<IProject>();
///<inheritdoc/>
public IModuleFileManager FileManager { get; }
///<inheritdoc/>
public string? TaskfileName { get; protected set; }
/// <summary>
/// The git repository of the module
/// </summary>
public Repository Repository { get; }
/// <summary>
/// The project explorer for the module
/// </summary>
public abstract IProjectExplorer ModuleExplorer { get; }
public ModuleBase(BuildConfig config, DirectoryInfo root)
{
WorkingDir = root;
Config = config;
//Default to module taskfile name
TaskfileName = config.ModuleTaskFileName;
//Init repo for root working dir
Repository = new(root.FullName);
FileManager = new ModuleFileManager(config, this);
TaskVars = null!;
TaskFile = new(config.TaskExeName, () => ModuleName);
}
///<inheritdoc/>
public virtual async Task LoadAsync(TaskfileVars vars)
{
if(Repository?.Head?.Tip?.Sha is null)
{
throw new BuildStepFailedException("This repository does not have any commit history. Cannot continue");
}
//Store paraent vars
TaskVars = vars;
string moduleSemVer = this.GetModuleCiVersion(Config.DefaultCiVersion, Config.SemverStyle);
//Build module local environment variables
TaskVars.Set("MODULE_NAME", ModuleName);
TaskVars.Set("OUTPUT_DIR", FileManager.OutputDir);
TaskVars.Set("MODULE_DIR", WorkingDir.FullName);
//Store current head-sha before update step
TaskVars.Set("HEAD_SHA", Repository.Head.Tip.Sha);
TaskVars.Set("BRANCH_NAME", Repository.Head.FriendlyName);
TaskVars.Set("BUILD_VERSION", moduleSemVer);
//Full path to module archive file
TaskVars.Set("FULL_ARCHIVE_FILE_NAME", Path.Combine(WorkingDir.FullName, Config.SourceArchiveName));
TaskVars.Set("ARCHIVE_FILE_NAME", Config.SourceArchiveName);
TaskVars.Set("ARCHIVE_FILE_FORMAT", Config.SourceArchiveFormat);
//Remove any previous projects
Projects.Clear();
Log.Information("Discovering projects in module {sln}", ModuleName);
//Discover all projects in for the module
foreach (IProject project in ModuleExplorer.DiscoverProjects())
{
//Store in collection
Projects.Add(project);
}
//Load all projects
await Projects.RunAllAsync(p => p.LoadAsync(TaskVars.Clone()));
Log.Information("Sucessfully loaded {count} projects into module {sln}", Projects.Count, ModuleName);
Log.Information("{modname} CI build SemVer will be {semver}", ModuleName, moduleSemVer);
}
///<inheritdoc/>
public virtual async Task DoStepSyncSource()
{
Log.Information("Checking for source code updates in module {mod}", ModuleName);
//Do a git pull to update our sources
await TaskFile.ExecCommandAsync(this, TaskfileComamnd.Update, true);
//Set the latest commit sha after an update
TaskVars.Set("HEAD_SHA", Repository.Head.Tip.Sha);
//Update lastest build number
TaskVars.Set("BUILD_VERSION", this.GetModuleCiVersion(Config.DefaultCiVersion, Config.SemverStyle));
//Update module semver after source sync
string moduleSemVer = this.GetModuleCiVersion(Config.DefaultCiVersion, Config.SemverStyle);
TaskVars.Set("BUILD_VERSION", moduleSemVer);
Log.Information("{modname} CI build SemVer will now be {semver}", ModuleName, moduleSemVer);
}
///<inheritdoc/>
public virtual async Task<bool> CheckForChangesAsync()
{
//Check source for updates
await Projects.RunAllAsync(p => FileManager.CheckSourceChangedAsync(p, Config, Repository.Head.Tip.Sha));
//Check if any project is not up-to-date
return Projects.Any(static p => !p.UpToDate);
}
///<inheritdoc/>
public virtual async Task DoStepBuild()
{
//Clean the output dir
FileManager.CleanOutput();
//Recreate the output dir
FileManager.CreateOutput();
//Run taskfile to build
await TaskFile.ExecCommandAsync(this, TaskfileComamnd.Build, true);
//Run build for all projects
foreach (IProject proj in Projects)
{
//Exec
await TaskFile.ExecCommandAsync(proj, TaskfileComamnd.Build, true);
}
}
///<inheritdoc/>
public virtual async Task DoStepPostBuild(bool success)
{
//Run taskfile postbuild, not required to produce a sucessful result
await TaskFile.ExecCommandAsync(this, success ? TaskfileComamnd.PostbuildSuccess : TaskfileComamnd.PostbuildFailure, false);
//Run postbuild for all projects
foreach (IProject proj in Projects)
{
//Run postbuild for projects
await TaskFile.ExecCommandAsync(proj, success ? TaskfileComamnd.PostbuildSuccess : TaskfileComamnd.PostbuildFailure, false);
}
//Run postbuild for all projects
await Projects.RunAllAsync(async (p) =>
{
//If the operation was a success, commit the sum change
if (success)
{
Log.Verbose("Committing sum change for {sm}", p.ProjectName);
//Commit sum changes now that build has completed successfully
await FileManager.CommitSumChangeAsync(p);
}
});
}
///<inheritdoc/>
public virtual async Task DoStepPublish()
{
//Run taskfile postbuild, not required to produce a sucessful result
await TaskFile.ExecCommandAsync(this, TaskfileComamnd.Publish, true);
//Run postbuild for all projects
foreach (IProject proj in Projects)
{
//Run postbuild for projects
await TaskFile.ExecCommandAsync(proj, TaskfileComamnd.Publish, true);
}
}
///<inheritdoc/>
public virtual async Task DoRunTests(bool failOnError)
{
//Run taskfile to build
await TaskFile.ExecCommandAsync(this, TaskfileComamnd.Test, failOnError);
//Run build for all projects
foreach (IProject proj in Projects)
{
//Exec
await TaskFile.ExecCommandAsync(proj, TaskfileComamnd.Test, failOnError);
}
}
///<inheritdoc/>
public virtual async Task CleanAsync()
{
try
{
//Run taskfile to build
await TaskFile.ExecCommandAsync(this, TaskfileComamnd.Clean, true);
//Clean all projects
foreach (IProject proj in Projects)
{
//Clean the project output dir
await TaskFile.ExecCommandAsync(proj, TaskfileComamnd.Clean, true);
}
//Clean the output dir
FileManager.CleanOutput();
}
catch (BuildStepFailedException)
{
throw;
}
catch (Exception ex)
{
throw new BuildStepFailedException("Failed to remove the module output directory", ex, ModuleName);
}
}
public override string ToString() => ModuleName;
public virtual void Dispose()
{
//Dispose the respository
Repository.Dispose();
//empty list
Projects.Clear();
}
}
}
|