diff options
| author | William Boman <william@redwill.se> | 2022-09-29 01:02:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-29 01:02:45 +0200 |
| commit | 342c81c29b74a4254dd2636005549832a6b44631 (patch) | |
| tree | 3a8c4f96d5bc0bf8dee2be67b6fc76ceb0e3c8c8 /lua | |
| parent | feat(verible): add support for CentOS 7.9 (#471) (diff) | |
| download | mason-342c81c29b74a4254dd2636005549832a6b44631.tar mason-342c81c29b74a4254dd2636005549832a6b44631.tar.gz mason-342c81c29b74a4254dd2636005549832a6b44631.tar.bz2 mason-342c81c29b74a4254dd2636005549832a6b44631.tar.lz mason-342c81c29b74a4254dd2636005549832a6b44631.tar.xz mason-342c81c29b74a4254dd2636005549832a6b44631.tar.zst mason-342c81c29b74a4254dd2636005549832a6b44631.zip | |
refactor(platform): more functional approach to parsing (#479)
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/mason-core/functional/init.lua | 1 | ||||
| -rw-r--r-- | lua/mason-core/functional/table.lua | 11 | ||||
| -rw-r--r-- | lua/mason-core/platform.lua | 83 |
3 files changed, 46 insertions, 49 deletions
diff --git a/lua/mason-core/functional/init.lua b/lua/mason-core/functional/init.lua index 5d4efdb7..a153e8fa 100644 --- a/lua/mason-core/functional/init.lua +++ b/lua/mason-core/functional/init.lua @@ -93,6 +93,7 @@ _.pick = tbl.pick _.keys = tbl.keys _.size = tbl.size _.to_pairs = tbl.to_pairs +_.from_pairs = tbl.from_pairs _.invert = tbl.invert ---@module "mason-core.functional.type" diff --git a/lua/mason-core/functional/table.lua b/lua/mason-core/functional/table.lua index 150de9ce..e96b0157 100644 --- a/lua/mason-core/functional/table.lua +++ b/lua/mason-core/functional/table.lua @@ -37,6 +37,17 @@ _.to_pairs = fun.curryN(function(tbl) end, 1) ---@generic K, V +---@param pairs { [1]: K, [2]: V }[] +---@return table<K, V> +_.from_pairs = fun.curryN(function(pairs) + local result = {} + for _, pair in ipairs(pairs) do + result[pair[1]] = pair[2] + end + return result +end, 1) + +---@generic K, V ---@param tbl table<K, V> ---@return table<V, K> _.invert = fun.curryN(function(tbl) diff --git a/lua/mason-core/platform.lua b/lua/mason-core/platform.lua index 848e7af1..f6c3a4b5 100644 --- a/lua/mason-core/platform.lua +++ b/lua/mason-core/platform.lua @@ -115,74 +115,59 @@ end ---@type async fun(): table M.os_distribution = _.lazy(function() - local Result = require "mason-core.result" + local parse_os_release = _.compose(_.from_pairs, _.map(_.split "="), _.split "\n") - ---Parses the provided contents of an /etc/\*-release file and identifies the Linux distribution. - ---@param contents string The contents of a /etc/\*-release file. - ---@return table<string, any> - local function parse_linux_dist(contents) - local lines = vim.split(contents, "\n") + ---@param entries table<string, string> + local function parse_ubuntu(entries) + -- Parses the Ubuntu OS VERSION_ID into their version components, e.g. "18.04" -> {major=18, minor=04} + local version_id = entries.VERSION_ID:gsub([["]], "") + local version_parts = vim.split(version_id, "%.") + local major = tonumber(version_parts[1]) + local minor = tonumber(version_parts[2]) - local entries = {} - - for i = 1, #lines do - local line = lines[i] - local index = line:find "=" - if index then - local key = line:sub(1, index - 1) - local value = line:sub(index + 1) - entries[key] = value - end - end - - if entries.ID == "ubuntu" then - -- Parses the Ubuntu OS VERSION_ID into their version components, e.g. "18.04" -> {major=18, minor=04} - local version_id = entries.VERSION_ID:gsub([["]], "") - local version_parts = vim.split(version_id, "%.") - local major = tonumber(version_parts[1]) - local minor = tonumber(version_parts[2]) + return { + id = "ubuntu", + version_id = version_id, + version = { major = major, minor = minor }, + } + end - return { - id = "ubuntu", - version_id = version_id, - version = { major = major, minor = minor }, - } - elseif entries.ID == '"centos"' then - -- Parses the CentOS VERSION_ID into a major version (the only thing available). - local version_id = entries.VERSION_ID:gsub([["]], "") - local major = tonumber(version_id) + ---@param entries table<string, string> + local function parse_centos(entries) + -- Parses the CentOS VERSION_ID into a major version (the only thing available). + local version_id = entries.VERSION_ID:gsub([["]], "") + local major = tonumber(version_id) - return { - id = "centos", - version_id = version_id, - version = { major = major }, - } - else - return { - id = "linux-generic", - version = {}, - } - end + return { + id = "centos", + version_id = version_id, + version = { major = major }, + } end + ---Parses the provided contents of an /etc/*-release file and identifies the Linux distribution. + local parse_linux_dist = _.cond { + { _.prop_eq("ID", "ubuntu"), parse_ubuntu }, + { _.prop_eq("ID", [["centos"]]), parse_centos }, + { _.T, _.always { id = "linux-generic", version = {} } }, + } + return M.when { linux = function() local spawn = require "mason-core.spawn" return spawn .bash({ "-c", "cat /etc/*-release" }) - :map_catching(function(result) - return parse_linux_dist(result.stdout) - end) + :map_catching(_.compose(parse_linux_dist, parse_os_release, _.prop "stdout")) :recover(function() return { id = "linux-generic", version = {} } end) :get_or_throw() end, mac = function() - return Result.success { id = "macOS" } + return { id = "macOS", version = {} } end, win = function() - return Result.success { id = "windows" } + return { id = "windows", version = {} } end, } end) |
