Skip to content

Commit

Permalink
fix: 完善开发文档 - 支持全平台部署开发调试
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Feb 26, 2024
1 parent 7b216b8 commit c06abff
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 717 deletions.
25 changes: 19 additions & 6 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,32 @@ dev

```bash
# siyuan plugin
# -d 默认 dist
# -t 默认 dist
pnpm makeLink -p siyuan
pnpm dev -p siyuan

# siyuan plugin
# pnpm makeLink -p widget
# pnpm dev -p widget -t
# siyuan widget
pnpm makeLink -p widget -d widget -t widget
# -d 默认 widget
pnpm dev -p widget

# chrome extension
# -d 默认 chrome,实际地址是:extension/extension
pnpm dev -p chrome

# firefox extension
# -d 默认 firefox,实际地址是:extension/firefox
pnpm dev -p firefox

# nginx
pnpm dev -p nginx -d nginx
# -d 默认 nginx
pnpm dev -p nginx
# ================================================
# serve nginx
cd ./nginx
# npm i -g serve
serve -l 9000 -C
```

## Build
Expand All @@ -43,11 +56,11 @@ artifacts structure

```
├── build
├── package-widget.zip
├── package.zip
├── package-widget.zip
├── siyuan-plugin-publisher-1.19.1.zip
├── siyuan-publisher-nginx-1.19.1
├── sy-post-publisher-chrome-1.19.1.zip
├── sy-post-publisher-edge-1.19.1.zip
├── sy-post-publisher-firefox-1.19.1.zip
└── sy-post-publisher-widget-1.19.1.zip
```
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>发布工具挂件版</title>
<title>思源笔记发布工具</title>
<%- injectScript %>
</head>
<body>
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
"vercelBuild": "python3 scripts/vercel_build.py",
"nginxBuild": "python scripts/nginx_build.py",
"widgetBuild": "python scripts/widget_build.py",
"widgetTest": "python scripts/widget_build.py -t",
"extBuild": "python scripts/ext_build.py",
"syncVersion": "python scripts/version.py",
"parseChangelog": "python scripts/parse_changelog.py",
Expand Down
62 changes: 55 additions & 7 deletions scripts/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,72 @@
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output.")
parser.add_argument("-p", "--platform", help="Build for different platforms, like siyuan, widget, static.")
parser.add_argument("-t", "--type", help="Build browser extension for publishing, like chrome, edge, firefox.")
parser.add_argument("-d", "--dist", required=False, help="the dist for building files")
args = parser.parse_args()

if args.verbose:
print("Verbose mode enabled.")

# 设置环境变量
if not args.platform:
if not args.platform or args.platform == '':
args.platform = 'siyuan'
if not args.dist or args.dist == '':
args.dist = args.platform
dist_name = args.dist
os.environ['BUILD_TYPE'] = args.platform

# zhi-build
zhi_build_cmd = "zhi-build --serve --production -d " + args.dist
print(zhi_build_cmd)
os.system(zhi_build_cmd)
if args.platform == 'plugin':
# zhi-build
zhi_build_cmd = "zhi-build --serve --production -d " + args.dist
print(zhi_build_cmd)
os.system(zhi_build_cmd)
elif args.platform == 'widget':
# 复制挂件需要的其他文件
dist_folder = f"./{args.dist}/"
if not os.path.exists(dist_folder):
os.makedirs(dist_folder)
scriptutils.cp_file("./LICENSE", dist_folder)
scriptutils.cp_file("./src/assets/README.md", dist_folder)
scriptutils.cp_file("./src/assets/README_zh_CN.md", dist_folder)
scriptutils.cp_file("./widget.json", dist_folder)
scriptutils.cp_file("./src/assets/icon.png", dist_folder)
scriptutils.cp_file("./src/assets/preview.png", dist_folder)
scriptutils.cp_file("./policy.md", dist_folder)
print("复制挂件需要的其他文件.")
elif args.platform == 'chrome' or args.platform == 'edge' or args.platform == 'firefox':
if not args.dist or args.dist == '':
args.dist = args.platform
dist_name = f"extension/{args.dist}"
dist_folder = "./" + dist_name + "/"
print("Building folder for " + dist_name)
print("Building folder path: " + dist_folder)

# Copy necessary files.
scriptutils.cp_folder("./src/extensions", dist_folder)
scriptutils.cp_file("./LICENSE", dist_folder)
# scriptutils.cp_file("./src/assets/key.pem", dist_folder)
print("Copied required extension files.")

