-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrust.v
More file actions
94 lines (85 loc) · 2.2 KB
/
Copy pathrust.v
File metadata and controls
94 lines (85 loc) · 2.2 KB
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
import os { exists, join_path_single, read_lines, write_file }
import toml { parse_file }
import prantlf.debug { rwd }
import prantlf.osutil { find_file }
fn set_cargo_version(ver string, pkg_dir string, opts &Opts) ! {
cargo_file := join_path_single(pkg_dir, 'Cargo.toml')
if !exists(cargo_file) {
return
}
mut cargo_version := false
mut cargo_lines := read_lines_from_file(cargo_file)!
lck_file := join_path_single(pkg_dir, 'Cargo.lock')
lck_is := exists(lck_file)
mut lck_lines := []string{}
mut lck_version := false
if lck_is {
lck_lines = read_lines_from_file(lck_file)!
}
d.log('setting cargo version to "%s"', version)
for i := 0; i < cargo_lines.len; i++ {
if cargo_lines[i].starts_with('version = "') {
cargo_lines[i] = 'version = "${ver}"'
cargo_version = true
break
}
}
if lck_is {
cargo := parse_file(cargo_file)!
name := cargo.value('package.name').string()
name_line := 'name = "${name}"'
mut this_package := false
for i := 0; i < lck_lines.len; i++ {
line := lck_lines[i]
if this_package {
if line.starts_with('name = "') {
this_package = false
} else if line.starts_with('version = "') {
lck_lines[i] = 'version = "${ver}"'
lck_version = true
break
}
} else if line == name_line {
this_package = true
}
}
}
if !cargo_version {
return error('version not found in Cargo.toml')
}
if lck_is && !lck_version {
return error('version not found in Cargo.lock')
}
if !opts.dry_run {
write_lines_to_file(cargo_file, cargo_lines)!
if lck_is {
write_lines_to_file(lck_file, lck_lines)!
}
}
if opts.verbose {
mut mode := if opts.dry_run {
' (dry-run)'
} else {
''
}
if lck_is {
mode = ' and "${rwd(lck_file)}"${mode}'
}
println('updated version in "${rwd(cargo_file)}"${mode}')
}
}
fn find_cargo() !(string, string) {
return find_file('Cargo.toml') or { error('Cargo.toml not found') }
}
fn read_lines_from_file(file string) ![]string {
dfile := d.rwd(file)
d.log('reading file "%s"', dfile)
lines := read_lines(file)!
return lines
}
fn write_lines_to_file(file string, lines []string) ! {
dfile := d.rwd(file)
d.log('writing file "%s"', dfile)
text := lines.join('\n')
write_file(file, text)!
}