-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.vsh
executable file
·151 lines (138 loc) · 3.83 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env -S v
import cli
import os
import time
import net.http
import regex
const lib_url = 'https://raw.githubusercontent.com/webview/webview/refs/tags/0.10.0'
const lib_dir = '${@VMODROOT}/src'
const cxx = if _ := find_abs_path_of_executable('g++') {
'g++'
} else if _ := find_abs_path_of_executable('clang++') {
'clang++'
} else {
panic("Can't find C++ compiler. Make sure g++ or clang++ is executable.")
}
// == Build Docs ==============================================================
// Remove redundant readme section from module page.
fn rm_readme_section(html string) string {
mut r := regex.regex_opt(r'<section id="readme_webview".*</section>') or { panic(err) }
sec_start, sec_end := r.find(html)
return '${html[..sec_start]}</section>${html[sec_end..]}'
.replace('<li class="open"><a href="#readme_webview">README</a></li>', '')
}
fn build_docs() ! {
// Cleanup old docs.
rmdir_all('_docs') or {}
// Build docs.
mut p := new_process(@VEXE)
p.set_args(['doc', '-readme', '-m', '-f', 'html', '.'])
p.wait()
// Prepare html.
mut webview_html := read_file('_docs/webview.html')!
webview_html = rm_readme_section(webview_html)
write_file('_docs/webview.html', webview_html)!
}
// == Download & Build Library ================================================
fn spinner(ch chan bool, silent bool) {
if silent {
return
}
runes := [`-`, `\\`, `|`, `/`]
mut pos := 0
for {
if ch.closed {
print('\r')
return
}
if pos == runes.len - 1 {
pos = 0
} else {
pos += 1
}
print('\r${runes[pos]}')
flush()
time.sleep(100 * time.millisecond)
}
}
@[if windows]
fn download_webview2() {
http.download_file('https://www.nuget.org/api/v2/package/Microsoft.Web.WebView2',
'${lib_dir}/webview2.zip') or { panic(err) }
unzip_res := execute('powershell -command Expand-Archive -LiteralPath ${lib_dir}/webview2.zip -DestinationPath ${lib_dir}/webview2')
if unzip_res.exit_code != 0 {
eprintln(unzip_res.output)
exit(1)
}
}
fn download(silent bool) {
println('Downloading...')
spinner_ch := chan bool{}
spawn spinner(spinner_ch, silent)
defer { spinner_ch.close() }
http.download_file('${lib_url}/webview.h', '${lib_dir}/webview.h') or { panic(err) }
http.download_file('${lib_url}/webview.cc', '${lib_dir}/webview.cc') or { panic(err) }
download_webview2()
}
fn build(silent bool) {
mut cmd := '${cxx} -c ${lib_dir}/webview.cc -DWEBVIEW_STATIC -o ${lib_dir}/webview.o'
cmd += $if darwin { ' -std=c++11' } $else { ' -std=c++17' }
$if linux {
cmd += ' $(pkg-config --cflags gtk+-3.0 webkit2gtk-4.0)'
} $else $if windows {
defer {
// Cleanup
rm('${lib_dir}/webview2.zip') or {}
rmdir_all('${lib_dir}/webview2') or {}
}
cmd += ' -I${lib_dir}/webview2/build/native/include'
}
println('Building...')
spinner_ch := chan bool{}
spawn spinner(spinner_ch, silent)
defer { spinner_ch.close() }
build_res := execute(cmd)
if build_res.exit_code != 0 {
eprintln(build_res.output)
exit(1)
}
println('\rSuccessfully built the webview library.')
}
fn run(cmd cli.Command) ! {
// Remove old library files
execute('rm ${lib_dir}/webview.*')
silent := cmd.flags.get_bool('silent')!
download(silent)
time.sleep(100 * time.millisecond)
build(silent)
}
// == Commands ================================================================
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: run
commands: [
cli.Command{
name: 'docs'
description: 'Build docs used for GitHub pages.'
execute: fn (_ cli.Command) ! {
build_docs() or { eprintln('Failed building docs. ${err}') }
}
},
]
flags: [
cli.Flag{
flag: .bool
name: 'silent'
},
]
}
cmd.parse(os.args)