-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnvim-circleci.lua
More file actions
100 lines (86 loc) · 2.77 KB
/
nvim-circleci.lua
File metadata and controls
100 lines (86 loc) · 2.77 KB
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
-- main module file
local module = require("nvim-circleci.module")
local config = require("nvim-circleci.config")
local M = {}
local providerMap = {
["git@github.com"] = "gh",
-- Untested
["git@gitlab.com"] = "gl",
["git@bitbucket.org"] = "bb",
}
local function getTopLevelOfRepo()
local handle = io.popen("git rev-parse --show-toplevel")
local repoRoot = handle:read("*a")
handle:close()
repoRoot = string.gsub(repoRoot, "\n", "")
return repoRoot
end
local function checkForCircleCIConfig(root)
local circleDirectory = ".circleci"
local file = io.open(circleDirectory, "r")
return file ~= nil and io.close(file)
end
local function getRemoteOriginUrl(repoRoot)
local configFilePath = repoRoot .. "/.git/config"
local file = io.open(configFilePath, "r")
if not file then
local parentPath = string.match(repoRoot, "(.*/)")
local parentConfigPath = parentPath .. "config"
file = io.open(parentConfigPath) -- git worktrees setup
if not file then
return nil
end
end
local url = nil
for line in file:lines() do
if string.match(line, "^%s*url%s*=") then
url = string.match(line, "= (.*)")
break
end
end
file:close()
return url
end
local function formatRemoteOriginToProjectSlug(remoteOrigin)
local firstPart = string.match(remoteOrigin, "^[^:]*")
local providerPrefix = providerMap[firstPart]
if providerPrefix then
local formatted = string.gsub(remoteOrigin, firstPart, providerPrefix)
formatted = string.gsub(formatted, "%.git$", "")
formatted = string.gsub(formatted, ":", "/")
return formatted
else
return remoteOrigin
end
end
-- setup is the public method to setup your plugin
M.setup = function(args)
-- you can define your setup function here. Usually configurations can be merged, accepting outside params and
-- you can also put some validation here for those.
config.mergeConfig(config.config, args)
-- Usage
local repoRoot = getTopLevelOfRepo()
if checkForCircleCIConfig(repoRoot) then
local remoteOriginUrl = getRemoteOriginUrl(repoRoot)
if remoteOriginUrl then
local projectSlug = formatRemoteOriginToProjectSlug(remoteOriginUrl)
if projectSlug then
require("telescope").load_extension("circleci")
config.config.project_slug = projectSlug
end
else
print("Error opening git config file")
end
local lspConfig = args.lsp or { enable = false }
if lspConfig.enable then
require("nvim-circleci.lsp").start("CircleCI Language Server", lspConfig.config, repoRoot)
if lspConfig.config.enable_yaml then
require("nvim-circleci.lsp").start("Yaml Language Server", lspConfig.config, repoRoot)
end
end
end
end
--[[ M.getMyPipelines = function()
module.my_first_function()
end ]]
return M