#! /usr/bin/env python3 """Сборка всей программы """ import os import sys import datetime import subprocess import shutil def help() -> None: """Получить справку по сборке """ h: str = """ Справка по сборке: ./make.py help - Справка по сборке ./make.py build - Сборка всей программы""" print(h) class Build: """Класс сборки """ def __init__(self) -> None: self.date_build: str = str(datetime.datetime.now()) """Дата сборки""" self.date_build = self.date_build.replace(" ", "_") cmd: list[str] = ['git', 'describe', '--tags', '--abbrev=0'] output: bytes = subprocess.Popen( cmd, stdout=subprocess.PIPE).communicate()[0] tag: str = output.decode("utf-8") tag = tag[:-1] self.tag: str = tag """Тег из гита""" cmd = ['go', 'version'] output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] go_version: str = output.decode("utf-8") go_version = go_version[11:][:-1].replace(" ", "_") out: str = f"date={self.date_build}\t\t" +\ f"tag={self.tag}\t\t" +\ f"go_version={go_version}" print(out) self.go_version: str = go_version """Версия golang""" def run(self) -> None: """Запуск сборки""" print("cleaning") try: shutil.rmtree('./bin') except OSError: pass try: os.mkdir("./bin") except FileExistsError: pass try: os.mkdir("./bin/web") except FileExistsError: pass try: os.mkdir("./bin/web/static") except FileExistsError: pass try: os.mkdir("./bin/web/tmpl") except FileExistsError: pass print("copying") os.system('cp -r ./web ./bin') print("format") os.system("go fmt ./...") print("build") cmd: str = """go build -ldflags "-w -s -X """ +\ f"""main.GoVersion={self.go_version} -X """ +\ """main.Version=$TAG -X """ +\ """main.Date=$BUILD_DATE" -o """ +\ '../bin/server.exe ./cmd/server/main.go' print(cmd) os.system(cmd) print('stripping') os.system('strip -s ./bin/server.exe') print('packing exe') os.system('upx -f ./bin/server.exe') exeSize: int = os.path.getsize("./bin/server.exe") print(f'size exe={exeSize/1000**2:.2f} MB') class Dev: """Класс разработки """ def __init__(self) -> None: pass def run(self) -> None: print("cleaning") try: shutil.rmtree('./bin') except OSError: pass try: os.mkdir("./bin") except FileExistsError: pass try: os.mkdir("./bin/web") except FileExistsError: pass try: os.mkdir("./bin/web/static") except FileExistsError: pass try: os.mkdir("./bin/web/tmpl") except FileExistsError: pass print("copying") os.system('cp -r ./web ./bin') print("format") os.system("go fmt ./...") print("build") cmd: str = """go build """ +\ '-race -o ./bin/server.exe ./cmd/server/main.go' print(cmd) os.system(cmd) os.chdir("./bin") os.unsetenv('STAGE') os.putenv('STAGE', 'local') os.system('./server.exe') if __name__ == '__main__': os.system('clear') args: list[str] = sys.argv if len(args) < 2: help() sys.exit(0) strCmd: str = args[1] match strCmd: case "help": # Получить справку по сборке help() case"build": # Сборка всей программы b: Build = Build() b.run() case "dev": # Разработка d: Dev = Dev() d.run() case "": help() case _: pass