From 7d0de3a4b90c9399f601dedac1d6a2a59b0da3f0 Mon Sep 17 00:00:00 2001 From: Francesco Abbate Date: Fri, 25 Jun 2021 15:37:04 +0200 Subject: [PATCH] WIP: try to introduce project workspace save --- data/core/init.lua | 20 ++++++++++++++---- data/core/project.lua | 48 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index 418dfc35..23752493 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -82,6 +82,13 @@ function core.new_project_from_directory(dir_path_abs) end +function core.project_workspace_name() + for _, dir in ipairs(core.project_entries) do + return dir.item.filename + end +end + + local function strip_leading_path(filename) return filename:sub(2) end @@ -304,7 +311,7 @@ local function create_user_directory() error("cannot create directory: \"" .. dirname_create .. "\"") end end - for _, modname in ipairs {'plugins', 'projects', 'colors', 'fonts'} do + for _, modname in ipairs {'plugins', 'projects', 'ws', 'colors', 'fonts'} do local subdirname = dirname_create .. '/' .. modname if not system.mkdir(subdirname) then error("cannot create directory: \"" .. subdirname .. "\"") @@ -480,7 +487,7 @@ function core.init() core.log_items = {} core.docs = {} core.project_entries = {} - core.project_name = "" + -- core.project_name = "" local init_files = {} local delayed_errors = {} @@ -642,8 +649,13 @@ end function core.on_quit_project() - local filename = USERDIR .. PATHSEP .. "workspace.lua" - core.try(project.save_workspace, filename) + -- local filename = USERDIR .. PATHSEP .. "workspace.lua" + -- core.try(project.save_workspace, filename) + if core.project_name then + core.try(project.save, filename) + else + core.try(project.save_unnamed) + end end diff --git a/data/core/project.lua b/data/core/project.lua index 3ec0367f..20ae966e 100644 --- a/data/core/project.lua +++ b/data/core/project.lua @@ -123,8 +123,8 @@ function project.save_workspace(filename) end local project_entries_text = common.serialize(topdir_entries) fp:write(string.format( - "return { project_name = %q, working_dir = %q, documents = %s, project_entries = %s }\n", - core.project_name, core.working_dir, node_text, project_entries_text)) + "return { project_name = %s, working_dir = %q, documents = %s, project_entries = %s }\n", + core.project_name and string.format("%q", core.project_name) or "nil", core.working_dir, node_text, project_entries_text)) fp:close() end end @@ -146,6 +146,50 @@ function project.save(name) core.log("Saved project %s.", core.project_name) end +local function workspace_files_for(basename) + local workspace_dir = USERDIR .. PATHSEP .. "ws" + local info_wsdir = system.get_file_info(workspace_dir) + if not info_wsdir then + local ok, err = system.mkdir(workspace_dir) + if not ok then + error("cannot create workspace directory: \"" .. err .. "\"") + end + end + return coroutine.wrap(function() + local files = system.list_dir(workspace_dir) or {} + local n = #basename + for _, file in ipairs(files) do + if file:sub(1, n) == basename then + local id = tonumber(file:sub(n + 1):match("^-(%d+)$")) + if id then + coroutine.yield(workspace_dir .. PATHSEP .. file, id) + end + end + end + end) +end + +local function get_workspace_filename(basename) + local id_list = {} + for filename, id in workspace_files_for(basename) do + id_list[id] = true + end + local id = 1 + while id_list[id] do + id = id + 1 + end + return common.path_join(USERDIR, "ws", basename .. "-" .. tostring(id)) +end + +function project.save_unnamed() + local name = core.project_workspace_name() + -- empty projects shoud return nil and we don't wave them + if name then + local filename = get_workspace_filename(name) + save_workspace(filename) + end +end + function project.load_workspace(filename) local load = loadfile(filename)