001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.tools;
017:
018: import com.google.gwt.user.tools.util.ArgHandlerEclipse;
019: import com.google.gwt.user.tools.util.ArgHandlerIgnore;
020: import com.google.gwt.user.tools.util.ArgHandlerOverwrite;
021: import com.google.gwt.util.tools.ArgHandlerOutDir;
022: import com.google.gwt.util.tools.ArgHandlerString;
023: import com.google.gwt.util.tools.ToolBase;
024: import com.google.gwt.util.tools.Utility;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.HashMap;
029: import java.util.Map;
030:
031: /**
032: * Creates a new GWT project.
033: */
034: public final class ProjectCreator extends ToolBase {
035:
036: private static final String PACKAGE_PATH;
037:
038: static {
039: String path = ProjectCreator.class.getName();
040: path = path.substring(0, path.lastIndexOf('.') + 1);
041: PACKAGE_PATH = path.replace('.', '/');
042: }
043:
044: public static void main(String[] args) {
045: ProjectCreator creator = new ProjectCreator();
046: if (creator.processArgs(args)) {
047: if (creator.run()) {
048: return;
049: }
050: }
051:
052: System.exit(1);
053: }
054:
055: /**
056: * @param eclipse The name of project to create.
057: * @param ant The name of an ant file to create.
058: * @param outDir The directory to write into.
059: * @param overwrite Overwrite an existing files if they exist.
060: * @param ignore Ignore existing files if they exist.
061: * @throws IOException
062: */
063: static void createProject(String eclipse, String ant, File outDir,
064: boolean overwrite, boolean ignore) throws IOException {
065:
066: // Figure out the installation directory
067: String installPath = Utility.getInstallPath();
068:
069: // Create a map of replacements.
070: //
071: Map<String, String> replacements = new HashMap<String, String>();
072: replacements.put("@gwtUserPath", installPath + '/'
073: + "gwt-user.jar");
074:
075: Utility.getDirectory(outDir, "src", true);
076: Utility.getDirectory(outDir, "test", true);
077:
078: if (ant != null) {
079: // Create an ant build file
080: replacements.put("@projectName", ant);
081: File antXML = Utility.createNormalFile(outDir, ant
082: + ".ant.xml", overwrite, ignore);
083: if (antXML != null) {
084: String out = Utility.getFileFromClassPath(PACKAGE_PATH
085: + "project.ant.xmlsrc");
086: Utility.writeTemplateFile(antXML, out, replacements);
087: }
088: }
089:
090: if (eclipse != null) {
091: // Create an eclipse project file
092: replacements.put("@projectName", eclipse);
093: File dotProject = Utility.createNormalFile(outDir,
094: ".project", overwrite, ignore);
095: if (dotProject != null) {
096: String out = Utility.getFileFromClassPath(PACKAGE_PATH
097: + ".projectsrc");
098: Utility
099: .writeTemplateFile(dotProject, out,
100: replacements);
101: }
102:
103: // Create an eclipse classpath file
104: File dotClasspath = Utility.createNormalFile(outDir,
105: ".classpath", overwrite, ignore);
106: if (dotClasspath != null) {
107: String out = Utility.getFileFromClassPath(PACKAGE_PATH
108: + ".classpathsrc");
109: Utility.writeTemplateFile(dotClasspath, out,
110: replacements);
111: }
112: }
113: }
114:
115: private String ant = null;
116:
117: private String eclipse = null;
118:
119: private boolean ignore = false;
120: private File outDir = null;
121: private boolean overwrite = false;
122:
123: protected ProjectCreator() {
124:
125: registerHandler(new ArgHandlerString() {
126:
127: @Override
128: public String getPurpose() {
129: return "Generate an Ant buildfile to compile source (.ant.xml will be appended)";
130: }
131:
132: @Override
133: public String getTag() {
134: return "-ant";
135: }
136:
137: @Override
138: public String[] getTagArgs() {
139: return new String[] { "projectName" };
140: }
141:
142: @Override
143: public boolean setString(String str) {
144: ant = str;
145: return true;
146: }
147:
148: });
149:
150: registerHandler(new ArgHandlerEclipse() {
151: @Override
152: public String getPurpose() {
153: return "Generate an eclipse project";
154: }
155:
156: @Override
157: public boolean setString(String str) {
158: eclipse = str;
159: return true;
160: }
161: });
162:
163: registerHandler(new ArgHandlerOutDir() {
164: @Override
165: public void setDir(File dir) {
166: outDir = dir;
167: }
168: });
169:
170: registerHandler(new ArgHandlerOverwrite() {
171: @Override
172: public boolean setFlag() {
173: if (ignore) {
174: System.err
175: .println("-overwrite cannot be used with -ignore.");
176: return false;
177: }
178: overwrite = true;
179: return true;
180: }
181: });
182:
183: registerHandler(new ArgHandlerIgnore() {
184: @Override
185: public boolean setFlag() {
186: if (overwrite) {
187: System.err
188: .println("-ignore cannot be used with -overwrite.");
189: return false;
190: }
191: ignore = true;
192: return true;
193: }
194: });
195: }
196:
197: protected boolean run() {
198: try {
199: if (ant == null && eclipse == null) {
200: System.err
201: .println("Please specify either -ant or -eclipse.");
202: printHelp();
203: return false;
204: }
205: createProject(eclipse, ant, outDir, overwrite, ignore);
206: return true;
207: } catch (IOException e) {
208: System.err.println(e.getClass().getName() + ": "
209: + e.getMessage());
210: return false;
211: }
212: }
213: }
|