-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.lua
150 lines (130 loc) · 3.52 KB
/
util.lua
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
local core = require "core"
local common = require "core.common"
local DocView = require "core.docview"
---Utility functions for the SCM plugin.
local util = {}
---@return string project_dir
function util.get_current_project()
if core.active_view and core.active_view.doc and core.active_view.doc.abs_filename then
local filename = core.active_view.doc.abs_filename
for _, project in ipairs(core.projects) do
if common.path_belongs_to(filename, project.path) then
return project.path
end
end
end
return core.root_project().path
end
---@param filepath string
---@return string? project_dir
function util.get_file_project_dir(filepath)
for _, project in ipairs(core.projects or {}) do
if common.path_belongs_to(filepath, project.path) then
return project.path
end
end
return nil
end
---Get project directory for given file or current project dir if non given.
---@param filepath? string
---@return string? project_dir
function util.get_project_dir(filepath)
local project
if not filepath then
project = util.get_current_project()
else
project = util.get_file_project_dir(filepath)
end
return project
end
function util.reload_doc(abs_filename)
for _, doc in ipairs(core.docs) do
if doc.abs_filename == abs_filename then
doc:reload()
break
end
end
end
---@return core.doc?
function util.get_current_doc()
if core.active_view and core.active_view:extends(DocView) then
local doc = core.active_view.doc
if doc and doc.abs_filename then
return doc
end
end
return nil
end
---Split a string by the given delimeter
---@param s string The string to split
---@param delimeter string Delimeter without lua patterns
---@param delimeter_pattern? string Optional delimeter with lua patterns
---@return table
function util.split(s, delimeter, delimeter_pattern)
if not delimeter_pattern then
delimeter_pattern = delimeter
end
local result = {};
for match in (s..delimeter):gmatch("(.-)"..delimeter_pattern) do
table.insert(result, match);
end
return result;
end
---Check if a file exists.
---@param file_path string
---@return boolean
function util.file_exists(file_path)
local file = io.open(file_path, "r")
if file ~= nil then
file:close()
return true
end
return false
end
---Check if a command exists on the system by inspecting the PATH envar.
---@param command string
---@return boolean
function util.command_exists(command)
local command_win = nil
if PLATFORM == "Windows" then
if not command:find("%.exe$") then
command_win = command .. ".exe"
end
end
if
util.file_exists(command)
or
(command_win and util.file_exists(command_win))
then
return true
end
local env_path = os.getenv("PATH")
if env_path then
local path_list = {}
if PLATFORM ~= "Windows" then
path_list = util.split(env_path, ":")
else
path_list = util.split(env_path, ";")
end
-- Automatic support for brew, macports, etc...
if PLATFORM == "Mac OS X" then
if
system.get_file_info("/usr/local/bin")
and
not string.find(env_path, "/usr/local/bin", 1, true)
then
table.insert(path_list, 1, "/usr/local/bin")
end
end
for _, path in pairs(path_list) do
local path_fix = path:gsub("[/\\]$", "") .. PATHSEP
if util.file_exists(path_fix .. command) then
return true
elseif command_win and util.file_exists(path_fix .. command_win) then
return true
end
end
end
return false
end
return util