Skip to content

Commit

Permalink
refactor: store project metadata in metadata module (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Mar 24, 2024
1 parent b21db15 commit eae3f91
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
14 changes: 3 additions & 11 deletions build.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@
// By default, just `v build.vsh` will use debug mode.
import os
import cli
import time
import term
import v.vmod
import metadata

const version = vmod.decode(@VMOD_FILE) or { panic(err) }.version
const bin_path = './bin/v-analyzer' + $if windows { '.exe' } $else { '' }

const build_commit = os.execute('git rev-parse --short HEAD').output.trim_space()
const build_datetime = time.now().format_ss()

enum ReleaseMode {
release
debug
Expand Down Expand Up @@ -63,7 +58,7 @@ fn prepare_output_dir() {
}

fn build(mode ReleaseMode, explicit_debug bool) {
println('Building v-analyzer at commit: ${build_commit}, build time: ${build_datetime} ...')
println('Building v-analyzer at commit: ${metadata.build_commit}, build time: ${metadata.build_datetime} ...')

prepare_output_dir()
println('${term.green('✓')} Prepared output directory')
Expand Down Expand Up @@ -91,12 +86,9 @@ fn build(mode ReleaseMode, explicit_debug bool) {

// main program:

os.setenv('BUILD_DATETIME', build_datetime, true)
os.setenv('BUILD_COMMIT', build_commit, true)

mut cmd := cli.Command{
name: 'v-analyzer-builder'
version: version
version: metadata.full_version
description: 'Builds the v-analyzer binary.'
posix_mode: true
execute: fn (_ cli.Command) ! {
Expand Down
7 changes: 7 additions & 0 deletions metadata/embed.v → metadata/metadata.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
module metadata

import os
import v.vmod
import v.embed_file
import time

pub const manifest = vmod.decode(@VMOD_FILE) or { panic(err) }
pub const build_commit = os.execute('git rev-parse --short HEAD').output.trim_space()
pub const build_datetime = time.now().format_ss()
pub const full_version = manifest.version + '.' + build_commit

struct EmbedFS {
pub mut:
Expand Down
17 changes: 6 additions & 11 deletions server/general.v
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import server.protocol
import server.semantic
import server.progress
import server.intentions
import v.vmod

pub const manifest = vmod.decode(@VMOD_FILE) or { panic(err) }
pub const build_datetime = $env('BUILD_DATETIME')
pub const build_commit = $env('BUILD_COMMIT')

// initialize sends the server capabilities to the client
pub fn (mut ls LanguageServer) initialize(params lsp.InitializeParams, mut wr ResponseWriter) lsp.InitializeResult {
Expand Down Expand Up @@ -90,8 +85,8 @@ pub fn (mut ls LanguageServer) initialize(params lsp.InitializeParams, mut wr Re
}
}
server_info: lsp.ServerInfo{
name: server.manifest.name
version: server.manifest.version
name: metadata.manifest.name
version: metadata.manifest.version
}
}
}
Expand Down Expand Up @@ -421,11 +416,11 @@ fn (mut ls LanguageServer) print_info(process_id int, client_info lsp.ClientInfo
} else {
'Unknown'
}
ls.client.log_message('v-analyzer version: ${server.manifest.version}, commit: ${server.build_commit}, OS: ${os.user_os()} x${arch}',
ls.client.log_message('v-analyzer version: ${metadata.manifest.version}, commit: ${metadata.build_commit}, OS: ${os.user_os()} x${arch}',
.info)
ls.client.log_message('v-analyzer executable path: ${os.executable()}', .info)
ls.client.log_message('v-analyzer build with V ${@VHASH}', .info)
ls.client.log_message('v-analyzer build at ${server.build_datetime}', .info)
ls.client.log_message('v-analyzer build at ${metadata.build_datetime}', .info)
ls.client.log_message('Client / Editor: ${client_name} (PID: ${process_id})', .info)

loglib.with_fields({
Expand All @@ -435,7 +430,7 @@ fn (mut ls LanguageServer) print_info(process_id int, client_info lsp.ClientInfo
'arch': 'x${arch}'
'executable': os.executable()
'build_with': @VHASH
'build_at': server.build_datetime
'build_commit': server.build_commit
'build_at': metadata.build_datetime
'build_commit': metadata.build_commit
}).info('v-analyzer started')
}
14 changes: 5 additions & 9 deletions src/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import jsonrpc
import streams
import analyzer
import lsp.log
import v.vmod

const manifest = vmod.decode(@VMOD_FILE) or { panic(err) }

const full_version = manifest.version + '.' + server.build_commit
import metadata

// default_tcp_port is default TCP port that the analyzer uses to connect to the socket
// when the --socket flag is passed at startup.
Expand Down Expand Up @@ -90,9 +86,9 @@ fn setup_logger(to_file bool) {

fn main() {
mut cmd := cli.Command{
name: manifest.name
version: full_version
description: manifest.description
name: metadata.manifest.name
version: metadata.full_version
description: metadata.manifest.description
execute: run
posix_mode: true
}
Expand Down Expand Up @@ -128,7 +124,7 @@ fn main() {
description: 'Checks for v-analyzer updates.'
execute: check_updates_cmd
posix_mode: true
version: full_version
version: metadata.full_version
})

cmd.add_flags([
Expand Down

0 comments on commit eae3f91

Please sign in to comment.