Skip to content

Commit 65fce82

Browse files
authored
✨ add automated link scripts to add the Instabug maven repo to the pr… (#55)
* ✨ add automated link scripts to add the Instabug maven repo to the project level build.gradle * 📝 change maven repo url to our instabug repo
1 parent 357ac64 commit 65fce82

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

link_bridge.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
const exec = require('child_process').exec;
2+
require('./link_gradle');
23
exec("ruby ./node_modules/instabug-reactnative/link.rb || echo \"Ruby doesn't exist, if you're building this for Android only, then feel free to ignore this error, otherwise please install Ruby and run 'react-native link instabug-reactnative' again\"");

link_gradle.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
'use strict';
2+
var fs = require('fs');
3+
4+
const LOG_LEVEL_SUCCESS = 0;
5+
const LOG_LEVEL_WARN = 1;
6+
7+
const CHAR_OPEN_PARAN = '{';
8+
const CHAR_CLOSED_PARAN = '}';
9+
10+
const GRADLE_FILE_PATH = 'android/build.gradle';
11+
const MAVEN_REPO_URL =
12+
'https://sdks.instabug.com/nexus/repository/instabug-cp';
13+
14+
function getPosition(string, substring, occurrenceInString) {
15+
return string.split(substring, occurrenceInString).join(substring).length;
16+
}
17+
18+
function findRepositoriesBlockEnd(block) {
19+
let repositoriesStartBlockIndex = getPosition(block, CHAR_OPEN_PARAN, 2);
20+
let count = 1;
21+
let blockEndIndex = -1;
22+
for (let i = repositoriesStartBlockIndex + 1; i < block.length; i++) {
23+
if (block.charAt(i) === CHAR_OPEN_PARAN) {
24+
count++;
25+
}
26+
27+
if (block.charAt(i) === CHAR_CLOSED_PARAN) {
28+
count--;
29+
}
30+
31+
if (count === 0) {
32+
blockEndIndex = i;
33+
break;
34+
}
35+
}
36+
37+
return blockEndIndex;
38+
}
39+
40+
function readFile(filePath, success) {
41+
fs.readFile(filePath, 'utf-8', function(err, data) {
42+
if (err) {
43+
console.log(process.cwd());
44+
finish(
45+
LOG_LEVEL_WARN,
46+
`Linking process could not be completed because of\n${err.message}`
47+
);
48+
}
49+
success(data);
50+
});
51+
}
52+
53+
function writeFile(data) {
54+
fs.writeFile(GRADLE_FILE_PATH, data, err => {
55+
if (err) {
56+
finish(
57+
LOG_LEVEL_WARN,
58+
`Linking process could not be completed because of\n${err.message}`
59+
);
60+
}
61+
finish(LOG_LEVEL_SUCCESS, 'Linking process completed successfully');
62+
});
63+
}
64+
65+
function finish(logLevel, message) {
66+
if (logLevel === LOG_LEVEL_SUCCESS) {
67+
console.info(message);
68+
} else {
69+
console.warn(message);
70+
}
71+
72+
process.exit(0);
73+
}
74+
75+
function generateNewGradleFile(data) {
76+
77+
if (data.includes(MAVEN_REPO_URL)) {
78+
finish(LOG_LEVEL_SUCCESS, '');
79+
}
80+
81+
const regex = /allprojects\ *\n*\ *{/;
82+
if (!regex.test(data)) {
83+
finish(
84+
LOG_LEVEL_WARN,
85+
'Something went wrong while trying to complete the linking process. '
86+
);
87+
}
88+
89+
90+
const matchedRegex = data.match(regex);
91+
const block = data.substring(matchedRegex.index, data.length);
92+
const blockEndIndex = findRepositoriesBlockEnd(block);
93+
94+
if (blockEndIndex === -1) {
95+
finish(
96+
LOG_LEVEL_WARN,
97+
'Something went wrong while trying to complete the linking process. '
98+
);
99+
}
100+
101+
let updatedBlock = `${block.substring(0, blockEndIndex)}\tmaven {
102+
\t url "${MAVEN_REPO_URL}"
103+
\t}
104+
${block.substring(blockEndIndex, block.length)}`;
105+
const newGradleFile = `${data.substring(
106+
0,
107+
matchedRegex.index
108+
)}${updatedBlock}`;
109+
return newGradleFile;
110+
}
111+
112+
113+
readFile(GRADLE_FILE_PATH, function(data) {
114+
const newFile = generateNewGradleFile(data)
115+
writeFile(newFile);
116+
});

unlink_bridge.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
const exec = require('child_process').exec;
2+
require('./unlink_gradle');
23
exec("ruby ./node_modules/instabug-reactnative/unlink.rb || echo \"Ruby doesn't exist, if you're building this for Android only, then feel free to ignore this error, otherwise please install Ruby and run 'react-native link instabug-reactnative' again\"");

unlink_gradle.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
'use strict';
2+
var fs = require('fs');
3+
4+
const LOG_LEVEL_SUCCESS = 0;
5+
const LOG_LEVEL_WARN = 1;
6+
7+
const CHAR_OPEN_PARAN = '{';
8+
const CHAR_CLOSED_PARAN = '}';
9+
10+
const GRADLE_FILE_PATH = 'android/build.gradle';
11+
const MAVEN_REPO_URL =
12+
'https://sdks.instabug.com/nexus/repository/instabug-cp';
13+
14+
function readFile(filePath, success) {
15+
fs.readFile(filePath, 'utf-8', function(err, data) {
16+
if (err) {
17+
finish(
18+
LOG_LEVEL_WARN,
19+
`Linking process could not be completed because of\n${err.message}`
20+
);
21+
}
22+
success(data);
23+
});
24+
}
25+
26+
function findClosureStart(data, index) {
27+
const alphRegex = /[a-z]/;
28+
let maven = '';
29+
let foundOpenParan = false;
30+
let closureStart = -1;
31+
for (let i = index; i >= 0; i--) {
32+
if (!foundOpenParan) {
33+
foundOpenParan = data.charAt(i) === CHAR_OPEN_PARAN;
34+
}
35+
36+
if (alphRegex.test(data.charAt(i)) && foundOpenParan) {
37+
maven = data.charAt(i) + maven;
38+
}
39+
if (maven === 'maven') {
40+
closureStart = i;
41+
break;
42+
}
43+
}
44+
return closureStart;
45+
}
46+
47+
function findClosureEnd(data, index) {
48+
const startIndex = index + MAVEN_REPO_URL.length + 2;
49+
let closureEnd = -1;
50+
//after
51+
for (let i = startIndex; i < data.length; i++) {
52+
if (data.charAt(i) === CHAR_CLOSED_PARAN) {
53+
closureEnd = i;
54+
break;
55+
}
56+
}
57+
return closureEnd;
58+
}
59+
60+
function removeMavenRepo(data) {
61+
const regex = /\"https:\/\/oss.sonatype.org\/content\/repositories\/snapshots\"/;
62+
if (!regex.test(data)) {
63+
finish(LOG_LEVEL_SUCCESS, 'Already Unlinked');
64+
}
65+
const start = findClosureStart(data, data.match(regex).index);
66+
const end = findClosureEnd(data, data.match(regex).index);
67+
let newGradle =
68+
data.substring(0, start) + data.substring(end + 1, data.length);
69+
return newGradle;
70+
}
71+
72+
function writeFile(data) {
73+
fs.writeFile(GRADLE_FILE_PATH, data, err => {
74+
if (err) {
75+
finish(
76+
LOG_LEVEL_WARN,
77+
`Unlinking process could not be completed because of\n${err.message}`
78+
);
79+
}
80+
finish(LOG_LEVEL_SUCCESS, 'Unlinking process completed successfully');
81+
});
82+
}
83+
84+
function finish(logLevel, message) {
85+
if (logLevel === LOG_LEVEL_SUCCESS) {
86+
console.info(message);
87+
} else {
88+
console.warn(message);
89+
}
90+
91+
process.exit(0);
92+
}
93+
94+
readFile(GRADLE_FILE_PATH, function(data) {
95+
const newGradle = removeMavenRepo(data);
96+
writeFile(newGradle);
97+
});

0 commit comments

Comments
 (0)