-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathrelease.nu
More file actions
executable file
·151 lines (117 loc) · 4.05 KB
/
Copy pathrelease.nu
File metadata and controls
executable file
·151 lines (117 loc) · 4.05 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
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 nu
use std assert
export def main [version: string] {
# make sure the working tree is clean
assert_repo_is_clean
let read_version = version_read_cargo_toml
let requested_version = $version | version_parse
let requested_version_tag = $requested_version | version_to_string
$"Updating version from ($read_version | version_to_string) to ($requested_version | version_to_string)" | echo_section_headline
if not (is_newer $read_version $requested_version) {
print "Requested version is older than current version. Aborting."
exit 1
}
# update the cargo toml
patch_cargo_toml $requested_version
# update cargo lock
let update_all_deps = if not ('Cargo.lock' | path exists) { true } else { (((input "Update dependencies in Cargo.lock? (y/N) " --numchar 1 --default "N") | str lowercase) == "y") }
if $update_all_deps {
update_cargo_lock_all
} else {
update_cargo_lock
}
# replace NEXTRELEASE with version
update_next_release cli/src/command_line.rs $version
update_next_release src/configuration.rs $version
update_next_release config.toml $version
update_next_release README.md $version
# show diff so we can review the replacements
git_diff
if ((input "Proceed with commit? (Y/n) " --numchar 1 --default "Y") | str lowercase) == "n" {
exit 1
}
# commit
git_commit $requested_version_tag
# tag a new git version
git_tag $requested_version_tag
## from here on we go online!
# push
git_push
# the rest is being handled by the github release action
}
def echo_section_headline []: string -> nothing {
print $"\n(ansi yellow)++ ($in)(ansi reset)"
}
def assert_repo_is_clean [] {
if (git diff --quiet | complete | get exit_code) != 0 {
print "The git repository is not clean! Aborting..."
exit 1
} else {}
}
def git_diff [] {
git --no-pager diff
}
def git_tag [tag: string] {
assert_repo_is_clean
$"Creating Git Tag ($tag) " | echo_section_headline
git tag ($tag)
}
def git_push [] {
"Pushing to GitHub" | echo_section_headline
git push; git push --tags
}
def patch_cargo_toml [version: list<int>] {
"Updating Cargo.toml" | echo_section_headline
let version_str = $version | str join '.'
# Update workspace.package.version
sed -i $"/\\[workspace.package\\]/,/^version =/s/^version = .*/version = \"($version_str)\"/" Cargo.toml
open --raw Cargo.toml
| str replace --regex '(satty_cli = \{ path = "cli", version = ")[^"]*"' $'${1}($version_str)"'
| save -f Cargo.toml
}
def update_cargo_lock [] {
"Updating Cargo.lock - satty crates" | echo_section_headline
cargo update satty satty_cli
}
def update_cargo_lock_all [] {
"Updating Cargo.lock - satty crates and deps" | echo_section_headline
cargo generate-lockfile
}
def update_next_release [filename: string, version: string] {
sed -i -e $'s,NEXTRELEASE,($version),g' $filename
}
def git_commit [tag: string] {
"Committing..." | echo_section_headline
git commit -am $"Updating version to ($tag)"
}
def version_parse []: string -> list<int> {
$in | str trim -c 'v' --left | split row '.' | each {|n| into int }
}
def version_to_string []: list<int> -> string {
$"v($in | str join '.')"
}
def version_read_cargo_toml []: nothing -> list<int> {
open Cargo.toml | get workspace.package.version | version_parse
}
def is_newer [
old: list<int>,
new: list<int>
]: nothing -> bool {
let length = [($old | length) ($new | length)] | math min
for i in 0..<($length) {
if ($new | get $i) > ($old | get $i) {
return true
} else {}
if ($new | get $i) < ($old | get $i) {
return false
} else {}
}
return false
}
#[test]
def test_versions [] {
assert (is_newer [1 0 0] [2 0 0]) "major version"
assert (is_newer [1 0 0] [1 1]) "minor version, shorter"
assert (not (is_newer [1 1 0] [1 1])) "minor version, shorter"
assert (not (is_newer [1 1 0] [0 1])) "minor version, shorter"
}