Last active
November 2, 2016 14:28
-
-
Save aliokan/0a9abded7c079ad0260f651245964db2 to your computer and use it in GitHub Desktop.
Haxe script that allows to find unused classes in Haxe project.
This file contains 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
-cp src | |
--run Main | |
# project path | |
/Users/ali_o_kan/Documents/hexMachina-Gallery-Classical-MVC/ | |
# relative hxml path | |
build/build-js.hxml | |
# relative classpath | |
src/ |
This file contains 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
import sys.FileSystem; | |
/** | |
* Original discussion | |
* http://stackoverflow.com/questions/40235585/find-unused-classes-or-dead-code-in-haxe-project/ | |
*/ | |
class Main { | |
static public function main() : Void | |
{ | |
var projectPath = Sys.args()[0]; | |
var hxmlRelativePath = Sys.args()[1]; | |
var srcRelativePath = Sys.args()[2]; | |
var srcPath = projectPath + srcRelativePath; | |
var srcFileList = []; | |
var dceFileList = []; | |
// -- set the current working directory to root project | |
Sys.setCwd(projectPath); | |
// -- build project in verbose mode with no output | |
var p = new sys.io.Process("haxe", [hxmlRelativePath, "-v", "--no-output", "-D", "dce=full", "-D", "dce-debug"]); | |
// -- read all process logs | |
var logs = p.stdout.readAll().toString(); | |
// -- populate fileList with files find in src path | |
listHxFiles(srcPath, srcFileList); | |
var notUsedSrcFileList = srcFileList.copy(); | |
trace(notUsedSrcFileList.length + ' classes are finded in "$srcPath" directory.'); | |
// -- filter to find all classes used | |
var filterClassesUsed : EReg = ~/Parsed (.*)/m; | |
// -- filter to find all classes removed by DCE | |
var filterDCEClassRemoved : EReg = ~/\[DCE\] Removed class (.*)/m; | |
while(logs != null) | |
{ | |
if(filterClassesUsed.match(logs)) | |
{ | |
logs = filterClassesUsed.matchedRight(); | |
notUsedSrcFileList.remove( projectPath + filterClassesUsed.matched(1) ); | |
} else if ( filterDCEClassRemoved.match(logs) ) | |
{ | |
logs = filterDCEClassRemoved.matchedRight(); | |
var item = filterDCEClassRemoved.matched(1).split('.').join('/') + '.hx'; | |
if( srcFileList.indexOf( srcPath + item ) != -1 ) | |
{ | |
dceFileList.push( srcPath + item ); | |
} | |
} else | |
{ | |
break; | |
} | |
} | |
trace(notUsedSrcFileList.length + " classes are not used in project."); | |
trace(notUsedSrcFileList); | |
trace(dceFileList.length + " classes are remove by DCE."); | |
trace(dceFileList); | |
p.kill(); | |
} | |
private static function listHxFiles( folder:String, fileList : Array<String> ) | |
{ | |
if (FileSystem.exists(folder)) { | |
//trace("directory found: " + folder); | |
for (file in FileSystem.readDirectory(folder)) { | |
var path = folder + file; | |
if (!FileSystem.isDirectory(path)) { | |
//trace("file found: " + path); | |
if(path.substr(-3) == ".hx") fileList.push(path); | |
} else { | |
listHxFiles(path + "/", fileList); | |
} | |
} | |
} else { | |
trace('"$folder" does not exists'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment