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  // SVNAdd.java
20  // Since: Jul 7, 2008
21  //
22  // $URL: http://svn.utgenome.org/utgb/trunk/utgb/utgb-shell/src/main/java/org/utgenome/shell/SVNAdd.java $ 
23  // $Author: leo $
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.io.File;
28  
29  import org.tmatesoft.svn.core.SVNDepth;
30  import org.tmatesoft.svn.core.SVNException;
31  import org.tmatesoft.svn.core.SVNPropertyValue;
32  import org.tmatesoft.svn.core.wc.SVNClientManager;
33  import org.tmatesoft.svn.core.wc.SVNWCClient;
34  import org.xerial.util.StringUtil;
35  import org.xerial.util.log.Logger;
36  import org.xerial.util.opt.OptionParser;
37  import org.xerial.util.opt.OptionParserException;
38  
39  /**
40   * Subcommand to add the current project to the subversion repository
41   * 
42   * @author leo
43   * 
44   */
45  public class SVNAdd extends UTGBShellSubCommandBase {
46  
47  	private static Logger _logger = Logger.getLogger(SVNAdd.class);
48  
49  	private OptionParser optionParser = new OptionParser(this);
50  
51  	public SVNAdd() {
52  
53  	}
54  
55  	private static String[] addTargetFiles = { "README", "pom.xml" };
56  	private static String[] addTargetDirs = { "config", "eclipse", "src" };
57  	private static String[] ignoreTargetFiles = { "target", "gwt-home", "www", ".project", ".classpath" };
58  
59  	SVNClientManager svnClientManager = null;
60  	SVNWCClient workingCopyClient = null;
61  
62  	@Override
63  	public void execute(String[] args) throws UTGBShellException {
64  
65  		try {
66  			optionParser.parse(args);
67  
68  			if (!isInProjectRoot())
69  				throw new UTGBShellException("must be in the project root");
70  
71  			svnClientManager = SVNClientManager.newInstance();
72  			workingCopyClient = svnClientManager.getWCClient();
73  
74  			// svn add the current directory
75  
76  			svnAddDir(globalOption.projectDir, false);
77  
78  			// svn add
79  			for (String target : addTargetDirs) {
80  				// recursively add the folder contents to the version management
81  				svnAddDir(target, true);
82  			}
83  			for (String target : addTargetFiles) {
84  				svnAddFile(target);
85  			}
86  
87  			// svn:ignore
88  			String ignoreTarget = StringUtil.join(ignoreTargetFiles, "\n");
89  			svnIgnore(".", ignoreTarget, false);
90  
91  			// db folder
92  			svnAddDir("db", false);
93  			svnIgnore("db", "*.db", false);
94  
95  			// add tomcat folder and ignore work files
96  			svnAddDir("tomcat", false);
97  			svnAddDir("tomcat/conf", true);
98  			svnAddDir("tomcat/webapps", true);
99  			svnIgnore("tomcat", "work", false);
100 		}
101 		catch (OptionParserException e) {
102 			throw new UTGBShellException(e);
103 		}
104 		catch (SVNException e) {
105 			throw new UTGBShellException(e);
106 		}
107 	}
108 
109 	public void svnAddDir(String dir, boolean isRecursive) throws SVNException {
110 		assert (workingCopyClient != null);
111 		_logger.info("svn add " + dir);
112 		File f = new File(dir);
113 		boolean createDir = !f.exists();
114 		// workingCopyClient.doAdd(f, false, createDir, false, isRecursive); // File, force, mkdir,
115 		// climbUnversionedParents, recursive
116 		workingCopyClient.doAdd(f, true, createDir, false, SVNDepth.fromRecurse(isRecursive), false, true);
117 
118 	}
119 
120 	public void svnAddFile(String file) throws SVNException {
121 		assert (workingCopyClient != null);
122 		_logger.info("svn add " + file);
123 		File f = new File(file);
124 		// workingCopyClient.doAdd(new File(file), false, false, false, false);
125 		workingCopyClient.doAdd(new File(file), true, false, false, SVNDepth.fromRecurse(false), false, false);
126 	}
127 
128 	public void svnIgnore(String file, String ignoreTargets, boolean isRecursive) throws SVNException {
129 		assert (workingCopyClient != null);
130 		_logger.info("set svn:ignore on directory =" + file + ", targets = " + ignoreTargets);
131 		// workingCopyClient.doSetProperty(new File(file), "svn:ignore", ignoreTargets, false, isRecursive, null); //
132 		// path, propName, propValue, force, recursive, handler
133 		workingCopyClient.doSetProperty(new File(file), "svn:ignore", SVNPropertyValue.create(ignoreTargets), false, SVNDepth.fromRecurse(isRecursive), null,
134 				null);
135 	}
136 
137 	@Override
138 	public String name() {
139 		return "svn-add";
140 	}
141 
142 	public String getDetailedDescription() {
143 		return loadUsage("help-svnadd.txt");
144 	}
145 
146 	public String getOneLinerDescription() {
147 		return "add the current project to the SVN repostiory";
148 	}
149 
150 	public String getOptionList() {
151 		return optionParser.getUsage();
152 	}
153 
154 }