refactor build steps

This commit is contained in:
Pig Fang 2020-08-15 14:49:58 +08:00
parent 2d48bf7b01
commit e265cb43a8
No known key found for this signature in database
GPG Key ID: A8198F548DADA9E2
3 changed files with 15 additions and 145 deletions

View File

@ -14,9 +14,15 @@ jobs:
uses: actions/checkout@v2
- name: Install dependencies
run: yarn
- name: Build
run: ./build.ps1
- name: Download nano
run: ./download-nano.ps1
shell: pwsh
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Build
run: ./nano
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish
run: ./publish.ps1
shell: pwsh

143
build.ps1
View File

@ -1,143 +0,0 @@
Install-Module powershell-yaml -Force
function Get-Trans {
param (
# Plugin name
[Parameter(Mandatory = $false)]
[string]
$Plugin,
# L10n key
[Parameter(Mandatory)]
[string]
$Key,
# Language
[Parameter(Mandatory)]
[string]
$Lang
)
process {
$realKey = $Key.Split("::")[1]
$segment = $realKey.Split(".")
$head = $segment[0]
$filePath = if ($Plugin) {
"./plugins/$Plugin/lang/$Lang/$head.yml"
}
else {
"./lang/$Lang/$head.yml"
}
$obj = Get-Content $filePath -Raw | ConvertFrom-Yaml
$temp = ""
for ($i = 0; $i -lt $segment.Count; $i++) {
if ($i -eq 0) {
continue
}
if ($obj[$segment[$i]]) {
$temp = $obj[$segment[$i]]
}
else {
return $Key
}
}
return $temp
}
}
Write-Host 'Marketplace Builder'
$env:NODE_ENV = 'production'
git clone "https://github.com/bs-community/plugins-dist.git" .dist
$registry = Get-Content '.dist/registry_zh_CN.json' | ConvertFrom-Json
$packages = $registry.packages
$plugins = Get-ChildItem -Path ./plugins -Directory | ForEach-Object { $_.Name }
yarn build
[PSCustomObject[]]$updated = @()
foreach ($plugin in $plugins) {
Set-Location "./plugins/$plugin"
$manifest = Get-Content "package.json" | ConvertFrom-Json
$version = $manifest.version
if ($packages | Where-Object { $_.name -eq $plugin -and $_.version -eq $version }) {
Set-Location '../../'
continue
}
$updated += @{
name = if ($manifest.title.Contains('::')) {
Get-Trans -Key $manifest.title -Lang zh_CN
}
else {
$manifest.title
}
version = $manifest.version
}
Write-Host "[$plugin] Bump to $version"
if (Test-Path 'node_modules') {
Remove-Item 'node_modules' -Recurse -Force
}
if (Test-Path '.gitignore') {
Remove-Item '.gitignore' -Force
}
Get-ChildItem ./assets -Recurse | ForEach-Object {
if ($_.Name.EndsWith('.ts')) {
Remove-Item $_
}
}
if (Test-Path 'composer.json') {
composer install
Remove-Item 'composer.json' -Force
Remove-Item 'composer.lock' -Force
}
Set-Location '..'
zip -9 -r "../.dist/${plugin}_$version.zip" $plugin
Set-Location '..'
}
ConvertTo-Json $updated | Out-File -FilePath 'updated.json'
foreach ($lang in 'en', 'zh_CN') {
$packages = $plugins | ForEach-Object {
$manifest = Get-Content "./plugins/${_}/package.json" | ConvertFrom-Json
$name = $manifest.name
$version = $manifest.version
$url = "https://cdn.jsdelivr.net/gh/bs-community/plugins-dist/${name}_$version.zip"
[PSCustomObject]@{
name = $name
version = $version
title = if ($manifest.title.Contains('::')) {
Get-Trans -Plugin $_ -Key $manifest.title -Lang $lang
}
else {
$manifest.title
}
description = if ($manifest.title.Contains('::')) {
Get-Trans -Plugin $_ -Key $manifest.description -Lang $lang
}
else {
$manifest.description
}
author = $manifest.author
require = $manifest.require
dist = [PSCustomObject]@{
type = 'zip'
url = $url
shasum = (Get-FileHash -Path ".dist/${name}_$version.zip" -Algorithm SHA256).Hash.ToLower()
}
}
}
$registry.packages = $packages
ConvertTo-Json $registry -Depth 10 | Out-File -FilePath ".dist/registry_$lang.json"
}
Copy-Item -Path '.dist/registry_zh_CN.json' -Destination '.dist/registry.json'

7
download-nano.ps1 Normal file
View File

@ -0,0 +1,7 @@
$token = 'Bearer ' + $env:GITHUB_TOKEN
$headers = @{"authorization"=$token}
$nanoRelease = (Invoke-WebRequest 'https://api.github.com/repos/bs-community/nano/releases/latest' -Headers $headers).Content | ConvertFrom-Json
$nanoBinUrl = ((Invoke-WebRequest $nanoRelease.assets_url -Headers $headers).Content | ConvertFrom-Json).browser_download_url
bash -c "curl -fSL $nanoBinUrl --header 'authorization: $token' -o nano"
chmod +x ./nano