-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
289 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 39 additions & 42 deletions
81
PublishSPAforGitHubPages.Build/GetGHPagesBaseUrlFromRepositoryUrl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
PublishSPAforGitHubPages.Build/Models/AssetsManifestFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
PublishSPAforGitHubPages.Build/Models/AssetsManifestFileEntry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.