Skip to content

Commit

Permalink
feat(main): add test_update switch option
Browse files Browse the repository at this point in the history
  • Loading branch information
lengyanyu258 committed Jul 18, 2024
1 parent 5c4ff2c commit f186854
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 152 deletions.
75 changes: 75 additions & 0 deletions .github/actions/auto-update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Auto Update & Commit
description: "下载字体文件并执行脚本更新之后自动提交更改"

runs:
using: "composite"
steps:
- name: Download Sarasa Gothic Mono Slab SC TTF Release Asset
id: download_7z
uses: robinraju/[email protected]
with:
# The source repository path.
# Expected format {owner}/{repo}
# Default: ${{ github.repository }}
repository: "be5invis/Sarasa-Gothic"

# A flag to set the download target as latest release
# The default value is 'false'
latest: true

# The name of the file to download.
# Use this field only to specify filenames other than tarball or zipball, if any.
# Supports wildcard pattern (eg: '*', '*.deb', '*.zip' etc..)
fileName: "SarasaMonoSlabSC-TTF-Unhinted-*.7z"

# Relative path under $GITHUB_WORKSPACE to place the downloaded file(s)
# It will create the target directory automatically if not present
# eg: out-file-path: "my-downloads" => It will create directory $GITHUB_WORKSPACE/my-downloads
out-file-path: "tmp"

- name: Extract downloaded 7z file Asset
shell: bash
# p7zip-full is already the newest version (ubuntu-latest).
run: 7z x -t7z -otmp ${{ fromJson(steps.download_7z.outputs.downloaded_files)[0] }}

- run: poetry run python main.py --all --auto_update --publish

- name: Auto commit to repo.
id: auto_commit
uses: stefanzweifel/git-auto-commit-action@v5
with:
# Optional. Commit message for the created commit.
# Defaults to "Apply automatic changes"
commit_message: "docs(pages): AUTO update data[BOT]."

# Optional. Options used by `git-commit`.
# See https://git-scm.com/docs/git-commit#_options
commit_options: "--no-verify --signoff"

# Optional glob pattern of files which should be added to the commit
# Defaults to all (.)
# See the `pathspec`-documentation for git
# - https://git-scm.com/docs/git-add#Documentation/git-add.txt-ltpathspecgt82308203
# - https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec
file_pattern: "docs/"

# Optional. Option used by `git-status` to determine if the repository is
# dirty. See https://git-scm.com/docs/git-status#_options
status_options: "--untracked-files=no"

# Optional. Options used by `git-add`.
# See https://git-scm.com/docs/git-add#_options
add_options: "-u"

# Optional. Skip internal call to `git fetch`
skip_fetch: true

# Optional. Skip internal call to `git checkout`
skip_checkout: true

# Optional. Prevents the shell from expanding filenames.
# Details: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
disable_globbing: true

- if: steps.auto_commit.outputs.changes_detected == 'true'
run: echo "Update Done!"
116 changes: 0 additions & 116 deletions .github/workflows/update on schedule.yaml

This file was deleted.

56 changes: 56 additions & 0 deletions .github/workflows/update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Update Game Data

on:
schedule:
# UTC time for 10:30 & 16:30 (UTC+8)
- cron: "30 2,8 * * *"
workflow_dispatch:

# Global permissions configuration starts here
permissions:
# 'write' access to repository contents
contents: write

concurrency:
group: "update"
cancel-in-progress: false

jobs:
update:
runs-on: ubuntu-latest
steps:
- name: Cehckout root repo firstly.
uses: actions/checkout@v4

- name: Checkout ArknightsGameData Repo.
uses: actions/checkout@v4
with:
repository: "Kengxxiao/ArknightsGameData"
path: "Github/ArknightsGameData"
filter: "blob:none"
sparse-checkout: "zh_CN/gamedata/"

- name: Checkout ArknightsGameResource Repo.
uses: actions/checkout@v4
with:
repository: "yuanyan3060/ArknightsGameResource"
path: "Github/ArknightsGameResource"
filter: "blob:none"
sparse-checkout: "gamedata/"

- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v5
with:
python-version: "3.11"
cache: "poetry"
- name: Install Poetry Dependences.
run: poetry install