# Make some adaptors
if args.platform == 'chrome' or args.platform == 'edge':
# Delete Firefox configuration.
scriptutils.rm_folder(dist_folder + "mv2")
print("Deleted Firefox configuration.")
elif args.platform == 'firefox':
scriptutils.mv_file(dist_folder + "mv2/manifest-v2-for-firefox.json", dist_folder + "manifest.json")
scriptutils.mv_file(dist_folder + "mv2/background-v2-for-firefox.js", dist_folder + "background.js")
scriptutils.rm_folder(dist_folder + "mv2")
print("Deleted Chrome configuration.")
else:
raise Exception("Not supported platform: " + args.platform)

os.environ["VITE_SIYUAN_API_URL"] = "http://127.0.0.1:6806"
elif args.platform == 'nginx':
pass
else:
pass

# vite-build
vite_cmd = "vue-tsc --noEmit && vite build --watch --outDir " + args.dist
vite_cmd = "vue-tsc --noEmit && vite build --watch --outDir " + dist_name
print(vite_cmd)
os.system(vite_cmd)
20 changes: 6 additions & 14 deletions scripts/ext_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ def do_chrome_package(source_folder, is_chrome):
scriptutils.rm_folder(source_folder + "mv2")
print("Deleted Firefox configuration.")

# Copy the files to the chrome folder.
scriptutils.mkdir("./extension/chrome")
scriptutils.cp_folder(source_folder, "./extension/chrome")

# Package Chrome extension.
data = scriptutils.read_json_file(cwd + "package.json")
version = data["version"]
Expand Down Expand Up @@ -75,14 +71,8 @@ def do_firefox_package(source_folder):
scriptutils.mv_file(source_folder + "mv2/manifest-v2-for-firefox.json", source_folder + "manifest.json")
scriptutils.mv_file(source_folder + "mv2/background-v2-for-firefox.js", source_folder + "background.js")
scriptutils.rm_folder(source_folder + "mv2")

print("Deleted Chrome configuration.")

# Copy the files to the firefox folder.
scriptutils.mkdir("./extension/firefox")
scriptutils.cp_folder(source_folder, "./extension/firefox")
print("Published Firefox V2 extension.")

# Package Firefox extension.
data = scriptutils.read_json_file(cwd + "package.json")
version = data["version"]
Expand Down Expand Up @@ -115,7 +105,9 @@ def do_firefox_package(source_folder):
print("Verbose mode enabled.")

# Build the project.
dist_name = "extension/dist"
if not args.type:
args.type = "chrome"
dist_name = f"extension/{args.type}"
if args.dist is not None and args.dist != "":
dist_name = str(args.dist)
dist_folder = "./" + dist_name + "/"
Expand All @@ -131,10 +123,10 @@ def do_firefox_package(source_folder):
# scriptutils.cp_file("./src/assets/key.pem", dist_folder)
print("Copied required extension files.")

# Set the BUILD_TYPE environment variable in node.
os.environ["BUILD_TYPE"] = "chrome"
# Set the BUILD_TYPE environment variable in node
os.environ["BUILD_TYPE"] = args.type
os.environ["VITE_SIYUAN_API_URL"] = "http://127.0.0.1:6806"
print(f"BUILD_TYPE=>chrome")
print(f"BUILD_TYPE=>{args.type}")
build_cmd = "vue-tsc --noEmit && vite build --outDir " + dist_name
print("Build command: " + build_cmd)
os.system(build_cmd)
Expand Down
24 changes: 13 additions & 11 deletions scripts/make_dev_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,27 @@ def get_siyuan_dir():
return conf['data']


def choose_target(workspaces):
def choose_target(workspaces, plugin_type="plugin"):
count = len(workspaces)
_log(f'>>> Got {count} SiYuan {"workspaces" if count > 1 else "workspace"}')
for i, workspace in enumerate(workspaces):
_log(f'\t[{i}] {workspace["path"]}')

if count == 1:
return f'{workspaces[0]["path"]}/data/plugins'
return f'{workspaces[0]["path"]}/data/{plugin_type}s'
else:
index = input(f'\tPlease select a workspace[0-{count - 1}]: ')
return f'{workspaces[int(index)]["path"]}/data/plugins'
return f'{workspaces[int(index)]["path"]}/data/{plugin_type}s'


