]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Source/MigrationTools/org/tianocore/migration/ModuleInfo.java
Add MigrationTool.java
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / ModuleInfo.java
index b238551cac84ca18dc6e7af3094358c61bb515a1..2f3af0309e9c16b8fda479de182c0eab1d5bfec2 100644 (file)
@@ -21,16 +21,24 @@ import java.util.regex.*;
 information and all the temporary data.\r
 */\r
 public class ModuleInfo {\r
-       ModuleInfo(String modulepath, UI ui, Database db) throws Exception {\r
+       ModuleInfo(String modulepath) throws Exception {\r
                this.modulepath = modulepath;\r
-               this.ui = ui;\r
-               this.db = db;\r
+               \r
+               ui.println("Choose where to place the result");\r
+               if ((outputpath = ui.getFilepath()) == null) {\r
+                       outputpath = modulepath; \r
+               }\r
+               ui.println(outputpath);\r
+               \r
                moduleScan();\r
        }\r
+\r
+       public static UI ui = null;                             //if MIM is still usefull, this can be given to it\r
+       public static Database db = null;                       //if MIM is still usefull, this can be given to it\r
        \r
-       private String modulepath = null;\r
-       private Database db = null;\r
-       private UI ui = null;\r
+       public String modulepath = null;\r
+       \r
+       public String outputpath = null;\r
        \r
        public String modulename = null;\r
        public String guidvalue = null;\r
@@ -39,6 +47,7 @@ public class ModuleInfo {
        \r
        public Set<String> localmodulesources = new HashSet<String>();          //contains both .c and .h\r
        public Set<String> preprocessedccodes = new HashSet<String>();\r
+       public Set<String> msaorinf = new HashSet<String>();                            //only a little, hash may be too big for this\r
        \r
        public Set<String> hashfuncc = new HashSet<String>();\r
        public Set<String> hashfuncd = new HashSet<String>();\r
@@ -55,55 +64,26 @@ public class ModuleInfo {
        private static String migrationcomment = "//%$//";\r
        \r
        private void moduleScan() throws Exception {\r
-               String[] list = new File(modulepath).list();\r
-               boolean hasInf = false;\r
-               String infname = null;\r
-               boolean hasMsa = false;\r
-               String msaname = null;\r
-               \r
-               for (int i = 0 ; i < list.length ; i++) {\r
-                       if (new File(list[i]).isDirectory()) {\r
-                               ;\r
-                       } else {\r
-                               if (list[i].contains(".c") || list[i].contains(".C")) {\r
-                                       localmodulesources.add(list[i]);\r
-                               } else if (list[i].contains(".h") || list[i].contains(".H")) {\r
-                                       localmodulesources.add(list[i]);        //the case that several .inf or .msa found is not concerned\r
-                               } else if (list[i].contains(".dxs")) {\r
-                                       localmodulesources.add(list[i]);\r
-                               } else if (list[i].contains(".uni")) {\r
-                                       localmodulesources.add(list[i]);\r
-                               } else if (list[i].contains(".inf")) {\r
-                                       if (ui.yesOrNo("Found .inf file : " + list[i] + "\nDo you want to use this file as this module's .inf?")) {\r
-                                               hasInf = true;\r
-                                               infname = list[i];\r
-                                       } else {\r
-                                               continue;\r
-                                       }\r
-                               } else if (list[i].contains(".msa")) {\r
-                                       if (ui.yesOrNo("Found .msa file : " + list[i] + "\nDo you want to use this file as this module's .msa?")) {\r
-                                               hasMsa = true;\r
-                                               msaname = list[i];\r
-                                       } else {\r
-                                               continue;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
+               Common.toDoAll(modulepath, ModuleInfo.class.getMethod("enroll", String.class), this, null, Common.FILE);\r
                \r
-               ModuleReader mr = new ModuleReader(modulepath, this, db);\r
-               if (hasInf) {                                                   // this sequence shows using .inf as default\r
-                       mr.readInf(infname);\r
-               } else if (hasMsa) {\r
-                       mr.readMsa(msaname);\r
-               } else {\r
+               String filename = null;\r
+               if (msaorinf.isEmpty()) {\r
                        ui.println("No INF nor MSA file found!");\r
+                       System.exit(0);\r
+               } else {\r
+                       filename = ui.choose("Found .inf or .msa file for module\n" + modulepath + "\nChoose one Please", msaorinf.toArray());\r
+               }\r
+               //ModuleReader mr = new ModuleReader(modulepath, this, db, ui);\r
+               if (filename.contains(".inf")) {\r
+                       ModuleReader.readInf(filename, this);\r
+               } else if (filename.contains(".msa")) {\r
+                       ModuleReader.readMsa(filename, this);\r
                }\r
                \r
                CommentOutNonLocalHFile();\r
                parsePreProcessedSourceCode();\r
-               \r
-               new SourceFileReplacer(modulepath, this, db, ui).flush();       // some adding library actions are taken here,so it must be put before "MsaWriter"\r
+\r
+               new SourceFileReplacer(modulepath, outputpath, this, db, ui).flush();   // some adding library actions are taken here,so it must be put before "MsaWriter"\r
                \r
                // show result\r
                if (ui.yesOrNo("Parse of the Module Information has completed. View details?")) {\r
@@ -120,12 +100,10 @@ public class ModuleInfo {
                        show(hashr8only, "hashr8only : ");\r
                }\r
                \r
-               new MsaWriter(modulepath, this, db).flush();\r
-               \r
-               // remove temp directory\r
-               //File tempdir = new File(modulepath + File.separator + "temp");\r
-               //System.out.println("Deleting Dir");\r
-               //if (tempdir.exists()) tempdir.d;\r
+               new MsaWriter(modulepath, outputpath, this, db, ui).flush();\r
+\r
+               Common.deleteDir(modulepath + File.separator + "temp");\r
+               //Common.toDoAll(modulepath + File.separator + "temp", Common.class.getMethod("deleteDir", String.class), null, null, Common.DIR);\r
                \r
                ui.println("Errors Left : " + db.error);\r
                ui.println("Complete!");\r
@@ -137,7 +115,7 @@ public class ModuleInfo {
                ui.println(show + hash.size());\r
                ui.println(hash);\r
        }\r
-\r
+       \r
        // add '//' to all non-local include lines\r
        private void CommentOutNonLocalHFile() throws IOException {\r
                BufferedReader rd;\r
@@ -146,20 +124,18 @@ public class ModuleInfo {
                PrintWriter outfile;\r
 \r
                Pattern ptninclude = Pattern.compile("[\"<](.*[.]h)[\">]");\r
-               Matcher mtcinclude;\r
-               \r
-               File tempdir = new File(modulepath + File.separator + "temp" + File.separator);\r
-               if (!tempdir.exists()) tempdir.mkdir();\r
-               \r
+               Matcher mtrinclude;\r
+\r
                Iterator<String> ii = localmodulesources.iterator();\r
                while ( ii.hasNext() ) {\r
                        curFile = ii.next();\r
                        rd = new BufferedReader(new FileReader(modulepath + File.separator + curFile));\r
+                       Common.ensureDir(modulepath + File.separator + "temp" + File.separator + curFile);\r
                        outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "temp" + File.separator + curFile)));\r
                        while ((line = rd.readLine()) != null) {\r
                                if (line.contains("#include")) {\r
-                                       mtcinclude = ptninclude.matcher(line);\r
-                                       if (mtcinclude.find() && localmodulesources.contains(mtcinclude.group(1))) {\r
+                                       mtrinclude = ptninclude.matcher(line);\r
+                                       if (mtrinclude.find() && localmodulesources.contains(mtrinclude.group(1))) {\r
                                        } else {\r
                                                line = migrationcomment + line;\r
                                        }\r
@@ -170,15 +146,7 @@ public class ModuleInfo {
                        outfile.close();\r
                }\r
        }\r
-       /*\r
-       private void search(String line, Pattern ptn, Method md) {\r
-               matmacro = Func.ptntmacro.matcher(line);\r
-               while (matmacro.find()) {\r
-                       if ((temp = Func.registerMacro(matmacro, this, db)) != null) {\r
-                       }\r
-               }\r
-       }\r
-       */\r
+\r
        private void parsePreProcessedSourceCode() throws Exception {\r
                //Cl cl = new Cl(modulepath);\r
                //cl.execute("Fat.c");\r
@@ -187,19 +155,20 @@ public class ModuleInfo {
                //System.out.println("Note!!!! The CL is not implemented now , pls do it manually!!! RUN :");\r
                //System.out.println("cl " + modulepath + "\\temp\\*.c" + " -P");\r
                //String[] list = new File(modulepath + File.separator + "temp").list();        // without CL , add\r
-               String[] list = new File(modulepath).list();\r
-               for (int i = 0 ; i < list.length ; i++) {\r
-                       if (list[i].contains(".c")) {                                                                                   // without CL , change to .i\r
-                               preprocessedccodes.add(list[i]);\r
-                       }\r
-               }\r
-               //\r
-               Iterator<String> ii = preprocessedccodes.iterator();\r
                BufferedReader rd = null;\r
                String ifile = null;\r
                String line = null;\r
                String temp = null;\r
-               //StringBuffer result = new StringBuffer();\r
+               \r
+               Iterator<String> ii = localmodulesources.iterator();\r
+               while (ii.hasNext()) {\r
+                       temp = ii.next();\r
+                       if (temp.contains(".c")) {\r
+                               preprocessedccodes.add(temp);\r
+                       }\r
+               }\r
+               \r
+               ii = preprocessedccodes.iterator();\r
                \r
                Pattern patefifuncc = Pattern.compile("g?(BS|RT)\\s*->\\s*([a-zA-Z_]\\w*)",Pattern.MULTILINE);\r
                Pattern patentrypoint = Pattern.compile("EFI_([A-Z]*)_ENTRY_POINT\\s*\\(([^\\(\\)]*)\\)",Pattern.MULTILINE);\r
@@ -234,7 +203,7 @@ public class ModuleInfo {
                        // find guid\r
                        matguid = Guid.ptnguid.matcher(line);                                                                           // several ways to implement this , which one is faster ? :\r
                        while (matguid.find()) {                                                                                                        // 1.currently , find once , then call to identify which is it\r
-                               if ((temp = Guid.register(matguid, this, db)) != null) {                        // 2.use 3 different matchers , search 3 times to find each\r
+                               if ((temp = Guid.register(matguid, this, db)) != null) {                                // 2.use 3 different matchers , search 3 times to find each\r
                                        //matguid.appendReplacement(result, db.getR9Guidname(temp));            // search the database for all 3 kinds of guids , high cost\r
                                }\r
                        }\r
@@ -288,7 +257,40 @@ public class ModuleInfo {
                }\r
        }\r
        \r
-       public static void main(String[] args) throws Exception {\r
-               FirstPanel.init();\r
+       public final void enroll(String filepath) throws Exception {\r
+               String[] temp;\r
+               if (filepath.contains(".c") || filepath.contains(".C") || filepath.contains(".h") || \r
+                               filepath.contains(".H") || filepath.contains(".dxs") || filepath.contains(".uni")) {\r
+                       temp = filepath.split("\\\\");\r
+                       localmodulesources.add(temp[temp.length - 1]);\r
+               } else if (filepath.contains(".inf") || filepath.contains(".msa")) {\r
+                       temp = filepath.split("\\\\");\r
+                       msaorinf.add(temp[temp.length - 1]);\r
+               }\r
+       }\r
+       \r
+       public static final void seekModule(String filepath) throws Exception {\r
+               if (isModule(filepath)) {\r
+                       //System.out.println("I'm in");\r
+                       new ModuleInfo(filepath);\r
+               }\r
        }\r
-}\r
+\r
+       private static final boolean isModule(String path) {\r
+               String[] list = new File(path).list();\r
+               for (int i = 0 ; i < list.length ; i++) {\r
+                       if (!new File(list[i]).isDirectory()) {\r
+                               if (list[i].contains(".inf") || list[i].contains(".msa")) {\r
+                                       return true;\r
+                               }\r
+                       }\r
+               }\r
+               return false;\r
+       }\r
+/*\r
+       public static final void main(String[] args) throws Exception {\r
+               ui = FirstPanel.init();\r
+               db = new Database();\r
+       }\r
+       */\r
+}
\ No newline at end of file