- name: Test if need update
id: test_update
run: poetry run python main.py --all --auto_update --test_update --no_dump

- if: steps.test_update.outputs.test_update == 'True'
name: Auto update data
uses: ./.github/actions/auto-update
15 changes: 9 additions & 6 deletions game_data/game_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class GameData(Count, Dump):
__unknown: dict[str, list[str]] = {"files": [], "commands": [], "heads": []}

__need_update: bool = False
__updated: bool = False
__counted: bool = False

Expand Down Expand Up @@ -81,8 +82,8 @@ def date(self) -> date:
return self.__date

@property
def updated(self) -> bool:
return self.__updated
def need_update(self) -> bool:
return self.__need_update

@Info("loading...")
def __load_data(self):
Expand All @@ -101,11 +102,13 @@ def parse_version(ver: str):
self.__version = parse_version(content.split(":")[-1].strip())
self.__date = date.fromisoformat(content.split()[-2].strip().replace("/", "-"))

old_version = parse_version(self.data["excel"]["gamedata_const"]["dataVersion"])
info_data = self.data.get("info", {}).get("data", {})
old_version = max(
parse_version(self.data["excel"]["gamedata_const"]["dataVersion"]),
parse_version(info_data.get("数据版本", "0.0.0")),
)
if self.version > old_version:
info_data = self.data.get("info", {}).get("data", {})
if self.version > parse_version(info_data.get("数据版本", "0.0.0")):
self.update()
self.__need_update = True

@Info("updating story...")
def __update_story(self):
Expand Down
76 changes: 47 additions & 29 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,29 @@
from config import Config
from .config import Config
from .game_data import GameData


def main():
def manipulate(game_data: GameData):
from datetime import datetime, timedelta, timezone

from game_data import GameData
# Used by GitHub Actions
if args.auto_update:
if not game_data.need_update:
print("No need to update!")

data_dir_set = {data_dir_path}
if args.all:
data_dir_set.update({*Config.DATA_DIRS})
if args.test_update:
import os

game_data_objs: list[GameData] = []
for data_dir in data_dir_set:
game_data_objs.append(
GameData(
data_dir_path=data_dir,
config=Config.game_data_config,
count_config=Config.count_config,
dump_config=Config.dump_config,
args=args,
)
)
game_data_objs.reverse()
for game_data in game_data_objs:
if game_data.updated:
break
else:
if args.auto_update:
print("No need to update!")
return
game_data = game_data_objs[-1]
# 设置环境变量以供 GitHub Actions 捕获
with open(os.environ["GITHUB_OUTPUT"], "a") as github_output:
print(f"test_update={game_data.need_update}", file=github_output)

if args.update:
if args.no_dump:
return

if args.update or game_data.need_update:
game_data.update()
if args.count:
game_data.count()
if args.no_dump:
return

datetime_now = datetime.now(timezone(timedelta(hours=8)))
info = {
Expand All @@ -60,6 +47,32 @@ def main():
game_data.publish(Config.xlsx_file_path, dumped_file)


def main():
data_dir_set = {data_dir_path}
if args.all:
data_dir_set.update({*Config.DATA_DIRS})

game_data_objs: list[GameData] = []
for data_dir in data_dir_set:
game_data_objs.append(
GameData(
data_dir_path=data_dir,
config=Config.game_data_config,
count_config=Config.count_config,
dump_config=Config.dump_config,
args=args,
)
)
game_data_objs.reverse()
for game_data in game_data_objs:
if game_data.need_update:
break
else:
game_data = game_data_objs[-1]

manipulate(game_data)


if __name__ == "__main__":
import argparse
import sys
Expand Down Expand Up @@ -114,6 +127,11 @@ def main():
action="store_true",
help="Try to update all DATA_DIRS & Publish it.",
)
switch.add_argument(
"--test_update",
action="store_true",
help="Test Update by GitHub Action flag.",
)
switch.add_argument(
"--auto_update",
action="store_true",
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.poetry]
package-mode = false
name = "arknights-word-count"
version = "3.7.1"
version = "3.8.0"
description = "《明日方舟》字词统计"
authors = ["Shawn Sun <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit f186854

Please sign in to comment.