-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.nu
158 lines (138 loc) · 5.64 KB
/
install.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
# less, tmux, etc do not continue colors on the next line -> set them at the beginning of every line
let RESET_LINE = $'(ansi esc)[0G(ansi esc)[K'
# ESC[0G = jump to beginning of line; ESC[K = erase line
print $'(ansi g)Installing packer.nu.'
print $'(ansi g)====================='
# Check if git is installed
try {
^git --version | null
} catch {
print $'(ansi r)Git is required to use packer.nu. Please install git and then retry installation.'
exit 1
}
print -n $'(ansi y)Setting up install env..'
let PACKER_REPO = 'https://github.com/jan9103/packer.nu'
let NU_CONFIG_DIR = ($nu.history-path | path dirname)
# true if windows, false if GNU/Linux
let IS_WINDOWS = ($nu.os-info.family == 'windows')
let PACKER_DIR = if not $IS_WINDOWS {
if ($'/home/($env.USER)' == $env.HOME) {
'~/.local/share/nushell/packer'
} else {
$'($env.HOME)/.local/share/nushell/packer'
}
} else {
# Windows nushell data dir is ~/AppData/Local/nushell
$'($env.LOCALAPPDATA)/nushell/packer'
}
let PACKER_PACKAGE_DIR = $'($PACKER_DIR)/start/packer.nu'
let ABS_PACKER_DIR = ($PACKER_DIR | path expand)
let ABS_PACKER_PACKAGE_DIR = ($PACKER_PACKAGE_DIR | path expand)
print $'($RESET_LINE)(ansi g)Install env is set up'
# append some lines to a file
def 'append_to_file' [
file: path
lines: list
] {
open $file
| lines
| append $lines
| str join (char nl)
| save -f $file
}
print -n $'(ansi y)Creating directories..'
for subdir in ['start', 'bin', 'lib'] {
mkdir $'($ABS_PACKER_DIR)/($subdir)'
}
print $'($RESET_LINE)(ansi g)Created directories.'
if not ($'($NU_CONFIG_DIR)/packages.nuon' | path exists) {
print -n $'(ansi y)Creating defaut packages.nuon..'
$"{\n\tpackages: [\n\t\t{source: '($PACKER_REPO)'}\n\t]\n}\n# vim: ft=nu"
[
'{'
' packages: ['
$" {source: '($PACKER_REPO)'}"
' ]'
'}'
'# vim: ft=nu'
]
| str join (char nl)
| save -r $'($NU_CONFIG_DIR)/packages.nuon'
print $'($RESET_LINE)(ansi g)Created default packages.nuon.'
} else { print $'(ansi b)Kept existing packages.nuon.' }
# install packer as package
if not ($PACKER_PACKAGE_DIR | path exists) {
print $'(ansi y)Git cloning packer.nu:(ansi reset)'
mkdir $'($ABS_PACKER_DIR)/start'
^git clone $PACKER_REPO $ABS_PACKER_PACKAGE_DIR
print $'(ansi g)Git cloned packer.nu.'
} else { print $'(ansi u)Already git cloned packer.nu.(ansi reset)' }
let regenerate_config = not (open $nu.config-path | str contains "\n### packer.nu ###\n")
let regenerate_env = not (open $nu.env-path | str contains "\n### packer.nu ###\n")
let enable_conditional_loading = (
if $regenerate_env {
print ''
print $'(ansi yb)Do you want to use conditional packages?(ansi reset)'
print $'(ansi y) very few packages require it and it slows'
print $'(ansi y) down your startup-time quite a bit.'
print $"(ansi y) you can enable/disable it later by \(un-)commenting"
print $'(ansi y) a line in your env.nu and config.nu each.'
input '(y/n)> '
} else {'y'}
)
if $regenerate_env {
print -n $'(ansi y)Adding packer load section to ($nu.env-path).'
append_to_file $nu.env-path ([
''
'### packer.nu ###'
(if ($PACKER_DIR | str starts-with '~') {
# if '~' is used we have to expand it
# ther reason why tilde is preferred is for portability
$"load-env {NU_PACKER_HOME: \('($PACKER_DIR)' | path expand\)}"
} else {
$"load-env {NU_PACKER_HOME: '($PACKER_DIR)'}"
})
'# recovery command'
"def 'packer compile' [] { nu -c $'use ($env.NU_PACKER_HOME)/start/packer.nu/api_layer/packer.nu; packer compile' }"
'# bootstrap packer.nu'
"if not ($'($env.NU_PACKER_HOME)/start/packer.nu/api_layer/packer_api.nu' | path exists) {"
" print $'(ansi ub)Bootstrapping packer.nu...(ansi reset)'"
' nu -c (http get https://raw.githubusercontent.com/jan9103/packer.nu/main/install.nu)'
" print $'(ansi ub)Bootstrapped packer.nu.'"
" print $'(ansi ub)Installing packages...(ansi reset)'"
" nu -c $'use ($env.NU_PACKER_HOME)/start/packer.nu/api_layer/packer.nu; packer install'"
" print $'(ansi ub)Installed packages.(ansi reset)'"
'}'
'# compile conditional package loader'
"# conditional packages have to be generated in the env, since you can't generate and import in the same file."
(if $enable_conditional_loading == 'y' {
$"nu -c 'use ($PACKER_DIR)/start/packer.nu/api_layer/packer.nu; packer compile_cond_init ($PACKER_DIR)/conditional_packages.nu'"
} else {
$"#nu -c 'use ($PACKER_DIR)/start/packer.nu/api_layer/packer.nu; packer compile_cond_init ($PACKER_DIR)/conditional_packages.nu'"
})
"if not ($'($env.NU_PACKER_HOME)/packer_packages.nu' | path exists) { 'export-env {}' | save $'($env.NU_PACKER_HOME)/packer_packages.nu' }"
] | flatten | compact)
print $'($RESET_LINE)(ansi g)Added packer load section to ($nu.env-path).'
} else { print $'(ansi u)Skipped adding packer load section to ($nu.env-path) to avoid duplicates.' }
if $regenerate_config {
print -n $'(ansi y)Adding packer load section to ($nu.config-path)..'
append_to_file $nu.config-path [
''
'### packer.nu ###'
'# load packer api'
$"overlay use ($PACKER_PACKAGE_DIR)/api_layer/packer_api.nu"
'# load packages'
$'overlay use ($PACKER_DIR)/packer_packages.nu'
'# load conditional packages'
(if $enable_conditional_loading == 'y' {
$'overlay use ($PACKER_DIR)/conditional_packages.nu'
} else {
$'#overlay use ($PACKER_DIR)/conditional_packages.nu'
})
]
print $'($RESET_LINE)(ansi g)Added packer load section to ($nu.config-path).'
} else { print $'(ansi u)Skipped adding packer load section to ($nu.config-path) to avoid duplicates.' }
if not ($'($ABS_PACKER_DIR)/conditional_packages.nu' | path exists) {
'export-env {}' | save -r $'($ABS_PACKER_DIR)/conditional_packages.nu'
}
print $'(ansi g)Finished install script.(ansi reset)'