Skip to content

Commit

Permalink
feat: #374 使用python重构项目构建脚本-重构挂件打包脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Feb 2, 2023
1 parent ff4dc4c commit 56317b0
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 64 deletions.
25 changes: 25 additions & 0 deletions components.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
/*
* Copyright (c) 2023, Terwer . All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Terwer designates this
* particular file as subject to the "Classpath" exception as provided
* by Terwer in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
* or visit www.terwer.space if you need additional information or have any
* questions.
*/

// generated by unplugin-vue-components
// We suggest you to commit this file into source control
// Read more: https://github.com/vuejs/core/pull/3399
Expand Down
2 changes: 2 additions & 0 deletions components/picgo/PicgoSetting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@
</template>
<script setup lang="ts">
import { isElectron } from "~/utils/browserUtil"
console.log("pico test")
</script>
46 changes: 34 additions & 12 deletions scripts/scriptutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import glob
import json
import os
import pathlib
import shutil
import sys
import zipfile
Expand Down Expand Up @@ -128,6 +129,21 @@ def rm_files(regex):
rm_file(file)


def cp_folder(src, dst, remove_folder=False):
"""
拷贝文件夹
:param src: 源文件夹,例如:"/path/to/source/folder"
:param dst: 目的地,例如:"/path/to/destination/folder"
:param remove_folder: 是否删除文件夹
"""
if os.path.exists(dst) and remove_folder:
rm_folder(dst)

if not os.path.exists(dst):
mkdir(dst)
shutil.copytree(src, dst, ignore_dangling_symlinks=True, dirs_exist_ok=True)


def mkdir(dirname):
"""
创建目录
Expand Down Expand Up @@ -169,19 +185,25 @@ def write_json_file(filename, data):
json.dump(data, f, indent=2, ensure_ascii=False)


def zip_folder(folder, filename):
def zip_folder(src_folder, tmp_folder_name, build_zip_path, build_zip_name):
"""
压缩文件夹为zip
:param folder: 文件夹
:param filename: 文件名
:return:
:param src_folder: 需要压缩的文件所在的目录
:param tmp_folder_name: 临时目录,也是解压后的默认目录
:param build_zip_path: zip保存目录
:param build_zip_name: zip文件名称
"""
with zipfile.ZipFile(filename, 'w') as zf:
for root, dirs, files in os.walk(folder):
for file in files:
# 第一行写法包括目录本身,第二行不包括目录
# zf.write(os.path.join(root, file))
zf.write(os.path.join(root, file), compress_type=zipfile.ZIP_DEFLATED)
mkdir(tmp_folder_name)
cp_folder(src_folder, tmp_folder_name)

mkdir(build_zip_path)
print("tmp_folder_name:" + tmp_folder_name)
print("build_zip_path:" + build_zip_path)
print("build_zip_name:" + build_zip_name)

rm_file(build_zip_name)
create_zip(tmp_folder_name, build_zip_name, [], build_zip_path)
rm_folder(tmp_folder_name)


def create_zip(root_path, file_name, ignored=[], storage_path=None):
Expand All @@ -202,11 +224,11 @@ def create_zip(root_path, file_name, ignored=[], storage_path=None):
else:
zip_root = os.path.join(root_path, file_name)

zipf = zipfile.ZipFile(zip_root, 'w', zipfile.ZIP_DEFLATED)
zipf = zipfile.ZipFile(zip_root, 'w', zipfile.ZIP_STORED)

def iter_subtree(path, layer=0):
# iter the directory
path = zipfile.Path(path)
path = pathlib.Path(path)
for p in path.iterdir():
if layer == 0 and p.name in ignored:
continue
Expand Down
41 changes: 30 additions & 11 deletions scripts/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,17 @@

import argparse
import os
import subprocess

import scriptutils

if __name__ == "__main__":
# 获取当前工作空间
cwd = scriptutils.get_workdir()

# 切换工作空间
scriptutils.switch_workdir()

# 获取当前工作空间
cwd = scriptutils.get_workdir()

# 参数解析
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--dist", required=False, help="the dist for building files")
Expand Down Expand Up @@ -129,20 +130,38 @@
# 读取 JSON 文件
data = scriptutils.read_json_file(cwd + "package.json")
v = data["version"]
build_folder_name = "./dist/"
build_zip_name = "build/sy-post-publisher-widget-" + v + ".zip"
scriptutils.mkdir("./build")
print("build_folder_name:" + build_folder_name)
print("build_zip_name:" + build_zip_name)

scriptutils.rm_file(build_zip_name)
scriptutils.zip_folder(build_folder_name, build_zip_name)

src_folder = dist_folder
tmp_folder_name = "./sy-post-publisher"
build_zip_path = "./build"
build_zip_name = "sy-post-publisher-widget-" + v + ".zip"

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

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

if args.publish:
os.chdir("../sy-post-publisher")
print("挂件发布切换路径,当前路径:" + os.getcwd())

# 删除所有文件
os.system("git rm -rf .")

# 拷贝文件
os.chdir("../src-sy-post-publisher")
scriptutils.cp_folder("./dist", "../sy-post-publisher")
scriptutils.cp_file("./.gitignore", "../sy-post-publisher")

# 添加新文件到仓库
os.chdir("../sy-post-publisher")
os.system("git add -A")

os.chdir("../src-sy-post-publisher")
print("路径还原,当前路径:" + os.getcwd())
print("拷贝文件到 sy-post-publisher 用于挂件版本发布.")

print("发布完毕.")
41 changes: 0 additions & 41 deletions scripts/widget.sh

This file was deleted.

0 comments on commit 56317b0

Please sign in to comment.