diff --git a/bin/util/DefineTool.dart b/bin/util/DefineTool.dart index c8dd3b1..377f02c 100644 --- a/bin/util/DefineTool.dart +++ b/bin/util/DefineTool.dart @@ -12,24 +12,39 @@ class DefineTool { //忽略列表 List exdirList = ["bin", "build", ".git", ".svn", "debug", "release"]; //要开启的宏 - List definesList = []; + List definesList = []; //扫描文件类型 - List fileNameList = [".dart", ".yaml", ".yml", ".podspec", ".java", ".kt", ".go",".rs",".js",".ts",".php",".cs",".swift",".py"]; + List fileNameList = [ + ".dart", + ".yaml", + ".yml", + ".podspec", + ".java", + ".kt", + ".go", + ".rs", + ".js", + ".ts", + ".php", + ".cs", + ".swift", + ".py" + ]; //扫描后的文件列表 List codeList = []; //设置宏 - void setDefines(List defines){ + void setDefines(List defines) { definesList = defines; } - void start(){ + void start() { //获取当前路径 String path = Directory.current.path; //获取需要定义的文件列表 _doFileListDir(path); //遍历代码进行解析,并生成新代码 - for(int i=0;i temp = definedFile22(data); FileUtil.writeToFileData(temp, codeList[i].path); - }else{ + } else { file.writeAsStringSync(newCode); } - } } - - //获取需要定义的文件列表 + + //获取需要定义的文件列表 void _doFileListDir(String path) { Directory dir = Directory(path); List listFile = dir.listSync(); @@ -69,7 +83,10 @@ class DefineTool { if (fileNameList.contains(endName)) { List data = FileUtil.readData(file.path); String code = utf8.decode(data); - if(code.contains("#ifdef") || code.contains("#else") || code.contains("#endif") || code.contains("#ifndef")){ + if (code.contains("#ifdef") || + code.contains("#else") || + code.contains("#endif") || + code.contains("#ifndef")) { codeList.add(File(file.path)); } } @@ -78,14 +95,13 @@ class DefineTool { //当前状态 int defineState = 0; //0表示未定义 1表示ifdef 2表示ifndef //遍历代码进行解析,并生成新代码 - for(int i=0;i data = FileUtil.readData(file.path); String code = utf8.decode(data); - - } } + //寻找下一行的KeyWordItem KeyWordItem? getNextLineWordItem(CodeParseUtil parseUtil, KeyWordItem item) { int index = parseUtil.list_keyWordItems.indexOf(item); @@ -94,16 +110,18 @@ class DefineTool { for (; index < parseUtil.list_keyWordItems.length; index++) { KeyWordItem keyWordItem = parseUtil.list_keyWordItems[index]; KeyWordItem? nextItem = null; - if(index= 0; index--) { // print("index "+index.toString()+" "+parseUtil.list_keyWordItems[index].type.toString()+" "+parseUtil.list_keyWordItems[index].word); KeyWordItem keyWordItem = parseUtil.list_keyWordItems[index]; - if(isNextLine){ + if (isNextLine) { return keyWordItem; } - if (keyWordItem.type == KeyWordType.TYPE_LINE && keyWordItem.word.contains("/*")) { + if (keyWordItem.type == KeyWordType.TYPE_LINE && + keyWordItem.word.contains("/*")) { return keyWordItem; - } - else if(keyWordItem.word.contains("*/")){ + } else if (keyWordItem.word.contains("*/")) { return keyWordItem; - } - else if(keyWordItem.type == KeyWordType.TYPE_LINE){ + } else if (keyWordItem.type == KeyWordType.TYPE_LINE) { isNextLine = true; } } @@ -157,57 +176,57 @@ class DefineTool { } //判断list中是否存在list2 - bool isCon(List list, List list2,{bool isFirst=false}) { + bool isCon(List list, List list2, {bool isFirst = false}) { int type = 0; int i2 = 0; - if(!isFirst){ + if (!isFirst) { for (int i = 0; i < list.length; i++) { - switch (type) { - case 0: - if (list2[i2] == list[i]) { - i2++; - if (i2 == list2.length) { - return true; + switch (type) { + case 0: + if (list2[i2] == list[i]) { + i2++; + if (i2 == list2.length) { + return true; + } + } else { + i2 = 0; } - } else { - i2 = 0; - } - break; + break; + } } - } - }else{ + } else { for (int i = 0; i < list.length; i++) { - switch (type) { - case 0: - if(list[i] == " ".codeUnitAt(0)){ - continue; - }else{ - type = 1; - i--; - } - break; - case 1: - if (list2[i2] == list[i]) { - i2++; - if (i2 == list2.length) { - return true; + switch (type) { + case 0: + if (list[i] == " ".codeUnitAt(0)) { + continue; + } else { + type = 1; + i--; } - } else { - i2 = 0; - return false; - } - break; + break; + case 1: + if (list2[i2] == list[i]) { + i2++; + if (i2 == list2.length) { + return true; + } + } else { + i2 = 0; + return false; + } + break; + } } } - } - + return false; } bool isConList(List list, List list2) { - for(int i=0;i definedFile22(List data) { List buffer = []; List> lines = splitCode(data); - + int endindex = 0; //endif所在的行 for (int i = 0; i < lines.length; i++) { List curline = lines[i]; List nextline = lines[i]; - if(i!=lines.length-1){ - nextline = lines[i+1]; + if (i != lines.length - 1) { + nextline = lines[i + 1]; } - if (isCon(curline, "#ifdef".codeUnits) && !isCon(curline, "\"".codeUnits)) { + if (isCon(curline, "#ifdef".codeUnits) && + !isCon(curline, "\"".codeUnits)) { for (int j = i; j < lines.length; j++) { - if (isCon(lines[j], "#endif".codeUnits) && !isCon(curline, "\"".codeUnits)) { + if (isCon(lines[j], "#endif".codeUnits) && + !isCon(curline, "\"".codeUnits)) { endindex = j; break; } } if (isConList(curline, definesList)) { - print("第${i+1}行 去除注释"); + print("第${i + 1}行 去除注释"); //删除# for (int j = i + 1; j < endindex; j++) { nextline = lines[j]; - if (isCon(nextline, "#".codeUnits,isFirst: true)) { + if (isCon(nextline, "#".codeUnits, isFirst: true)) { nextline.removeAt(0); } } } else { - print("第${i+1}行 找到#ifdef"); + print("第${i + 1}行 找到#ifdef"); for (int j = i + 1; j < endindex; j++) { nextline = lines[j]; - if (!isCon(nextline, "#".codeUnits,isFirst: true)) { + if (!isCon(nextline, "#".codeUnits, isFirst: true)) { nextline.insertAll(0, "#".codeUnits); } } @@ -261,178 +282,180 @@ class DefineTool { } //解析代码并生成新代码 - String defineCode(String codeText){ + String defineCode(String codeText) { //当前状态 - int defineState = 0; //0表示未定义 1表示ifdef 2表示ifndef + int defineState = 0; // 0表示未定义 1表示ifdef 2表示ifndef //是否包含 bool isContains = false; //是否加注释 bool isAddAnnotation = false; //是否去除注释 - bool isRemoveAnnotation = false; - + CodeParseUtil parseUtil = CodeParseUtil(); - parseUtil.parseCode(codeText); - // print(parseUtil.toStringColor()); - for(int i=0;i dlist = keyWordItem.word.split(" "); - print("dlist "+ keyWordItem.word+","+definesList.toString()); - if(definesList.length>=1){ - for(int n=0;n dlist = keyWordItem.word.split(" "); + print("dlist " + keyWordItem.word + "," + definesList.toString()); + if (definesList.length >= 1) { + for (int n = 0; n < definesList.length; n++) { // String define = dlist[n]; - if(keyWordItem.word.contains(definesList[n]) && !isContains){ - isContains = true; - print("isContains = true;"); - } - } - //如果包含则对后面的内容去除注释 - if(isContains){ - isContains = true; - var wordItem = getNextLineWordItem(parseUtil, keyWordItem); - if(wordItem!.word.startsWith("/*")){ - wordItem.word = wordItem.word.substring(2,wordItem.word.length); - isRemoveAnnotation = true; - } - }else{ //否则加上注释 - isContains = false; - var wordItem = getNextLineWordItem(parseUtil, keyWordItem); - print("加上注释:"+wordItem!.word); - if(!wordItem.word.startsWith("/*")){ - if(wordItem.type == KeyWordType.TYPE_LINE){ - wordItem.word = wordItem.word+"/*"; - }else{ - wordItem.word = "/*"+wordItem.word; - } - isAddAnnotation = true; - } - } - } - }else if(keyWordItem.word.contains("#ifndef")){ - defineState = 2; - isContains = false; - // List ndlist = keyWordItem.word.split(" "); - if(definesList.length>=1){ - for(int n=0;n ndlist = keyWordItem.word.split(" "); + if (definesList.length >= 1) { + for (int n = 0; n < definesList.length; n++) { + String define = definesList[n]; + if (keyWordItem.word.contains(define) && !isContains) { + isContains = true; + } + } + //如果包含则对后面的内容加上注释 + if (isContains) { + isContains = true; var wordItem = getNextLineWordItem(parseUtil, keyWordItem); - if(!wordItem!.word.startsWith("/*")){ - if(wordItem.type == KeyWordType.TYPE_LINE){ - wordItem.word = wordItem.word+"/*"; - }else{ - wordItem.word = "/*"+wordItem.word; + if (!wordItem!.word.startsWith("/*")) { + if (wordItem.type == KeyWordType.TYPE_LINE) { + wordItem.word = wordItem.word + "/*"; + } else { + wordItem.word = "/*" + wordItem.word; } + isAddAnnotation = true; - // print("else 加上注释"); } - }else if(defineState == 2 && !isContains){ + } else { + //否则去除注释 + isContains = false; var wordItem = getNextLineWordItem(parseUtil, keyWordItem); - if(wordItem!.word.startsWith("/*")){ - wordItem.word = wordItem.word.substring(2,wordItem.word.length); + if (wordItem!.word.startsWith("/*")) { + wordItem.word = + wordItem.word.substring(2, wordItem.word.length); isRemoveAnnotation = true; - // print("else 去除注释"); } } + } + } else if (keyWordItem.word.contains("#else")) { + // print(" isAddAnnotation = "+isAddAnnotation.toString() + " isRemoveAnnotation = "+isRemoveAnnotation.toString() + " defineState = "+defineState.toString() + " isContains = "+isContains.toString()); + if (isAddAnnotation) { - }else if(keyWordItem.word.contains("#endif")){ - defineState = 0; - // print("isAddAnnotation "+isAddAnnotation.toString()+" isRemoveAnnotation "+isRemoveAnnotation.toString()); - if(isAddAnnotation){ - var wordItem = getForwardLineWordItem(parseUtil, keyWordItem); - wordItem!.word = wordItem.word+"*/"; - isAddAnnotation = false; - // print("endif 加上注释"+wordItem!.word); + var wordItem = getForwardLineWordItem(parseUtil, keyWordItem); + wordItem!.word = wordItem.word + "*/"; + isAddAnnotation = false; + } else if (isRemoveAnnotation) { + // print("去除注释"); + var wordItem = getForwardLineWordItem(parseUtil, keyWordItem); + if (wordItem!.word.endsWith("*/")) { + wordItem.word = + wordItem.word.substring(0, wordItem.word.length - 2); } - else if(isRemoveAnnotation){ - var wordItem = getForwardLineWordItem(parseUtil, keyWordItem); - if(wordItem!.word.endsWith("*/")){ - wordItem.word = wordItem.word.substring(0,wordItem.word.length-2); + isRemoveAnnotation = false; + } + if (defineState == 1) { + defineState = 2; + } else if (defineState == 2) { + defineState = 1; + } + // print("defineState "+defineState.toString()+" isContains "+isContains.toString()); + //如果当前是define并且包含,则去除注释 + if (defineState == 1 && isContains) { + var wordItem = getNextLineWordItem(parseUtil, keyWordItem); + if (wordItem!.word.startsWith("/*")) { + wordItem.word = wordItem.word.substring(2, wordItem.word.length); + isRemoveAnnotation = true; + // print("else 去除注释"); + } + } else if (defineState == 1 && !isContains) { + var wordItem = getNextLineWordItem(parseUtil, keyWordItem); + if (!wordItem!.word.startsWith("/*")) { + if (wordItem.type == KeyWordType.TYPE_LINE) { + wordItem.word = wordItem.word + "/*"; + } else { + wordItem.word = "/*" + wordItem.word; } - isRemoveAnnotation = false; - // print("endif 去除注释"); + + isAddAnnotation = true; + // print("else 加上注释"+wordItem!.word); } + } else if (defineState == 2 && isContains) { + //如果当前是ndefine并且包含,则加上注释 + + var wordItem = getNextLineWordItem(parseUtil, keyWordItem); + if (!wordItem!.word.startsWith("/*")) { + if (wordItem.type == KeyWordType.TYPE_LINE) { + wordItem.word = wordItem.word + "/*"; + } else { + wordItem.word = "/*" + wordItem.word; + } + isAddAnnotation = true; + // print("else 加上注释"); + } + } else if (defineState == 2 && !isContains) { + var wordItem = getNextLineWordItem(parseUtil, keyWordItem); + if (wordItem!.word.startsWith("/*")) { + wordItem.word = wordItem.word.substring(2, wordItem.word.length); + isRemoveAnnotation = true; + // print("else 去除注释"); + } + } + } else if (keyWordItem.word.contains("#endif")) { + defineState = 0; + // print("isAddAnnotation "+isAddAnnotation.toString()+" isRemoveAnnotation "+isRemoveAnnotation.toString()); + if (isAddAnnotation) { + var wordItem = getForwardLineWordItem(parseUtil, keyWordItem); + wordItem!.word = wordItem.word + "*/"; + isAddAnnotation = false; + // print("endif 加上注释"+wordItem!.word); + } else if (isRemoveAnnotation) { + var wordItem = getForwardLineWordItem(parseUtil, keyWordItem); + if (wordItem!.word.endsWith("*/")) { + wordItem.word = + wordItem.word.substring(0, wordItem.word.length - 2); + } + isRemoveAnnotation = false; + // print("endif 去除注释"); } } } - - return parseUtil.toString(); - } + } -} \ No newline at end of file + return parseUtil.toString(); + } +}