Skip to content

Commit 15a81d0

Browse files
committed
Use Cake for IO where possible
1 parent 5d10525 commit 15a81d0

12 files changed

Lines changed: 72 additions & 89 deletions

Build/Tasks/BuildAll.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information
44
namespace DotNetNuke.Build.Tasks
55
{
6+
using Cake.Common.IO;
67
using Cake.Frosting;
78

89
/// <summary>A cake task to compile the platform and create all of the packages.</summary>
@@ -27,11 +28,11 @@ public override void Run(Context context)
2728

2829
private static void RevertSqlDataProvider(Context context)
2930
{
30-
var fileName = context.GetTwoDigitsVersionNumber() + ".SqlDataProvider";
31-
var filePath = "./Dnn Platform/Website/Providers/DataProviders/SqlDataProvider/" + fileName;
32-
if (!context.SqlDataProviderExists && System.IO.File.Exists(filePath))
31+
var fileName = context.File($"{context.GetTwoDigitsVersionNumber()}.SqlDataProvider");
32+
var filePath = context.Directory("./Dnn Platform/Website/Providers/DataProviders/SqlDataProvider/") + fileName;
33+
if (!context.SqlDataProviderExists && context.FileExists(filePath))
3334
{
34-
System.IO.File.Delete(filePath);
35+
context.DeleteFile(filePath);
3536
}
3637
}
3738
}

Build/Tasks/CopyWebConfigToDevSite.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public override void Run(Context context)
3434
.Replace("{DbOwner}", context.Settings.DbOwner)
3535
.Replace("{ObjectQualifier}", context.Settings.ObjectQualifier);
3636
var res = context.XmlTransform(trans, conf);
37-
var webConfig = context.File(System.IO.Path.Combine(context.Settings.WebsitePath, "web.config"));
37+
var webConfig = context.Settings.WebsitePath + context.File("web.config");
3838
context.FileWriteText(webConfig, res);
3939
}
4040
}

Build/Tasks/CreateDatabase.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ namespace DotNetNuke.Build.Tasks
1212
using Cake.Common.Xml;
1313
using Cake.Frosting;
1414

15+
using Dnn.CakeUtils;
16+
1517
using Microsoft.Data.SqlClient;
1618

