mirror of https://github.com/axmolengine/axmol.git
Improve make-pkg.ps1
Add a hack inject dotnet runtime 6.0+ to support create cross platform zip on windows.
This commit is contained in:
parent
54daf354cd
commit
9af781f419
|
@ -1,5 +1,5 @@
|
|||
# Auto detect text files and perform LF normalization
|
||||
* text=auto
|
||||
* text=auto eol=lf
|
||||
# Custom for Visual Studio
|
||||
*.cs diff=csharp
|
||||
*.sln merge=union
|
||||
|
|
|
@ -2,8 +2,6 @@ param(
|
|||
$version = $null
|
||||
)
|
||||
|
||||
Write-Host "Creating package $pkg_file_path ..."
|
||||
|
||||
$AX_ROOT = (Resolve-Path $PSScriptRoot/../..).Path
|
||||
|
||||
if (!$version -or ($version -eq 'auto')) {
|
||||
|
@ -51,6 +49,8 @@ $excludes = @(
|
|||
$pkg_file_name = "axmol-$version.zip"
|
||||
$pkg_file_path = $(Join-Path $AX_ROOT $pkg_file_name)
|
||||
|
||||
Write-Host "Creating package $pkg_file_path ..."
|
||||
|
||||
$compress_args = @{
|
||||
Path = $AX_ROOT
|
||||
CompressionLevel = 'Optimal'
|
||||
|
@ -73,6 +73,9 @@ function Compress-ArchiveEx() {
|
|||
[switch]$Force
|
||||
)
|
||||
|
||||
$Script:S_IFREG = 0x8000
|
||||
# $S_IFDIR = 0x4000
|
||||
|
||||
if ($RelativeBasePath) {
|
||||
Push-Location $RelativeBasePath
|
||||
}
|
||||
|
@ -85,8 +88,120 @@ function Compress-ArchiveEx() {
|
|||
Add-Type -AssemblyName System.IO.Compression
|
||||
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
||||
}
|
||||
$archive = [System.IO.Compression.ZipFile]::Open($DestinationPath, [System.IO.Compression.ZipArchiveMode]::Create)
|
||||
|
||||
$pwsh_ver = $PSVersionTable.PSVersion.ToString()
|
||||
if (([System.Version]$pwsh_ver -ge [System.Version]'7.0.0.0') -and $IsWindows) {
|
||||
|
||||
if (-not ([System.Management.Automation.PSTypeName]'UnixFileStream').Type) {
|
||||
Add-Type -TypeDefinition @"
|
||||
// A hack to create unix style .zip on windows
|
||||
// refers:
|
||||
// - https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipVersion.cs#L24
|
||||
// - https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs#L529C26-L529C50
|
||||
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
|
||||
public class MyZipFile : ZipArchive
|
||||
{
|
||||
public UnixFileStream Stream { get; set; }
|
||||
public MyZipFile(UnixFileStream stream, ZipArchiveMode mode, bool leaveOpen) : base(stream, mode, leaveOpen)
|
||||
{
|
||||
Stream = stream;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing)
|
||||
Stream.IsDisposing = true;
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
}
|
||||
|
||||
public class UnixFileStream : FileStream
|
||||
{
|
||||
internal enum ZipVersionMadeByPlatform : byte
|
||||
{
|
||||
Windows = 0,
|
||||
Unix = 3
|
||||
}
|
||||
|
||||
// public const uint DirectoryFileHeaderSignatureConstant = 0x02014B50;
|
||||
// public const uint LocalFileHeaderSignatureConstant = 0x04034B50;
|
||||
int m_hints = -1;
|
||||
int m_hints2 = -1;
|
||||
|
||||
public UnixFileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) : base(path, mode, access, share, bufferSize, useAsync)
|
||||
{
|
||||
}
|
||||
|
||||
public override void WriteByte(byte value)
|
||||
{
|
||||
if (m_hints2 != -1) ++m_hints;
|
||||
if (IsDisposing)
|
||||
{
|
||||
if (m_hints != -1) ++m_hints;
|
||||
|
||||
if (m_hints == 2)
|
||||
{ // hint: CurrentZipPlatform: hack set to unix
|
||||
value = (byte)ZipVersionMadeByPlatform.Unix;
|
||||
}
|
||||
}
|
||||
base.WriteByte(value);
|
||||
}
|
||||
|
||||
public override void Write(byte[] array, int offset, int count)
|
||||
{
|
||||
if (IsDisposing)
|
||||
{ // hint: entryHeaderSignature
|
||||
if ((count == 4 && array[0] == 0x50 && array[1] == 0x4b && array[2] == 0x01 && array[3] == 0x02) || m_hints != -1)
|
||||
++m_hints;
|
||||
|
||||
if (m_hints == 17) // hint: filepath
|
||||
{
|
||||
var path = Encoding.UTF8.GetString(array);
|
||||
array = Encoding.UTF8.GetBytes(path.Replace('\\', '/'));
|
||||
m_hints = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if ((count == 4 && array[0] == 0x50 && array[1] == 0x4b && array[2] == 0x03 && array[3] == 0x04) || m_hints2 != -1)
|
||||
++m_hints2;
|
||||
|
||||
if (m_hints2 == 10) {
|
||||
var path = Encoding.UTF8.GetString(array);
|
||||
array = Encoding.UTF8.GetBytes(path.Replace('\\', '/'));
|
||||
m_hints2 = -1;
|
||||
}
|
||||
|
||||
base.Write(array, offset, count);
|
||||
}
|
||||
|
||||
public bool IsDisposing { set; get; } = false;
|
||||
|
||||
public static ZipArchive CreateUnixZipFile(string archiveFileName)
|
||||
{
|
||||
var fs = new UnixFileStream(archiveFileName, FileMode.CreateNew, FileAccess.Write, FileShare.None, bufferSize: 0x1000, useAsync: false);
|
||||
try
|
||||
{
|
||||
return new MyZipFile(fs, ZipArchiveMode.Create, leaveOpen: false);
|
||||
}
|
||||
catch
|
||||
{
|
||||
fs.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
"@
|
||||
}
|
||||
|
||||
$archive = [UnixFileStream]::CreateUnixZipFile($DestinationPath)
|
||||
}
|
||||
else {
|
||||
$archive = [System.IO.Compression.ZipFile]::Open($DestinationPath, [System.IO.Compression.ZipArchiveMode]::Create)
|
||||
}
|
||||
$compressionLevelValue = @{
|
||||
'Optimal' = [System.IO.Compression.CompressionLevel]::Optimal
|
||||
'Fastest' = [System.IO.Compression.CompressionLevel]::Fastest
|
||||
|
@ -115,21 +230,41 @@ function Compress-ArchiveEx() {
|
|||
if ($rname.StartsWith('./')) { $rname = $rname.TrimStart('./') }
|
||||
$excluded = (&$_is_exclude -uxpath $rname)
|
||||
if (!$excluded) {
|
||||
if($prefix) { $rname = Join-Path $prefix $rname }
|
||||
|
||||
if (!$path.PSIsContainer) {
|
||||
++$Script:total
|
||||
$zentry = $archive.CreateEntry($rname)
|
||||
if ($path.UnixStat) {
|
||||
# when run on unix, set permissions same with origin file
|
||||
Write-Host "a $rname"
|
||||
# preserve unix file permissions mode
|
||||
# refer https://github.com/PowerShell/Microsoft.PowerShell.Archive/pull/146/files
|
||||
$zentry.ExternalAttributes = ((0x8000 -bor $path.UnixStat.Mode) -shl 16)
|
||||
$uxmode = $null
|
||||
if ($path.UnixStat) {
|
||||
$uxmode = $path.UnixStat.Mode
|
||||
}
|
||||
else {
|
||||
$fileext = Split-Path $rname -Extension
|
||||
if (!$fileext -or $rname.EndsWith('.sh')) {
|
||||
$filestatus = $(git -C $AX_ROOT ls-files -s $rname)
|
||||
if ($filestatus) {
|
||||
$uxmode = [Convert]::ToInt32($filestatus.Split(' ')[0], 8)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$uxmode) {
|
||||
# default unix file permissions
|
||||
$uxmode = [Convert]::ToInt32('100644', 8)
|
||||
}
|
||||
|
||||
if ($prefix) {
|
||||
$rname = Join-Path $prefix $rname
|
||||
}
|
||||
$zentry = $archive.CreateEntry($rname)
|
||||
$zentry.ExternalAttributes = (($Script:S_IFREG -bor $uxmode) -shl 16)
|
||||
$zentryWriter = New-Object -TypeName System.IO.BinaryWriter $zentry.Open()
|
||||
$zentryWriter.Write([System.IO.File]::ReadAllBytes($path))
|
||||
$zentryWriter.Flush()
|
||||
$zentryWriter.Close()
|
||||
} else {
|
||||
|
||||
++$Script:total
|
||||
}
|
||||
else {
|
||||
$sub_paths = Get-ChildItem $path
|
||||
foreach ($sub_path in $sub_paths) {
|
||||
&$_zip_add $archive $sub_path $compressionLevel $prefix
|
||||
|
@ -137,11 +272,11 @@ function Compress-ArchiveEx() {
|
|||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "x` $path"
|
||||
Write-Host "x $rname"
|
||||
}
|
||||
}
|
||||
else {
|
||||
Write-Host "x $path, LinkType=$($Path.LinkType)"
|
||||
Write-Host "x $rname, LinkType=$($Path.LinkType)"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue