make.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #! /usr/bin/env python3
  2. """Сборка всей программы
  3. """
  4. import os
  5. import sys
  6. import datetime
  7. import subprocess
  8. import shutil
  9. def help() -> None:
  10. """Получить справку по сборке
  11. """
  12. h: str = """
  13. Справка по сборке:
  14. ./make.py help - Справка по сборке
  15. ./make.py build - Сборка всей программы"""
  16. print(h)
  17. class Build:
  18. """Класс сборки
  19. """
  20. def __init__(self) -> None:
  21. self.date_build: str = str(datetime.datetime.now())
  22. """Дата сборки"""
  23. self.date_build = self.date_build.replace(" ", "_")
  24. cmd: list[str] = ['git', 'describe', '--tags', '--abbrev=0']
  25. output: bytes = subprocess.Popen(
  26. cmd, stdout=subprocess.PIPE).communicate()[0]
  27. tag: str = output.decode("utf-8")
  28. tag = tag[:-1]
  29. self.tag: str = tag
  30. """Тег из гита"""
  31. cmd = ['go', 'version']
  32. output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
  33. go_version: str = output.decode("utf-8")
  34. go_version = go_version[11:][:-1].replace(" ", "_")
  35. out: str = f"date={self.date_build}\t\t" +\
  36. f"tag={self.tag}\t\t" +\
  37. f"go_version={go_version}"
  38. print(out)
  39. self.go_version: str = go_version
  40. """Версия golang"""
  41. def run(self) -> None:
  42. """Запуск сборки"""
  43. print("cleaning")
  44. try:
  45. shutil.rmtree('./bin')
  46. except OSError:
  47. pass
  48. try:
  49. os.mkdir("./bin")
  50. except FileExistsError:
  51. pass
  52. try:
  53. os.mkdir("./bin/web")
  54. except FileExistsError:
  55. pass
  56. try:
  57. os.mkdir("./bin/web/static")
  58. except FileExistsError:
  59. pass
  60. try:
  61. os.mkdir("./bin/web/tmpl")
  62. except FileExistsError:
  63. pass
  64. print("copying")
  65. os.system('cp -r ./web ./bin')
  66. print("format")
  67. os.system("go fmt ./...")
  68. print("build")
  69. cmd: str = """go build -ldflags "-w -s -X """ +\
  70. f"""main.GoVersion={self.go_version} -X """ +\
  71. """main.Version=$TAG -X """ +\
  72. """main.Date=$BUILD_DATE" -o """ +\
  73. '../bin/server.exe ./cmd/server/main.go'
  74. print(cmd)
  75. os.system(cmd)
  76. print('stripping')
  77. os.system('strip -s ./bin/server.exe')
  78. print('packing exe')
  79. os.system('upx -f ./bin/server.exe')
  80. exeSize: int = os.path.getsize("./bin/server.exe")
  81. print(f'size exe={exeSize/1000**2:.2f} MB')
  82. class Dev:
  83. """Класс разработки
  84. """
  85. def __init__(self) -> None:
  86. pass
  87. def run(self) -> None:
  88. print("cleaning")
  89. try:
  90. shutil.rmtree('./bin')
  91. except OSError:
  92. pass
  93. try:
  94. os.mkdir("./bin")
  95. except FileExistsError:
  96. pass
  97. try:
  98. os.mkdir("./bin/web")
  99. except FileExistsError:
  100. pass
  101. try:
  102. os.mkdir("./bin/web/static")
  103. except FileExistsError:
  104. pass
  105. try:
  106. os.mkdir("./bin/web/tmpl")
  107. except FileExistsError:
  108. pass
  109. print("copying")
  110. os.system('cp -r ./web ./bin')
  111. print("format")
  112. os.system("go fmt ./...")
  113. print("build")
  114. cmd: str = """go build """ +\
  115. '-race -o ./bin/server.exe ./cmd/server/main.go'
  116. print(cmd)
  117. os.system(cmd)
  118. os.chdir("./bin")
  119. os.unsetenv('STAGE')
  120. os.putenv('STAGE', 'local')
  121. os.system('./server.exe')
  122. if __name__ == '__main__':
  123. os.system('clear')
  124. args: list[str] = sys.argv
  125. if len(args) < 2:
  126. help()
  127. sys.exit(0)
  128. strCmd: str = args[1]
  129. match strCmd:
  130. case "help": # Получить справку по сборке
  131. help()
  132. case"build": # Сборка всей программы
  133. b: Build = Build()
  134. b.run()
  135. case "dev": # Разработка
  136. d: Dev = Dev()
  137. d.run()
  138. case "":
  139. help()
  140. case _: pass