-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.nu
176 lines (168 loc) · 4.7 KB
/
run.nu
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use ./lib/utils.nu *
use ./lib/os.nu
use ./lib/deps.nu
use ./lib/gen.nu
use ./lib/interpret.nu
use ./lib/extractor.nu
use ./lib/download.nu
def setup [
defs
data
--os-type: string
--target: string
--cache: string
--dry-run: bool
--clean: bool
] {
gen stage $in $clean {
os: $os_type
dry_run: $dry_run
defs: $defs
data: $data
target: $target
cache: $cache
can_ignore: true
act: null
args: null
}
| gen optm
| interpret $dry_run
}
def run [
req
pkgs
defs
data
--os-type: string
--target: string
--cache: string
--dry-run: bool
--clean: bool
] {
$pkgs
| deps sort $req
| deps resolve
| deps merge $defs --os-type $os_type
| (setup $defs $data
--os-type $os_type
--target $target
--dry-run $dry_run
--clean $clean
--cache $cache
)
}
def update-version [manifest] {
mut data = {}
for item in ($manifest | transpose k v) {
let i = $item.v?
print $'==> ($item.k)'
let url = $i.version?.url?
let ext = $i.version?.extract?
let header = $i.version?.header?
let header = if ($header | is-empty) { [] } else {
$header | transpose k v | each {|x| [-H $"($x.k): ($x.v)"] } | flatten
}
if ($url | not-empty) {
let ver = (extractor run (curl -sSL $header $url) $ext)
print $ver
$data = ($data | upsert $item.k $ver)
}
}
$data
}
def download-recipe [defs versions --cache:string] {
mkdir /tmp/npup
let ctx = {
defs: $defs
data: { versions: $versions }
cache: $cache
}
for y in ($defs | columns | each {|x| gen resolve $ctx $x }) {
for i in $y {
if $i.type == 'download' {
if ($i.url? | is-empty) {
print $'# ($i.name)'
} else {
let x = download resolve $i
print $'# download ($x.file)'
let t = [$cache $x.file] | filter {|x| $x | not-empty } | path join
if ($cache | find -r '^https?://' | is-empty) {
wget -c ($x.url) -O ($t)
} else {
let lt = ['/tmp/npup' $x.file] | path join
wget -c ($x.url) -O ($lt)
curl -T ($lt) ($t)
}
}
}
}
}
rm -rf /tmp/npup
}
export def main [
--dry-run
--clean
--cache: string
--target: string = '/usr/local'
...args:string@compos
] {
print $"#===> $env.DEBUG = ($env.DEBUG?)"
let act = $args.0
let req = $args | range 1.. | prepend default
let manifest = open $"($env.FILE_PWD)/manifest.yml"
let data = open $"($env.FILE_PWD)/data.yml"
let ostype = (os type)
match $act {
setup => {
(run $req
$manifest.pkgs $manifest.defs $data
--os-type $ostype
--target $target
--dry-run $dry_run
--clean $clean
--cache $cache
)
}
gensh => {
let ostype = if ($args.1? | is-empty) { $ostype } else { $args.1 }
(run $req
$manifest.pkgs $manifest.defs $data
--os-type $ostype
--target $target
--dry-run true
--clean $clean
--cache $cache
)
}
update => {
let x = (update-version $manifest.defs)
$data
| upsert versions ($data.versions | merge $x)
| to yaml
| save -f $"($env.FILE_PWD)/data.yml"
}
download => {
download-recipe $manifest.defs $data.versions --cache $cache
}
debug => {
$manifest.pkgs
| deps sort $req | log 'deps sort'
| deps resolve | log 'deps resolve'
| deps merge $manifest.defs --os-type $ostype | log 'deps merge'
}
_ => {
echo $manifest | to json
}
}
}
def compos [context: string, offset: int] {
let pkgs = open $"($env.PWD)/manifest.yml" | get pkgs | get name
[$context $offset] | completion-generator from tree [
{ value: gensh, description: 'gen sh -c', next: (
[debian arch alpine redhat] | each {|x| { value: $x, next: $pkgs } }
) }
{ value: build, description: 'Dockerfile' }
{ value: update, description: 'versions' }
{ value: download, description: 'assets' }
]
}