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  // Repair.java
20  // Since: Feb 1, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.BufferedReader;
28  import java.io.File;
29  import java.io.FileReader;
30  import java.io.FileWriter;
31  import java.io.StringWriter;
32  
33  import org.utgenome.config.TrackConfiguration;
34  import org.utgenome.config.UTGBConfig;
35  import org.utgenome.shell.Create.ScaffoldFileFilter;
36  import org.xerial.lens.XMLLens;
37  import org.xerial.silk.SilkWriter;
38  import org.xerial.util.StringUtil;
39  import org.xerial.util.log.Logger;
40  import org.xerial.util.opt.Argument;
41  import org.xerial.util.opt.Option;
42  
43  public class Repair extends UTGBShellCommand {
44  	private static Logger _logger = Logger.getLogger(Repair.class);
45  
46  	@Option(symbol = "p", longName = "package", varName = "PACKAGE_NAME", description = "specify the java package name. e.g. org.yourdomain.track")
47  	private String packageName = null;
48  
49  	@Option(symbol = "g", longName = "group", varName = "GROUP_NAME", description = "specify the maven group name of this project. default = org.utgenome.track")
50  	private String group = "org.utgenome.track";
51  
52  	@Option(symbol = "f", longName = "filepath", varName = "PATH", description = "repair the specified file/folder only")
53  	private String repairTargetPath = null;
54  
55  	private ScaffoldFileFilter scaffoldFilter = new Create.CreateAllScaffoldFileFilter();
56  
57  	@Argument(index = 0)
58  	private String projectName = null;
59  
60  	public Repair() {
61  	}
62  
63  	private String targetFile;
64  
65  	@Override
66  	public void execute(String[] args) throws Exception {
67  
68  		if (repairTargetPath != null) {
69  			scaffoldFilter = new Create.ScaffoldFileFilter() {
70  				public boolean accept(String pathname) {
71  					return pathname.startsWith(repairTargetPath);
72  				}
73  			};
74  		}
75  
76  		if (!isInProjectRoot()) {
77  			// no configuration file is found
78  			File oldConfigXML = getObsolteConfigurationFile();
79  			if (oldConfigXML.exists()) {
80  				_logger.info(String.format("old-track configuration file %s is found", oldConfigXML));
81  
82  				// convert the old config file to the silk format
83  				TrackConfiguration oldConfig = XMLLens.loadXML(TrackConfiguration.class, new BufferedReader(new FileReader(oldConfigXML)));
84  				UTGBConfig newConfig = oldConfig.convert();
85  				String silk = newConfig.toSilk();
86  
87  				// output the new configuration file
88  				File newConfigFile = new File("config/common.silk");
89  				if (!newConfigFile.exists()) {
90  					_logger.info("needs upgrade");
91  					_logger.info("generating " + newConfigFile);
92  					FileWriter fout = new FileWriter(newConfigFile);
93  					fout.append(silk);
94  					fout.append(StringUtil.newline());
95  					fout.flush();
96  					fout.close();
97  				}
98  
99  				// generate the new configuration files
100 				for (String configFile : new String[] { "config/development.silk", "config/production.silk", "config/test.silk" }) {
101 					targetFile = configFile;
102 					Create.createScaffold(newConfig, "./", new ScaffoldFileFilter() {
103 						public boolean accept(String logicalPathName) {
104 							return logicalPathName.startsWith(targetFile);
105 						}
106 					});
107 				}
108 			}
109 
110 		}
111 
112 		try {
113 			UTGBConfig config = loadUTGBConfig();
114 			Create.createScaffold(config, getProjectRoot().getPath(), scaffoldFilter);
115 		}
116 		catch (UTGBShellException e) {
117 			_logger.info(String.format("No %s file found or the config file is collapsed", getConfigFile()));
118 			// create from scratch
119 			if (projectName == null)
120 				throw new UTGBShellException("please specify your package name with -p option and the project name as a command line argument.");
121 			if (packageName == null)
122 				packageName = projectName;
123 			UTGBConfig config = new UTGBConfig();
124 			config.projectName = projectName;
125 			config.javaPackage = packageName;
126 			config.group = group;
127 			Create.createScaffold(config, "./", scaffoldFilter);
128 		}
129 
130 	}
131 
132 	public static String toSilk(UTGBConfig config) {
133 		StringWriter buf = new StringWriter();
134 		SilkWriter w = new SilkWriter(buf);
135 
136 		return null;
137 	}
138 
139 	@Override
140 	public String name() {
141 		return "repair";
142 	}
143 
144 	@Override
145 	public String getOneLinerDescription() {
146 		return "repair or restore template files";
147 	}
148 
149 }