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 80c9898 commit 6e2d66d
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 91 deletions.
37 changes: 24 additions & 13 deletions scripts/dev.py
Expand Up @@ -40,22 +40,33 @@
# Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
# or visit www.terwer.space if you need additional information or have any
# questions.
#
# 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.

import os
import sys

if __name__ == "__main__":
CWD = "./"
if os.getcwd().endswith("scripts"):
CWD = "../"
import scriptutils

# 打印当前python版本
print("当前python版本:" + sys.version)
# 打印当前路径
print("当前路径:" + os.getcwd())

print("切换路径")
os.chdir(CWD)
print("当前路径:" + os.getcwd())
if __name__ == "__main__":
# 切换工作空间
scriptutils.switch_workdir()

os.system("vite --port 6006")
127 changes: 125 additions & 2 deletions scripts/scriptutils.py
Expand Up @@ -40,23 +40,60 @@
# Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
# or visit www.terwer.space if you need additional information or have any
# questions.
#
# 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.

import distutils
import glob
import json
import os
import shutil
import sys
import zipfile
from distutils import dir_util
from distutils import file_util


def switch_workdir():
def get_workdir():
"""
获取工作空间
"""
cwd = "./"
if os.getcwd().endswith("scripts"):
cwd = "../"

# 打印当前python版本
print("当前python版本:" + sys.version)
# 打印当前路径
print("当前路径:" + os.getcwd())
print("当前路径:" + os.path.abspath(cwd))

return cwd


def switch_workdir():
"""
切换工作空间
"""
# 获取当前工作空间
cwd = get_workdir()

print("切换路径")
os.chdir(cwd)
Expand Down Expand Up @@ -89,3 +126,89 @@ def rm_files(regex):
file_list = glob.glob(regex)
for file in file_list:
rm_file(file)


def mkdir(dirname):
"""
创建目录
:param dirname: 目录
"""
if not os.path.exists(dirname):
distutils.dir_util.mkpath(dirname)


def rm_folder(folder):
"""
删除文件夹,它会递归的删除文件夹中的所有文件和子文件夹
:param folder: 文件夹
"""
if os.path.exists(folder):
shutil.rmtree(folder)


def read_json_file(filename):
"""
读取 JSON 文件
:param filename: 文件名
"""
# 读取 JSON 文件
print("读取文件:" + os.path.abspath(filename))
with open(filename, "r", encoding="utf-8") as f:
data = json.load(f)
return data


def write_json_file(filename, data):
"""
写入 JSON 文件
:param filename: 文件名
:param data: JSON 数据
"""
# 写入 JSON 文件
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)


def zip_folder(folder, filename):
"""
压缩文件夹为zip
:param folder: 文件夹
:param filename: 文件名
:return:
"""
print("aaa")


def create_zip(root_path, file_name, ignored=[], storage_path=None):
"""Create a ZIP
This function creates a ZIP file of the provided root path.
Args:
root_path (str): Root path to start from when picking files and directories.
file_name (str): File name to save the created ZIP file as.
ignored (list): A list of files and/or directories that you want to ignore. This
selection is applied in root directory only.
storage_path: If provided, ZIP file will be placed in this location. If None, the
ZIP will be created in root_path
"""
if storage_path is not None:
zip_root = os.path.join(storage_path, file_name)
else:
zip_root = os.path.join(root_path, file_name)

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

def iter_subtree(path, layer=0):
# iter the directory
path = zipfile.Path(path)
for p in path.iterdir():
if layer == 0 and p.name in ignored:
continue
zipf.write(p, str(p).replace(root_path, '').lstrip('/'))

if p.is_dir():
iter_subtree(p, layer=layer + 1)

iter_subtree(root_path)
zipf.close()
37 changes: 24 additions & 13 deletions scripts/serve.py
Expand Up @@ -40,22 +40,33 @@
# Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
# or visit www.terwer.space if you need additional information or have any
# questions.
#
# 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.

import os
import sys

if __name__ == "__main__":
CWD = "./"
if os.getcwd().endswith("scripts"):
CWD = "../"
import scriptutils

# 打印当前python版本
print("当前python版本:" + sys.version)
# 打印当前路径
print("当前路径:" + os.getcwd())

print("切换路径")
os.chdir(CWD)
print("当前路径:" + os.getcwd())
if __name__ == "__main__":
# 切换工作空间
scriptutils.switch_workdir()

os.system("vercel dev --listen 6006")
52 changes: 31 additions & 21 deletions scripts/version.py
Expand Up @@ -40,11 +40,30 @@
# Please contact Terwer, Shenzhen, Guangdong, China, youweics@163.com
# or visit www.terwer.space if you need additional information or have any
# questions.
#
# 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.

import argparse
import json
import os
import sys

import scriptutils


def parse_json(filename, version_field, new_version):
Expand All @@ -56,29 +75,20 @@ def parse_json(filename, version_field, new_version):
"""

# 读取 JSON 文件
print("读取文件:" + os.path.abspath(filename))
with open(filename, "r", encoding="utf-8") as f:
data = json.load(f)
# print(data)
data = scriptutils.read_json_file(filename)

# 修改 JSON 文件中的属性
data[version_field] = new_version

# 将修改后的 JSON 写回到文件中
with open(filename, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
scriptutils.write_json_file(filename, data)


if __name__ == "__main__":
CWD = "./"
if os.getcwd().endswith("scripts"):
CWD = "../"

# 打印当前python版本
print("当前python版本:" + sys.version)
# 打印当前路径
print("当前路径:" + os.path.abspath(CWD))
# 获取当前工作空间
cwd = scriptutils.get_workdir()

# 参数解析
parser = argparse.ArgumentParser()
parser.add_argument("version", help="the file to be processed")
parser.add_argument("-v", "--verbose", action="store_true", help="enable verbose output")
Expand All @@ -88,13 +98,13 @@ def parse_json(filename, version_field, new_version):
print("Verbose mode enabled")

# widget.json
parse_json(CWD + "public/widget.json", "version", args.version)
parse_json(cwd + "public/widget.json", "version", args.version)

# manifest.json
parse_json(CWD + "public/manifest.dev.json", "version", args.version)
parse_json(CWD + "public/manifest.prod.json", "version", args.version)
parse_json(cwd + "public/manifest.dev.json", "version", args.version)
parse_json(cwd + "public/manifest.prod.json", "version", args.version)

# mv2 manifest.json
parse_json(CWD + "public/mv2/manifest-v2-for-firefox.json", "version", args.version)
parse_json(cwd + "public/mv2/manifest-v2-for-firefox.json", "version", args.version)

print("修改完毕,新版本为:" + args.version)

0 comments on commit 6e2d66d

Please sign in to comment.