rcc: port from bash to Go, for simpler cross-platform go-generate

This commit is contained in:
mappu 2024-10-26 14:20:36 +13:00
parent 448d6531ef
commit 9e48407b63
3 changed files with 89 additions and 55 deletions

89
cmd/miqt-rcc/main.go Normal file
View File

@ -0,0 +1,89 @@
// miqt-rcc compiles a Qt resource XML file (*.qrc) to a binary resource file and
// creates a Go stub to load it.
package main
import (
"flag"
"fmt"
"go/format"
"io/ioutil"
"os"
"os/exec"
"strings"
)
func main() {
// Parse arguments
input := flag.String("Input", "", "Path to .qrc input file")
outputRcc := flag.String("OutputRcc", "", "(Optional) Path to .rcc output file. If omitted, inferred from the input file path")
outputGo := flag.String("OutputGo", "", "(Optional) Path to .go output file. If omitted, interred from the input file path")
packageName := flag.String("Package", "main", "Package to use in generated Go files")
variableName := flag.String("VariableName", "_resourceRcc", "Temporary global variable name for loading embedded data")
flag.Parse()
// Check if input file exists
if _, err := os.Stat(*input); os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Input file '%s' not found\n", *input)
os.Exit(1)
}
// Fill in default output names, if not specified
if *outputRcc == "" {
*outputRcc = strings.TrimSuffix(*input, `.qrc`) + `.rcc`
}
if *outputGo == "" {
*outputGo = strings.TrimSuffix(*input, `.qrc`) + `.go`
}
// Compile qrc to binary resource file
rccCmd := exec.Command(`rcc`, `--binary`, `-o`, *outputRcc, *input)
rccCmd.Stderr = os.Stderr
rccCmd.Stdout = os.Stdout
err := rccCmd.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "rcc: %s\n", err.Error())
os.Exit(1)
}
// Create Go file that loads the resource
goSrcData := `
package ` + *packageName + `
//go:generate miqt-rcc "` + strings.Join(os.Args[1:], `" "`) + `"
import (
"embed"
"github.com/mappu/miqt/qt"
)
//go:embed ` + *outputRcc + `
var ` + *variableName + ` []byte
func init() {
_ = embed.FS{}
qt.QResource_RegisterResourceWithRccData(& ` + *variableName + `[0])
}
`
outputData, err := format.Source([]byte(goSrcData))
if err != nil {
panic(err)
}
err = ioutil.WriteFile(*outputGo, outputData, 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Writing to '%s': %s\n", *outputGo, err.Error())
os.Exit(1)
}
fmt.Println("RCC OK")
os.Exit(0)
}

BIN
cmd/miqt-rcc/miqt-rcc Executable file

Binary file not shown.

View File

@ -1,55 +0,0 @@
#!/bin/bash
#
# miqt-rcc compiles a Qt resource XML file (*.qrc) to a binary resource file and
# creates a Go stub to load it.
set -eu
main() {
if [[ $# -lt 1 ]] ; then
echo "Usage: miqt-rcc.sh input.qrc [output.rcc] [output.go] [packageName] [variableName]" >&2
exit 1
fi
local ARG_INPUT_QRC="$1"
local ARG_DEST_RCC="${2:-$(basename -s .qrc "$ARG_INPUT_QRC").rcc}"
local ARG_DEST_GO="${3:-$(basename -s .rcc "$ARG_DEST_RCC").go}"
local ARG_PACKAGENAME="${4:-main}"
local ARG_VARIABLENAME="${5:-_resourceRcc}"
if [[ ! -f ${ARG_INPUT_QRC} ]] ; then
echo "Input file ${ARG_INPUT_QRC} not found" >&2
exit 1
fi
# Compile qrc to binary resource file
rcc --binary -o "${ARG_DEST_RCC}" "$ARG_INPUT_QRC"
# Create Go file that loads the resource
cat > "${ARG_DEST_GO}" <<EOF
package ${ARG_PACKAGENAME}
//go:generate miqt-rcc.sh $@
import (
"embed"
"github.com/mappu/miqt/qt"
)
//go:embed ${ARG_DEST_RCC}
var ${ARG_VARIABLENAME} []byte
func init() {
_ = embed.FS{}
qt.QResource_RegisterResourceWithRccData(&${ARG_VARIABLENAME}[0])
}
EOF
gofmt -s -w "${ARG_DEST_GO}"
echo "RCC OK"
}
main "$@"