def get_plugin_name():
def get_plugin_name(plugin_type="plugin"):
# 检查 plugin.json 是否存在
if not os.path.exists('./plugin.json'):
_error('失败!找不到 plugin.json')
if not os.path.exists(f'./{plugin_type}.json'):
_error('失败!找不到 {plugin_type}.json')
sys.exit(1)
# 获取插件名称
# 加载 plugin.json
with open('./plugin.json', 'r') as file:
# 加载 plugin.json or widget.json
with open(f'./{plugin_type}.json', 'r') as file:
plugin = json.load(file)
plugin_name = plugin.get('name')
if not plugin_name or plugin_name == '':
Expand Down Expand Up @@ -169,6 +169,7 @@ def _cmp_path(path1, path2):
parser.add_argument("-f", "--forder", required=False, help="the targetDir for building files")
parser.add_argument("-v", "--verbose", action="store_true", help="Enable verbose output.")
parser.add_argument("-p", "--platform", help="Build for different platforms, like siyuan, widget, static.")
parser.add_argument("-t", "--type", help="Build plugin type, like plugins, widgets.")
args = parser.parse_args()

if args.verbose:
Expand All @@ -180,11 +181,12 @@ def _cmp_path(path1, path2):
args.dist = 'dist'
devOutDir = args.dist
if args.forder:
# 用于其他模式
targetDir = args.forder
name = ''
else:
# 获取插件名称
name = get_plugin_name()
# 获取插件名称,用于思源笔记插件、挂件
name = get_plugin_name(args.type)
_log(f'>>> 成功获取到插件名称: {name}')

# 获取插件目录
Expand All @@ -203,7 +205,7 @@ def _cmp_path(path1, path2):
_error('\t无法从环境变量 "SIYUAN_PLUGIN_DIR" 获取 SiYuan 目录,失败!')
sys.exit(1)
else:
targetDir = choose_target(res)
targetDir = choose_target(res, args.type)
_log(f'>>> 成功获取到目标目录: {targetDir}')
# 检查目录是否存在
if not os.path.exists(targetDir):
Expand Down
21 changes: 9 additions & 12 deletions scripts/widget_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
parser.add_argument("-d", "--dist", required=False, help="the dist for building files")
parser.add_argument("-v", "--verbose", action="store_true", help="enable verbose output")
parser.add_argument("-nb", "--nobuild", action="store_true", help="ignore build")
parser.add_argument("-t", "--test", action="store_true", help="copy files to public workspace for local testing")
args = parser.parse_args()

if args.verbose:
Expand All @@ -49,6 +48,8 @@
if args.dist is not None and args.dist != "":
dist_name = str(args.dist)
dist_folder = "./" + dist_name + "/"
if not os.path.exists(dist_folder):
os.makedirs(dist_folder)
print("dist_name:" + dist_name)
print("dist_folder:" + dist_folder)

Expand All @@ -57,9 +58,6 @@
else:
# 在 node 里面可以通过 process.env.BUILD_TYPE 读取
os.environ["BUILD_TYPE"] = "widget"
build_cmd = "vue-tsc --noEmit && vite build --outDir " + dist_name
print("构建命令:" + build_cmd)
os.system(build_cmd)

# 复制挂件需要的其他文件
scriptutils.cp_file("./LICENSE", dist_folder)
Expand All @@ -70,6 +68,10 @@
scriptutils.cp_file("./src/assets/preview.png", dist_folder)
scriptutils.cp_file("./policy.md", dist_folder)
print("复制挂件需要的其他文件.")

build_cmd = "vue-tsc --noEmit && vite build --outDir " + dist_name
print("构建命令:" + build_cmd)
os.system(build_cmd)
print("项目构建完成.")

# 挂件打包
Expand All @@ -84,13 +86,8 @@

# 压缩dist为zip
scriptutils.zip_folder(src_folder, tmp_folder_name, build_zip_path, build_zip_name)
scriptutils.cp_file(os.path.join(build_zip_path, build_zip_name), os.path.join(build_zip_path, "package-widget.zip"))
scriptutils.cp_file(os.path.join(build_zip_path, build_zip_name),
os.path.join(build_zip_path, "package-widget.zip"))
print("将dist文件打包成zip,用于挂件版本发布.")

if args.test:
# scriptutils.cp_folder(dist_folder, "/Users/terwer/Documents/mydocs/SiYuanWorkspace/public/data/widgets/sy-post-publisher/")
# print("拷贝文件到本地 public 工作空间测试.")
scriptutils.cp_folder(dist_folder, "/Users/terwer/Documents/mydocs/SiYuanWorkspace/test/data/widgets/sy-post-publisher/")
print("拷贝文件到本地 test 工作空间测试.")

print("发布完毕.")
print("发布完毕.")
Binary file removed testdata/assets/3259282.jpeg
Binary file not shown.
Loading

0 comments on commit c06abff

Please sign in to comment.