5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-05 05:52:33 +08:00
wails/v3/internal/commands/build-assets.go
Lea Anthony e5c14d24ae
Initial implementation of dev mode.
Huge thanks to @atterpac!
2024-01-19 21:34:49 +11:00

79 lines
2.2 KiB
Go

package commands
import (
"embed"
_ "embed"
"fmt"
"github.com/leaanthony/gosod"
"io/fs"
"os"
"path/filepath"
"runtime"
"strings"
"time"
)
//go:embed build_assets
var buildAssets embed.FS
type BuildAssetsOptions struct {
Dir string `description:"The directory to generate the files into" default:"build"`
Name string `description:"The name of the project"`
Binary string `description:"The name of the binary"`
ProductName string `description:"The name of the product" default:"My Product"`
ProductDescription string `description:"The description of the product" default:"My Product Description"`
ProductVersion string `description:"The version of the product" default:"0.1.0"`
ProductCompany string `description:"The company of the product" default:"My Company"`
ProductCopyright string `description:"The copyright notice" default:"\u00a9 now, My Company"`
ProductComments string `description:"Comments to add to the generated files" default:"This is a comment"`
ProductIdentifier string `description:"The product identifier, e.g com.mycompany.myproduct"`
Silent bool `description:"Suppress output to console"`
}
func GenerateBuildAssets(options *BuildAssetsOptions) error {
var err error
options.Dir, err = filepath.Abs(options.Dir)
if err != nil {
return err
}
// If directory doesn't exist, create it
if _, err := os.Stat(options.Dir); os.IsNotExist(err) {
err = os.MkdirAll(options.Dir, 0755)
if err != nil {
return err
}
}
if options.ProductComments == "" {
options.ProductComments = fmt.Sprintf("(c) %d %s", time.Now().Year(), options.ProductCompany)
}
if options.ProductIdentifier == "" {
options.ProductIdentifier = "com.wails." + normaliseName(options.Name)
}
if options.Binary == "" {
options.Binary = normaliseName(options.Name)
if runtime.GOOS == "windows" {
options.Binary += ".exe"
}
}
tfs, err := fs.Sub(buildAssets, "build_assets")
if err != nil {
return err
}
if !options.Silent {
println("Generating build assets in " + options.Dir)
}
return gosod.New(tfs).Extract(options.Dir, options)
}
func normaliseName(name string) string {
return strings.ToLower(strings.ReplaceAll(name, " ", "-"))
}