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$ 
23  // $Author$
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  
37  /**
38   * Sub-command to add the current project to the subversion repository
39   * 
40   * @author leo
41   * 
42   */
43  public class SVNAdd extends UTGBShellCommand {
44  
45  	private static Logger _logger = Logger.getLogger(SVNAdd.class);
46  
47  	public SVNAdd() {
48  
49  	}
50  
51  	private static String[] addTargetFiles = { "README", "pom.xml" };
52  	private static String[] addTargetDirs = { "config", "eclipse", "src" };
53  	private static String[] ignoreTargetFiles = { "target" };
54  
55  	SVNClientManager svnClientManager = null;
56  	SVNWCClient workingCopyClient = null;
57  
58  	@Override
59  	public void execute(String[] args) throws Exception {
60  
61  		if (!isInProjectRoot())
62  			throw new UTGBShellException("must be in the project root");
63  
64  		svnClientManager = SVNClientManager.newInstance();
65  		workingCopyClient = svnClientManager.getWCClient();
66  
67  		// svn add the current directory
68  
69  		svnAddDir(globalOption.projectDir != null ? globalOption.projectDir : "", false);
70  
71  		// svn add
72  		for (String target : addTargetDirs) {
73  			// recursively add the folder contents to the version management
74  			svnAddDir(target, true);
75  		}
76  		for (String target : addTargetFiles) {
77  			svnAddFile(target);
78  		}
79  
80  		// svn:ignore
81  		String ignoreTarget = StringUtil.join(ignoreTargetFiles, "\n");
82  		svnIgnore(".", ignoreTarget, false);
83  
84  		// db folder
85  		svnAddDir("db", false);
86  		svnIgnore("db", "*.db", false);
87  
88  		// add tomcat folder and ignore work files
89  		// svnAddDir("tomcat", false);
90  		// svnAddDir("tomcat/conf", true);
91  		// svnAddDir("tomcat/webapps", true);
92  		// svnIgnore("tomcat", "work", false);
93  
94  		svnAddDir("war", false);
95  		svnAddDir("war/WEB-INF", true);
96  		svnIgnore("war", "utgb", false);
97  
98  	}
99  
100 	public void svnAddDir(String dir, boolean isRecursive) throws SVNException {
101 		assert (workingCopyClient != null);
102 		_logger.info("svn add " + dir);
103 		File f = new File(dir);
104 		boolean createDir = !f.exists();
105 		// workingCopyClient.doAdd(f, false, createDir, false, isRecursive); // File, force, mkdir,
106 		// climbUnversionedParents, recursive
107 		workingCopyClient.doAdd(f, true, createDir, false, SVNDepth.fromRecurse(isRecursive), false, true);
108 
109 	}
110 
111 	public void svnAddFile(String file) throws SVNException {
112 		assert (workingCopyClient != null);
113 		_logger.info("svn add " + file);
114 		File f = new File(file);
115 		// workingCopyClient.doAdd(new File(file), false, false, false, false);
116 		workingCopyClient.doAdd(new File(file), true, false, false, SVNDepth.fromRecurse(false), false, false);
117 	}
118 
119 	public void svnIgnore(String file, String ignoreTargets, boolean isRecursive) throws SVNException {
120 		assert (workingCopyClient != null);
121 		_logger.info("set svn:ignore on directory =" + file + ", targets = " + ignoreTargets);
122 		// workingCopyClient.doSetProperty(new File(file), "svn:ignore", ignoreTargets, false, isRecursive, null); //
123 		// path, propName, propValue, force, recursive, handler
124 		workingCopyClient.doSetProperty(new File(file), "svn:ignore", SVNPropertyValue.create(ignoreTargets), false, SVNDepth.fromRecurse(isRecursive), null,
125 				null);
126 	}
127 
128 	@Override
129 	public String name() {
130 		return "svn-add";
131 	}
132 
133 	public String getOneLinerDescription() {
134 		return "add the current project to the SVN repostiory";
135 	}
136 
137 }