Skip to content

Commit 330e539

Browse files
committed
build: update install.vsh to make repeated usage of path expand fn obsolete
update names of path consts
1 parent be121f7 commit 330e539

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

install.vsh

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import cli
1010
import net.http
1111

1212
const installer_version = '0.0.2'
13-
const analyzer_sources_path = norm_expand_tilde_to_home('~/.config/v-analyzer/sources')
14-
const analyzer_bin_path = norm_expand_tilde_to_home('~/.config/v-analyzer/bin')
15-
const analyzer_bin_path_with_name = norm_expand_tilde_to_home('~/.config/v-analyzer/bin/v-analyzer')
13+
const analyzer_config_dir_path = join_path(home_dir(), '.config', 'v-analyzer')
14+
const analyzer_sources_dir_path = join_path(analyzer_config_dir_path, 'sources')
15+
const analyzer_bin_dir_path = join_path(analyzer_config_dir_path, 'bin')
16+
const analyzer_bin_file_path = join_path(analyzer_bin_dir_path, 'v-analyzer')
1617

1718
struct ReleaseAsset {
1819
tag_name string @[json: '-']
@@ -138,12 +139,12 @@ fn install_from_binary(asset ReleaseAsset, update bool) ! {
138139
println('${term.green('✓')} Successfully downloaded ${term.bold('v-analyzer')} archive')
139140

140141
println('Extracting ${term.bold('v-analyzer')} archive...')
141-
os.mkdir_all(analyzer_bin_path) or {
142-
println('Failed to create directory: ${analyzer_bin_path}')
142+
os.mkdir_all(analyzer_bin_dir_path) or {
143+
println('Failed to create directory: ${analyzer_bin_dir_path}')
143144
return
144145
}
145146

146-
szip.extract_zip_to_dir(archive_temp_path, analyzer_bin_path) or {
147+
szip.extract_zip_to_dir(archive_temp_path, analyzer_bin_dir_path) or {
147148
println('Failed to extract archive: ${err}')
148149
return
149150
}
@@ -154,14 +155,14 @@ fn install_from_binary(asset ReleaseAsset, update bool) ! {
154155
println('${term.green('✓')} ${term.bold('v-analyzer')} successfully updated to ${term.bold(asset.tag_name)}')
155156
}
156157

157-
println('Path to the ${term.bold('binary')}: ${analyzer_bin_path_with_name}')
158+
println('Path to the ${term.bold('binary')}: ${analyzer_bin_file_path}')
158159

159160
if !update {
160-
show_hint_about_path_if_needed(analyzer_bin_path_with_name)
161+
show_hint_about_path_if_needed(analyzer_bin_file_path)
161162
}
162163

163-
os.mkdir_all(analyzer_sources_path) or {
164-
println('Failed to create directory: ${analyzer_sources_path}')
164+
os.mkdir_all(analyzer_sources_dir_path) or {
165+
println('Failed to create directory: ${analyzer_sources_dir_path}')
165166
return
166167
}
167168
}
@@ -239,7 +240,7 @@ fn update_from_sources(update bool, nightly bool) ! {
239240
if need_pull {
240241
println('Updating ${term.bold('v-analyzer')} sources...')
241242

242-
res := os.execute('git -C ${analyzer_sources_path} pull')
243+
res := os.execute('git -C ${analyzer_sources_dir_path} pull')
243244
if res.exit_code != 0 {
244245
errorln('Failed to update sources: ${res.output}')
245246
return
@@ -265,12 +266,12 @@ fn update_from_sources(update bool, nightly bool) ! {
265266
println('${term.green('✓')} ${term.bold('v-analyzer')} successfully updated to ${updated_version}')
266267
}
267268

268-
println('Path to the ${term.bold('binary')}: ${analyzer_bin_path_with_name}')
269+
println('Path to the ${term.bold('binary')}: ${analyzer_bin_file_path}')
269270
return
270271
}
271272

272273
fn get_latest_commit_hash() !string {
273-
hash_res := os.execute('git -C ${analyzer_sources_path} log -1 --format=%H')
274+
hash_res := os.execute('git -C ${analyzer_sources_dir_path} log -1 --format=%H')
274275
if hash_res.exit_code != 0 {
275276
return error('Failed to get hash of the latest commit: ${hash_res.output}')
276277
}
@@ -295,15 +296,15 @@ fn install_from_sources(no_interaction bool) ! {
295296
println('cd v-analyzer')
296297
println('v build.vsh')
297298
println(term.gray('# Optionally you can move the binary to the standard location:'))
298-
println('mkdir -p ${analyzer_bin_path}')
299-
println('cp ./bin/v-analyzer ${analyzer_bin_path}')
299+
println('mkdir -p ${analyzer_bin_dir_path}')
300+
println('cp ./bin/v-analyzer ${analyzer_bin_dir_path}')
300301
return
301302
}
302303
}
303304

304305
if already_cloned() {
305-
os.rmdir_all(analyzer_sources_path) or {
306-
errorln('Failed to remove directory: ${analyzer_sources_path}: ${err}')
306+
os.rmdir_all(analyzer_sources_dir_path) or {
307+
errorln('Failed to remove directory: ${analyzer_sources_dir_path}: ${err}')
307308
return
308309
}
309310
}
@@ -313,15 +314,15 @@ fn install_from_sources(no_interaction bool) ! {
313314
clone_repository()!
314315
build_from_sources()!
315316

316-
println('Path to the ${term.bold('binary')}: ${analyzer_bin_path_with_name}')
317+
println('Path to the ${term.bold('binary')}: ${analyzer_bin_file_path}')
317318

318-
show_hint_about_path_if_needed(analyzer_bin_path_with_name)
319+
show_hint_about_path_if_needed(analyzer_bin_file_path)
319320
}
320321

321322
fn clone_repository() ! {
322323
println('Cloning ${term.bold('v-analyzer')} repository...')
323324

324-
exit_code := run_command('git clone ${git_clone_options} https://github.com/vlang/v-analyzer.git ${analyzer_sources_path} 2>&1') or {
325+
exit_code := run_command('git clone ${git_clone_options} https://github.com/vlang/v-analyzer.git ${analyzer_sources_dir_path} 2>&1') or {
325326
errorln('Failed to clone v-analyzer repository: ${err}')
326327
return
327328
}
@@ -338,7 +339,7 @@ fn build_from_sources() ! {
338339

339340
compiler_flag := $if windows { '-cc gcc' } $else { '' }
340341

341-
chdir(analyzer_sources_path)!
342+
chdir(analyzer_sources_dir_path)!
342343
install_deps_cmd := os.execute('v ${compiler_flag} install')
343344
if install_deps_cmd.exit_code != 0 {
344345
errorln('Failed to install dependencies for ${term.bold('v-analyzer')}')
@@ -348,7 +349,7 @@ fn build_from_sources() ! {
348349

349350
println('${term.green('✓')} Dependencies for ${term.bold('v-analyzer')} installed successfully')
350351

351-
chdir(analyzer_sources_path)!
352+
chdir(analyzer_sources_dir_path)!
352353
exit_code := run_command('v ${compiler_flag} build.vsh 1>/dev/null') or {
353354
errorln('Failed to build ${term.bold('v-analyzer')}: ${err}')
354355
return
@@ -360,27 +361,27 @@ fn build_from_sources() ! {
360361

361362
println('Moving ${term.bold('v-analyzer')} binary to the standard location...')
362363

363-
os.mkdir_all(analyzer_bin_path) or {
364-
println('Failed to create directory: ${analyzer_bin_path}')
364+
os.mkdir_all(analyzer_bin_dir_path) or {
365+
println('Failed to create directory: ${analyzer_bin_dir_path}')
365366
return
366367
}
367368

368-
os.cp_all('${analyzer_sources_path}/bin/v-analyzer', analyzer_bin_path, true) or {
369-
println('Failed to copy ${term.bold('v-analyzer')} binary to ${analyzer_bin_path}: ${err}')
369+
os.cp_all('${analyzer_sources_dir_path}/bin/v-analyzer', analyzer_bin_dir_path, true) or {
370+
println('Failed to copy ${term.bold('v-analyzer')} binary to ${analyzer_bin_dir_path}: ${err}')
370371
return
371372
}
372373

373-
println('${term.green('✓')} Successfully moved ${term.bold('v-analyzer')} binary to ${analyzer_bin_path}')
374+
println('${term.green('✓')} Successfully moved ${term.bold('v-analyzer')} binary to ${analyzer_bin_dir_path}')
374375

375376
println('${term.green('✓')} ${term.bold('v-analyzer')} built successfully')
376377
}
377378

378379
fn already_cloned() bool {
379-
if !os.exists(analyzer_sources_path) {
380+
if !os.exists(analyzer_sources_dir_path) {
380381
return false
381382
}
382383

383-
files := os.ls(analyzer_sources_path) or { return false }
384+
files := os.ls(analyzer_sources_dir_path) or { return false }
384385
return files.len > 0
385386
}
386387

@@ -459,11 +460,6 @@ fn run_command(cmd string) !int {
459460
return command.exit_code
460461
}
461462

462-
fn norm_expand_tilde_to_home(path string) string {
463-
norm_path := os.norm_path(path)
464-
return os.expand_tilde_to_home(norm_path)
465-
}
466-
467463
pub fn errorln(msg string) {
468464
eprintln('${term.red('[ERROR]')} ${msg}')
469465
}

0 commit comments

Comments
 (0)