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 9fd6d43 commit 5f958b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
23 changes: 16 additions & 7 deletions src/paths_d_embed.v → src/init_d_embed.v
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@ const (
sound = $embed_file('../assets/audio/pop.wav')
)

fn write_embedded() ! {
fn init() {
if !os.exists(paths.ui) {
dist_ui_path := os.join_path('dist', 'ui')
for file in ui {
_, mut out_path := file.path.rsplit_once(dist_ui_path) or {
return error('failed to prepare path for ${file.path}')
panic('failed to prepare path for ${file.path}')
}
out_path = os.join_path(paths.ui, out_path)
os.mkdir_all(os.dir(out_path)) or { panic(err) }
out_dir := os.dir(out_path)
if !os.exists(os.dir(out_dir)) {
os.mkdir_all(out_dir) or {}
}
os.write_file(out_path, file.to_string()) or { panic(err) }
}
}
if !os.exists(paths.sound) {
os.mkdir_all(os.dir(paths.sound)) or {}
os.write_file(paths.sound, sound.to_string())!
out_dir := os.dir(paths.sound)
if !os.exists(os.dir(out_dir)) {
os.mkdir_all(out_dir) or {}
}
os.write_file(paths.sound, sound.to_string()) or { panic(err) }
}
if !os.exists(paths.icon) {
os.mkdir_all(os.dir(paths.icon)) or {}
os.write_file(paths.icon, icon.to_string())!
out_dir := os.dir(paths.icon)
if !os.exists(os.dir(out_dir)) {
os.mkdir_all(out_dir) or {}
}
os.write_file(paths.icon, icon.to_string()) or { panic(err) }
}
}
12 changes: 4 additions & 8 deletions src/paths.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,19 @@ 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')
}
Expand All @@ -41,4 +36,5 @@ fn init() {
// 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 5f958b1

Please sign in to comment.