Skip to content

Commit

Permalink
自动根据metadata修改package.json、manifest.json
Browse files Browse the repository at this point in the history
  • Loading branch information
SSmJaE committed Mar 17, 2023
1 parent 7ed394e commit 9b8e72e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 60 deletions.
94 changes: 64 additions & 30 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,65 @@
name : release
name: release

on: push

on :
- push

jobs:
metadata:
name: 根据metadata,修改package.json
runs-on: ubuntu-latest

steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Setup Python # Set Python version
uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: 修改package.json
run: python ./scripts/version.py

- name: Commit changes
uses: EndBug/add-and-commit@v9
with:
default_author: github_actions
message: "根据metadata自动修改package.json的版本"
add: "package.json"

check:
name : 检查是否有新版本
name: 检查是否有新版本
runs-on: ubuntu-latest


needs: [metadata]

steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Check if version has been updated
id: check
uses: EndBug/version-check@v2

- name: Log when changed
if: steps.check.outputs.changed == 'true'
run: >
run: >
echo "Version change found in commit ${{ steps.check.outputs.commit }}!
New version: ${{ steps.check.outputs.version }} (${{ steps.check.outputs.type }})"'
outputs:
hasNewVersion: ${{ steps.check.outputs.changed }}

release:
name: 自动release
runs-on: ubuntu-latest

needs: [check]
if: needs.check.outputs.hasNewVersion == 'true'

steps:
- name: Checkout repo
uses: actions/checkout@v3

- name: Setup Node
uses: actions/setup-node@v3
with:
Expand All @@ -44,8 +68,8 @@ jobs:
- name: Install dependencies
uses: pnpm/action-setup@v2
with:
version : latest
run_install : false
version: latest
run_install: false

- name: Get pnpm store directory
id: pnpm-cache
Expand All @@ -66,35 +90,45 @@ jobs:

- name: 打包userscript
run: pnpm run build:js

- name: 发布userscript
uses: softprops/action-gh-release@v1
with:
tag_name : ${{ steps.check.outputs.version }}
tag_name: ${{ steps.check.outputs.version }}
files: ./dist/*.js

# 发布extension
- name: Setup Python # Set Python version
uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: Install dependencies
run: pip install python-dotenv
- name : 生成manifest.json
run: python ./scripts/metadata.py
- name : build extension

- name: 生成manifest.json
run: python ./scripts/manifest.py

- name: build extension
run: pnpm run build:crx
- name : zip
run : python ./scripts/zip.py
- name : 发布extension

- name: zip
run: python ./scripts/zip.py

- name: 发布extension
uses: softprops/action-gh-release@v1
with:
tag_name : ${{ steps.check.outputs.version }}
tag_name: ${{ steps.check.outputs.version }}
files: ./dist/*.zip


- name: 同步release到专门的repo中,方便第三方进行webhook
uses: actions/github-script@v6
with:
github-token: ${{ secrets.TOKEN }}
script: |
await github.rest.actions.createWorkflowDispatch({
owner: 'SSmJaE',
repo: 'MyUserScripts',
workflow_id: 'main.yml',
ref: 'master'
})
23 changes: 3 additions & 20 deletions scripts/metadata.py → scripts/manifest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
""" 根据metadata修改manifest.json """


import json
import os

Expand All @@ -16,23 +19,6 @@

PROJECT = META["projects"][PLATFORM]

# print(PROJECT)

# 根据metadata修改package.json

PACKAGE: dict

with open("package.json", "r", encoding="utf-8") as f:
package = json.load(f)


with open("package.json", "w", encoding="utf-8") as f:
package["version"] = PROJECT["version"]
json.dump(package, f, indent=4, ensure_ascii=False)


# 根据metadata修改manifest.json

with open("scripts/manifest.template.json", "r", encoding="utf-8") as f:
manifest = json.load(f)
manifest["version"] = PROJECT["version"]
Expand All @@ -49,6 +35,3 @@

with open("dist/manifest.json", "w", encoding="utf-8") as f2:
f2.write(json.dumps(manifest, indent=4, ensure_ascii=False))

# 根据metadata修改生成userscript的header
# 这个直接让vite-plugin-monkey来做
10 changes: 0 additions & 10 deletions scripts/release.py

This file was deleted.

35 changes: 35 additions & 0 deletions scripts/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import json
import os

from dotenv import load_dotenv

load_dotenv() # take environment variables from .env.

PLATFORM = os.getenv("COMPILE_PLATFORM", "welearn")


META: dict

with open("metadata.json", "r", encoding="utf-8") as f:
META = json.load(f)


PROJECT = META["projects"][PLATFORM]

# print(PROJECT)

# 根据metadata修改package.json

PACKAGE: dict

with open("package.json", "r", encoding="utf-8") as f:
package = json.load(f)


with open("package.json", "w", encoding="utf-8") as f:
package["version"] = PROJECT["version"]
json.dump(package, f, indent=4, ensure_ascii=False)


# 根据metadata修改生成userscript的header
# 这个直接让vite-plugin-monkey来做

0 comments on commit 9b8e72e

Please sign in to comment.