From 1a9a72942fffc9f62cb8f1ae4e1d5ca32aa1f399 Mon Sep 17 00:00:00 2001 From: "Artem V. Navrotskiy" Date: Mon, 8 Jul 2024 10:49:18 +0300 Subject: [PATCH] Add `GetVersion` method to public API (#1479) Signed-off-by: Artem V. Navrotskiy --- kclvm/Cargo.lock | 1 + kclvm/api/Cargo.toml | 1 + kclvm/api/src/service/capi.rs | 10 ++++++++++ kclvm/api/src/service/jsonrpc.rs | 9 +++++++++ kclvm/api/src/service/service_impl.rs | 24 ++++++++++++++++++++++++ kclvm/spec/gpyrpc/gpyrpc.proto | 11 +++++++++++ kclvm/version/src/lib.rs | 1 + 7 files changed, 57 insertions(+) diff --git a/kclvm/Cargo.lock b/kclvm/Cargo.lock index c3464c392..14afbc67e 100644 --- a/kclvm/Cargo.lock +++ b/kclvm/Cargo.lock @@ -1698,6 +1698,7 @@ dependencies = [ "kclvm-sema", "kclvm-tools", "kclvm-utils", + "kclvm-version", "maplit", "once_cell", "prost", diff --git a/kclvm/api/Cargo.toml b/kclvm/api/Cargo.toml index 4d8fdb53c..3d9fa50ff 100644 --- a/kclvm/api/Cargo.toml +++ b/kclvm/api/Cargo.toml @@ -31,6 +31,7 @@ kclvm-ast-pretty = {path = "../ast_pretty"} kclvm-runtime = {path = "../runtime"} kclvm-tools = {path = "../tools" } kclvm-query = {path = "../query"} +kclvm-version = { path = "../version" } kcl-language-server = {path = "../tools/src/LSP"} kclvm-utils = {path = "../utils"} diff --git a/kclvm/api/src/service/capi.rs b/kclvm/api/src/service/capi.rs index 381ad1ca5..ad651c48f 100644 --- a/kclvm/api/src/service/capi.rs +++ b/kclvm/api/src/service/capi.rs @@ -166,6 +166,7 @@ pub extern "C" fn kclvm_service_call_with_length( pub(crate) fn kclvm_get_service_fn_ptr_by_name(name: &str) -> u64 { match name { "KclvmService.Ping" => ping as *const () as u64, + "KclvmService.GetVersion" => get_version as *const () as u64, "KclvmService.ParseFile" => parse_file as *const () as u64, "KclvmService.ParseProgram" => parse_program as *const () as u64, "KclvmService.LoadPackage" => load_package as *const () as u64, @@ -200,6 +201,15 @@ pub(crate) fn ping( call!(serv, args, result_len, PingArgs, ping) } +/// get_version is used to get kclvm service version +pub(crate) fn get_version( + serv: *mut kclvm_service, + args: *const c_char, + result_len: *mut usize, +) -> *const c_char { + call!(serv, args, result_len, GetVersionArgs, get_version) +} + /// parse_file provides users with the ability to parse kcl single file /// /// # Parameters diff --git a/kclvm/api/src/service/jsonrpc.rs b/kclvm/api/src/service/jsonrpc.rs index 60736107b..b2a536e83 100644 --- a/kclvm/api/src/service/jsonrpc.rs +++ b/kclvm/api/src/service/jsonrpc.rs @@ -67,6 +67,14 @@ fn register_kclvm_service(io: &mut IoHandler) { }; futures::future::ready(catch!(kclvm_service_impl, args, ping)) }); + io.add_method("KclvmService.GetVersion", |params: Params| { + let kclvm_service_impl = KclvmServiceImpl::default(); + let args: GetVersionArgs = match params.parse() { + Ok(val) => val, + Err(err) => return futures::future::ready(Err(err)), + }; + futures::future::ready(catch!(kclvm_service_impl, args, get_version)) + }); io.add_method("KclvmService.ParseFile", |params: Params| { let kclvm_service_impl = KclvmServiceImpl::default(); let args: ParseFileArgs = match params.parse() { @@ -231,6 +239,7 @@ fn register_builtin_service(io: &mut IoHandler) { let result = ListMethodResult { method_name_list: vec![ "KclvmService.Ping".to_owned(), + "KclvmService.GetVersion".to_owned(), "KclvmService.ParseFile".to_owned(), "KclvmService.ParseProgram".to_owned(), "KclvmService.ExecProgram".to_owned(), diff --git a/kclvm/api/src/service/service_impl.rs b/kclvm/api/src/service/service_impl.rs index 9b203f150..57ca1f325 100644 --- a/kclvm/api/src/service/service_impl.rs +++ b/kclvm/api/src/service/service_impl.rs @@ -86,6 +86,30 @@ impl KclvmServiceImpl { }) } + /// GetVersion KclvmService, return the kclvm service version information + /// + /// # Examples + /// + /// ``` + /// use kclvm_api::service::service_impl::KclvmServiceImpl; + /// use kclvm_api::gpyrpc::*; + /// let serv = KclvmServiceImpl::default(); + /// let args = &GetVersionArgs { + /// ..Default::default() + /// }; + /// let get_version_result = serv.get_version(args).unwrap(); + /// assert!(get_version_result.version_info.to_string().contains("Version"), "{0}", get_version_result.version_info); + /// ``` + /// + pub fn get_version(&self, _args: &GetVersionArgs) -> anyhow::Result { + Ok(GetVersionResult { + version: kclvm_version::VERSION.to_string(), + checksum: kclvm_version::CHECK_SUM.to_string(), + git_sha: kclvm_version::GIT_SHA.to_string(), + version_info: kclvm_version::get_version_info(), + }) + } + /// Parse KCL program with entry files. /// /// # Examples diff --git a/kclvm/spec/gpyrpc/gpyrpc.proto b/kclvm/spec/gpyrpc/gpyrpc.proto index fcaf07022..b80d89b43 100644 --- a/kclvm/spec/gpyrpc/gpyrpc.proto +++ b/kclvm/spec/gpyrpc/gpyrpc.proto @@ -46,6 +46,7 @@ service BuiltinService { // gpyrpc.KclvmService service KclvmService { rpc Ping(Ping_Args) returns(Ping_Result); + rpc GetVersion(GetVersion_Args) returns(GetVersion_Result); rpc ExecProgram(ExecProgram_Args) returns(ExecProgram_Result); rpc BuildProgram(BuildProgram_Args) returns(BuildProgram_Result); @@ -83,6 +84,16 @@ message Ping_Result { string value = 1; } +message GetVersion_Args { + // empty +} +message GetVersion_Result { + string version = 1; + string checksum = 2; + string git_sha = 3; + string version_info = 4; +} + message ListMethod_Args { // empty } diff --git a/kclvm/version/src/lib.rs b/kclvm/version/src/lib.rs index 46b77f8a4..60afcb410 100644 --- a/kclvm/version/src/lib.rs +++ b/kclvm/version/src/lib.rs @@ -2,6 +2,7 @@ pub const VERSION: &str = include_str!("./../../../VERSION"); pub const CHECK_SUM: &str = "c020ab3eb4b9179219d6837a57f5d323"; +pub const GIT_SHA: &str = env!("VERGEN_GIT_SHA"); /// Get kCL full version string with the format `{version}-{check_sum}`. #[inline]