-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
136 lines (110 loc) · 3.37 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import hypha.core.builder as builder
from hypha.core.logging import *
import hypha.core.utils as utils
import sys
import argparse
import zipfile
import requests
import io
import os
import subprocess
import time
from multiprocessing import Process, Pipe
import socket
import hashlib
import base64
PHP_URL_WIN = "https://windows.php.net/downloads/releases/php-8.0.30-nts-Win32-vs16-x64.zip"
OS_TYPE = None
PORT = 80
PHPSILENT = True
def downloadPHP():
if (OS_TYPE == "lin"):
try:
subprocess.run(
["php", "-v"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
return
except FileNotFoundError:
error("Please install PHP to use the development enviroment.")
os._exit()
elif (OS_TYPE == "win"):
if (os.path.isfile("hypha/php/php.exe")):
log("PHP has already been downloaded!")
return
log("Downloading PHP...")
r = requests.get(PHP_URL_WIN)
log("Unzipping...")
z = zipfile.ZipFile(io.BytesIO(r.content))
z.extractall("hypha/php")
def phpServerCommand(osType, port, silent):
cmdArgs = "-S 0.0.0.0:" + str(port) + " build/routes.php -t build"
if (osType == "lin"):
fullCmd = ["hypha/php/php.exe"] + cmdArgs.split()
else:
fullCmd = ["hypha/php/php.exe"] + cmdArgs.split()
if (silent):
subprocess.run(
fullCmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
else:
subprocess.run(fullCmd)
error("Error while starting php server. Please use -v")
def startDevServer():
log("Starting server...")
phpThread = Process(target=phpServerCommand, args=(OS_TYPE, PORT, PHPSILENT))
phpThread.start()
time.sleep(2)
log("Server started in port " + str(PORT))
appDir = os.getcwd()
lastHash = utils.md5_dir(appDir)
try:
while (True):
time.sleep(1)
hash = utils.md5_dir(appDir)
if (hash != lastHash):
log("Detected changes, rebuilding...")
builder.build()
lastHash = hash
except KeyboardInterrupt:
log("Closing server...")
phpThread.terminate()
log("Bye!")
os._exit(1)
def build():
builder.buildInit()
builder.build()
def dev():
downloadPHP()
builder.buildInit()
builder.build()
startDevServer()
def main():
global OS_TYPE, PORT, PHPSILENT
parser = argparse.ArgumentParser(
prog="Hypha",
description="The Javascript Framework for PHP"
)
parser.add_argument("cmd", type=str, help="build / dev")
parser.add_argument("-p", "--port", type=int, default=80, help="Port of the dev server")
parser.add_argument("-v", "--verbose", action="store_true", help="Show messages of the PHP server")
args = parser.parse_args()
PORT = args.port
PHPSILENT = (not args.verbose)
if (os.name == "nt"):
OS_TYPE = "win"
elif (os.name == "posix"):
OS_TYPE = "lin"
else:
error("Unknown OS, please open a github issue with this stack trace.\nOS Name: " + str(os.name))
os._exit(1)
if (args.cmd == "build"):
build()
elif (args.cmd == "dev"):
# Start dev server with automatic building and restarting
dev()
if (__name__ == "__main__"):
main()