-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathsession.lua
More file actions
155 lines (132 loc) · 3.87 KB
/
session.lua
File metadata and controls
155 lines (132 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
local osys = require("cmake-tools.osys")
local utils = require("cmake-tools.utils")
local function find_file_in_dir_or_parent(dir, fname)
local path = dir .. "/" .. fname
local file = io.open(path, "r")
if file then
file:close()
return path
end
local parentDir = dir:match("(.+)/[^/]+$")
if not parentDir then
return nil
end
return find_file_in_dir_or_parent(parentDir, fname)
end
local session = {
dir = {
unix = vim.fn.expand("~") .. "/.cache/cmake_tools_nvim/",
mac = vim.fn.expand("~") .. "/.cache/cmake_tools_nvim/",
win = vim.fn.expand("~") .. "/AppData/Local/cmake_tools_nvim/",
},
}
---@return string
local function get_cache_path()
if osys.islinux then
return session.dir.unix
elseif osys.ismac then
return session.dir.mac
elseif osys.iswsl then
return session.dir.unix
elseif osys.isbsd then
return session.dir.unix
elseif osys.iswin32 then
return session.dir.win
end
end
---@param cwd string neovim working directory
---@return string
local function get_local_config_path(cwd)
return find_file_in_dir_or_parent(cwd, ".cmake-tools.lua")
end
---@param cwd string neovim working directory
---@return string
local function get_current_path(cwd)
local clean_path = cwd:gsub("/", "")
clean_path = clean_path:gsub("\\", "")
clean_path = clean_path:gsub(":", "")
return get_cache_path() .. clean_path .. ".lua"
end
local function init_cache()
local cache_path = get_cache_path()
if not utils.file_exists(cache_path) then
utils.mkdir(cache_path)
end
end
---@param cwd string neovim working directory
local function init_session(cwd)
local path = get_local_config_path(cwd)
if not path then
init_cache()
path = get_current_path(cwd)
end
if not utils.file_exists(path) then
local file = io.open(path, "w")
if file then
file:close()
end
end
end
---@param cwd string neovim working directory (used as cache key)
---@return SerializedConfig raw session data, or empty table if none exists
function session.load(cwd)
local path = get_local_config_path(cwd)
if not path then
path = get_current_path(cwd)
end
if utils.file_exists(path) then
local config = dofile(path)
return config or {}
end
return {}
end
---@param config Config
---@param old_config SerializedConfig
---@return Config merged config with session state applied
function session.update(config, old_config)
if next(old_config) == nil then
return config
end
local mt = getmetatable(config)
local build_directory = old_config.build_directory
local old_build_dir = old_config.base_settings and old_config.base_settings.build_dir
old_config.build_directory = nil
config = vim.tbl_deep_extend("force", config, old_config)
setmetatable(config, mt)
if build_directory and old_build_dir then
config:update_build_dir(build_directory, old_build_dir)
end
return config
end
---@param cwd string neovim working directory (used as cache key)
---@param config Config current config to persist
function session.save(cwd, config)
local path = get_local_config_path()
if not path then
init_session(cwd)
path = get_current_path(cwd)
end
local file = io.open(path, "w")
---@class SerializedConfig
local serialized_object = {
build_directory = config:build_directory_path(),
build_type = config.build_type,
variant = config.variant,
build_target = config.build_target,
launch_target = config.launch_target,
kit = config.kit,
configure_preset = config.configure_preset,
env_script = config.env_script,
build_preset = config.build_preset,
test_preset = config.test_preset,
selected_test = config.selected_test,
base_settings = config.base_settings,
target_settings = config.target_settings,
cwd = config.cwd,
}
if file then
file:write(tostring("return " .. vim.inspect(serialized_object)))
file:close()
end
end
return session