-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
177 lines (160 loc) · 5.22 KB
/
init.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
-- mod-version:3
local core = require "core"
local command = require "core.command"
local common = require "core.common"
local config = require "core.config"
local keymap = require "core.keymap"
local www = require "libraries.www"
local terminal = require "plugins.terminal"
local ptm = {}
-- FUTURE_TODO: after PROJECT REWORK is complete, use core.root_project().path instead of core.project_dir
-- Configuration Options
config.plugins.ptm = common.merge({
-- ?
}, config.plugins.ptm)
-- Functions
local templates = {}
-- NOTE: this function will return the first template that matches
local function get_template(template_name)
local template = nil
for _, v in pairs(templates) do
if template_name == v.name then
core.log("Template found: " .. template_name)
return v
end
end
core.log("Template not found: " .. template_name)
return nil
end
-- Creates and fills a file
local function create_and_fill_file(project_title, dir, file_name, file_content)
system.mkdir(core.project_dir .. "/" .. project_title .. "/" .. dir)
local f = io.open(core.project_dir .. "/" .. project_title .. "/" .. dir .. "/" .. file_name, "w")
if f then
f:write(file_content)
f:close()
else
core.log("Error: could not open file: " .. file_name)
end
end
-- Download a file with lite-xl-www
-- WIP: fix, cannot set undefined var connection ...
-- local agent = www.new()
-- local function download_file(file, filename)
-- local f = io.open(filename, "wb")
-- core.add_thread(function()
-- agent:get(file, {
-- response = function(response, chunk)
-- f:write(chunk)
-- end
-- })
-- end)
-- end
-- Template generation
local function generate_template(template_name, project_title, template_content)
-- Create and fill files
for k, file in pairs(template_content.files) do
create_and_fill_file(project_title, file.path, k, file.content)
end
-- Create directories
for k, dir in pairs(template_content.dirs) do
system.mkdir(core.project_dir .. "/" .. project_title .. "/" .. dir .. "/")
end
-- Download external libraries
for k, lib in pairs(template_content.ext_libs) do
system.chdir(core.project_dir .. "/" .. project_title .. "/" .. lib.path)
-- FUTURE_TODO: Add download status in StatusView
-- TODO: Add timely queue for executing generation functions (es. no more sleep 3)
-- download_file(lib.url, lib.filename)
process.start({ "wget", lib.url })
end
-- Create and fill config files for the LSP server
for k, file in pairs(template_content.lsp_config_files) do
create_and_fill_file(project_title, file.path, k, file.content)
end
-- Run commands
for k, cmd in pairs(template_content.commands) do
system.chdir(core.project_dir .. "/" .. project_title)
-- FIX: fix commands list print inside terminal
core.log("Executing command: " .. table.concat(cmd, " "))
command.perform("terminal:execute", table.concat(cmd, " "))
-- process.start({ "xdg-run", table.concat(cmd, " ")})
core.log("Command executed: " .. table.concat(cmd, " "))
end
end
-- Template selection
local function select_template(template_name, project_title)
system.mkdir(core.project_dir .. "/" .. project_title)
local template_content = get_template(template_name)
if template_content then
core.log("Template found: " .. template_name)
generate_template(template_name, project_title, template_content)
else
core.log("Error: template not found!")
end
end
-- Main
function ptm.add_template()
return function (t)
table.insert(templates, t)
end
end
local function parse_list()
local list = system.list_dir(USERDIR .. "/plugins/ptm/templates")
local list_matched = {}
local temp_string = ""
for k, v in pairs(list) do
temp_string = string.gsub(list[k], ".lua", "")
table.insert(list_matched, temp_string)
end
return list_matched
end
function ptm.load()
-- Get template filenames
local templates_list = parse_list()
-- Load template files
for _, v in ipairs(templates_list) do
require("plugins.ptm.templates." .. v)
end
end
local function project_template_manager()
-- Get input for template name
core.command_view:enter("Choose template", {
-- Submit the desired template name
submit = function(template_name)
-- Get input for project folder title
core.command_view:enter("Choose project title", {
submit = function(project_title)
-- Check if folder already exists
if system.get_file_info(core.project_dir .. "/" .. project_title) == nil then
-- Submit chosen template for selection
select_template(template_name, project_title)
else
core.log("WARNING: a folder with this title exists already!")
end
end
})
end,
-- Suggest template names
suggest = function(template_name)
local template_list = {}
-- Get list of template names
for k, v in pairs(templates) do
table.insert(template_list, v["name"])
end
-- Match current input with templates names
return common.fuzzy_match(template_list, template_name)
end
})
end
-- Commands
command.add(nil, {
["ptm:choose-template"] = function ()
project_template_manager()
end
})
-- Key bindings
keymap.add {
["alt+p"] = "ptm:choose-template"
}
return ptm