Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
feat: Parallel Jetifier, scale up to cpu count (#24)
Browse files Browse the repository at this point in the history
* feat: parallel files jetifying
* fix: typo
* removed unnecessary console.logs
  • Loading branch information
Yassine Fathi authored and mikehardy committed Jun 30, 2019
1 parent 1708d9c commit 0a16472
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 28 deletions.
26 changes: 12 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
const { readFileSync, writeFileSync } = require('fs');
const { loadCSV, readDir } = require('./src/utils');
const { fork } = require('child_process');
const { join } = require('path');
const { getClassesMapping, readDir, chunk } = require('./src/utils');

const cpus = require('os').cpus().length;

const arg = process.argv.slice(2)[0];
const mode = arg && ((arg === 'reverse') || (arg === '-r')) ? 'reverse' : 'forward';

const SEARCH_DIR = 'node_modules';

const csv = loadCSV();
const classesMapping = getClassesMapping();
const files = readDir(SEARCH_DIR);

for (const file of files) {
let data = readFileSync(file, { encoding: 'utf8' });
for (const c in csv) {
if (data.includes(mode === 'forward' ? c : csv[c])) {
console.log(`${mode}-jetifying: ${file}`);
data = mode === 'forward' ? data.replace(new RegExp(c, 'g'), csv[c]) : data.replace(new RegExp(csv[c], 'g'), c);
writeFileSync(file, data, { encoding: 'utf8' });
}
}
}
console.log(`Jetifier found ${files.length} file(s) to ${mode}-jetify. Using ${cpus} workers...`);

for (const filesChunk of chunk(files, cpus)) {
const worker = fork(join(__dirname, 'src', 'worker.js'));
worker.send({ filesChunk, classesMapping, mode });
}
File renamed without changes.
File renamed without changes.
46 changes: 32 additions & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
const { readFileSync, readdirSync, statSync } = require('fs');
const { join } = require('path');

const chunk = (array, chunkSize) => {
const size = Math.ceil(array.length / chunkSize);
const result = [];
while (array.length) {
result.push(array.splice(0, size));
}
return result;
};

const readDir = (dir, filesList = []) => {
const files = readdirSync(dir);
for (let file of files) {
Expand All @@ -16,29 +25,38 @@ const readDir = (dir, filesList = []) => {
return filesList;
};

const loadCSV = () => {
const csvFilePath = join(__dirname, 'androidx-class-mapping.csv');
const loadCSVFile = () => {
const csvFilePath = join(__dirname, '..', 'mapping', 'androidx-class-mapping.csv');
const lines = readFileSync(csvFilePath, { encoding: 'utf8' }).split(/\r?\n/);


// Remove redundant "Support Library class,Android X class" from array
lines.shift();

// last element will always be an empty line so removing it from the array
if(lines[lines.length - 1] === "") {
lines.pop();
if (lines[lines.length - 1] === "") {
lines.pop();
}

const result = {};
for (let line of lines) {

return lines;
};

const getClassesMapping = () => {
const csv = loadCSVFile();
const result = [];
for (let line of csv) {
const oldValue = line.split(',')[0];
const newValue = line.split(',')[1];
result[oldValue] = newValue;
result.push([oldValue, newValue]);
}

// renderscript must be added to the canonical androidx-class-mapping.csv - it is not upstream
result['android.support.v8.renderscript'] = 'android.renderscript';
result.push(['android.support.v8.renderscript', 'android.renderscript']);

return result;
}
};

module.exports = {
loadCSV,
readDir
}
getClassesMapping,
readDir,
chunk
};
18 changes: 18 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { readFileSync, writeFileSync } = require('fs');

process.on('message', ({ filesChunk, classesMapping, mode }) => {

for (const file of filesChunk) {
let data = readFileSync(file, { encoding: 'utf8' });
for (const [oldClass, newClass] of classesMapping) {
if (data.includes(mode === 'forward' ? oldClass : newClass)) {
data = mode === 'forward' ?
data.replace(new RegExp(oldClass, 'g'), newClass) :
data.replace(new RegExp(newClass, 'g'), oldClass);
writeFileSync(file, data, { encoding: 'utf8' });
}
}
}

process.exit(0);
});

0 comments on commit 0a16472

Please sign in to comment.