-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.vsh
executable file
·119 lines (109 loc) · 3.18 KB
/
build.vsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env -S v
import cli
import os
const app_root = @VMODROOT
fn exec(cmd string, work_folder string, args []string) ! {
mut p := new_process(find_abs_path_of_executable(cmd) or {
return error('failed to find `${cmd}`.\nMake sure ${cmd} is executable.')
})
p.set_work_folder(work_folder)
p.set_args(args)
p.wait()
}
fn build_ui() ! {
// Install node modules
exec('npm', '${'${app_root}/ui'}', ['install'])!
// Build static web app
exec('npm', '${app_root}/ui', ['run', 'build'])!
// Remove `dist/` if it exits - ignore errors if it doesn't
rmdir_all('dist') or {}
// Create `dist/` - fail is next to impossible as `dist/` does not exist at this point
mkdir_all('dist/ui')!
// Copy UI files to dist
cp_all('ui/build/', 'dist/ui/', false)!
}
fn gen_embeds() ! {
println('Embedding files')
chdir(app_root)!
// Use LVbag to generate the embed file lists.
lvb := find_abs_path_of_executable('lvb') or { './lvb' }
execute_opt('${lvb} -bag ui -o src/lvb.v -f dist/ui')!
// Append a version const.
mut f := open_append('src/lvb.v')!
// `git describe --tags` -> v0.3.0@g3804a82. Use in App when constants evaluate at comptime.
version := "const version = '${execute_opt('git describe --tags')!.output.trim_space()}'"
f.write_string(version)!
f.close()
// Format.
execute_opt('v fmt -w src/lvb.v')!
}
fn build_bin(flags string) ! {
cc := $if macos { 'clang' } $else { 'gcc' }
out_file := 'emoji-mart' + $if windows { '.exe' } $else { '' }
cmd := 'v -cc ${cc} ${flags} -o ${app_root}/dist/${out_file} ${app_root}/src'
println('Building binary: ${cmd}')
execute_opt($if windows { 'powershell -command ${cmd}' } $else { cmd })!
}
fn build(cmd cli.Command) ! {
if !cmd.flags.get_bool('skip-ui')! {
build_ui()!
}
if !cmd.flags.get_bool('skip-bin')! {
gen_embeds()!
mut flags := '-d embed' + if cmd.name == 'dev' { '' } else { ' -prod' }
build_bin(flags)!
}
if cmd.flags.get_bool('appimage')! {
build_appimage() or { return error('failed to build appimage. "${err}"') }
}
}
fn build_appimage() ! {
println('Building appimage')
mkdir_all('dist/appimage/AppDir/usr/bin')!
mkdir_all('dist/appimage/AppDir/usr/share/icons')!
cp('dist/emoji-mart', 'dist/appimage/AppDir/usr/bin/emoji-mart')!
cp('assets/AppImageBuilder.yml', 'dist/appimage/AppImageBuilder.yml')!
cp('assets/emoji-mart.png', 'dist/appimage/AppDir/usr/share/icons/emoji-mart.png')!
exec('appimage-builder', '${app_root}/dist/appimage', ['--recipe', 'AppImageBuilder.yml',
'--skip-tests'])!
}
mut cmd := cli.Command{
name: 'build.vsh'
posix_mode: true
required_args: 0
pre_execute: fn (cmd cli.Command) ! {
if cmd.args.len > cmd.required_args {
eprintln('Unknown commands ${cmd.args}.\n')
cmd.execute_help()
exit(0)
}
}
execute: build
commands: [
cli.Command{
name: 'dev'
description: 'Build the dev binary.'
execute: build
},
]
flags: [
cli.Flag{
flag: .bool
name: 'appimage'
description: 'Create an appimage from the built binary.'
global: true
},
cli.Flag{
flag: .bool
name: 'skip-bin'
description: 'Skip building the binary.'
},
cli.Flag{
flag: .bool
name: 'skip-ui'
description: 'Skip building the UI.'
},
]
}
cmd.parse(os.args)
println('\rFinished.')