import os
import shutil
import subprocess
from pathlib import Path

version = subprocess.run(
    ["poetry", "version", "-s"], capture_output=True, text=True
).stdout.rstrip()

dir = Path(__file__).resolve().parents[1]
Path(dir / "dist" / "gaphor").mkdir(parents=True, exist_ok=True)

dist_location = dir / "dist" / "gaphor"
seven_zip = Path("C:/Program Files/7-Zip/7z.exe")
makensis = Path("C:/Program Files (x86)/NSIS/makensis.exe")
payload = dir / "dist" / "payload.7z"
portable = dir / "dist" / f"gaphor-{version}-portable"

MINGW = "mingw64"


def clean_files():
    print("Cleaning files")
    shutil.rmtree(portable, ignore_errors=True)
    if payload.is_file:
        payload.unlink()


def build_installer():
    print("Building Installer")
    icon = dir / "windows" / "gaphor.ico"
    shutil.copy(icon, dist_location / "gaphor.ico")
    os.chdir(dist_location)
    subprocess.run(
        [
            str(makensis),
            "-NOCD",
            f"-DVERSION={version}",
            str(dir / "windows" / "win_installer.nsi"),
        ],
        capture_output=True,
        text=True,
    )

    shutil.move(
        str(dist_location / "gaphor-LATEST.exe"),
        dir / "dist" / f"gaphor-{version}-installer.exe",
    )


def concatenate_files(input_files, output_file):
    for input_file in input_files:
        with open(input_file, "rb") as reader, open(output_file, "wb") as writer:
            data = reader.readlines()
            writer.writelines(data)


def unix2dos(file_path):
    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():
    print("Building portable installer")
    portable.mkdir(parents=True)
    shutil.copy(dir / "windows" / "gaphor.lnk", portable / "gaphor.lnk")
    shutil.copy(dir / "windows" / "README-PORTABLE.txt", portable / "README.txt")
    unix2dos(portable / "README.txt")
    config_path = portable / "config"
    config_path.mkdir()
    shutil.copytree(dist_location, portable / "data")

    subprocess.run([str(seven_zip), "a", str(payload), str(portable)])
    concatenate_files([seven_zip.parent / "7z.sfx", payload], f"{portable}.exe")


clean_files()
build_installer()
build_portable_installer()
clean_files()
print("Windows Installer builds are complete!")

@@ -1,12 +1,48 @@
+from __future__ import annotations%0A%0A
 import os%0Aim
@@ -209,16 +209,22 @@
 p()%0A%0Adir
+: Path
  = Path(
@@ -254,21 +254,29 @@
 ents%5B1%5D%0A
+dist: 
 Path
-(
+ = 
 dir / %22d
@@ -271,32 +271,42 @@
 h = dir / %22dist%22
+%0APath(dist
  / %22gaphor%22).mkd
@@ -342,36 +342,32 @@
 e)%0A%0A
-dist_location = dir / %22
+gaphor_dist: Path = 
 dist
-%22
  / %22
@@ -383,16 +383,22 @@
 even_zip
+: Path
  = Path(
@@ -434,90 +434,28 @@
 e%22)%0A
-makensis = Path(%22C:/Program Files (x86)/NSIS/makensis.exe%22)%0Apayload = dir / %22
+payload: Path = 
 dist
-%22
  / %22
@@ -474,31 +474,29 @@
 portable
- = dir / %22
+: Path = 
 dist
-%22
  / f%22gap
@@ -524,27 +524,8 @@
 e%22%0A%0A
-MINGW = %22mingw64%22%0A%0A
 %0Adef
@@ -538,16 +538,24 @@
 _files()
+ -%3E None
 :%0A    pr
@@ -688,32 +688,40 @@
 uild_installer()
+ -%3E None
 :%0A    print(%22Bui
@@ -802,29 +802,27 @@
 y(icon, 
-dist_location
+gaphor_dist
  / %22gaph
@@ -843,29 +843,27 @@
 s.chdir(
-dist_location
+gaphor_dist
 )%0A    su
@@ -902,20 +902,17 @@
     
-str(
+%22
 makensis
 ),%0A 
@@ -907,17 +907,17 @@
 makensis
-)
+%22
 ,%0A      
@@ -1118,29 +1118,27 @@
     str(
-dist_location
+gaphor_dist
  / %22gaph
@@ -1162,28 +1162,20 @@
         
-dir / %22
 dist
-%22
  / f%22gap
@@ -1245,16 +1245,28 @@
 ut_files
+: list%5BPath%5D
 , output
@@ -1270,17 +1270,31 @@
 put_file
-)
+: Path) -%3E None
 :%0A    fo
@@ -1502,17 +1502,31 @@
 ile_path
-)
+: Path) -%3E None
 :%0A    wi
@@ -1834,16 +1834,24 @@
 taller()
+ -%3E None
 :%0A    pr
@@ -2197,21 +2197,19 @@
 ree(
-dist_location
+gaphor_dist
 , po
@@ -2319,16 +2319,25 @@
 e_files(
+%0A        
 %5Bseven_z
@@ -2372,19 +2372,41 @@
 d%5D, 
-f%22%7B
+dist / %22gaphor-%7Bversion%7D-
 portable
 %7D.ex
@@ -2405,14 +2405,18 @@
 able
-%7D
 .exe%22
+%0A    
 )%0A%0A%0A

Add type hints