Skip to content

Commit

Permalink
🎨 use safe way to init paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 11, 2023
1 parent fc84723 commit 52b3b3e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/paths_d_embed.v → src/init_d_embed.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ const (
sound = $embed_file('../assets/audio/pop.wav')
)

fn init() {
write_embedded() or {
eprintln('Failed writing embedded files: `${err}`')
exit(1)
}
}

fn write_embedded() ! {
if !os.exists(paths.ui) {
dist_ui_path := os.join_path('dist', 'ui')
Expand Down
14 changes: 5 additions & 9 deletions src/paths.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,27 @@ mut:

const (
app_name = 'emoji-mart'
paths = &Paths{}
paths = get_paths()!
)

fn init() {
mut p := &Paths{}
unsafe {
p = paths
}
fn get_paths() !Paths {
mut p := Paths{}
$if embed ? {
tmp_dir := join_path(os.temp_dir(), '${app_name}-@${version}')
p.ui = join_path(tmp_dir, 'ui')
p.sound = join_path(tmp_dir, 'assets', 'pop.wav')
p.icon = join_path(tmp_dir, 'assets', 'icon.ico')
write_embedded() or { eprintln('Failed writing embedded files: `${err}`') }
} $else {
app_root := @VMODROOT
p.ui = join_path(app_root, 'ui')
p.sound = join_path(app_root, 'assets', 'pop.wav')
p.sound = join_path(app_root, 'assets', 'audio', 'pop.wav')
p.icon = join_path(app_root, 'assets', 'emoji-mart.ico')
}
// Config
p.cfg_dir = join_path(os.config_dir() or { panic(err) }, app_name)
p.cfg_dir = join_path(os.config_dir()!, app_name)
p.cfg_file = join_path(p.cfg_dir, '${app_name}.toml')
// Cache
p.cache_dir = join_path(os.cache_dir(), app_name, 'LocalStorage')
p.cache_file = join_path(p.cache_dir, 'localStorage.json')
return p
}

0 comments on commit 52b3b3e

Please sign in to comment.