View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-shell Project
18  //
19  // Create.java
20  // Since: Jan 9, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import org.utgenome.config.UTGBConfig;
31  import org.xerial.util.StringUtil;
32  import org.xerial.util.lens.ObjectLens;
33  import org.xerial.util.log.Logger;
34  import org.xerial.util.opt.Argument;
35  import org.xerial.util.opt.Option;
36  
37  /**
38   * Create sub command
39   * 
40   * @author leo
41   * 
42   */
43  public class Create extends UTGBShellCommand {
44  	private static Logger _logger = Logger.getLogger(Create.class);
45  
46  	private String lang = "java";
47  
48  	@Option(symbol = "p", longName = "package", varName = "PACKAGE_NAME", description = "specify the package name, e.g. org.yourdomain.track")
49  	private String packageName = null;
50  
51  	@Argument
52  	private String projectName;
53  
54  	@Option(symbol = "g", longName = "group", varName = "GROUP_NAME", description = "specify the maven group name of this project. default = org.utgenome.track")
55  	private String group = "org.utgenome.track";
56  
57  	public static enum OverwriteMode {
58  		INTERACTIVE, YES_TO_ALL, NO_TO_ALL
59  	}
60  
61  	public Create() {
62  
63  	}
64  
65  	@Override
66  	public void execute(String[] args) throws Exception {
67  
68  		if (projectName == null)
69  			throw new UTGBShellException("No project name is given. See utgb create --help for the usage.");
70  
71  		for (int i = 0; i < projectName.length(); i++) {
72  			if (StringUtil.isWhiteSpace(projectName.substring(i, i + 1))) {
73  				System.err.println("White spaces are not allowed in the project name: " + projectName);
74  				return;
75  			}
76  		}
77  
78  		if (packageName == null)
79  			packageName = ObjectLens.getCanonicalParameterName(projectName);
80  
81  		String outputFolder = globalOption.projectDir;
82  		if (outputFolder == null || outputFolder.length() <= 0)
83  			outputFolder = projectName;
84  		else {
85  			if (!outputFolder.endsWith("/"))
86  				outputFolder = outputFolder + "/";
87  
88  			outputFolder = outputFolder + projectName;
89  		}
90  
91  		UTGBConfig config = new UTGBConfig();
92  		config.projectName = projectName;
93  		config.group = group;
94  		config.javaPackage = packageName;
95  
96  		// create the scaffold
97  		createScaffold(config, outputFolder, new CreateAllScaffoldFileFilter());
98  
99  	}
100 
101 	public static interface ScaffoldFileFilter {
102 		public boolean accept(String logicalPathName);
103 	}
104 
105 	public static class CreateAllScaffoldFileFilter implements ScaffoldFileFilter {
106 		public boolean accept(String pathname) {
107 			return true;
108 		}
109 	}
110 
111 	public static void createScaffold(UTGBConfig config, String outputFolder, ScaffoldFileFilter generateFileFilter) throws IOException, UTGBShellException {
112 		ScaffoldGenerator scaffoldGenerator = new ScaffoldGenerator(outputFolder, generateFileFilter);
113 		scaffoldGenerator.createProjectScaffold(config);
114 	}
115 
116 	/**
117 	 * Create directories including its parent folders if not exist
118 	 * 
119 	 * @param dir
120 	 */
121 	public static void mkdirs(File dir) {
122 		if (!dir.exists()) {
123 			_logger.info("create a directory: " + getPath(dir));
124 			dir.mkdirs();
125 		}
126 	}
127 
128 	@Override
129 	public String name() {
130 		return "create";
131 	}
132 
133 	@Override
134 	public String getOneLinerDescription() {
135 		return "create a new project for implmenting your own track.";
136 	}
137 
138 }