Skip to content

Commit

Permalink
Added data directory path to options
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Jul 29, 2018
1 parent d0a0ea8 commit 36e83aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions astilectron.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Options struct {
AppIconDarwinPath string // Darwin systems requires a specific .icns file
AppIconDefaultPath string
BaseDirectoryPath string
DataDirectoryPath string
ElectronSwitches []string
}

Expand Down
23 changes: 21 additions & 2 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,14 @@ func newPaths(os, arch string, o Options) (p *Paths, err error) {
return
}

// Init data directory path
if err = p.initDataDirectory(o.DataDirectoryPath, o.AppName); err != nil {
err = errors.Wrap(err, "initializing data directory failed")
return
}

// Init other paths
//!\\ Order matters
p.initDataDirectory(o.AppName)
p.appIconDarwinSrc = o.AppIconDarwinPath
if len(p.appIconDarwinSrc) > 0 && !filepath.IsAbs(p.appIconDarwinSrc) {
p.appIconDarwinSrc = filepath.Join(p.dataDirectory, p.appIconDarwinSrc)
Expand Down Expand Up @@ -86,12 +91,26 @@ func (p *Paths) initBaseDirectory(baseDirectoryPath string) (err error) {
return
}

func (p *Paths) initDataDirectory(appName string) {
func (p *Paths) initDataDirectory(dataDirectoryPath, appName string) (err error) {
// Path is specified in the options
if len(dataDirectoryPath) > 0 {
// We need the absolute path
if p.dataDirectory, err = filepath.Abs(dataDirectoryPath); err != nil {
err = errors.Wrapf(err, "computing absolute path of %s failed", dataDirectoryPath)
return
}
return
}

// If the APPDATA env exists, we use it
if v := os.Getenv("APPDATA"); len(v) > 0 {
p.dataDirectory = filepath.Join(v, appName)
return
}

// Default to base directory path
p.dataDirectory = p.baseDirectory
return
}

// AstilectronDownloadSrc returns the download URL of the (currently platform-independent) astilectron zip file
Expand Down
9 changes: 5 additions & 4 deletions paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ func TestPaths(t *testing.T) {
assert.Equal(t, "/path/to/darwin/icon", p.AppIconDarwinSrc())
assert.Equal(t, "/path/to/base/directory/icon", p.AppIconDefaultSrc())
assert.Equal(t, "https://github.com/electron/electron/releases/download/v"+VersionElectron+"/electron-v"+VersionElectron+"-darwin-x64.zip", p.ElectronDownloadSrc())
p, err = newPaths("darwin", "amd64", Options{AppName: "Test app", BaseDirectoryPath: "/path/to/base/directory"})
p, err = newPaths("darwin", "amd64", Options{AppName: "Test app", BaseDirectoryPath: "/path/to/base/directory", DataDirectoryPath: "/path/to/data/directory"})
assert.NoError(t, err)
assert.Equal(t, "/path/to/base/directory/vendor/electron-darwin-amd64/Test app.app/Contents/MacOS/Test app", p.AppExecutable())
assert.Equal(t, "/path/to/base/directory/vendor/electron-darwin-amd64-v"+VersionElectron+".zip", p.ElectronDownloadDst())
assert.Equal(t, "/path/to/base/directory/vendor/electron-darwin-amd64-v"+VersionElectron+".zip", p.ElectronUnzipSrc())
assert.Equal(t, "/path/to/data/directory", p.DataDirectory())
assert.Equal(t, "/path/to/data/directory/vendor/electron-darwin-amd64/Test app.app/Contents/MacOS/Test app", p.AppExecutable())
assert.Equal(t, "/path/to/data/directory/vendor/electron-darwin-amd64-v"+VersionElectron+".zip", p.ElectronDownloadDst())
assert.Equal(t, "/path/to/data/directory/vendor/electron-darwin-amd64-v"+VersionElectron+".zip", p.ElectronUnzipSrc())
const pad = "/path/to/appdata"
os.Setenv(k, pad)
p, err = newPaths("windows", "amd64", Options{})
Expand Down

0 comments on commit 36e83aa

Please sign in to comment.