Skip to content

Commit

Permalink
[ci] Add asset library publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
IceflowRE committed May 14, 2024
1 parent 0048575 commit f0963b7
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 51 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,60 @@ jobs:
addon:
name: ${{ matrix.config.name }}
runs-on: ubuntu-latest

#environment: asset-library
strategy:
fail-fast: false
matrix:
config:
- {
id: "aspect_ratio_resize_container",
name: "AspectRatioResizeContainer",
asset-id: "2089",
}
- {
id: "custom_theme_overrides",
name: "Custom Theme Overrides",
asset-id: "2091",
}
- {
id: "git_sha_project_setting",
name: "Git SHA Project Setting",
asset-id: "1979",
}
- {
id: "glogging",
name: "GLogging",
asset-id: "no-deploy",
}
- {
id: "hide_private_properties",
name: "Hide Private Properties",
asset-id: "1989",
}
- {
id: "icon_explorer",
name: "Icon Explorer",
asset-id: "2511",
}
- {
id: "icons_patcher",
name: "Icons Patcher",
asset-id: "1980",
}
- {
id: "licenses",
name: "License Manager",
asset-id: "1969",
}
- {
id: "qr_code",
name: "QR Code",
asset-id: "2090",
}
- {
id: "texture_button_colored",
name: "TextureButtonColored",
asset-id: "2092",
}

steps:
Expand Down Expand Up @@ -102,13 +112,19 @@ jobs:
run: sleep 5s
shell: bash

