-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.cake
More file actions
258 lines (224 loc) · 7.42 KB
/
build.cake
File metadata and controls
258 lines (224 loc) · 7.42 KB
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
#tool "nuget:?package=NuGet.CommandLine&version=6.8.0"
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release"); // Default to Release for production builds
var projectName = Argument("project", "");
var outputDir = "./output/packages";
var solutionFile = "./GetSecure.sln";
// Detect project from Git tag if not specified
if (string.IsNullOrEmpty(projectName))
{
try
{
var gitTag = EnvironmentVariable("GITHUB_REF_NAME") ?? "";
if (!string.IsNullOrEmpty(gitTag))
{
Information($"Detected Git tag: {gitTag}");
// Parse tag format: <projectname>-v<version> (any project name)
var match = System.Text.RegularExpressions.Regex.Match(gitTag, @"^(.+)-v(.+)$");
if (match.Success)
{
projectName = match.Groups[1].Value;
var version = match.Groups[2].Value;
Information($"Auto-detected project: {projectName}, version: {version}");
}
}
}
catch (Exception ex)
{
Information($"Could not auto-detect project from Git tag: {ex.Message}");
}
}
// Helper function to get project path
string GetProjectPath(string projectName)
{
// Look for project in /src/ folder
var projectPath = $"./src/{projectName}/{projectName}.csproj";
if (FileExists(projectPath))
{
return projectPath;
}
// Fallback: try to find any .csproj file with the project name in /src/
var srcDir = "./src";
if (DirectoryExists(srcDir))
{
var projectFiles = GetFiles($"{srcDir}/**/*{projectName}*.csproj");
if (projectFiles.Any())
{
return projectFiles.First().FullPath;
}
}
throw new Exception($"Project not found: {projectName}. Expected location: {projectPath}");
}
// Clean output directory
Task("Clean")
.Does(() =>
{
if (DirectoryExists(outputDir))
{
DeleteDirectory(outputDir, new DeleteDirectorySettings { Recursive = true });
}
CreateDirectory(outputDir);
});
// Generate solution file
Task("Generate-Solution")
.Does(() =>
{
Information("Generating solution file...");
// Remove existing solution file if it exists
if (FileExists(solutionFile))
{
DeleteFile(solutionFile);
}
// Create new solution file using CLI command
StartProcess("dotnet", new ProcessSettings
{
Arguments = "new sln --name GetSecure --output ."
});
// Add all projects from /src/ folder to solution
var srcDir = "./src";
if (DirectoryExists(srcDir))
{
var projectFiles = GetFiles($"{srcDir}/**/*.csproj");
foreach (var projectFile in projectFiles)
{
StartProcess("dotnet", new ProcessSettings
{
Arguments = $"sln {solutionFile} add {projectFile.FullPath}"
});
}
}
Information($"Solution file created: {solutionFile}");
});
// Restore NuGet packages
Task("Restore")
.IsDependentOn("Generate-Solution")
.Does(() =>
{
Information("Restoring NuGet packages...");
DotNetRestore(solutionFile);
});
// Build the solution
Task("Build")
.IsDependentOn("Restore")
.Does(() =>
{
Information($"Building solution in {configuration} configuration...");
DotNetBuild(solutionFile, new DotNetBuildSettings
{
Configuration = configuration,
NoRestore = true
});
});
// Pack NuGet packages
Task("Pack")
.IsDependentOn("Build")
.Does(() =>
{
Information("Packing NuGet packages...");
var packSettings = new DotNetPackSettings
{
Configuration = configuration,
OutputDirectory = outputDir,
NoBuild = true,
NoRestore = true,
IncludeSymbols = true,
SymbolPackageFormat = "snupkg"
};
// Projects are expected to be pre-versioned in their project files
if (string.IsNullOrEmpty(projectName))
{
// Pack all projects in solution
Information("Packing all projects in solution...");
DotNetPack(solutionFile, packSettings);
}
else
{
// Pack specific project
var projectPath = GetProjectPath(projectName);
Information($"Packing specific project: {projectName} ({projectPath})");
DotNetPack(projectPath, packSettings);
}
Information($"Packages created in: {outputDir}");
});
// Publish packages to NuGet (uses Release configuration by default)
Task("Publish")
.IsDependentOn("Pack")
.Does(() =>
{
var apiKey = EnvironmentVariable("NUGET_API_KEY");
var source = EnvironmentVariable("NUGET_SOURCE") ?? "https://api.nuget.org/v3/index.json";
if (string.IsNullOrEmpty(apiKey))
{
throw new Exception("NUGET_API_KEY environment variable is required for publishing");
}
Information($"Publishing packages to: {source}");
var allPackages = GetFiles($"{outputDir}/*.nupkg");
var packages = allPackages;
if (!string.IsNullOrEmpty(projectName))
{
// Filter packages for specific project
packages = new FilePathCollection(
allPackages.Where(p => p.GetFilename().ToString().StartsWith(projectName)),
new PathComparer(IsRunningOnUnix())
);
Information($"Publishing packages for project: {projectName}");
}
foreach (var package in packages)
{
Information($"Publishing: {package.GetFilename()}");
DotNetNuGetPush(package.FullPath, new DotNetNuGetPushSettings
{
ApiKey = apiKey,
Source = source,
SkipDuplicate = true
});
}
Information("Publishing completed successfully!");
});
// Default task
Task("Default")
.IsDependentOn("Pack");
// Run tests
Task("Test")
.IsDependentOn("Build")
.Does(() =>
{
Information("Running tests...");
DotNetTest("./tests/", new DotNetTestSettings
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
Loggers = new[] { "trx" },
ResultsDirectory = "./output/test-results"
});
});
// Clean up generated files
Task("Clean-All")
.Does(() =>
{
if (FileExists(solutionFile))
{
DeleteFile(solutionFile);
}
if (DirectoryExists(outputDir))
{
DeleteDirectory(outputDir, new DeleteDirectorySettings { Recursive = true });
}
// Clean build artifacts
var cleanSettings = new DotNetCleanSettings
{
Configuration = configuration
};
// Clean all projects in /src/ folder
var srcDir = "./src";
if (DirectoryExists(srcDir))
{
var projectFiles = GetFiles($"{srcDir}/**/*.csproj");
foreach (var projectFile in projectFiles)
{
DotNetClean(projectFile.FullPath, cleanSettings);
}
}
});
RunTarget(target);