]>
Commit | Line | Data |
---|---|---|
503d09fc RBR |
1 | ------------------------------------------------------------------------------- |
2 | -- Utilities shared by all modules. Categorize better if > 5 public functions | |
3 | ------------------------------------------------------------------------------- | |
4 | local Util = {} | |
5 | ------------------------------------------------------------------------------- | |
6 | -- Internal Functions | |
7 | ------------------------------------------------------------------------------- | |
8 | local fs = vim.loop | |
9 | ||
10 | local function create_directory(directory) | |
11 | local stat = fs.fs_stat(directory) | |
12 | if stat then | |
13 | if stat.type == 'directory' then | |
14 | return | |
15 | else | |
16 | error('Expected directory but found file at: ' .. directory) | |
17 | end | |
18 | else | |
19 | local parent = directory:match('(.+)/[^/]*$') | |
20 | if parent then | |
21 | create_directory(parent) | |
22 | end | |
23 | local success, error = fs.fs_mkdir(directory, 493) | |
24 | if not success then | |
25 | error('Could not directory at: ' .. directory .. '. ' .. error) | |
26 | end | |
27 | end | |
28 | end | |
29 | ------------------------------------------------------------------------------- | |
30 | -- Public Interface | |
31 | ------------------------------------------------------------------------------- | |
32 | function Util.ensure_directory_exists(path) | |
33 | local full_path = vim.fn.expand(path) | |
34 | create_directory(path) | |
35 | end | |
36 | ||
37 | function Util.join(...) | |
38 | local separator = '/' | |
39 | local paths = {...} | |
40 | return table.concat(paths, separator):gsub(separator..'+', separator) | |
41 | end | |
42 | ||
43 | function Util.directory_name(file_path) | |
44 | local pattern = '(.+)/[^/]+$' | |
45 | return file_path:match(pattern) | |
46 | end | |
47 | ||
48 | return Util |