forked from sagioto/maven2gradle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.js
65 lines (62 loc) · 2.24 KB
/
converter.js
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
/**
* Created by sbernstein on 4/8/15.
*/
function parseAndGenerate(){
var text = document.getElementById('input').value;
var parser = new DOMParser();
var parsedDeps = parser.parseFromString(text, 'text/xml');
var shouldAddOuterClosure = parsedDeps.getElementsByTagName('dependencies').length;
if(!shouldAddOuterClosure){
text = '<root>' + text + '</root>';
parsedDeps = parser.parseFromString(text, 'text/xml');
}
var depElems = parsedDeps.getElementsByTagName('dependency');
var grDeps = [];
for(var i = 0; i < depElems.length; i++) {
var depElem = depElems[i];
var scopeElems = depElem.getElementsByTagName('scope');
var scope = 'implementation';
if (scopeElems.length && scopeElems[0].innerHTML == 'runtime') {
scope = 'runtimeOnly';
} else if (scopeElems.length && scopeElems[0].innerHTML == 'provided') {
scope = 'compileOnly';
} else if (scopeElems.length && scopeElems[0].innerHTML == 'test') {
scope = 'testImplementation';
}
var group = depElem.getElementsByTagName('groupId')[0].innerHTML;
var artifact = depElem.getElementsByTagName('artifactId')[0].innerHTML;
var versionElems = depElem.getElementsByTagName('version');
var version = '*';
if (versionElems.length) {
version = versionElems[0].innerHTML;
}
grDeps.push(scope + '(' + '"' + group + ":" + artifact + ":" + version + '")');
}
var grDepsOutput = grDeps.join('\n');
if(shouldAddOuterClosure){
grDepsOutput = 'dependencies {\n\t' + grDepsOutput.replace(/\n/g, '\n\t') + '\n}'
}
return grDepsOutput;
}
function convert(){
var ok = document.getElementById('success');
var empty = document.getElementById('empty');
var fail = document.getElementById('failed');
ok.hidden = true;
empty.hidden = true;
fail.hidden = true;
var output = document.getElementById('output');
try {
output.value = parseAndGenerate();
if(output.value){
ok.hidden = false;
output.select();
} else {
empty.hidden = false;
}
}
catch (err){
output.value = '';
fail.hidden = false;
}
}