-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtechStackGeneric.nim
362 lines (318 loc) · 12.5 KB
/
techStackGeneric.nim
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
##
## Copyright (c) 2023-2024, Crash Override, Inc.
##
## This file is part of Chalk
## (see https://crashoverride.com/docs/chalk)
##
import std/[hashes, re, sequtils, sets, tables]
import ".."/[config, plugin_api, util]
const FT_ANY = "*"
var
languages = newTable[string, string]()
# rule identifiers by exact path
pthRules = newTable[string, HashSet[string]]()
# rules to exclude by exact path
excludePthRules = newTable[string, HashSet[string]]()
# rule identifiers by filetype
ftRules = newTable[string, HashSet[string]]()
# rules to be excluded from given filetypes
excludeFtRules = newTable[string, HashSet[string]]()
# tech stack rules by each identifier
tsRules = newTable[string, TechStackRule]()
# regex by name
regexes = newTable[string, Regex]()
# category: [subcategory: seq[regex_name]]
categories = newTable[string, TableRef[string, seq[string]]]()
# category: [subcategory: bool]
inFileScope = newTable[string, TableRef[string, bool]]()
# category: [subcategory: bool]
inHostScope = newTable[string, TableRef[string, bool]]()
# limits for what portion from the start of the file a rule must read into
headLimits = newTable[string, int]()
# key: rule, vals: filetypes for which this rules applies
ruleFiletypes = newTable[string, seq[string]]()
# key: rule, vals: filetypes for which this rules does not applies
ruleExcludeFiletypes = newTable[string, seq[string]]()
template scanFileStream(strm: FileStream) =
let
splFile = splitFile(filePath)
rule_names = categories[category][subcategory]
var applicable_rules: seq[string]
# applicable rules are eithr rules that apply to all filetypes (FT_ANY)
# or to the filetype matching the given extension
for rule_name in categories[category][subcategory]:
if filePath in excludePthRules and rule_name in excludePthRules[filePath]:
continue
# first check if the rule should always be added for this exact file path
if filePath in pthRules and rule_name in pthRules[filePath]:
applicable_rules.add(rule_name)
continue
# If we don't have a filepath based, match, go by file extension and check
# if the extension should be getting excluded or not.
# first check if the rule applies to all file types (FT_ANY). In this
# case we may only have excluded filetypes, so make a pass for those,
# otherwise add it
if rule_name notin tsRules:
continue
let tsRule = tsRules[rule_name]
if FT_ANY in ftRules and rule_name in ftRules[FT_ANY]:
# make a pass and check if we should exclude the rule
var exclude = false
if rule_name in ruleExcludeFiletypes:
for ft in ruleExcludeFiletypes[rule_name]:
# if the filetype does not match the current extension proceed
if ft != splFile.ext and ft != "":
continue
# if we have a matching extension and a rule for that extenion,
# append the rule in the rule to be run
if ft in excludeFtRules and rule_name in excludeFtRules[ft]:
exclude = true
break
# add the rule only if its explicitly added and not excluded
if not exclude:
applicable_rules.add(rule_name)
# done processing FT_ANY
continue
# if the rule does not apply to all filetypes, check the ones for which
# it actually does apply.
if rule_name in ruleFiletypes:
for ft in ruleFiletypes[rule_name]:
# if the filetype does not match the current extension proceed
if ft != splFile.ext and ft != "":
continue
# if we have a matching extension and a rule for that extenion,
# append the rule in the rule to be run
if ft in ftRules and rule_name in ftRules[ft]:
applicable_rules.add(rule_name)
break
var
line = ""
i = 0
abort = false
while strm.readLine(line):
i += 1
if inFileScope[category][subcategory] or abort:
break
for rule_name in applicable_rules:
if i >= headLimits[rule_name]:
abort = true
break
if find(line, regexes[rule_name]) != -1:
inFileScope[category][subcategory] = true
break
proc scanFile(filePath: string, category: string, subcategory: string) =
withFileStream(filePath, mode = fmRead, strict = true):
if stream == nil:
return
scanFileStream(stream)
proc getProcNames(): HashSet[string] =
result = initHashSet[string]()
for kind, path in walkDir("/proc/"):
for ch in path.splitPath().tail:
try:
if ch notin "0123456789":
continue
let p_path = path / "status"
var data = p_path.readFile()
for line in data.split("\n"):
if "Name:" in line:
var name = line.split("Name:")[1].strip()
result.incl(name)
except:
continue
# The current host based detection simply checks for the
# presence of configuration files, therefore we don't need
# to do more thatn check if the file paths exist. However we could
# expand with proper plugins per category looking for things like
# ps output etc in upcoming revisions
proc hostHasTechStack(scope: hostScope, proc_names: HashSet[string]): bool =
# first check directories and filepaths, then processes
let scopedDirs = scope.getDirectories()
var fExists = false
var dExists = false
if scopedDirs.isSome():
for path in scopedDirs.get():
if dirExists(path):
dExists = true
break
let filepaths = scope.getFilepaths()
if filepaths.isSome():
for path in filepaths.get():
if fileExists(path):
fExists = true
break
let names = scope.getProcessNames()
if names.isSome():
let
rule_names = toHashSet(names.get())
intersection = proc_names * rule_names
if len(intersection) > 0:
if scope.getStrict():
return fExists or dExists
return true
return false
# FIXME check that we don't fall into infinite loops with a symlink here
proc scanDirectory(directory: string, category: string, subcategory: string) =
if inFileScope[category][subcategory]:
return
for filePath in walkDir(directory):
if inFileScope[category][subcategory]:
break
if filePath.kind == pcFile:
scanFile(filePath.path, category, subcategory)
continue
if filePath.kind == pcDir:
scanDirectory(filePath.path, category, subcategory)
continue
proc getLanguages(directory: string, langs: var HashSet[string]) =
for filePath in walkDir(directory):
if filePath.kind == pcFile:
let splFile = splitFile(filePath.path)
if splFile.ext == "":
continue
if splFile.ext notin languages:
continue
langs.incl(languages[splFile.ext])
continue
if filePath.kind == pcDir:
getLanguages(filePath.path, langs)
continue
proc detectLanguages(): HashSet[string] =
result = initHashSet[string]()
let canLoad = chalkConfig.getUseTechStackDetection()
if not canLoad:
return result
for item in getContextDirectories():
let fpath = expandFilename(item)
if fpath.dirExists():
getLanguages(fpath, result)
else:
let (head, _) = splitPath(fPath)
if head.dirExists():
getLanguages(head, result)
proc detectTechCwd(): TableRef[string, seq[string]] =
result = newTable[string, seq[string]]()
var hasResults = false
for category, subcategories in categories:
for subcategory, _ in subcategories:
if not (category in inFileScope and subcategory in inFileScope[category]):
continue
# re-initialize to false again
# XXX check the diff between load time and invocation state
# does this need to be re-set upon every invocation here?
inFileScope[category][subcategory] = false
for item in getContextDirectories():
if inFileScope[category][subcategory]:
break
let fpath = expandFilename(item)
if fpath.dirExists():
scanDirectory(fpath, category, subcategory)
else:
let (head, _) = splitPath(fPath)
if head.dirExists():
scanDirectory(head, category, subcategory)
if inFileScope[category][subcategory]:
hasResults = true
if hasResults:
for category, subcategories in categories:
for subcategory, _ in subcategories:
if not (category in inFileScope and subcategory in inFileScope[category]):
continue
if inFileScope[category][subcategory]:
result.mgetOrPut(category, @[]).add(subcategory)
proc loadState() =
once:
for langName, val in chalkConfig.linguistLanguages:
languages[val.getExtension()] = langName
for key, val in chalkConfig.techStackRules:
let
category = val.getCategory()
subcategory = val.getSubcategory()
categories.
mgetOrPut(category, newTable[string, seq[string]]()).
mgetOrPut(subcategory, @[]).
add(key)
if val.hostScope != nil:
if category notin inHostScope:
inHostScope[category] = newTable[string, bool]()
inHostScope[category][subcategory] = false
else:
if val.fileScope == nil:
error("One of file_scope, host_scope must be defined for rule " & key & ". Skipping")
continue
if category notin inFileScope:
inFileScope[category] = newTable[string, bool]()
inFileScope[category][subcategory] = false
tsRules[key] = val
regexes[key] = re(val.fileScope.getRegex())
headLimits[key] = val.fileScope.getHead()
let filetypes = val.fileScope.getFiletypes()
if filetypes.isSome():
let ftypes = filetypes.get()
ruleFiletypes[key] = ftypes
for ft in ftypes:
ftRules.mgetOrPut(ft, initHashSet[string]()).incl(key)
else:
# we only have exclude rules therefore we match by default
# XXX move to a template for looking things up and adding if
# they don't exist
ftRules.mgetOrPut(FT_ANY, initHashSet[string]()).incl(key)
let excludeFiletypes = val.fileScope.getExcludedFiletypes()
if excludeFiletypes.isSome():
let exclFtps = excludeFiletypes.get()
ruleExcludeFiletypes[key] = exclFtps
for ft in exclFtps:
excludeFtRules.mgetOrPut(ft, initHashSet[string]()).incl(key)
# get paths and excluded paths that need to always be considered
let filepaths = val.fileScope.getFilepaths()
if filepaths.isSome():
let fpaths = filepaths.get()
for path in fpaths:
pthRules.mgetOrPut(path, initHashSet[string]()).incl(key)
let excludeFilepaths = val.fileScope.getExcludedFilepaths()
if excludeFilepaths.isSome():
let excfpaths = excludeFilepaths.get()
for path in excfpaths:
excludePthRules.mgetOrPut(path, initHashSet[string]()).incl(key)
proc techStackRuntime*(self: Plugin, objs: seq[ChalkObj]): ChalkDict {.cdecl.} =
result = ChalkDict()
let canLoad = chalkConfig.getUseTechStackDetection()
if not canLoad:
trace("Skipping tech stack runtime detection plugin")
return result
loadState()
let procNames = getProcNames()
var finalHost = newTable[string, seq[string]]()
for key, val in chalkConfig.techStackRules:
if val.hostScope == nil:
continue
let
category = val.getCategory()
subcategory = val.getSubcategory()
if (category in inHostScope and
subcategory in inHostScope[category] and
not inHostScope[category][subcategory]):
let isTechStack = hostHasTechStack(val.hostScope, procNames)
inHostScope[category][subcategory] = isTechStack
if isTechStack:
finalHost.mgetOrPut(category, @[]).add(subcategory)
if len(finalHost) > 0:
result["_INFERRED_TECH_STACKS_HOST"] = pack[TableRef[string, seq[string]]](finalHost)
proc techStackArtifact*(self: Plugin, objs: ChalkObj): ChalkDict {.cdecl.} =
result = ChalkDict()
let canLoad = chalkConfig.getUseTechStackDetection()
if not canLoad:
trace("Skipping tech stack detection plugin for artifacts")
return result
loadState()
let
final = detectTechCwd()
langs = detectLanguages()
if len(langs) > 0:
final["language"] = toSeq(langs)
if len(final) > 0:
result["INFERRED_TECH_STACKS"] = pack[TableRef[string, seq[string]]](final)
proc loadtechStackGeneric*() =
newPlugin("tech_stack_generic",
ctArtCallback = ChalkTimeArtifactCb(techStackArtifact),
rtHostCallback = RunTimeHostCb(techStackRuntime))