1719
/// <summary>A cake task to crete a localdb database named <c>Dnn_Platform</c>.</summary>
@@ -59,10 +61,7 @@ LOG ON (
5961
// #####################################################################
6062
// run initial schema first
6163
// #####################################################################
62-
var fileContents = System.IO.File.ReadAllText(
63-
"./Website/Providers/DataProviders/SqlDataProvider/"
64-
+ schemaScriptName.ToString()
65-
+ ".SqlDataProvider");
64+
var fileContents = context.ReadFile($"./Website/Providers/DataProviders/SqlDataProvider/{schemaScriptName}.SqlDataProvider");
6665

6766
var sqlDelimiterRegex = new System.Text.RegularExpressions.Regex(
6867
@"(?<=(?:[^\w]+|^))GO(?=(?: |\t)*?(?:\r?\n|$))",
@@ -125,7 +124,7 @@ LOG ON (
125124
{
126125
context.Information("Updated to v{0}", currentFileToProcess);
127126

128-
fileContents = System.IO.File.ReadAllText(file.ToString());
127+
fileContents = context.ReadFile(file);
129128

130129
sqlStatements = sqlDelimiterRegex.Split(fileContents);
131130
foreach (string statement in sqlStatements)

Build/Tasks/CreateDeploy.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
// See the LICENSE file in the project root for more information
44
namespace DotNetNuke.Build.Tasks
55
{
6-
using System;
7-
using System.IO;
8-
using System.Linq;
9-
106
using Cake.Common.IO;
117
using Cake.Frosting;
128

@@ -26,12 +22,12 @@ public override void Run(Context context)
2622
var packageZip = context.ArtifactsDir + context.File($"DNN_Platform_{context.GetBuildNumber()}_Deploy.zip");
2723

2824
var deployDir = context.Directory("./DotNetNuke/");
29-
Directory.Move(context.WebsiteDir.Path.FullPath, deployDir.Path.FullPath);
25+
context.MoveDirectory(context.WebsiteDir, deployDir);
3026
var files = context.GetFilesByPatterns(deployDir, IncludeAll, context.PackagingPatterns.InstallExclude);
3127
files.Add(context.GetFilesByPatterns(deployDir, context.PackagingPatterns.InstallInclude));
3228
context.Zip(string.Empty, packageZip, files);
3329
context.AddFilesToZip(packageZip, "./Build/Deploy", context.GetFiles("./Build/Deploy/*"), append: true);
34-
Directory.Move(deployDir.Path.FullPath, context.WebsiteDir.Path.FullPath);
30+
context.MoveDirectory(deployDir, context.WebsiteDir);
3531
}
3632
}
3733
}

Build/Tasks/CreateGitHubPullRequest.cs

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ namespace DotNetNuke.Build.Tasks
1616
using Cake.Common;
1717
using Cake.Common.Build;
1818
using Cake.Common.Diagnostics;
19+
using Cake.Common.IO;
1920
using Cake.Core;
2021
using Cake.Core.IO;
22+
using Cake.FileHelpers;
2123
using Cake.Frosting;
2224

25+
using Dnn.CakeUtils;
26+
2327
using Microsoft.IdentityModel.JsonWebTokens;
2428
using Microsoft.IdentityModel.Tokens;
2529

@@ -48,8 +52,6 @@ namespace DotNetNuke.Build.Tasks
4852
public sealed class CreateGitHubPullRequest : FrostingTask<Context>
4953
{
5054
private const string TargetBranch = "develop";
51-
private const string BugReportPath = ".github/ISSUE_TEMPLATE/bug-report.yml";
52-
private const string SolutionInfoPath = "SolutionInfo.cs";
5355
private const string GitUserName = "DNN Platform CI Bot";
5456
private const string GitUserEmail = "noreply@dnncommunity.org";
5557

@@ -62,6 +64,9 @@ public override void Run(Context context)
6264
return;
6365
}
6466

67+
var solutionInfoPath = context.File("SolutionInfo.cs");
68+
var bugReportPath = context.File(".github/ISSUE_TEMPLATE/bug-report.yml");
69+
6570
var sourceBranch = context.AzurePipelines().IsRunningOnAzurePipelines
6671
? context.AzurePipelines().Environment.Repository.SourceBranch
6772
: context.GitHubActions().IsRunningOnGitHubActions
@@ -109,11 +114,11 @@ public override void Run(Context context)
109114
context.Information("Authenticated as GitHub App installation.");
110115

111116
// Update bug-report.yml with version info from GitHub releases
112-
UpdateBugReportVersions(context, client, owner, repo);
117+
UpdateBugReportVersions(context, client, owner, repo, bugReportPath);
113118

114119
// Reset SolutionInfo.cs if only the commit count/SHA changed (not the major.minor.patch)
115120
// to avoid creating a PR for every single commit.
116-
ResetSolutionInfoIfVersionUnchanged(context);
121+
ResetSolutionInfoIfVersionUnchanged(context, solutionInfoPath);
117122

118123
// Only proceed with the PR if there are actual changes
119124
if (!HasUncommittedChanges(context))
@@ -282,7 +287,7 @@ private static string NormalizePem(string pem)
282287
return sb.ToString();
283288
}
284289

285-
private static void UpdateBugReportVersions(Context context, GitHubClient client, string owner, string repo)
290+
private static void UpdateBugReportVersions(Context context, GitHubClient client, string owner, string repo, FilePath bugReportPath)
286291
{
287292
context.Information("Fetching GitHub releases to update bug report template...");
288293
var releases = client.Repository.Release.GetAll(owner, repo).GetAwaiter().GetResult();
@@ -322,7 +327,7 @@ private static void UpdateBugReportVersions(Context context, GitHubClient client
322327

323328
// Parse the YAML template and update the affected-versions options
324329
var yaml = new YamlStream();
325-
using (var reader = new StreamReader(BugReportPath))
330+
using (var reader = new StreamReader(bugReportPath.FullPath))
326331
{
327332
yaml.Load(reader);
328333
}
@@ -341,7 +346,7 @@ private static void UpdateBugReportVersions(Context context, GitHubClient client
341346

342347
if (optionsNode == null)
343348
{
344-
context.Warning("Could not locate affected-versions options in {0}, skipping update.", BugReportPath);
349+
context.Warning("Could not locate affected-versions options in {0}, skipping update.", bugReportPath);
345350
return;
346351
}
347352

@@ -355,8 +360,8 @@ private static void UpdateBugReportVersions(Context context, GitHubClient client
355360
yaml.Save(stringWriter, false);
356361

357362
// YamlStream.Save wraps output in document markers (--- / ...) that the original file doesn't use
358-
File.WriteAllText(BugReportPath, StripDocumentMarkers(stringWriter.ToString()));
359-
context.Information("Updated {0} with {1} version option(s).", BugReportPath, options.Count);
363+
context.FileWriteText(bugReportPath, StripDocumentMarkers(stringWriter.ToString()));
364+
context.Information("Updated {0} with {1} version option(s).", bugReportPath, options.Count);
360365
}
361366

362367
private static string StripDocumentMarkers(string yaml)
@@ -415,25 +420,25 @@ private static bool HasStagedChanges(ICakeContext context)
415420
return process.GetExitCode() != 0;
416421
}
417422

418-
private static void ResetSolutionInfoIfVersionUnchanged(Context context)
423+
private static void ResetSolutionInfoIfVersionUnchanged(Context context, FilePath solutionInfoPath)
419424
{
420425
var committedProcess = context.StartAndReturnProcess(
421426
"git",
422427
new ProcessSettings
423428
{
424-
Arguments = $"show HEAD:{SolutionInfoPath}",
429+
Arguments = $"show HEAD:{solutionInfoPath}",
425430
RedirectStandardOutput = true,
426431
});
427432
committedProcess.WaitForExit();
428433

429434
if (committedProcess.GetExitCode() != 0)
430435
{
431-
context.Information("Could not read committed {0}, skipping reset check.", SolutionInfoPath);
436+
context.Information("Could not read committed {0}, skipping reset check.", solutionInfoPath);
432437
return;
433438
}
434439

435440
var committedContent = string.Join("\n", committedProcess.GetStandardOutput());
436-
var currentContent = File.ReadAllText(SolutionInfoPath);
441+
var currentContent = context.ReadFile(solutionInfoPath);
437442

438443
var committedVersion = ExtractAssemblyVersion(committedContent);
439444
var currentVersion = ExtractAssemblyVersion(currentContent);
@@ -442,12 +447,12 @@ private static void ResetSolutionInfoIfVersionUnchanged(Context context)
442447

443448
if (string.Equals(committedVersion, currentVersion, StringComparison.Ordinal))
444449
{
445-
context.Information("Major.Minor.Patch has not changed. Resetting {0} to avoid a noisy PR.", SolutionInfoPath);
446-
Git(context, $"checkout -- {SolutionInfoPath}");
450+
context.Information("Major.Minor.Patch has not changed. Resetting {0} to avoid a noisy PR.", solutionInfoPath);
451+
Git(context, $"checkout -- {solutionInfoPath}");
447452
}
448453
else
449454
{
450-
context.Information("Major.Minor.Patch changed ({0} → {1}). Keeping {2} modifications.", committedVersion, currentVersion, SolutionInfoPath);
455+
context.Information("Major.Minor.Patch changed ({0} → {1}). Keeping {2} modifications.", committedVersion, currentVersion, solutionInfoPath);
451456
}
452457
}
453458

Build/Tasks/CreateUpgrade.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
// See the LICENSE file in the project root for more information
44
namespace DotNetNuke.Build.Tasks
55
{
6-
using System;
7-
using System.IO;
8-
using System.Linq;
9-
106
using Cake.Common.Diagnostics;
117
using Cake.Common.IO;
128
using Cake.Frosting;

Build/Tasks/GeneratePackagesChecksums.cs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,17 @@ namespace DotNetNuke.Build.Tasks
55
{
66
using System;
77
using System.Globalization;
8-
using System.IO;
9-
using System.Linq;
10-
using System.Security.Cryptography;
118
using System.Text;
129

1310
using Cake.Common.Diagnostics;
1411
using Cake.Common.IO;
12+
using Cake.Common.Security;
1513
using Cake.Core.IO;
1614
using Cake.FileHelpers;
1715
using Cake.Frosting;
1816

1917
using Dnn.CakeUtils;
2018

21-
using Path = System.IO.Path;
22-
2319
/// <summary>A cake task to generate a <c>checksums.md</c> file with the artifact checksums.</summary>
2420
[IsDependentOn(typeof(CleanArtifacts))]
2521
[IsDependentOn(typeof(UpdateDnnManifests))]
@@ -47,7 +43,7 @@ public override void Run(Context context)
4743
foreach (var file in files)
4844
{
4945
var fileName = file.GetFilename();
50-
var hash = GetFileHash(file);
46+
var hash = GetFileHash(context, file);
5147
checksumsMarkdown.AppendLine(CultureInfo.InvariantCulture, $"| {fileName} | {hash} |");
5248
}
5349

@@ -58,12 +54,10 @@ public override void Run(Context context)
5854
context.Information($"Saved checksums to {filePath}");
5955
}
6056

61-
private static string GetFileHash(FilePath file)
57+
private static string GetFileHash(Context context, FilePath file)
6258
{
63-
using var hasher = SHA256.Create();
64-
using var stream = File.OpenRead(file.FullPath);
65-
var hashBytes = hasher.ComputeHash(stream);
66-
return Convert.ToHexStringLower(hashBytes);
59+
var hash = context.CalculateFileHash(file, HashAlgorithm.SHA256);
60+
return Convert.ToHexStringLower(hash.ComputedHash);
6761
}
6862
}
6963
}

Build/Tasks/GenerateSecurityAnalyzerChecksums.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
namespace DotNetNuke.Build.Tasks
55
{
66
using System;
7-
using System.IO;
8-
using System.Linq;
9-
using System.Security.Cryptography;
107

118
using Cake.Common.Diagnostics;
9+
using Cake.Common.IO;
10+
using Cake.Common.Security;
11+
using Cake.Core.IO;
12+
using Cake.FileHelpers;
1213
using Cake.Frosting;
1314

1415
/// <summary>A cake task to generate the <c>Default.aspx</c> checksum for the Security Analyzer.</summary>
@@ -19,23 +20,21 @@ public sealed class GenerateSecurityAnalyzerChecksums : FrostingTask<Context>
1920
public override void Run(Context context)
2021
{
2122
context.Information("Generating default.aspx checksum…");
22-
const string sourceFile = "./Dnn Platform/Website/Default.aspx";
23-
const string destFile = "./Dnn.AdminExperience/Dnn.PersonaBar.Extensions/Components/Security/Resources/sums.resources";
24-
var hash = CalculateSha(sourceFile);
23+
var sourceFile = context.File("./Dnn Platform/Website/Default.aspx");
24+
var destFile = context.File("./Dnn.AdminExperience/Dnn.PersonaBar.Extensions/Components/Security/Resources/sums.resources");
25+
var hash = CalculateSha(context, sourceFile);
2526
var content = $"""
2627
<checksums>
2728
<sum name="Default.aspx" version="{context.Version.MajorMinorPatch}" type="Platform" sum="{hash}" />
2829
</checksums>
2930
""";
30-
File.WriteAllText(destFile, content);
31+
context.FileWriteText(destFile, content);
3132
}
3233

33-
private static string CalculateSha(string filename)
34+
private static string CalculateSha(Context context, FilePath file)
3435
{
35-
using var sha = SHA256.Create();
36-
using var stream = File.OpenRead(filename);
37-
var hash = sha.ComputeHash(stream);
38-
return Convert.ToHexStringLower(hash);
36+
var hash = context.CalculateFileHash(file, HashAlgorithm.SHA256);
37+
return Convert.ToHexStringLower(hash.ComputedHash);
3938
}
4039
}
4140
}

Build/Tasks/GenerateSqlDataProvider.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
// See the LICENSE file in the project root for more information
44
namespace DotNetNuke.Build.Tasks
55
{
6-
using System;
7-
using System.IO;
8-
using System.Linq;
9-
6+
using Cake.Common.IO;
7+
using Cake.FileHelpers;
108
using Cake.Frosting;
119

1210
/// <summary>A cake task to generate a SQL Data Provider script if it doesn't exist.</summary>
@@ -16,28 +14,29 @@ public sealed class GenerateSqlDataProvider : FrostingTask<Context>
1614
/// <inheritdoc />
1715
public override void Run(Context context)
1816
{
19-
var fileName = context.GetTwoDigitsVersionNumber()[..8] + ".SqlDataProvider";
20-
var filePath = "./Dnn Platform/Website/Providers/DataProviders/SqlDataProvider/" + fileName;
21-
if (File.Exists(filePath))
17+
var fileName = context.File($"{context.GetTwoDigitsVersionNumber()[..8]}.SqlDataProvider");
18+
var filePath = context.Directory("./Dnn Platform/Website/Providers/DataProviders/SqlDataProvider/") + fileName;
19+
if (context.FileExists(filePath))
2220
{
2321
context.SqlDataProviderExists = true;
2422
return;
2523
}
2624

2725
context.SqlDataProviderExists = false;
2826

29-
using (var file = new StreamWriter(filePath, true))
30-
{
31-
file.WriteLine("/************************************************************/");
32-
file.WriteLine("/***** SqlDataProvider *****/");
33-
file.WriteLine("/***** *****/");
34-
file.WriteLine("/***** *****/");
35-
file.WriteLine("/***** Note: To manually execute this script you must *****/");
36-
file.WriteLine("/***** perform a search and replace operation *****/");
37-
file.WriteLine("/***** for {databaseOwner} and {objectQualifier} *****/");
38-
file.WriteLine("/***** *****/");
39-
file.WriteLine("/************************************************************/");
40-
}
27+
const string DefaultSqlFileContents =
28+
"""
29+
/************************************************************/
30+
/***** SqlDataProvider *****/
31+
/***** *****/
32+
/***** *****/
33+
/***** Note: To manually execute this script you must *****/
34+
/***** perform a search and replace operation *****/
35+
/***** for {databaseOwner} and {objectQualifier} *****/
36+
/***** *****/
37+
/************************************************************/
38+
""";
39+
context.FileWriteText(filePath, DefaultSqlFileContents);
4140
}
4241
}
4342
}

0 commit comments

Comments
 (0)