Merge pull request #903 from gaphor/chore/bash2py

Upgrade Windows Build Script from Bash to Python
This commit is contained in:
Dan Yeaw 2021-07-22 22:05:44 -04:00 committed by GitHub
commit d3d864f6fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 119 additions and 67 deletions

View File

@ -147,7 +147,7 @@ jobs:
cd packaging
mingw32-make all
cd windows
mingw32-make all
python build-win-installer.py
- name: Sign Executables
env:
password: "${{ secrets.CERTPASSWORD }}"

View File

@ -32,6 +32,7 @@ repos:
rev: v0.7.2.1
hooks:
- id: shellcheck
args: [--exclude, SC1017]
- repo: https://github.com/asottile/pyupgrade
rev: v2.21.2
hooks:

View File

@ -1,13 +0,0 @@
help: ## Show this help
@echo "make <target>, where <target> is one of:"
@grep -hP "\t##" $(MAKEFILE_LIST) | sed -e 's/^\([a-z]*\):.*## / \1\t/' | expand -t14
all: dist
windows-dep:
pacman -S --needed --noconfirm p7zip dos2unix upx mingw-w64-x86_64-nsis mingw-w64-x86_64-wget
dist: windows-dep
./build-win-installer.sh
.PHONY: windows-dep all help dist

View File

@ -0,0 +1,115 @@
from __future__ import annotations
import os
import shutil
import subprocess
from pathlib import Path
version = subprocess.run(
["poetry", "version", "-s"], capture_output=True, text=True
).stdout.rstrip()
def clean_files(paths: list[Path]) -> None:
print("Cleaning files")
for path in paths:
if path.is_dir():
shutil.rmtree(path, ignore_errors=True)
elif path.is_file():
path.unlink(missing_ok=True) # type: ignore[call-arg]
def build_installer(icon: Path, files_to_package: Path, output_file: Path) -> None:
print("Building Installer")
shutil.copy(icon, files_to_package / "gaphor.ico")
os.chdir(files_to_package)
subprocess.run(
[
"makensis",
"-NOCD",
f"-DVERSION={version}",
str(working_dir / "windows" / "win_installer.nsi"),
],
capture_output=True,
text=True,
)
shutil.move(str(files_to_package / "gaphor-LATEST.exe"), output_file)
def concatenate_files(input_files: list[Path], output_file: Path) -> None:
print(f"Opening {output_file} for write")
with open(output_file, "wb") as writer:
for input_file in input_files:
print(f"Writing {input_file}")
with open(input_file, "rb") as reader:
shutil.copyfileobj(reader, writer)
def unix2dos(file_path: Path) -> None:
win_line_ending = b"\r\n"
unix_line_ending = b"\n"
with open(file_path, "rb") as open_file:
content = open_file.read()
content = content.replace(win_line_ending, unix_line_ending)
with open(file_path, "wb") as open_file:
open_file.write(content)
def build_portable_installer(
symlink_path: Path,
readme_path: Path,
files_to_package: Path,
portable_path: Path,
payload_path: Path,
seven_zip_path: Path,
output_file: Path,
) -> None:
print("Building portable installer")
shutil.copy(symlink_path, portable_path / symlink_path.name)
shutil.copy(readme_path, portable_path / "README.txt")
unix2dos(portable_path / "README.txt")
config_path = portable_path / "config"
config_path.mkdir()
shutil.copytree(files_to_package, portable_path / "data")
subprocess.run([str(seven_zip_path), "a", str(payload_path), str(portable_path)])
if payload_path.is_file():
print(f"Payload 7z archive found at {payload_path}")
else:
print("Payload 7z archive not found")
sfx_path = seven_zip_path.parent / "7z.sfx"
if sfx_path.is_file():
print(f"Sfx file found at {sfx_path}")
else:
print("Sfx file not found")
concatenate_files([seven_zip_path.parent / "7z.sfx", payload_path], output_file)
if __name__ == "__main__":
working_dir: Path = Path(__file__).resolve().parents[1]
dist: Path = working_dir / "dist"
Path(dist / "gaphor").mkdir(parents=True, exist_ok=True)
portable: Path = dist / f"gaphor-{version}-portable"
payload: Path = dist / "payload.7z"
clean_files([portable, payload])
icon = working_dir / "windows" / "gaphor.ico"
gaphor_files: Path = dist / "gaphor"
installer = dist / f"gaphor-{version}-installer.exe"
build_installer(icon, gaphor_files, installer)
portable.mkdir(parents=True)
symlink = working_dir / "windows" / "gaphor.lnk"
readme = working_dir / "windows" / "README-PORTABLE.txt"
seven_zip: Path = Path("C:/Program Files/7-Zip/7z.exe")
installer = dist / f"gaphor-{version}-portable.exe"
build_portable_installer(
symlink, readme, gaphor_files, portable, payload, seven_zip, installer
)
clean_files([portable, payload])
print("Windows Installer builds are complete!")

View File

@ -1,53 +0,0 @@
#!/usr/bin/env bash
set -euo pipefail
# DIR is the parent directory
DIR="$(dirname "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )")"
cd "${DIR}"
mkdir -p dist
DIST_LOCATION="${DIR}/dist/gaphor"
VERSION="$(poetry version --no-ansi | cut -d' ' -f2)"
MINGW="mingw64"
function build_installer {
cp "${DIR}"/windows/gaphor.ico "${DIST_LOCATION}"
(cd "${DIST_LOCATION}" && makensis -NOCD -DVERSION="$VERSION" "${DIR}"/windows/win_installer.nsi)
mv "${DIST_LOCATION}/gaphor-LATEST.exe" "$DIR/dist/gaphor-$VERSION-installer.exe"
}
function build_portable_installer {
local PORTABLE="$DIR/dist/gaphor-$VERSION-portable"
rm -rf "$PORTABLE"
mkdir "$PORTABLE"
cp "${DIR}"/windows/gaphor.lnk "$PORTABLE"
cp "${DIR}"/windows/README-PORTABLE.txt "$PORTABLE"/README.txt
unix2dos "$PORTABLE"/README.txt
mkdir "$PORTABLE"/config
cp -RT "${DIST_LOCATION}" "$PORTABLE"/data
rm -Rf 7zout 7z1900-x64.exe
7z a payload.7z "$PORTABLE"
wget.exe -P "$DIR" -c https://www.7-zip.org/a/7z1900-x64.exe
7z x -o7zout 7z1900-x64.exe
cat 7zout/7z.sfx payload.7z > "$PORTABLE".exe
rm -Rf 7zout 7z1900-x64.exe payload.7z "$PORTABLE"
}
function main {
# started from the wrong env -> switch
if [ "$(echo "$MSYSTEM" | tr '[:upper:]' '[:lower:]')" != "$MINGW" ]; then
"/${MINGW}.exe" "$0"
exit $?
fi
echo "build installer"
build_installer
echo "build portable installer"
build_portable_installer
}
main "$@";

Binary file not shown.

View File

@ -8,10 +8,12 @@ pacman --noconfirm -Suy
pacman --noconfirm -S --needed \
git \
upx \
mingw-w64-x86_64-make \
mingw-w64-x86_64-gcc \
mingw-w64-x86_64-gtk3 \
mingw-w64-x86_64-pkgconf \
mingw-w64-x86_64-nsis \
mingw-w64-x86_64-cairo \
mingw-w64-x86_64-gobject-introspection \
mingw-w64-x86_64-python \