mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
live: move to vlib/v/live
This commit is contained in:
parent
8571d9395b
commit
5c3ef588c3
@ -2,7 +2,7 @@ module main
|
||||
|
||||
// Build this example with `v -live message.v`
|
||||
import time
|
||||
import live
|
||||
import v.live
|
||||
|
||||
[live]
|
||||
fn print_message() {
|
||||
|
@ -89,7 +89,7 @@ fn (mut g Gen) generate_hotcode_reloading_main_caller() {
|
||||
if g.pref.os == .windows {
|
||||
g.writeln('\t\tlive_fn_mutex = CreateMutexA(0, 0, 0);')
|
||||
}
|
||||
g.writeln('\t\tlive__LiveReloadInfo* live_info = live__executable__new_live_reload_info(')
|
||||
g.writeln('\t\tv__live__LiveReloadInfo* live_info = v__live__executable__new_live_reload_info(')
|
||||
g.writeln('\t\t\t\t\t tos2("$file"),')
|
||||
g.writeln('\t\t\t\t\t tos2("$vexe"),')
|
||||
g.writeln('\t\t\t\t\t tos2("$vopts"),')
|
||||
@ -99,7 +99,7 @@ fn (mut g Gen) generate_hotcode_reloading_main_caller() {
|
||||
// g_live_info gives access to the LiveReloadInfo methods,
|
||||
// to the custom user code, through calling v_live_info()
|
||||
g.writeln('\t\t g_live_info = (void*)live_info;')
|
||||
g.writeln('\t\tlive__executable__start_reloader(live_info);')
|
||||
g.writeln('\t\tv__live__executable__start_reloader(live_info);')
|
||||
g.writeln('\t}\t// end of live code initialization section')
|
||||
g.writeln('')
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import os
|
||||
import time
|
||||
import dl
|
||||
import strconv
|
||||
import live
|
||||
import v.live
|
||||
|
||||
pub const (
|
||||
is_used = 1
|
||||
@ -50,9 +50,10 @@ pub fn start_reloader(mut r live.LiveReloadInfo) {
|
||||
go reloader(mut r)
|
||||
}
|
||||
|
||||
[if debuglive]
|
||||
fn elog(r &live.LiveReloadInfo, s string){
|
||||
eprintln(s)
|
||||
$if debuglive ? {
|
||||
eprintln(s)
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_and_reload_shared_lib(mut r live.LiveReloadInfo) ?bool {
|
@ -5,6 +5,7 @@ import time
|
||||
The goal of this test, is to simulate a developer, that has run a program, compiled with -live flag.
|
||||
|
||||
It does so by writing a new generated program containing a [live] fn pmessage() string {...} function,
|
||||
(that program is in `vlib/v/live/live_test_template.vv`)
|
||||
then runs the generated program at the start *in the background*,
|
||||
waits some time, so that the program could run a few iterations, then modifies its source
|
||||
(simulates a developer that has saved a new version of the program source),
|
||||
@ -39,70 +40,15 @@ const (
|
||||
res_stop_file = os.join_path(os.temp_dir(), 'STOP.txt')
|
||||
cleanup_files = [tmp_file, source_file, genexe_file, output_file, res_original_file,
|
||||
res_changed_file, res_another_file, res_stop_file]
|
||||
live_program_source = "
|
||||
module main
|
||||
|
||||
import time
|
||||
import os
|
||||
import live
|
||||
|
||||
fn append_to_file(fname string, s string) {
|
||||
mut f := os.open_append(fname) or {
|
||||
println('>>>> could not open file \$fname for appending, err: \$err ')
|
||||
return
|
||||
}
|
||||
f.writeln('\$s')
|
||||
//info := live.info()
|
||||
//f.writeln('>>> reloads: \${info.reloads} | ok reloads: \${info.reloads_ok}')
|
||||
f.close()
|
||||
}
|
||||
|
||||
fn myprintln(s string) {
|
||||
append_to_file('$output_file', s)
|
||||
println(s)
|
||||
os.flush()
|
||||
}
|
||||
|
||||
[live]
|
||||
fn pmessage() string {
|
||||
return 'ORIGINAL'
|
||||
}
|
||||
|
||||
const (
|
||||
delay = 20
|
||||
live_program_source = get_source_template()
|
||||
)
|
||||
fn edefault(name string, default string) string {
|
||||
res := os.getenv(name)
|
||||
if res == '' {
|
||||
return default
|
||||
}
|
||||
return res
|
||||
|
||||
fn get_source_template() string {
|
||||
src := os.read_file(os.join_path(os.dir(@FILE), 'live_test_template.vv')) or {
|
||||
panic(err)
|
||||
}
|
||||
return src.replace('#OUTPUT_FILE#', output_file)
|
||||
}
|
||||
fn main() {
|
||||
mut info := live.info()
|
||||
info.recheck_period_ms = 5
|
||||
myprintln('START')
|
||||
myprintln('DATE: ' + time.now().str())
|
||||
pmessage()
|
||||
pmessage()
|
||||
max_cycles := edefault('LIVE_CYCLES', '1').int()
|
||||
// NB: 1000 * 20 = maximum of ~20s runtime
|
||||
for i:=0; i<max_cycles; i++ {
|
||||
s := pmessage()
|
||||
myprintln(s)
|
||||
append_to_file(os.resource_abs_path(s + '.txt'), s)
|
||||
if s == 'STOP' {
|
||||
break
|
||||
}
|
||||
time.sleep_ms(delay)
|
||||
}
|
||||
pmessage()
|
||||
pmessage()
|
||||
myprintln('DATE: ' + time.now().str())
|
||||
myprintln('END')
|
||||
}
|
||||
"
|
||||
)
|
||||
|
||||
fn edefault(name string, default string) string {
|
||||
res := os.getenv(name)
|
||||
@ -206,7 +152,7 @@ fn setup_cycles_environment() {
|
||||
fn test_live_program_can_be_compiled() {
|
||||
setup_cycles_environment()
|
||||
eprintln('Compiling...')
|
||||
os.system('$vexe -live -o $genexe_file $source_file')
|
||||
os.system('$vexe -nocolor -live -o $genexe_file $source_file')
|
||||
//
|
||||
cmd := '$genexe_file > /dev/null &'
|
||||
eprintln('Running with: $cmd')
|
||||
@ -227,6 +173,8 @@ fn test_live_program_can_be_changed_2() {
|
||||
}
|
||||
|
||||
fn test_live_program_can_be_changed_3() {
|
||||
change_source('STOP')
|
||||
change_source('STOP')
|
||||
change_source('STOP')
|
||||
assert true
|
||||
}
|
61
vlib/v/live/live_test_template.vv
Normal file
61
vlib/v/live/live_test_template.vv
Normal file
@ -0,0 +1,61 @@
|
||||
module main
|
||||
|
||||
import time
|
||||
import os
|
||||
import v.live
|
||||
|
||||
fn append_to_file(fname string, s string) {
|
||||
mut f := os.open_append(fname) or {
|
||||
println('>>>> could not open file $fname for appending, err: $err ')
|
||||
return
|
||||
}
|
||||
f.writeln(s)
|
||||
//info := live.info()
|
||||
//f.writeln('>>> reloads: ${info.reloads} | ok reloads: ${info.reloads_ok}')
|
||||
f.close()
|
||||
}
|
||||
|
||||
fn myprintln(s string) {
|
||||
append_to_file('#OUTPUT_FILE#', s)
|
||||
println(s)
|
||||
os.flush()
|
||||
}
|
||||
|
||||
[live]
|
||||
fn pmessage() string {
|
||||
return 'ORIGINAL'
|
||||
}
|
||||
|
||||
const (
|
||||
delay = 20
|
||||
)
|
||||
fn edefault(name string, default string) string {
|
||||
res := os.getenv(name)
|
||||
if res == '' {
|
||||
return default
|
||||
}
|
||||
return res
|
||||
}
|
||||
fn main() {
|
||||
mut info := live.info()
|
||||
info.recheck_period_ms = 5
|
||||
myprintln('START')
|
||||
myprintln('DATE: ' + time.now().str())
|
||||
pmessage()
|
||||
pmessage()
|
||||
max_cycles := edefault('LIVE_CYCLES', '1').int()
|
||||
// NB: 1000 * 20 = maximum of ~20s runtime
|
||||
for i:=0; i<max_cycles; i++ {
|
||||
s := pmessage()
|
||||
myprintln(s)
|
||||
append_to_file(os.resource_abs_path(s + '.txt'), s)
|
||||
if s == 'STOP' {
|
||||
break
|
||||
}
|
||||
time.sleep_ms(delay)
|
||||
}
|
||||
pmessage()
|
||||
pmessage()
|
||||
myprintln('DATE: ' + time.now().str())
|
||||
myprintln('END')
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
module sharedlib
|
||||
|
||||
import live
|
||||
import v.live
|
||||
|
||||
pub const (
|
||||
is_used = live.is_used + 1
|
@ -2,7 +2,7 @@ module main
|
||||
|
||||
// This prelude is loaded in every v program compiled with -live,
|
||||
// in both the main executable, and in the shared library.
|
||||
import live
|
||||
import v.live
|
||||
|
||||
const (
|
||||
no_warning_live_is_used = live.is_used
|
||||
|
@ -2,7 +2,7 @@ module main
|
||||
|
||||
// This prelude is loaded in every v program compiled with -live,
|
||||
// but only for the main executable.
|
||||
import live.executable
|
||||
import v.live.executable
|
||||
|
||||
const (
|
||||
no_warning_live_executable_is_used = executable.is_used
|
||||
|
@ -2,7 +2,7 @@ module main
|
||||
|
||||
// This prelude is loaded in every v program compiled with -live,
|
||||
// but only for the shared library.
|
||||
import live.sharedlib
|
||||
import v.live.sharedlib
|
||||
|
||||
const (
|
||||
no_warning_live_shared_is_used = sharedlib.is_used
|
||||
|
Loading…
Reference in New Issue
Block a user