5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 02:50:36 +08:00

Use assets dir in project for all application assets

This commit is contained in:
Lea Anthony 2021-01-01 12:53:06 +11:00
parent c0fabc0bb7
commit 657df8bdda
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
15 changed files with 73 additions and 55 deletions

View File

@ -25,8 +25,8 @@ type Project struct {
// The path to the project directory
Path string
// Icons directory
IconsDir string `json:"icons_dir"`
// Assets directory
AssetsDir string `json:"assetsdir"`
// The output filename
OutputFilename string `json:"outputfilename"`
@ -75,9 +75,9 @@ func Load(projectPath string) (*Project, error) {
result.Name = "wailsapp"
}
// Set default icons directory if none given
if result.IconsDir == "" {
result.IconsDir = filepath.Join(result.Path, "icons")
// Set default assets directory if none given
if result.AssetsDir == "" {
result.AssetsDir = filepath.Join(result.Path, "assets")
}
// Fix up OutputFilename

View File

@ -49,39 +49,43 @@ func (b *BaseBuilder) fileExists(path string) bool {
return true
}
// buildStaticAssets will iterate through the projects static directory and add all files
// buildCustomAssets will iterate through the projects static directory and add all files
// to the application wide asset database.
func (b *BaseBuilder) buildStaticAssets(projectData *project.Project) error {
func (b *BaseBuilder) buildCustomAssets(projectData *project.Project) error {
// Add trailing slash to Asset directory
assetsDir := filepath.Join(projectData.Path, "assets") + "/"
assets := assetdb.NewAssetDB()
if b.fileExists(assetsDir) {
err := filepath.Walk(assetsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
normalisedPath := filepath.ToSlash(path)
localPath := strings.TrimPrefix(normalisedPath, assetsDir)
if len(localPath) == 0 {
return nil
}
if data, err := ioutil.ReadFile(filepath.Join(assetsDir, localPath)); err == nil {
assets.AddAsset(localPath, data)
}
return nil
})
customAssetsDir := filepath.Join(projectData.Path, "assets", "custom") + "/"
if !b.fileExists(customAssetsDir) {
err := fs.MkDirs(customAssetsDir)
if err != nil {
return err
}
}
assets := assetdb.NewAssetDB()
err := filepath.Walk(customAssetsDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
normalisedPath := filepath.ToSlash(path)
localPath := strings.TrimPrefix(normalisedPath, customAssetsDir)
if len(localPath) == 0 {
return nil
}
if data, err := ioutil.ReadFile(filepath.Join(customAssetsDir, localPath)); err == nil {
assets.AddAsset(localPath, data)
}
return nil
})
if err != nil {
return err
}
// Write assetdb out to root directory
assetsDbFilename := fs.RelativePath("../../../assetsdb.go")
b.addFileToDelete(assetsDbFilename)
err := ioutil.WriteFile(assetsDbFilename, []byte(assets.Serialize("assets", "wails")), 0644)
err = ioutil.WriteFile(assetsDbFilename, []byte(assets.Serialize("assets", "wails")), 0644)
if err != nil {
return err
}
@ -128,7 +132,7 @@ func (b *BaseBuilder) CleanUp() {
// Delete file. We ignore errors because these files will be overwritten
// by the next build anyway.
os.Remove(filename)
_ = os.Remove(filename)
})
}
@ -166,7 +170,7 @@ func (b *BaseBuilder) CompileProject(options *Options) error {
// Get application build directory
appDir := options.BuildDirectory
err := cleanBuildDirectory(options, options.Platform)
err := cleanBuildDirectory(options)
if err != nil {
return err
}
@ -307,8 +311,7 @@ func (b *BaseBuilder) NpmRunWithEnvironment(projectDir, buildTarget string, verb
}
// BuildFrontend executes the `npm build` command for the frontend directory
func (b *BaseBuilder) BuildFrontend(outputLogger *clilogger.CLILogger) error {
verbose := false
func (b *BaseBuilder) BuildFrontend(outputLogger *clilogger.CLILogger, verbose bool) error {
frontendDir := filepath.Join(b.projectData.Path, "frontend")

View File

@ -24,9 +24,15 @@ func newDesktopBuilder() *DesktopBuilder {
func (d *DesktopBuilder) BuildAssets(options *Options) error {
var err error
// Check icon directory exists
if !fs.DirExists(options.ProjectData.IconsDir) {
return fmt.Errorf("icon directory %s does not exist", options.ProjectData.IconsDir)
// Check assets directory exists
if !fs.DirExists(options.ProjectData.AssetsDir) {
// Path to default assets
defaultAssets := fs.RelativePath("./internal/assets")
// Copy the default assets directory
err := fs.CopyDir(defaultAssets, options.ProjectData.AssetsDir)
if err != nil {
return err
}
}
// Get a list of assets from the HTML
@ -42,7 +48,7 @@ func (d *DesktopBuilder) BuildAssets(options *Options) error {
}
// Build static assets
err = d.buildStaticAssets(d.projectData)
err = d.buildCustomAssets(d.projectData)
if err != nil {
return err
}
@ -95,7 +101,7 @@ func (d *DesktopBuilder) BuildBaseAssets(assets *html.AssetBundle, options *Opti
func (d *DesktopBuilder) processApplicationIcon(assetDir string) error {
// Copy default icon if one doesn't exist
iconFile := filepath.Join(d.projectData.IconsDir, "appicon.png")
iconFile := filepath.Join(d.projectData.AssetsDir, "appicon.png")
if !fs.FileExists(iconFile) {
err := fs.CopyFile(defaultIconPath(), iconFile)
if err != nil {

View File

@ -21,21 +21,29 @@ func (d *DesktopBuilder) convertToHexLiteral(bytes []byte) string {
return result
}
// We will compile all tray icons found at <projectdir>/icons/tray/*.png into the application
// We will compile all tray icons found at <projectdir>/assets/trayicons/*.png into the application
func (d *DesktopBuilder) processTrayIcons(assetDir string, options *Options) error {
var err error
// Get all the tray icon filenames
trayIconDirectory := filepath.Join(options.ProjectData.IconsDir, "tray")
var trayIconFilenames []string
if fs.DirExists(trayIconDirectory) {
trayIconFilenames, err = filepath.Glob(trayIconDirectory + "/*.png")
trayIconDirectory := filepath.Join(options.ProjectData.AssetsDir, "tray")
// If the directory doesn't exist, create it
if !fs.DirExists(trayIconDirectory) {
err = fs.MkDirs(trayIconDirectory)
if err != nil {
log.Fatal(err)
return err
}
}
var trayIconFilenames []string
trayIconFilenames, err = filepath.Glob(trayIconDirectory + "/*.png")
if err != nil {
log.Fatal(err)
return err
}
// Setup target
targetFilename := "trayicons"
targetFile := filepath.Join(assetDir, targetFilename+".c")
@ -108,12 +116,12 @@ func (d *DesktopBuilder) processDialogIcons(assetDir string, options *Options) e
var err error
// Get all the dialog icon filenames
dialogIconDirectory := filepath.Join(options.ProjectData.IconsDir, "dialog")
dialogIconDirectory := filepath.Join(options.ProjectData.AssetsDir, "dialog")
var dialogIconFilenames []string
// If the user has no custom dialog icons, copy the defaults
// If the directory does not exist, create it
if !fs.DirExists(dialogIconDirectory) {
defaultDialogIconsDirectory := fs.RelativePath("./internal/packager/icons/dialog")
defaultDialogIconsDirectory := fs.RelativePath("./internal/packager/icons/default/dialog")
err := fs.CopyDir(defaultDialogIconsDirectory, dialogIconDirectory)
if err != nil {
return err

View File

@ -28,7 +28,7 @@ func packageProject(options *Options, platform string) error {
}
// cleanBuildDirectory will remove an existing build directory and recreate it
func cleanBuildDirectory(options *Options, platform string) error {
func cleanBuildDirectory(options *Options) error {
buildDirectory := options.BuildDirectory
@ -49,8 +49,6 @@ func cleanBuildDirectory(options *Options, platform string) error {
return nil
}
func copyFileToBuildDirectory() {}
// Gets (and creates) the build base directory
func getBuildBaseDirectory(options *Options) (string, error) {
buildDirectory := filepath.Join(options.ProjectData.Path, "build")
@ -65,7 +63,7 @@ func getBuildBaseDirectory(options *Options) (string, error) {
// Gets the path to the default icon
func defaultIconPath() string {
return fs.RelativePath("internal/packager/icon64.png")
return fs.RelativePath("internal/packager/icon1024.png")
}
// Gets the platform dependent package assets directory

View File

@ -47,7 +47,7 @@ func packageApplication(options *Options) error {
}
// Generate Icons
err = processApplicationIcon(resourceDir, options.ProjectData.IconsDir)
err = processApplicationIcon(resourceDir, options.ProjectData.AssetsDir)
if err != nil {
return err
}
@ -56,15 +56,13 @@ func packageApplication(options *Options) error {
}
func processPList(options *Options, contentsDirectory string) error {
// Check if plist already exists in project dir
plistFile, err := fs.RelativeToCwd("info.plist")
if err != nil {
return err
}
plistFile := filepath.Join(options.ProjectData.AssetsDir, "mac", "info.plist")
// If the file doesn't exist, generate it
if !fs.FileExists(plistFile) {
err = generateDefaultPlist(options, plistFile)
err := generateDefaultPlist(options, plistFile)
if err != nil {
return err
}
@ -100,6 +98,11 @@ func generateDefaultPlist(options *Options, targetPlistFile string) error {
return err
}
// Create the directory if it doesn't exist
err = fs.MkDirs(filepath.Dir(targetPlistFile))
if err != nil {
return err
}
// Save the file
return ioutil.WriteFile(targetPlistFile, tpl.Bytes(), 0644)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 4.8 KiB

After

Width:  |  Height:  |  Size: 4.8 KiB

View File

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 12 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.0 KiB