5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-04 03:29:03 +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 // The path to the project directory
Path string Path string
// Icons directory // Assets directory
IconsDir string `json:"icons_dir"` AssetsDir string `json:"assetsdir"`
// The output filename // The output filename
OutputFilename string `json:"outputfilename"` OutputFilename string `json:"outputfilename"`
@ -75,9 +75,9 @@ func Load(projectPath string) (*Project, error) {
result.Name = "wailsapp" result.Name = "wailsapp"
} }
// Set default icons directory if none given // Set default assets directory if none given
if result.IconsDir == "" { if result.AssetsDir == "" {
result.IconsDir = filepath.Join(result.Path, "icons") result.AssetsDir = filepath.Join(result.Path, "assets")
} }
// Fix up OutputFilename // Fix up OutputFilename

View File

@ -49,25 +49,30 @@ func (b *BaseBuilder) fileExists(path string) bool {
return true 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. // 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 // Add trailing slash to Asset directory
assetsDir := filepath.Join(projectData.Path, "assets") + "/" customAssetsDir := filepath.Join(projectData.Path, "assets", "custom") + "/"
if !b.fileExists(customAssetsDir) {
err := fs.MkDirs(customAssetsDir)
if err != nil {
return err
}
}
assets := assetdb.NewAssetDB() assets := assetdb.NewAssetDB()
if b.fileExists(assetsDir) { err := filepath.Walk(customAssetsDir, func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(assetsDir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
} }
normalisedPath := filepath.ToSlash(path) normalisedPath := filepath.ToSlash(path)
localPath := strings.TrimPrefix(normalisedPath, assetsDir) localPath := strings.TrimPrefix(normalisedPath, customAssetsDir)
if len(localPath) == 0 { if len(localPath) == 0 {
return nil return nil
} }
if data, err := ioutil.ReadFile(filepath.Join(assetsDir, localPath)); err == nil { if data, err := ioutil.ReadFile(filepath.Join(customAssetsDir, localPath)); err == nil {
assets.AddAsset(localPath, data) assets.AddAsset(localPath, data)
} }
@ -76,12 +81,11 @@ func (b *BaseBuilder) buildStaticAssets(projectData *project.Project) error {
if err != nil { if err != nil {
return err return err
} }
}
// Write assetdb out to root directory // Write assetdb out to root directory
assetsDbFilename := fs.RelativePath("../../../assetsdb.go") assetsDbFilename := fs.RelativePath("../../../assetsdb.go")
b.addFileToDelete(assetsDbFilename) 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 { if err != nil {
return err return err
} }
@ -128,7 +132,7 @@ func (b *BaseBuilder) CleanUp() {
// Delete file. We ignore errors because these files will be overwritten // Delete file. We ignore errors because these files will be overwritten
// by the next build anyway. // 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 // Get application build directory
appDir := options.BuildDirectory appDir := options.BuildDirectory
err := cleanBuildDirectory(options, options.Platform) err := cleanBuildDirectory(options)
if err != nil { if err != nil {
return err return err
} }
@ -307,8 +311,7 @@ func (b *BaseBuilder) NpmRunWithEnvironment(projectDir, buildTarget string, verb
} }
// BuildFrontend executes the `npm build` command for the frontend directory // BuildFrontend executes the `npm build` command for the frontend directory
func (b *BaseBuilder) BuildFrontend(outputLogger *clilogger.CLILogger) error { func (b *BaseBuilder) BuildFrontend(outputLogger *clilogger.CLILogger, verbose bool) error {
verbose := false
frontendDir := filepath.Join(b.projectData.Path, "frontend") frontendDir := filepath.Join(b.projectData.Path, "frontend")

View File

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

View File

@ -21,21 +21,29 @@ func (d *DesktopBuilder) convertToHexLiteral(bytes []byte) string {
return result 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 { func (d *DesktopBuilder) processTrayIcons(assetDir string, options *Options) error {
var err error var err error
// Get all the tray icon filenames // Get all the tray icon filenames
trayIconDirectory := filepath.Join(options.ProjectData.IconsDir, "tray") 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 {
return err
}
}
var trayIconFilenames []string var trayIconFilenames []string
if fs.DirExists(trayIconDirectory) {
trayIconFilenames, err = filepath.Glob(trayIconDirectory + "/*.png") trayIconFilenames, err = filepath.Glob(trayIconDirectory + "/*.png")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return err return err
} }
}
// Setup target // Setup target
targetFilename := "trayicons" targetFilename := "trayicons"
targetFile := filepath.Join(assetDir, targetFilename+".c") targetFile := filepath.Join(assetDir, targetFilename+".c")
@ -108,12 +116,12 @@ func (d *DesktopBuilder) processDialogIcons(assetDir string, options *Options) e
var err error var err error
// Get all the dialog icon filenames // Get all the dialog icon filenames
dialogIconDirectory := filepath.Join(options.ProjectData.IconsDir, "dialog") dialogIconDirectory := filepath.Join(options.ProjectData.AssetsDir, "dialog")
var dialogIconFilenames []string 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) { if !fs.DirExists(dialogIconDirectory) {
defaultDialogIconsDirectory := fs.RelativePath("./internal/packager/icons/dialog") defaultDialogIconsDirectory := fs.RelativePath("./internal/packager/icons/default/dialog")
err := fs.CopyDir(defaultDialogIconsDirectory, dialogIconDirectory) err := fs.CopyDir(defaultDialogIconsDirectory, dialogIconDirectory)
if err != nil { if err != nil {
return err 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 // 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 buildDirectory := options.BuildDirectory
@ -49,8 +49,6 @@ func cleanBuildDirectory(options *Options, platform string) error {
return nil return nil
} }
func copyFileToBuildDirectory() {}
// Gets (and creates) the build base directory // Gets (and creates) the build base directory
func getBuildBaseDirectory(options *Options) (string, error) { func getBuildBaseDirectory(options *Options) (string, error) {
buildDirectory := filepath.Join(options.ProjectData.Path, "build") buildDirectory := filepath.Join(options.ProjectData.Path, "build")
@ -65,7 +63,7 @@ func getBuildBaseDirectory(options *Options) (string, error) {
// Gets the path to the default icon // Gets the path to the default icon
func defaultIconPath() string { func defaultIconPath() string {
return fs.RelativePath("internal/packager/icon64.png") return fs.RelativePath("internal/packager/icon1024.png")
} }
// Gets the platform dependent package assets directory // Gets the platform dependent package assets directory

View File

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