- name: Release addon
- name: Deploy to GitHub Release
if: ${{ steps.checkTag.outputs.exists == 'false' }}
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
gh release create ${{ matrix.config.id }}-${{ steps.prepare-artifacts.outputs.version }} ./archives/* --title "${{ matrix.config.name }} ${{ steps.prepare-artifacts.outputs.version }}" --notes "${{ steps.prepare-artifacts.outputs.notes }}"
#- name: Deploy to Godot Asset Library
# if: ${{ matrix.config.asset-id != 'no-deploy' }}
# working-directory: publisher
# run: |
# go run . asset-library -b ../ -a ${{ matrix.config.id }} -u "${{ secrets.GD_ASSET_LIBRARY_USER }}" -p "${{ secrets.GD_ASSET_LIBRARY_PASSWORD }}" --asset-id "${{ matrix.config.asset-id }}"

combined-release:
name: Combined Release
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion addons/aspect_ratio_resize_container/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ classifiers=[
]

[plugin.dependencies]
godot=">=4.2"
godot=">=4.2"
2 changes: 1 addition & 1 deletion addons/git_sha_project_setting/plugin.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[plugin]

name="Git SHA Project Setting"
description="Adds Git SHA as prohect setting at application/config/git_sha"
description="Adds Git SHA as project setting at application/config/git_sha"
author="Iceflower S"
version="2.1.1"
script="plugin.gd"
Expand Down
149 changes: 123 additions & 26 deletions publisher/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,66 +7,97 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"strings"

"github.com/spf13/cobra"

"github.com/kenyoni-software/godot-addons/publisher/internal"
)

const (
actionGithub = "github"
actionZip = "zip"
)

type githubActionCfg struct {
OutputFile string
}

type zipActionCfg struct {
OutputDir string
}

type assetLibraryActionCfg struct {
Username string
Password string
AssetId string
Host string
}

type cli struct {
rootCmd *cobra.Command

Addon string
BaseDir string

GithubAction struct {
OutputFile string
}
ZipAction struct {
OutputDir string
}
GithubAction githubActionCfg
ZipAction zipActionCfg
AssetLibrary assetLibraryActionCfg
}

func newCli() *cli {
c := cli{
GithubAction: struct{ OutputFile string }{},
ZipAction: struct{ OutputDir string }{},
GithubAction: githubActionCfg{},
AssetLibrary: assetLibraryActionCfg{},
ZipAction: zipActionCfg{},
}

c.rootCmd = &cobra.Command{
Use: "publisher",
Short: "Kenyoni Godot Addon publishing helper",
}
c.rootCmd.PersistentFlags().StringVarP(&c.BaseDir, "baseDir", "b", "./", "Base directory of the project.")
c.rootCmd.MarkFlagRequired("baseDir")
c.rootCmd.PersistentFlags().StringVarP(&c.Addon, "addon", "a", "", "Addon to proceed.")
c.rootCmd.MarkFlagRequired("addon")
c.rootCmd.PersistentFlags().StringVarP(&c.Addon, "Addon", "a", "", "Addon to proceed.")
c.rootCmd.MarkFlagRequired("Addon")

ghCmd := &cobra.Command{
Use: "github",
Short: "Save information about an addon to the given file, to be used with $GITHUB_OUTPUT",
Short: "Save information about an Addon to the given file, to be used with $GITHUB_OUTPUT",
Run: func(cmd *cobra.Command, args []string) {
doActionGithub(c.BaseDir, c.Addon, c.GithubAction.OutputFile)
doActionGithub(c.BaseDir, c.Addon, c.GithubAction)
},
}
ghCmd.Flags().StringVarP(&c.GithubAction.OutputFile, "output", "o", "", "Output file to write the result into.")
ghCmd.MarkFlagRequired("output")

gdAssetCmd := &cobra.Command{
Use: "asset-library",
Short: "Publish given Addon to an Asset Library.",
Run: func(cmd *cobra.Command, args []string) {
doActionAssetLibrary(c.BaseDir, c.Addon, c.AssetLibrary)
},
}
gdAssetCmd.Flags().StringVarP(&c.AssetLibrary.AssetId, "asset-id", "", "", "Asset ID.")
gdAssetCmd.MarkFlagRequired("asset-id")
gdAssetCmd.Flags().StringVarP(&c.AssetLibrary.Username, "username", "u", "", "Asset Library username.")
gdAssetCmd.MarkFlagRequired("username")
gdAssetCmd.Flags().StringVarP(&c.AssetLibrary.Password, "password", "p", "", "Asset Library password.")
gdAssetCmd.MarkFlagRequired("password")
// gdAssetCmd.Flags().StringVarP(&c.AssetLibrary.Host, "host", "host", "https://godotengine.org/asset-library/api", "Asset Library Host URL.")
gdAssetCmd.Flags().StringVarP(&c.AssetLibrary.Host, "host", "", "http://localhost:8080/asset-library/api", "Asset Library Host URL.")

zipCmd := &cobra.Command{
Use: "zip",
Short: "Zip specified addon release ready.",
Short: "Zip specified Addon release ready.",
Run: func(cmd *cobra.Command, args []string) {
doActionZip(c.BaseDir, c.Addon, c.ZipAction.OutputDir)
doActionZip(c.BaseDir, c.Addon, c.ZipAction)
},
}
zipCmd.Flags().StringVarP(&c.ZipAction.OutputDir, "output", "o", "", "Output directory to place the archive into.")
zipCmd.MarkFlagRequired("output")

c.rootCmd.AddCommand(ghCmd, zipCmd)
c.rootCmd.AddCommand(ghCmd, gdAssetCmd, zipCmd)

return &c
}
Expand All @@ -79,9 +110,9 @@ func (c *cli) Execute() {
}
}

func doActionGithub(baseDir string, addon string, outputFile string) {
addonCfg := newAddon(addon, baseDir)
plg, err := addonCfg.GetPluginCfg()
func doActionGithub(baseDir string, addonId string, cfg githubActionCfg) {
addon := internal.NewAddon(addonId, baseDir)
plg, err := addon.GetPluginCfg()
if err != nil {
log.Fatalln(err)
}
Expand All @@ -98,7 +129,7 @@ func doActionGithub(baseDir string, addon string, outputFile string) {
}
outputStr += fmt.Sprintf("notes<<%s\n%s%s\n", io.EOF, descStr, io.EOF)

oFile, err := os.OpenFile(outputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
oFile, err := os.OpenFile(cfg.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -109,8 +140,70 @@ func doActionGithub(baseDir string, addon string, outputFile string) {
}
}

func doActionZip(baseDir string, addon string, outputDir string) {
addonDir := filepath.Join(baseDir, "addons", addon)
var rxMinVersion = regexp.MustCompile("(?:>=)?(.+)")

func doActionAssetLibrary(baseDir string, addonId string, cfg assetLibraryActionCfg) {
addonDir := filepath.Join(baseDir, "addons", addonId)
_, err := os.Stat(addonDir)
if errors.Is(err, os.ErrNotExist) {
log.Fatalf("Directory '%s' does not exist", addonDir)
}
if err != nil {
log.Fatalln(err)
}

addon := internal.NewAddon(addonId, baseDir)
plgCfg, err := addon.GetPluginCfg()
if err != nil {
log.Fatalln(err)
}

alClient := internal.NewAssetLibraryClient(cfg.Host)
err = alClient.Login(cfg.Username, cfg.Password)
if err != nil {
log.Fatalf("Asset Library login failed: %v\n", err)
}
defer func(alClient *internal.AssetLibraryClient) {
err := alClient.Logout()
if err != nil {
log.Fatalf("logout failed: %v\n", err)
}
}(alClient)

gdMinversionMatch := rxMinVersion.FindStringSubmatch(plgCfg.Plugin.Dependencies.Godot)
var gdMinversion string
if gdMinversionMatch != nil {
gdMinversion = gdMinversionMatch[1]
} else {
log.Fatalln("could not retrieve Godot minimum version")
}
assetData := internal.AssetData{
AssetId: cfg.AssetId,
Title: plgCfg.Plugin.Name,
Description: plgCfg.Plugin.Description,
VersionString: plgCfg.Plugin.Version,
GodotVersion: gdMinversion,
CategoryId: internal.Cat2DTools,
License: "MIT",
DownloadProvider: "Custom",
DownloadCommit: fmt.Sprintf("https://github.com/kenyoni-software/godot-addons/releases/download/%s-%s/%s-%s.zip", addonId, plgCfg.Plugin.Version, addonId, strings.ReplaceAll(plgCfg.Plugin.Version, ".", "_")),
BrowseUrl: "https://github.com/kenyoni-software/godot-addons",
IssuesUrl: "https://github.com/kenyoni-software/godot-addons/issues",
IconUrl: "https://godotengine.org/assets/press/icon_color.png",
}
if cfg.AssetId == "" {
err = alClient.UpdateAssetEdit("18", assetData)
} else {
err = alClient.UpdateAsset(assetData)
}
if err != nil {
log.Fatalf("asset update failed: %v\n", err)
}

}

func doActionZip(baseDir string, addonId string, cfg zipActionCfg) {
addonDir := filepath.Join(baseDir, "addons", addonId)
_, err := os.Stat(addonDir)
if errors.Is(err, os.ErrNotExist) {
log.Fatalf("Directory '%s' does not exist", addonDir)
Expand All @@ -119,13 +212,17 @@ func doActionZip(baseDir string, addon string, outputDir string) {
log.Fatalln(err)
}

addonCfg := newAddon(addon, baseDir)
plgCfg, err := addonCfg.GetPluginCfg()
addon := internal.NewAddon(addonId, baseDir)
plgCfg, err := addon.GetPluginCfg()
if err != nil {
log.Fatalln(err)
}
outputFile := filepath.Join(addonCfg.ProjectPath(), "archives", addonCfg.Id()+"-"+strings.ReplaceAll(plgCfg.Plugin.Version, ".", "_")+".zip")
err = addonCfg.Zip(outputFile)
outputDir := cfg.OutputDir
if outputDir == "" {
outputDir = filepath.Join(addon.ProjectPath(), "archives")
}
outputFile := filepath.Join(outputDir, addon.Id()+"-"+strings.ReplaceAll(plgCfg.Plugin.Version, ".", "_")+".zip")
err = addon.Zip(outputFile)
if err != nil {
log.Fatalln(err)
}
Expand Down
Loading

0 comments on commit f0963b7

Please sign in to comment.