Last active
August 2, 2016 22:17
-
-
Save wtsnz/b9c178185745a466ecdaa691b8bd6d69 to your computer and use it in GitHub Desktop.
Super hacky script to delazy your swift files to decrease compile time.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Hacky script to convert lazy loaded closures into private instance functions | |
// The script ignores Pods, Carthage and build folders, and only looks for | |
// .swift files | |
// This doesn't work on all cases, and may break things. Be warned ⚠️ | |
// | |
// Usage: | |
// node delazy.js <folder path> | |
// | |
String.prototype.capitalizeFirstLetter = function() { | |
return this.charAt(0).toUpperCase() + this.slice(1); | |
} | |
String.prototype.contains = function(it) { return this.indexOf(it) != -1; }; | |
var fs = require('fs'); | |
var folder | |
process.argv.forEach(function (val, index, array) { | |
console.log(index + ': ' + val); | |
if (index == 2) { folder = val } | |
}); | |
var fs = require('fs'); | |
var walk = function(dir, done) { | |
var results = []; | |
fs.readdir(dir, function(err, list) { | |
if (err) return done(err); | |
var i = 0; | |
(function next() { | |
var file = list[i++]; | |
if (!file) return done(null, results); | |
file = dir + '/' + file; | |
fs.stat(file, function(err, stat) { | |
if (stat && stat.isDirectory()) { | |
walk(file, function(err, res) { | |
results = results.concat(res); | |
next(); | |
}); | |
} else { | |
results.push(file); | |
next(); | |
} | |
}); | |
})(); | |
}); | |
}; | |
walk(folder, function(err, files) { | |
var s = files.filter(function(element, i) { | |
if (element.contains('.git') || element.contains('Carthage') || element.contains('Pods')|| element.contains('build')|| element.contains('fastlane')) { | |
return false | |
} | |
if (element.contains('.swift')) { | |
return true | |
} | |
return false | |
}) | |
// console.log(s); | |
console.log('Found ' + s.length + ' files'); | |
s.forEach(function(val, index) { | |
console.log('processing ' + val) | |
processFile(val) | |
}) | |
}) | |
function processFile(path) { | |
var contents = fs.readFileSync(path).toString(); | |
var lines = contents.split('\n') | |
// console.log("lines: " + lines.length); | |
for (i in lines) { | |
var line = lines[i] | |
if (line.match(/lazy var.*= {/)) { | |
// console.log('Found lazy var on line ' + i + ' \'' + line + '\'') | |
var isPrivate = false | |
if (line.match(/private lazy var.*= {/)) { | |
isPrivate = true | |
} | |
var j = i | |
while (lines[j]) { | |
if (lines[j].match(/}\(\)/)) { | |
// console.log('Found closing statement on line ' + j) | |
break | |
} | |
j++ | |
} | |
var variableNameIndex = isPrivate ? 3 : 2 | |
var variableTypeIndex = isPrivate ? 4 : 3 | |
// Now we have the start line in 'i' + the end line in 'j' | |
// lets parse the start line to get the variable | |
var startLine = lines[i] | |
var variableName = startLine.trim().split(' ')[variableNameIndex] | |
variableName = variableName.substr(0, variableName.length - 1) | |
var variableType = startLine.trim().split(' ')[variableTypeIndex] | |
// If the variable type contains generics, skip it as we don't support that | |
if (variableType.contains('<')) { | |
console.log("Skipping due to generics") | |
continue | |
} | |
// console.log(variableName + ' ' + variableType) | |
// Calculate the function name | |
var functionName = 'create' + variableName.capitalizeFirstLetter() | |
var newVariableLineStart = isPrivate ? ' private lazy var ' : ' lazy var ' | |
var newVariableLine = newVariableLineStart + variableName + ': ' + variableType + ' = self.' + functionName + '()' | |
// console.log(functionName) | |
// console.log(newVariableLine) | |
// Calculate the second line | |
var createFunction = ' private func ' + functionName + '() -> ' + variableType + ' {' | |
// console.log(createFunction) | |
// Create the last line for the closure | |
var lastLine = ' }' | |
// Replace all the lines | |
lines[i] = newVariableLine | |
i++ | |
lines.splice(i, 0, createFunction) | |
j++ | |
lines[j] = lastLine | |
} | |
} | |
file = lines.join('\n') | |
var contents = fs.writeFileSync(path, file) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment