Skip to content

Commit

Permalink
Simplify and re-format source code
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakamoto committed Apr 19, 2024
1 parent a9bd83d commit 2c1370c
Show file tree
Hide file tree
Showing 10 changed files with 289 additions and 303 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace PublishSPAforGitHubPages.Build.Test;

public class GetGHPagesBaseUrlFromRepositoryUrlTest
{
public static IEnumerable<object[]> TestPattern = new[] {
new object[]{"HTTPS", ""},
new object[]{"HTTPS", "WorkDir"},
new object[]{"HTTPS.git", ""},
new object[]{"HTTPS.git", "WorkDir"},
new object[]{"SSH", ""},
new object[]{"SSH", "WorkDir"},
new object[]{"SSH.git", ""},
new object[]{"SSH.git", "WorkDir"},
};
public static IEnumerable<object[]> TestPattern = [
["HTTPS", ""],
["HTTPS", "WorkDir"],
["HTTPS.git", ""],
["HTTPS.git", "WorkDir"],
["SSH", ""],
["SSH", "WorkDir"],
["SSH.git", ""],
["SSH.git", "WorkDir"],
];

[TestCaseSource(nameof(TestPattern))]
public void GetBaseUrl_ProjectSite_Test(string protocol, string subDir)
Expand Down
8 changes: 4 additions & 4 deletions PublishSPAforGitHubPages.Build.Test/PublishTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public class PublishTest
{
private readonly HtmlParser _Parser = new();

public static readonly IEnumerable<object[]> TestPattern = new[] {
new object[]{"HTTPS", ""},
new object[]{"HTTPS", "WorkDir"},
public static readonly IEnumerable<object[]> TestPattern = [
["HTTPS", ""],
["HTTPS", "WorkDir"],
//new object[]{"HTTPS.git", ""},
//new object[]{"HTTPS.git", "WorkDir"},
//new object[]{"SSH", ""},
//new object[]{"SSH", "WorkDir"},
//new object[]{"SSH.git", ""},
//new object[]{"SSH.git", "WorkDir"},
};
];

private string GetBaseHref(string indexHtmlPath)
{
Expand Down
10 changes: 5 additions & 5 deletions PublishSPAforGitHubPages.Build.Test/RewriteIndexHtmlTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace PublishSPAforGitHubPages.Build.Test;

public class RewriteIndexHtmlTest
{
public static IEnumerable<object[]> TestPattern = new[] {
new object[]{ "index.html" },
new object[]{ "index - no autostart.html" },
new object[]{ "index - autostart is true.html" },
};
public static IEnumerable<object[]> TestPattern = [
["index.html"],
["index - no autostart.html"],
["index - autostart is true.html"],
];

[TestCaseSource(nameof(TestPattern))]
public void InjectBrotliLoader_Test(string caseFileName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,53 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;

namespace PublishSPAforGHPages
namespace PublishSPAforGHPages;

public class GetGHPagesBaseUrlFromRepositoryUrl : Microsoft.Build.Utilities.Task
{
public class GetGHPagesBaseUrlFromRepositoryUrl : Microsoft.Build.Utilities.Task
[Required]
public string WorkFolder { get; set; }

[Output]
public string BaseUrl { get; set; } = "";

public override bool Execute()
{
[Required]
public string WorkFolder { get; set; }
var gitDir = this.FindGitDir();
if (string.IsNullOrEmpty(gitDir)) return true;

[Output]
public string BaseUrl { get; set; } = "";
var gitConfigPath = Path.Combine(gitDir, "config");
var regexMatchRepoUrl = File.ReadLines(gitConfigPath)
.SkipWhile(line => line != "[remote \"origin\"]")
.Select(line => Regex.Match(line, @"^\s*url\s*=\s*((https://github.com/(?<owner>[^/]+)/(?<project>.+))|(git@github.com:(?<owner>[^/]+)/(?<project>.+)))\s*$"))
.FirstOrDefault(m => m.Success);

public override bool Execute()
if (regexMatchRepoUrl != null)
{
var gitDir = FindGitDir();
if (string.IsNullOrEmpty(gitDir)) return true;

var gitConfigPath = Path.Combine(gitDir, "config");
var regexMatchRepoUrl = File.ReadLines(gitConfigPath)
.SkipWhile(line => line != "[remote \"origin\"]")
.Select(line => Regex.Match(line, @"^\s*url\s*=\s*((https://github.com/(?<owner>[^/]+)/(?<project>.+))|(git@github.com:(?<owner>[^/]+)/(?<project>.+)))\s*$"))
.FirstOrDefault(m => m.Success);

if (regexMatchRepoUrl != null)
{
var owner = regexMatchRepoUrl.Groups["owner"].Value;
var project = regexMatchRepoUrl.Groups["project"].Value;
if (project.EndsWith(".git")) project = project.Substring(0, project.Length - 4);
if (owner + ".github.io" == project)
BaseUrl = "/";
else
BaseUrl = "/" + project + "/";
}

return true;
var owner = regexMatchRepoUrl.Groups["owner"].Value;
var project = regexMatchRepoUrl.Groups["project"].Value;
if (project.EndsWith(".git")) project = project.Substring(0, project.Length - 4);
if (owner + ".github.io" == project)
this.BaseUrl = "/";
else
this.BaseUrl = "/" + project + "/";
}

private string FindGitDir()
{
for (var baseDir = WorkFolder; baseDir != null; baseDir = Path.GetDirectoryName(baseDir))
{
var gitDir = Path.Combine(baseDir, ".git");
if (Directory.Exists(gitDir)) return gitDir;
}
return null;
}
return true;
}

private void LogMessage(string message)
private string FindGitDir()
{
for (var baseDir = this.WorkFolder; baseDir != null; baseDir = Path.GetDirectoryName(baseDir))
{
if (BuildEngine != null) Log.LogMessage(MessageImportance.High, message);
var gitDir = Path.Combine(baseDir, ".git");
if (Directory.Exists(gitDir)) return gitDir;
}
return null;
}

private void LogMessage(string message)
{
if (this.BuildEngine != null) this.Log.LogMessage(MessageImportance.High, message);
}
}
11 changes: 5 additions & 6 deletions PublishSPAforGitHubPages.Build/Models/AssetsManifestFile.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace PublishSPAforGHPages.Models
namespace PublishSPAforGHPages.Models;

public class AssetsManifestFile
{
public class AssetsManifestFile
{
public string version { get; set; }
public string version { get; set; }

public AssetsManifestFileEntry[] assets { get; set; }
}
public AssetsManifestFileEntry[] assets { get; set; }
}
11 changes: 5 additions & 6 deletions PublishSPAforGitHubPages.Build/Models/AssetsManifestFileEntry.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace PublishSPAforGHPages.Models
namespace PublishSPAforGHPages.Models;

public class AssetsManifestFileEntry
{
public class AssetsManifestFileEntry
{
public string url { get; set; }
public string url { get; set; }

public string hash { get; set; }
}
public string hash { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<RootNamespace>PublishSPAforGHPages</RootNamespace>
<IsTool>true</IsTool>
<LangVersion>Latest</LangVersion>
<ImplicitUsings>true</ImplicitUsings>
<GeneratePackageOnBuild Condition="'$(Configuration)' == 'Release'">true</GeneratePackageOnBuild>
<Authors>J.Sakamoto</Authors>
<Version>2.1.1</Version>
Expand Down
80 changes: 39 additions & 41 deletions PublishSPAforGitHubPages.Build/RecompressStaticFile.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,54 @@
using System.IO;
using System.IO.Compression;
using System.IO.Compression;
using BrotliSharpLib;
using Microsoft.Build.Framework;

namespace PublishSPAforGHPages
namespace PublishSPAforGHPages;

public class RecompressStaticFile : Microsoft.Build.Utilities.Task
{
public class RecompressStaticFile : Microsoft.Build.Utilities.Task
{
[Required]
public string File { get; set; }
[Required]
public string File { get; set; }

public bool EnableGzip { get; set; } = true;

public bool EnableBrotli { get; set; } = true;

public bool Recursive { get; set; }

public bool EnableGzip { get; set; } = true;
public override bool Execute()
{
var fileName = Path.GetFileName(this.File);
var baseDir = Path.GetDirectoryName(this.File);
var searchOption = this.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var targetFiles = Directory.GetFiles(baseDir, fileName, searchOption);
foreach (var targeFile in targetFiles)
{
this.Recompress(targeFile);
}

public bool EnableBrotli { get; set; } = true;
return true;
}

public bool Recursive { get; set; }
private void Recompress(string file)
{
using var sourceStream = System.IO.File.OpenRead(file);

public override bool Execute()
if (this.EnableGzip)
{
var fileName = Path.GetFileName(this.File);
var baseDir = Path.GetDirectoryName(this.File);
var searchOption = this.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var targetFiles = Directory.GetFiles(baseDir, fileName, searchOption);
foreach (var targeFile in targetFiles)
{
this.Recompress(targeFile);
}

return true;
using var outputStream = System.IO.File.Create(this.File + ".gz");
using var compressingStream = new GZipStream(outputStream, CompressionLevel.Optimal);
sourceStream.Seek(0, SeekOrigin.Begin);
sourceStream.CopyTo(compressingStream);
}

private void Recompress(string file)
if (this.EnableBrotli)
{
using var sourceStream = System.IO.File.OpenRead(file);

if (this.EnableGzip)
{
using var outputStream = System.IO.File.Create(this.File + ".gz");
using var compressingStream = new GZipStream(outputStream, CompressionLevel.Optimal);
sourceStream.Seek(0, SeekOrigin.Begin);
sourceStream.CopyTo(compressingStream);
}

if (this.EnableBrotli)
{
using var outputStream = System.IO.File.Create(this.File + ".br");
using var compressingStream = new BrotliStream(outputStream, CompressionMode.Compress);
compressingStream.SetQuality(11);
compressingStream.SetWindow(22);
sourceStream.Seek(0, SeekOrigin.Begin);
sourceStream.CopyTo(compressingStream);
}
using var outputStream = System.IO.File.Create(this.File + ".br");
using var compressingStream = new BrotliStream(outputStream, CompressionMode.Compress);
compressingStream.SetQuality(11);
compressingStream.SetWindow(22);
sourceStream.Seek(0, SeekOrigin.Begin);
sourceStream.CopyTo(compressingStream);
}
}
}
Loading

0 comments on commit 2c1370c

Please sign in to comment.