summaryrefslogtreecommitdiff
path: root/Tools/Java/Source/MigrationTools/org/tianocore/migration/MigrationTool.java
blob: c65a425df4d6789547f2d1783be4eb2bf9f572f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/** @file
 
 Copyright (c) 2006, Intel Corporation
 All rights reserved. This program and the accompanying materials
 are licensed and made available under the terms and conditions of the BSD License
 which accompanies this distribution.  The full text of the license may be found at
 http://opensource.org/licenses/bsd-license.php
 
 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
package org.tianocore.migration;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.swing.JFileChooser;

/**
 * The class is used as the main class of the MigrationTool, maintains the main
 * work flow, and all the global variables and constants. It extends nothing.
 * 
 */
public class MigrationTool {

	//
	// These two objects are serves globally, it is always required, and only
	// one instance is ever allowed.
	//
	public static UI ui = null;

	public static Database db = null;

	//
	// The global constant for MigrationTool generated comments.
	//
	public static String MIGRATIONCOMMENT = "//@MT:";

	//
	// Global switches that are changed by user by the FirstPanel.
	//
	public static boolean printModuleInfo = false;

	public static boolean doCritic = false;

	public static boolean defaultoutput = false;

	//
	// A hashmap that associates the reference to a ModuleInfo with its
	// outputpath.
	//
	public static final HashMap<ModuleInfo, String> ModuleInfoMap = new HashMap<ModuleInfo, String>();

	//
	// The starting point of the MigrationTool.
	//
	private static String startpath = null;

	/**
	 * This method defines the overall main work flow of the MigrationTool.
	 * 
	 * @param mi
	 * @throws Exception
	 */
	private static final void mainFlow(ModuleInfo mi) throws Exception {
		ModuleReader.aimAt(mi);
		SourceFileReplacer.fireAt(mi); // some adding library actions are taken
										// here,so it must be put before
										// "MsaWriter"

		// show result
		if (MigrationTool.printModuleInfo) {
			MigrationTool.ui.println("\nModule Information : ");
			MigrationTool.ui.println("Entrypoint : " + mi.entrypoint);
			show(mi.protocols, "Protocol : ");
			show(mi.ppis, "Ppi : ");
			show(mi.guids, "Guid : ");
			show(mi.hashfuncc, "call : ");
			show(mi.hashfuncd, "def : ");
			show(mi.hashEFIcall, "EFIcall : ");
			show(mi.hashnonlocalmacro, "macro : ");
			show(mi.hashnonlocalfunc, "nonlocal : ");
			show(mi.hashr8only, "hashr8only : ");
		}
		new MsaWriter(mi).flush();

		// mi.getMsaOwner().flush(MigrationTool.ModuleInfoMap.get(mi) +
		// File.separator + "Migration_" + mi.modulename + File.separator +
		// mi.modulename + ".___");

		if (MigrationTool.doCritic) {
			Critic.fireAt(ModuleInfoMap.get(mi) + File.separator + "Migration_"
					+ mi.modulename);
		}

		MigrationTool.ui.println("Errors Left : " + MigrationTool.db.error);
		MigrationTool.ui.println("Complete!");
	}

	/**
	 * This method is specially written to print the message for ModuleInfo,
	 * just for less code repeating.
	 * 
	 * @param hash
	 * @param show
	 */
	private static final void show(Set<String> hash, String show) {
		MigrationTool.ui.println(show + hash.size());
		MigrationTool.ui.println(hash);
	}

	/**
	 * This method designates the location of temp directory.
	 * 
	 * @param modulepath
	 * @return String
	 */
	public static final String getTempDir(String modulepath) {
		return "C:" + File.separator + "MigrationTool_Temp"
				+ modulepath.replace(startpath, "");
	}

	/**
	 * This method is the default output path generating scheme.
	 * 
	 * @param inputpath
	 * @return String
	 */
	private static final String assignOutPutPath(String inputpath) {
		if (MigrationTool.defaultoutput) {
			return inputpath.replaceAll(Common.STRSEPARATER, "$1");
		} else {
			return MigrationTool.ui.getFilepath(
					"Please choose where to place the output module",
					JFileChooser.DIRECTORIES_ONLY);
		}
	}

	/**
	 * This function is called by main loop of the MigrationTool which
	 * verifies whether a dir contains a module, thus generating a map
	 * which shows the corresponding path for each module.
	 * 
	 * @param filepath
	 * @throws Exception
	 */
	public static final void seekModule(String filepath) throws Exception {
		if (ModuleInfo.isModule(filepath)) {
			ModuleInfoMap.put(new ModuleInfo(filepath),
					assignOutPutPath(filepath));
		}
	}

	/**
	 * This is the main loop of the tool.
	 * 
	 * @param path
	 * @throws Exception
	 */
	public static final void startMigrateAll(String path) throws Exception {
		startpath = path;
		MigrationTool.ui.println("Project Migration");
		MigrationTool.ui.println("Copyright (c) 2006, Intel Corporation");

		if (new File("C:" + File.separator + "MigrationTool_Temp").exists()) {
			Common.deleteDir("C:" + File.separator + "MigrationTool_Temp");
		}

		Common.toDoAll(path, MigrationTool.class.getMethod("seekModule",
				String.class), null, null, Common.DIR);

		Iterator<ModuleInfo> miit = ModuleInfoMap.keySet().iterator();
		while (miit.hasNext()) {
			mainFlow(miit.next());
		}

		ModuleInfoMap.clear();

		Common.deleteDir("C:" + File.separator + "MigrationTool_Temp");
	}

	/**
	 * This main method initializes the environment. 
	 * 
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		ui = FirstPanel.getInstance();
		db = Database.getInstance();
	}
}