Implement xplr.util.version

Closes: https://github.com/sayanarijit/xplr/issues/540
pull/549/head
Arijit Basu 1 year ago committed by Arijit Basu
parent d4edf3302f
commit e559b96e31

@ -32,13 +32,13 @@ Knowing that we use the `{major}.{minor}.{patch}` versioning format,
e.g. e.g.
- `1.0.0` -> `1.0.x`: Bug fix (fully compatible). - `1.0.0` -> `1.0.x`: Patch (fully compatible).
- `1.0.0` -> `1.x.x`: Only backwards compatible. You can't generally use for - `1.0.0` -> `1.x.x`: Only backwards compatible. You can't generally use for
e.g. `app-1.0.0` with `config-1.1.0`. But vice versa is fine. e.g. `app-1.0.0` with `config-1.1.0`. But vice versa is fine.
- `1.0.0` -> `x.x.x`: Not compatible at all. - `1.0.0` -> `x.x.x`: Not compatible at all.
Note that until we're `v1`, we'll be using the `{minor}` version number as Note that until we're `v1`, we'll be using the `{minor}` version number as
`{major}`, and the `{patch}` fix number as `{minor}` to determine `{major}`, and the `{patch}` number as `{minor}` to determine
compatibility. compatibility.
</details> </details>

@ -1,3 +1,16 @@
### xplr.util.version
Get the current xplr version details.
Type: function() -> { major: number, minor: number, patch: number }
Example:
```lua
xplr.util.version()
-- { major = 0, minor = 0, patch = 0 }
```
### xplr.util.dirname ### xplr.util.dirname
Get the directory name of a given path. Get the directory name of a given path.

@ -29,15 +29,15 @@ fn parse_version(version: &str) -> Result<(u16, u16, u16, Option<u16>)> {
let major = configv.next().unwrap_or_default().parse::<u16>()?; let major = configv.next().unwrap_or_default().parse::<u16>()?;
let minor = configv.next().unwrap_or_default().parse::<u16>()?; let minor = configv.next().unwrap_or_default().parse::<u16>()?;
let bugfix = configv let patch = configv
.next() .next()
.and_then(|s| s.split('-').next()) .and_then(|s| s.split('-').next())
.unwrap_or_default() .unwrap_or_default()
.parse::<u16>()?; .parse::<u16>()?;
let beta = configv.next().unwrap_or_default().parse::<u16>().ok(); let pre = configv.next().unwrap_or_default().parse::<u16>().ok();
Ok((major, minor, bugfix, beta)) Ok((major, minor, patch, pre))
} }
/// Check the config version and notify users. /// Check the config version and notify users.

@ -1,3 +1,4 @@
use crate::app::VERSION;
use crate::explorer; use crate::explorer;
use crate::lua; use crate::lua;
use crate::msg::in_::external::ExplorerConfig; use crate::msg::in_::external::ExplorerConfig;
@ -18,6 +19,7 @@ use std::process::Command;
pub(crate) fn create_table(lua: &Lua) -> Result<Table> { pub(crate) fn create_table(lua: &Lua) -> Result<Table> {
let mut util = lua.create_table()?; let mut util = lua.create_table()?;
util = version(util, lua)?;
util = dirname(util, lua)?; util = dirname(util, lua)?;
util = basename(util, lua)?; util = basename(util, lua)?;
util = absolute(util, lua)?; util = absolute(util, lua)?;
@ -32,6 +34,42 @@ pub(crate) fn create_table(lua: &Lua) -> Result<Table> {
Ok(util) Ok(util)
} }
/// Get the xplr version details.
///
/// Type: function() -> { major: number, minor: number, patch: number }
///
/// Example:
///
/// ```lua
/// xplr.util.version()
/// -- { major = 0, minor = 0, patch = 0 }
/// ```
pub fn version<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> {
#[derive(Debug, Default, Serialize, Deserialize)]
struct Version {
major: u16,
minor: u16,
patch: u16,
}
let func = lua.create_function(|lua, ()| {
let (major, minor, patch, _) =
lua::parse_version(VERSION).map_err(LuaError::custom)?;
let version = Version {
major,
minor,
patch,
};
let res = lua::serialize(lua, &version).map_err(LuaError::custom)?;
Ok(res)
})?;
util.set("version", func)?;
Ok(util)
}
/// Get the directory name of a given path. /// Get the directory name of a given path.
/// ///
/// Type: function( path:string ) -> path:string|nil /// Type: function( path:string ) -> path:string|nil
@ -215,8 +253,8 @@ pub fn from_json<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> {
/// -- }]] /// -- }]]
/// ``` /// ```
pub fn to_json<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> { pub fn to_json<'a>(util: Table<'a>, lua: &Lua) -> Result<Table<'a>> {
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)] #[derive(Debug, Default, Serialize, Deserialize)]
pub struct Options { struct Options {
pretty: bool, pretty: bool,
} }

Loading…
Cancel
Save