-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathSearch.ts
More file actions
217 lines (187 loc) Β· 7.3 KB
/
Search.ts
File metadata and controls
217 lines (187 loc) Β· 7.3 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
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
import * as path from 'path';
import { GetMemberInfo } from './components/getMemberInfo';
import { IBMiMember, SearchHit, SearchResults } from './types';
import { Tools } from './Tools';
import IBMi from './IBMi';
export namespace Search {
export async function searchMembers(connection: IBMi, library: string, sourceFile: string, searchTerm: string, members: string|IBMiMember[], readOnly?: boolean,): Promise<SearchResults> {
const config = connection.getConfig();
const content = connection.getContent();
const infoComponent = connection.getComponent<GetMemberInfo>(GetMemberInfo.ID);
if (connection && config && content) {
let detailedMembers: IBMiMember[]|undefined;
let memberFilter: string|undefined;
if (typeof members === `string`) {
memberFilter = connection.sysNameInAmerican(`${members}.MBR`);
} else
if (Array.isArray(members)) {
if (members.length > connection.maximumArgsLength) {
detailedMembers = members;
memberFilter = "*.MBR";
} else {
memberFilter = members.map(member => `${member.name}.MBR`).join(` `);
}
}
// First, let's fetch the ASP info
const asp = await connection.lookupLibraryIAsp(library);
// Then search the members
const result = await connection.sendQsh({
command: `/usr/bin/grep -inHR -F "${sanitizeSearchTerm(searchTerm)}" ${memberFilter}`,
directory: connection.sysNameInAmerican(`${asp ? `/${asp}` : ``}/QSYS.LIB/${library}.LIB/${sourceFile}.FILE`)
});
if (!result.stderr) {
let hits = parseGrepOutput(
result.stdout || '', readOnly || content.isProtectedPath(library),
path => connection.sysNameInLocal(`${library}/${sourceFile}/${path.replace(/\.MBR$/, '')}`)
);
if (detailedMembers) {
// If the user provided a list of members, we need to filter the results to only include those members
hits = hits.filter(hit => {
const { name, dir } = path.parse(hit.path);
const [lib, spf] = dir.split(`/`);
return detailedMembers!.some(member => member.name === name && member.library === lib && member.file === spf);
});
}
// We need to fetch the member info for each hit so we can display the correct extension and info.
const memberInfos: IBMiMember[] = hits.map(hit => {
const { name, dir } = path.parse(hit.path);
const [library, file] = dir.split(`/`);
return {
name,
library,
file,
extension: ``
};
});
detailedMembers = await infoComponent?.getMultipleMemberInfo(connection, memberInfos);
// Add extension and member info to the hit.
for (const hit of hits) {
const { name, dir } = path.parse(hit.path);
const [lib, spf] = dir.split(`/`);
const foundMember = detailedMembers?.find(member => member.name === name && member.library === lib && member.file === spf);
if (foundMember) {
hit.path = connection.sysNameInLocal(`${asp ? `${asp}/` : ``}${lib}/${spf}/${name}.${foundMember.extension}`);
hit.member = foundMember;
}
}
return {
term: searchTerm,
hits
}
}
else {
throw new Error(result.stderr);
}
}
else {
throw new Error("Please connect to an IBM i");
}
}
export async function searchIFS(connection: IBMi, path: string, searchTerm: string): Promise<SearchResults | undefined> {
if (connection) {
const grep = connection.remoteFeatures.grep;
if (grep) {
const dirsToIgnore = IBMi.connectionManager.get<string[]>(`grepIgnoreDirs`) || [];
let ignoreString = ``;
if (dirsToIgnore.length > 0) {
ignoreString = dirsToIgnore.map(dir => `--exclude-dir=${dir}`).join(` `);
}
const grepRes = await connection.sendCommand({
command: `${grep} -inr -F -f - ${ignoreString} ${Tools.escapePath(path)}`,
stdin: searchTerm
});
if (grepRes.code == 0) {
const hits = parseGrepOutput(grepRes.stdout);
for (var i = 0; i < hits.length; i++) {
hits[i].file = await connection.content.getFileInfo(hits[i].path)
};
return {
term: searchTerm,
hits: hits
}
}
} else {
throw new Error(`Grep must be installed on the remote system.`);
}
}
else {
throw new Error("Please connect to an IBM i");
}
}
export async function findIFS(connection: IBMi, path: string, findTerm: string): Promise<SearchResults | undefined> {
if (connection) {
const find = connection.remoteFeatures.find;
if (find) {
const dirsToIgnore = IBMi.connectionManager.get<string[]>(`grepIgnoreDirs`) || [];
let ignoreString = ``;
if (dirsToIgnore.length > 0) {
ignoreString = dirsToIgnore.map(dir => `-type d -path '*/${dir}' -prune -o`).join(` `);
}
const findRes = await connection.sendCommand({
command: `${find} ${Tools.escapePath(path)} ${ignoreString} -type f -iname '*${findTerm}*' -print`
});
if (findRes.code == 0 && findRes.stdout) {
const hits = parseFindOutput(findRes.stdout);
for (var i = 0; i < hits.length; i++) {
hits[i].file = await connection.content.getFileInfo(hits[i].path)
};
return {
term: findTerm,
hits: hits
}
}
} else {
throw new Error(`Find must be installed on the remote system.`);
}
}
else {
throw new Error("Please connect to an IBM i");
}
}
function parseFindOutput(output: string, readonly?: boolean, pathTransformer?: (path: string) => string): SearchHit[] {
const results: SearchHit[] = [];
for (const line of output.split('\n')) {
const path = pathTransformer?.(line) || line;
results.push(results.find(r => r.path === path) || { path, readonly, lines: [] });
}
return results;
}
function parseGrepOutput(output: string, readonly?: boolean, pathTransformer?: (path: string) => string): SearchHit[] {
const results: SearchHit[] = [];
for (const line of output.split('\n')) {
if (line && !line.startsWith(`Binary`)) {
const parts = line.split(`:`); //path:line
const path = pathTransformer?.(parts[0]) || parts[0];
let result = results.find(r => r.path === path);
if (!result) {
result = {
path,
lines: [],
readonly,
};
results.push(result);
}
const contentIndex = nthIndex(line, `:`, 2);
if (contentIndex >= 0) {
const curContent = line.substring(contentIndex + 1);
result.lines.push({
number: Number(parts[1]),
content: curContent
})
}
}
}
return results;
}
function sanitizeSearchTerm(searchTerm: string): string {
return searchTerm.replace(/\\/g, `\\\\`).replace(/"/g, `\\"`);
}
function nthIndex(aString: string, pattern: string, n: number) {
let index = -1;
while (n-- && index++ < aString.length) {
index = aString.indexOf(pattern, index);
if (index < 0) break;
}
return index;
}
}