-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjh.js
More file actions
executable file
·308 lines (258 loc) · 10.3 KB
/
jh.js
File metadata and controls
executable file
·308 lines (258 loc) · 10.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
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
#!/usr/bin/env node
/*
jar-hunt, version 1.0.3
jh.js will generate <dependency> XML fragments for a directory of jar-files to be used in Maven pom.xml file.
This XML fragment is created by recursively searching the specified directory for jar-files,
generating a SHA-1 hash value for each encountered jar-file which is then used to search the Maven Central Repository.
Useful for assessing name and version of directories of vaguely named jar files.
usage:
node jh.js [-s -x $filename -e $filename] $directory_that_contains_jars
-e $filename to write error output, default is, "error.xml"
-r recursively search for jar-files
-s suppress "found" messages
-x $filename to write xml output, default is, "dependency.xml"
Thanks to Sonatype, Inc. for hosting central repository jar and look-up service (search.maven.org)
This program is not endorsed, owned by nor affiliated with Sonatype, Inc.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
Copyright 2012 Matthew David Huckaby
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
*/
var jarhunt_context = {
"configInstance": function () {
return {
"console": console,
"emitter": null,
"filenames": {
"dependency_xml": "dependency.xml",
"error_xml": "error.xml",
"this_script": __filename
},
"http_options": {
"host": "search.maven.org",
"port": 80
},
"logger": {
"error": null,
"info": null
},
"msgs": {
"found_jar": "found \t: %s\nhash \t: %s",
"help_text": ['jar-hunt, v.1.0.3',
'usage:',
'\t[node] jh.js [-e {filename} -r -s -x {filename}] {directory_that_contains_jars}\n',
'\t-e ${filename} to write error output, default is, "error.xml"',
'\t-r recursively search for jar-files',
'\t-s suppress "found" messages',
'\t-x ${filename} to write xml output, default is, "dependency.xml"'],
"validation_not_directory": "argument, \"%s\" is not a directory:",
"validation_dir_ne": "directory, \"%s\" does not exist."
},
"process": process,
"recursive": false,
"requires": {
"crypto": require('crypto'),
"events": require('events'),
"fs": require('fs'),
"http": require('http'),
"path": require('path'),
"util": require('util'),
"process": process
},
"url": {
"param_template": ":\"%s\"",
"template": "/solrsearch/select?q=1%s&rows=20&wt=json"
},
"xml": {
"error_template": "<error\nfile=\"%s\"\nurl=\"%s%s\" />\n",
"template":
"<dependency>\n" +
"\t<groupId>%s</groupId>\n" +
"\t<artifactId>%s</artifactId>\n" +
"\t<version>%s</version>\n" +
"</dependency>\n"
}
};
},
"jarHuntInstance": function (config) {
config = config || this.configInstance();
var initialize = {
"handle_cmd_line_arguments": function() {
var find_next_arg = function(i) {
return config.process.argv.length-1 > i ? config.process.argv[i+1] : null;
};
var unlinkCallback = function() {}
// cmd line arguments
for(var i=0;i<config.process.argv.length;i++) {
var arg = config.process.argv[i];
var next_arg = find_next_arg(i);
if('-e' === arg && next_arg) {
// set error log filename
config.requires.fs.unlink(next_arg, unlinkCallback);
config.logger.error =
config.requires.fs.createWriteStream(next_arg, {"flags": "w"});
}else if('-r' === arg) {
config.recursive = true;
}else if('-s' === arg) {
// suppress 'found' output
config.msgs.found_jar = null;
}else if('-x' === arg && next_arg) {
// set dependency log filename
config.requires.fs.unlink(next_arg, unlinkCallback);
config.logger.info =
config.requires.fs.createWriteStream(next_arg, {"flags": "w"});
}
}
config.emitter = new config.requires.events.EventEmitter();
return this;
},
"register_events": function(emitter) {
emitter.on('execute', function(dir, config) {
dir = dir ? dir : config.process.argv[config.process.argv.length-1];
config.requires.fs.readdir(dir, function(err, filenames) {
if(filenames) {
filenames.forEach(function(filename) {
var qualified_filename = (dir ? (dir + '/') : '') + filename;
config.emitter.emit('filter', qualified_filename, config);
});
}else{
config.console.log('no files found : ' + dir);
}
})
});
emitter.on('filter', function(filename, config) {
config.requires.fs.stat(filename, function(error, stats) {
if(config.recursive && stats.isDirectory()) {
config.emitter.emit('execute', filename, config);
}else{
if(filename.match(/\.jar$/)) {
config.emitter.emit('queue', filename, config);
}
}
})
});
var queue;
emitter.on('queue', function(filename, config) {
if(queue) {
queue.push(filename);
}else{
// first
queue = [];
emitter.emit('generate_hash_asynch', filename, config, function() {
emitter.emit('chew-queue', config);
})
}
});
emitter.on('chew-queue', function(config) {
if(queue.length) {
emitter.emit('generate_hash_asynch', queue.pop(), config, function() {
emitter.emit('chew-queue', config);
})
}
});
emitter.on('generate_hash_asynch', function(filename, config, post_read_callback) {
config.requires.fs.readFile(filename, function(err, data) {
post_read_callback(); // emfile error is avoided
var hash = config.requires.crypto.createHash('sha1').update(data).digest('hex');
if(config.msgs.found_jar) {
config.console.log(config.requires.util.format(config.msgs.found_jar, filename, hash));
}
config.emitter.emit('generate_url', filename, hash, config);
})
});
emitter.on('generate_url', function(filename, hash, config) {
var param = encodeURIComponent(config.requires.util.format(config.url.param_template, hash));
config.emitter.emit('search', filename, config.requires.util.format(config.url.template, param), config);
});
emitter.on('search', function(filename, path, config) {
var options = {
"host": config.http_options.host,
"port": config.http_options.port,
"path": path
};
config.requires.http.get(options, function(res) {
res.on('data', function(chunk) {
try{
var obj = JSON.parse(chunk);
var value =
config.requires.util.format(
config.xml.template,
obj.response.docs[0].g,
obj.response.docs[0].a,
obj.response.docs[0].v
);
config.emitter.emit('write_dependency_xml', value, config);
}catch(e) {
var error_message =
config.requires.util.format(
config.xml.error_template,
filename,
options.host,
options.path
);
config.emitter.emit('write_error_xml', error_message, config);
}
})
}).on('error', function(error) {
config.console.log(error);
config.process.exit();
})
});
emitter.on('write_dependency_xml', function(xml, config) {
if(!config.logger.info)
config.logger.info = config.requires.fs.createWriteStream(config.filenames.dependency_xml, {'flags': 'w'});
config.logger.info.write(xml);
});
emitter.on('write_error_xml', function(error, config) {
if(!config.logger.error)
config.logger.error = config.requires.fs.createWriteStream(config.filenames.error_xml, {'flags': 'w'});
config.logger.error.write(error);
});
return this
},
"validate_args": function() {
var last_arg = config.process.argv[config.process.argv.length-1];
// validate parameter count
if(last_arg === config.filenames.this_script) {
config.msgs.help_text.forEach(function(text) {
config.console.log(text);
});
config.process.exit();
}
// validate directory was supplied and that it exists
if(config.requires.fs.existsSync(last_arg)) {
var stat = config.requires.fs.statSync(last_arg);
if(!stat.isDirectory()) {
config.console.log(config.requires.util.format(config.msgs.validation_not_directory, last_arg));
config.requires.process.exit();
}
}else{
config.console.log(config.requires.util.format(config.msgs.validation_dir_ne, last_arg));
config.process.exit();
}
return this;
}
};
return {
"execute": function() {
initialize
.validate_args()
.handle_cmd_line_arguments()
.register_events(config.emitter);
config.emitter.emit('execute', null, config);
}
};
}
};
(function() {
jarhunt_context
.jarHuntInstance(jarhunt_context.configInstance()).execute();
})();