1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const microgrammar_1 = require("@atomist/microgrammar");
|
12 | const logger_1 = require("../../util/logger");
|
13 | const projectUtils_1 = require("./projectUtils");
|
14 | exports.DefaultOpts = {
|
15 | makeUpdatable: true,
|
16 | };
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | function findMatches(p, globPatterns, microgrammar, opts = exports.DefaultOpts) {
|
25 | return findFileMatches(p, globPatterns, microgrammar, opts)
|
26 | .then(fileHits => {
|
27 | let matches = [];
|
28 | fileHits.forEach(fh => matches = matches.concat(fh.matches));
|
29 | return matches;
|
30 | });
|
31 | }
|
32 | exports.findMatches = findMatches;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | function findFileMatches(p, globPatterns, microgrammar, opts = exports.DefaultOpts) {
|
41 | return projectUtils_1.gatherFromFiles(p, globPatterns, file => {
|
42 | return file.getContent()
|
43 | .then((content) => __awaiter(this, void 0, void 0, function* () {
|
44 | const matches = yield microgrammar.findMatchesAsync(transformIfNecessary(content, opts));
|
45 | if (matches.length > 0) {
|
46 | logger_1.logger.debug(`${matches.length} matches in '${file.path}'`);
|
47 | return new UpdatingFileHits(p, file, matches, content);
|
48 | }
|
49 | else {
|
50 | logger_1.logger.debug(`No matches in '${file.path}'`);
|
51 | return undefined;
|
52 | }
|
53 | }));
|
54 | });
|
55 | }
|
56 | exports.findFileMatches = findFileMatches;
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | function doWithFileMatches(p, globPatterns, microgrammar, action, opts = exports.DefaultOpts) {
|
66 | return projectUtils_1.doWithFiles(p, globPatterns, file => {
|
67 | return file.getContent()
|
68 | .then((content) => __awaiter(this, void 0, void 0, function* () {
|
69 | const matches = yield microgrammar.findMatchesAsync(transformIfNecessary(content, opts));
|
70 | if (matches && matches.length > 0) {
|
71 | logger_1.logger.debug(`${matches.length} matches in '${file.path}'`);
|
72 | const fh = new UpdatingFileHits(p, file, matches, content);
|
73 | if (opts.makeUpdatable === true) {
|
74 | fh.makeUpdatable();
|
75 | }
|
76 | action(fh);
|
77 | }
|
78 | else {
|
79 | logger_1.logger.debug(`No matches in '${file.path}'`);
|
80 | return undefined;
|
81 | }
|
82 | }));
|
83 | });
|
84 | }
|
85 | exports.doWithFileMatches = doWithFileMatches;
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | function doWithMatches(p, globPatterns, microgrammar, action, opts = exports.DefaultOpts) {
|
96 | const fileAction = (fh) => {
|
97 | fh.matches.forEach(action);
|
98 | };
|
99 | return doWithFileMatches(p, globPatterns, microgrammar, fileAction, opts);
|
100 | }
|
101 | exports.doWithMatches = doWithMatches;
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 | function doWithUniqueMatch(p, globPatterns, microgrammar, action, opts = exports.DefaultOpts) {
|
112 | let count = 0;
|
113 | const guardedAction = (fh) => {
|
114 | if (fh.matches.length !== 1) {
|
115 | throw new Error(`Expected 1 match, not ${fh.matches.length}`);
|
116 | }
|
117 | if (count++ !== 0) {
|
118 | throw new Error("More than one match found in project");
|
119 | }
|
120 | const m0 = fh.matches[0];
|
121 | action(m0);
|
122 | };
|
123 | return doWithFileMatches(p, globPatterns, microgrammar, guardedAction, opts)
|
124 | .then(files => {
|
125 | if (count++ === 0) {
|
126 | throw new Error("No unique match found in project");
|
127 | }
|
128 | return files;
|
129 | });
|
130 | }
|
131 | exports.doWithUniqueMatch = doWithUniqueMatch;
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 | function doWithAtMostOneMatch(p, globPatterns, microgrammar, action, opts = exports.DefaultOpts) {
|
141 | let count = 0;
|
142 | const guardedAction = (fh) => {
|
143 | if (fh.matches.length !== 1) {
|
144 | throw new Error(`Expected at most 1 match, not ${fh.matches.length}`);
|
145 | }
|
146 | if (count++ !== 0) {
|
147 | throw new Error("More than one match found in project");
|
148 | }
|
149 | const m0 = fh.matches[0];
|
150 | action(m0);
|
151 | };
|
152 | return doWithFileMatches(p, globPatterns, microgrammar, guardedAction, opts);
|
153 | }
|
154 | exports.doWithAtMostOneMatch = doWithAtMostOneMatch;
|
155 |
|
156 |
|
157 |
|
158 | class UpdatingFileHits {
|
159 | constructor(project, file, matches, content) {
|
160 | this.project = project;
|
161 | this.file = file;
|
162 | this.matches = matches;
|
163 | this.content = content;
|
164 | this.updatable = false;
|
165 | }
|
166 | makeUpdatable() {
|
167 | if (!this.updatable) {
|
168 | const um = microgrammar_1.Microgrammar.updatable(this.matches, this.content);
|
169 |
|
170 | this.matches = um.matches;
|
171 | this.file.recordAction(f => {
|
172 | return f.getContent().then(content => {
|
173 | if (content !== um.updated()) {
|
174 | return f.setContent(um.updated());
|
175 | }
|
176 | return f;
|
177 | });
|
178 | });
|
179 |
|
180 | this.project.recordAction(p => this.file.flush());
|
181 | this.updatable = true;
|
182 | }
|
183 | }
|
184 | }
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | function transformIfNecessary(rawContent, opts) {
|
192 | return !!opts && !!opts.contentTransformer ?
|
193 | opts.contentTransformer(rawContent) :
|
194 | rawContent;
|
195 | }
|
196 |
|
\ | No newline at end of file |