Added support for XDG user dirs.
This commit is contained in:
parent
100c0a7a66
commit
e86d95ccfc
@ -6,6 +6,15 @@ extends Object
|
|||||||
class_name GDBFsUtil
|
class_name GDBFsUtil
|
||||||
|
|
||||||
const __META_FILENAME_REGEX = "__gdb_fsutil_filename_regex__"
|
const __META_FILENAME_REGEX = "__gdb_fsutil_filename_regex__"
|
||||||
|
const __META_ENVVAR_REGEX = "__gdb_fsutil_envvar_regex__"
|
||||||
|
|
||||||
|
class SpecialFolder:
|
||||||
|
var path := ""
|
||||||
|
var name := ""
|
||||||
|
|
||||||
|
func _init(path_ : String, name_ : String):
|
||||||
|
self.path = path_
|
||||||
|
self.name = name_
|
||||||
|
|
||||||
################
|
################
|
||||||
# public stuff #
|
# public stuff #
|
||||||
@ -67,6 +76,23 @@ static func get_home_folder() -> String:
|
|||||||
|
|
||||||
return "/"
|
return "/"
|
||||||
|
|
||||||
|
static func get_special_folders() -> Array:
|
||||||
|
if OS.has_feature("X11"):
|
||||||
|
return __get_xdg_folders()
|
||||||
|
return []
|
||||||
|
|
||||||
|
static func replace_environment_variables(string : String) -> String:
|
||||||
|
var matches := __get_envvar_regex().search_all(string)
|
||||||
|
var parts := PoolStringArray()
|
||||||
|
var pos := 0
|
||||||
|
for the_match in matches:
|
||||||
|
var var_name : String = the_match.strings[-2] if !the_match.strings[-1] else the_match.strings[-1]
|
||||||
|
parts.append(string.substr(pos, the_match.get_start()))
|
||||||
|
parts.append(OS.get_environment(var_name))
|
||||||
|
pos = the_match.get_end()
|
||||||
|
parts.append(string.substr(pos))
|
||||||
|
return parts.join("")
|
||||||
|
|
||||||
#################
|
#################
|
||||||
# private stuff #
|
# private stuff #
|
||||||
#################
|
#################
|
||||||
@ -79,6 +105,19 @@ static func __get_filename_regex() -> RegEx:
|
|||||||
tree.set_meta(__META_FILENAME_REGEX, filename_regex)
|
tree.set_meta(__META_FILENAME_REGEX, filename_regex)
|
||||||
return filename_regex
|
return filename_regex
|
||||||
|
|
||||||
|
static func __get_envvar_regex() -> RegEx:
|
||||||
|
var tree := GDBUtility.get_scene_tree()
|
||||||
|
if tree.has_meta(__META_ENVVAR_REGEX):
|
||||||
|
return tree.get_meta(__META_ENVVAR_REGEX) as RegEx
|
||||||
|
var envvar_regex := RegEx.new()
|
||||||
|
if OS.has_feature("Windows"):
|
||||||
|
envvar_regex.compile("%(\\w+)%")
|
||||||
|
else:
|
||||||
|
envvar_regex.compile("\\$({(\\w+)}|(\\w+))")
|
||||||
|
|
||||||
|
tree.set_meta(__META_ENVVAR_REGEX, envvar_regex)
|
||||||
|
return envvar_regex
|
||||||
|
|
||||||
static func __find_all_by_name(path, name, result, files_only):
|
static func __find_all_by_name(path, name, result, files_only):
|
||||||
var dir = Directory.new()
|
var dir = Directory.new()
|
||||||
if dir.open(path) != OK:
|
if dir.open(path) != OK:
|
||||||
@ -98,3 +137,29 @@ static func __find_all_by_name(path, name, result, files_only):
|
|||||||
__find_all_by_name(full_name, name, result, files_only)
|
__find_all_by_name(full_name, name, result, files_only)
|
||||||
|
|
||||||
dir.list_dir_end()
|
dir.list_dir_end()
|
||||||
|
|
||||||
|
static func __get_xdg_folders() -> Array:
|
||||||
|
var user_dirs_filename := get_home_folder().plus_file(".config/user-dirs.dirs")
|
||||||
|
var user_dirs_file := File.new()
|
||||||
|
if !user_dirs_file.file_exists(user_dirs_filename):
|
||||||
|
return []
|
||||||
|
var status := user_dirs_file.open(user_dirs_filename, File.READ)
|
||||||
|
if status != OK:
|
||||||
|
printerr("Error fetching XDG user dirs.")
|
||||||
|
return []
|
||||||
|
|
||||||
|
var dir_regex := RegEx.new()
|
||||||
|
status = dir_regex.compile("^XDG_([A-Z]+)_DIR=\"([^\"]+)\"$")
|
||||||
|
assert(status == OK)
|
||||||
|
|
||||||
|
var folders := []
|
||||||
|
while !user_dirs_file.eof_reached():
|
||||||
|
var line := user_dirs_file.get_line().strip_edges()
|
||||||
|
var result := dir_regex.search(line)
|
||||||
|
if result == null:
|
||||||
|
continue
|
||||||
|
var folder_name : String = result.strings[1].capitalize()
|
||||||
|
var folder_path : String = replace_environment_variables(result.strings[2])
|
||||||
|
folders.append(SpecialFolder.new(folder_path, folder_name))
|
||||||
|
|
||||||
|
return folders
|
||||||
|
Loading…
x
Reference in New Issue
Block a user