Skip to content

Commit

Permalink
Merge pull request #16 from zy7y/develop
Browse files Browse the repository at this point in the history
#14 windows桌面执行程序
  • Loading branch information
zy7y authored Jan 10, 2024
2 parents 2c99014 + 9c8fec5 commit 6f290bb
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 18 deletions.
35 changes: 33 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ jobs:
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pywebview
pip install pyinstaller
- name: Build executable
run: |
pyinstaller --windowed --onefile main.py
pyinstaller --windowed --onefile client.py
- name: Upload executables
uses: actions/upload-release-asset@v1
Expand All @@ -52,6 +54,35 @@ jobs:
with:
# 获取变量
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ./dist/main.exe
asset_path: ./dist/client.exe
asset_name: dfs-generate.exe
asset_content_type: application/octet-stream

build-macos-app:
needs: release
runs-on: macos-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Install dependencies
run: |
pip install -r requirements.txt
pip install pywebview
pip install pyinstaller
- name: Build executable
run: |
python build.py
- name: Upload executables
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
with:
# 获取变量
upload_url: ${{ needs.release.outputs.upload_url }}
asset_path: ./dist/client.app
asset_name: dfs-generate.app
asset_content_type: application/octet-stream
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ source venv/bin/activate
```
```shell
# 安装依赖
pip install -r requrements.txt -i https://mirrors.aliyun.com/pypi/simple/
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
```
3. 启动项目
```shell
python main.py
uvicorn main:app --port 80
```
4. 浏览器访问
```shell
Expand Down
28 changes: 28 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import platform

import yapf_third_party

params = [
'--windowed',
'--onefile',
'--add-data', 'static:static',
'--add-data', f'{yapf_third_party.__file__.replace("__init__.py", "")}:yapf_third_party',
'--clean',
'--noconfirm',
'client.py',
]

current_os = platform.system()

from PyInstaller import __main__ as pyi

params.append("--icon"),
if current_os == "Windows":
params.append("static/logo.icns")
path = "./dist/client.exe"
else:
params.append("static/logo.icon")
path = "./dist/client"

pyi.run(params)
print(path)
35 changes: 35 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import threading

import uvicorn
import webview

import socket
import random

from main import app


def get_unused_port():
"""获取未被使用的端口"""
while True:
port = random.randint(1024, 65535) # 端口范围一般为1024-65535
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(("localhost", port))
sock.close()
return port
except OSError:
pass


def desktop_client():
port = get_unused_port()
t = threading.Thread(target=uvicorn.run, args=(app,), kwargs={"port": port})
t.daemon = True
t.start()
webview.create_window("DFS代码生成", url=f"http://127.0.0.1:{port}")
webview.start(debug=True)


if __name__ == "__main__":
desktop_client()
7 changes: 5 additions & 2 deletions entity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import uuid
from typing import Generic, List, Optional, TypeVar

import black
from yapf.yapflib.yapf_api import FormatCode
import isort
from pydantic import BaseModel, ConfigDict, field_serializer
from pydantic.alias_generators import to_camel
Expand Down Expand Up @@ -49,6 +49,9 @@ def get_last_uri_with_metadata(cls):
return uri, get_metadata_by_db_uri(uri)


SQLModel.metadata.create_all(engine)


T = TypeVar("T")


Expand Down Expand Up @@ -100,5 +103,5 @@ class CodeGen(BaseVo):

@field_serializer("code")
def serialize_code(self, code: str, _info):
_code = black.format_str(code, mode=black.FileMode())
_code = FormatCode(code, style_config="pep8")[0]
return isort.code(_code)
23 changes: 12 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import os

from fastapi import FastAPI, Query
from fastapi.requests import Request
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
import pkg_resources


from entity import CodeGen, Conf, DBConf, R, RList, Table
from generate.main import generate_code

app = FastAPI(
title="dfs-generate", description="FastAPI SQLModel 逆向生成代码", docs_url=None
)
app = FastAPI(title="dfs-generate", description="FastAPI SQLModel 逆向生成代码")


# 解决打包桌面程序static找不到
static_path = pkg_resources.resource_filename(__name__, "static")

app.mount("/static", StaticFiles(directory="static"), name="static")

app.mount("/static", StaticFiles(directory=static_path), name="static")


@app.get("/", include_in_schema=False)
def index():
return FileResponse("static/index.html")
return FileResponse(os.path.join(static_path, "index.html"))


@app.get("/tables", response_model=RList[Table])
Expand Down Expand Up @@ -61,9 +68,3 @@ def change_db(conf: DBConf):
except Exception as e:
print(e)
return R.error(msg="请确认信息是否填写正确")


if __name__ == "__main__":
import uvicorn

uvicorn.run("main:app", reload=True, port=80)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ fastapi==0.105.0
sqlmodel==0.0.14
PyMySQL==1.1.0
isort==5.13.2
black==23.12.1
uvicorn==0.24.0.post1
yapf==0.40.2
Binary file added static/logo.icns
Binary file not shown.
Binary file added static/logo.ico
Binary file not shown.

0 comments on commit 6f290bb

Please sign in to comment.