001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of 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,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.wizard.command.project.impl;
018:
019: import java.io.File;
020: import java.util.HashMap;
021: import java.util.HashSet;
022: import java.util.Map;
023: import java.util.Set;
024:
025: import org.romaframework.core.io.virtualfile.VirtualFile;
026: import org.romaframework.wizard.MainWizard;
027: import org.romaframework.wizard.ProjectManager;
028: import org.romaframework.wizard.WizardConstants;
029: import org.romaframework.wizard.command.BaseWizardCommand;
030: import org.romaframework.wizard.helper.FileManagementHelper;
031: import org.romaframework.wizard.properties.ProjectProperties;
032:
033: /**
034: * This wizard create a brand new project. See help to know syntax.
035: *
036: * @author Luca Garulli (luca.garulli@assetdata.it)
037: */
038: public class ProjectCreateWizard extends BaseWizardCommand {
039:
040: public static final String NAME = "create";
041:
042: public String getName() {
043: return NAME;
044: }
045:
046: public String getParameters() {
047: return "<project-type> <project-name> <src-root-package> [<project-path>]";
048: }
049:
050: public void execute(String[] iParameters) {
051: if (iParameters.length < 3)
052: syntaxError();
053:
054: // READ ARGUMENTS
055: String projectType = iParameters[1];
056: String projectName = iParameters[2];
057: String projectPackage = iParameters[3];
058: String projectPath = iParameters.length > 4 ? iParameters[4]
059: : ".";
060: projectPath += "/" + projectName;
061:
062: // SET PROJECT INFO AS SYSTEM PROPERTIES
063: System.setProperty(WizardConstants.VAR_PROJECT_NAME,
064: projectName);
065: System.setProperty(WizardConstants.VAR_PROJECT_PATH,
066: projectPath);
067: System.setProperty(WizardConstants.VAR_PROJECT_PACKAGE,
068: projectPackage);
069:
070: String path = FileManagementHelper.getVirtualPath(MainWizard
071: .getRomaHome()
072: + "modules/project-" + projectType);
073:
074: path += "/scaffolding/";
075:
076: VirtualFile scaffoldingPath = FileManagementHelper
077: .getVirtualFile(path);
078:
079: // EXECUTE THE COPY
080: if (FileManagementHelper.executeCopy(getIO(), scaffoldingPath,
081: projectPath, false) == 0)
082: return;
083:
084: createSourceDomainDirectory(projectPackage, projectPath);
085:
086: addProjectInProjectManager(projectName, projectPackage,
087: projectPath);
088:
089: addModule("core");
090:
091: addModule("project-" + projectType);
092:
093: getIO().getOutput().println(
094: "\n[ROMA] The project '" + projectName
095: + "' was correctly created.");
096: }
097:
098: private void addModule(String iProjectType) {
099: // EXECUTE THE CORE WIZARD
100: new ProjectAddModuleWizard().execute(new String[] {
101: ProjectAddModuleWizard.NAME, iProjectType });
102: }
103:
104: private void addProjectInProjectManager(String projectName,
105: String projectPackage, String projectPath) {
106: // CREATE PROJECT NAME AND PATH IN GLOBAL ROMA CONFIG
107: ProjectManager.getInstance().setProjectPath(projectName,
108: projectPath);
109:
110: // SET THE BRAND NEW PROJECT AS CURRENT
111: ProjectManager.getInstance().switchCurrentProject(projectName,
112: null);
113:
114: // FILL PROJECT PROPERTIES
115: ProjectProperties prjProp;
116: try {
117: prjProp = new ProjectProperties(projectPath);
118: Map<String, String> prjInfo = new HashMap<String, String>();
119: prjInfo.put(ProjectProperties.PACKAGE_VAR, projectPackage);
120: prjInfo.put(ProjectProperties.PACKAGE_PATH_VAR,
121: projectPackage.replace('.', '/'));
122: prjProp.setProperties(prjInfo);
123: } catch (Exception e) {
124: e.printStackTrace();
125: }
126: }
127:
128: private void createSourceDomainDirectory(String projectPackage,
129: String projectPath) {
130: // CREATE SRC DOMAIN PATH
131: String domainPath = projectPath + "/src/"
132: + projectPackage.replace('.', '/') + "/domain";
133:
134: getIO().getOutput().println(
135: "\n[ROMA] Domain classes path: " + domainPath + "...");
136:
137: new File(domainPath).mkdirs();
138: }
139:
140: public void help() {
141: StringBuffer buffer = new StringBuffer();
142: buffer.append(getCommandHelp());
143: buffer
144: .append("\n where: <project-type> is the type of project between types locally availables (see below)");
145: buffer
146: .append("\n <project-name> is the name of project to create");
147: buffer
148: .append("\n <src-root-package> Is the root package for sources");
149: buffer
150: .append("\n <project-path> is the path where to place the project scaffolding;");
151: buffer
152: .append("\n if not specified, the current path is taken");
153: buffer.append("\n");
154:
155: buffer
156: .append("\nProject types locally availables (discovered in the $ROMA_HOME/modules directory):\n");
157:
158: // PRINT AVAILABLE MODULE LIST
159: File modulesDir = new File(MainWizard.getRomaHome() + "modules");
160: File[] modulesFiles = modulesDir.listFiles();
161: Set<String> modules = new HashSet<String>();
162: String moduleName;
163: for (File moduleDir : modulesFiles) {
164: moduleName = moduleDir.getName();
165: if (!moduleName
166: .startsWith(WizardConstants.PROJECT_TYPE_PREFIX))
167: continue;
168:
169: if (moduleDir.isDirectory())
170: // ADD DIRECTORY MODULE
171: modules.add(moduleName
172: .substring(WizardConstants.PROJECT_TYPE_PREFIX
173: .length()));
174: else if (moduleDir.getName().endsWith(
175: WizardConstants.MODULE_PACKAGE_EXT))
176: // ADD ZIP MODULE
177: modules.add(moduleName.substring(
178: WizardConstants.PROJECT_TYPE_PREFIX.length(),
179: moduleName.length()
180: - WizardConstants.MODULE_PACKAGE_EXT
181: .length()));
182: }
183:
184: for (String module : modules) {
185: buffer.append("\n- " + module);
186: }
187:
188: buffer.append("\n");
189: buffer.append("\nExample:");
190: buffer.append("\n");
191: buffer.append("\nroma " + getName()
192: + " web blog com.mycompany.blog C:/temp");
193:
194: getIO().getOutput().println(buffer);
195: }
196: }
|