1 | |
package org.apache.maven.scm.provider.local.command.checkout; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.IOException; |
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
26 | |
|
27 | |
import org.apache.maven.scm.ScmException; |
28 | |
import org.apache.maven.scm.ScmFile; |
29 | |
import org.apache.maven.scm.ScmFileSet; |
30 | |
import org.apache.maven.scm.ScmFileStatus; |
31 | |
import org.apache.maven.scm.ScmVersion; |
32 | |
import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand; |
33 | |
import org.apache.maven.scm.command.checkout.CheckOutScmResult; |
34 | |
import org.apache.maven.scm.provider.ScmProviderRepository; |
35 | |
import org.apache.maven.scm.provider.local.command.LocalCommand; |
36 | |
import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils; |
37 | |
import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository; |
38 | |
import org.codehaus.plexus.util.FileUtils; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class LocalCheckOutCommand |
45 | |
extends AbstractCheckOutCommand |
46 | |
implements LocalCommand |
47 | |
{ |
48 | |
|
49 | |
protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet, |
50 | |
ScmVersion version, boolean recursive ) |
51 | |
throws ScmException |
52 | |
{ |
53 | 0 | LocalScmProviderRepository repository = (LocalScmProviderRepository) repo; |
54 | |
|
55 | 0 | if ( version != null ) |
56 | |
{ |
57 | 0 | throw new ScmException( "The local scm doesn't support tags." ); |
58 | |
} |
59 | |
|
60 | 0 | File root = new File( repository.getRoot() ); |
61 | |
|
62 | 0 | String module = repository.getModule(); |
63 | |
|
64 | 0 | File source = new File( root, module ); |
65 | |
|
66 | 0 | File baseDestination = fileSet.getBasedir(); |
67 | |
|
68 | 0 | if ( !root.exists() ) |
69 | |
{ |
70 | 0 | throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." ); |
71 | |
} |
72 | |
|
73 | 0 | if ( !source.exists() ) |
74 | |
{ |
75 | 0 | throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." ); |
76 | |
} |
77 | |
|
78 | |
List<ScmFile> checkedOutFiles; |
79 | |
|
80 | |
try |
81 | |
{ |
82 | 0 | if ( baseDestination.exists() ) |
83 | |
{ |
84 | 0 | FileUtils.deleteDirectory( baseDestination ); |
85 | |
} |
86 | |
|
87 | 0 | if ( !baseDestination.mkdirs() ) |
88 | |
{ |
89 | 0 | throw new ScmException( |
90 | |
"Could not create destination directory '" + baseDestination.getAbsolutePath() + "'." ); |
91 | |
} |
92 | |
|
93 | 0 | if ( getLogger().isInfoEnabled() ) |
94 | |
{ |
95 | 0 | getLogger().info( |
96 | |
"Checking out '" + source.getAbsolutePath() + "' to '" |
97 | |
+ baseDestination.getAbsolutePath() + "'." ); |
98 | |
} |
99 | |
|
100 | |
List<File> fileList; |
101 | |
|
102 | 0 | if ( fileSet.getFileList().isEmpty() ) |
103 | |
{ |
104 | |
@SuppressWarnings( "unchecked" ) |
105 | 0 | List<File> files = FileUtils.getFiles( source.getAbsoluteFile(), "**", null ); |
106 | 0 | fileList = files; |
107 | 0 | } |
108 | |
else |
109 | |
{ |
110 | 0 | fileList = fileSet.getFileList(); |
111 | |
} |
112 | |
|
113 | 0 | checkedOutFiles = checkOut( source, baseDestination, fileList, repository.getModule() ); |
114 | |
|
115 | |
|
116 | 0 | LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils( getLogger() ); |
117 | 0 | metadataUtils.writeMetadata( baseDestination, metadataUtils.buildMetadata( source ) ); |
118 | |
} |
119 | 0 | catch ( IOException ex ) |
120 | |
{ |
121 | 0 | throw new ScmException( "Error while checking out the files.", ex ); |
122 | 0 | } |
123 | |
|
124 | 0 | return new LocalCheckOutScmResult( null, checkedOutFiles ); |
125 | |
} |
126 | |
|
127 | |
private List<ScmFile> checkOut( File source, File baseDestination, List<File> files, String module ) |
128 | |
throws ScmException, IOException |
129 | |
{ |
130 | 0 | String sourcePath = source.getAbsolutePath(); |
131 | |
|
132 | 0 | List<ScmFile> checkedOutFiles = new ArrayList<ScmFile>(); |
133 | |
|
134 | 0 | for ( File file : files ) |
135 | |
{ |
136 | 0 | String dest = file.getAbsolutePath(); |
137 | |
|
138 | 0 | dest = dest.substring( sourcePath.length() + 1 ); |
139 | |
|
140 | 0 | File destination = new File( baseDestination, dest ); |
141 | |
|
142 | 0 | destination = destination.getParentFile(); |
143 | |
|
144 | 0 | if ( !destination.exists() && !destination.mkdirs() ) |
145 | |
{ |
146 | 0 | throw new ScmException( |
147 | |
"Could not create destination directory '" + destination.getAbsolutePath() + "'." ); |
148 | |
} |
149 | |
|
150 | 0 | FileUtils.copyFileToDirectory( file, destination ); |
151 | |
|
152 | 0 | File parent = file.getParentFile(); |
153 | |
|
154 | |
|
155 | 0 | if ( parent != null && parent.getName().equals( "CVS" ) ) |
156 | |
{ |
157 | 0 | continue; |
158 | |
} |
159 | |
|
160 | 0 | String fileName = "/" + module + "/" + dest; |
161 | |
|
162 | 0 | checkedOutFiles.add( new ScmFile( fileName, ScmFileStatus.CHECKED_OUT ) ); |
163 | 0 | } |
164 | |
|
165 | 0 | return checkedOutFiles; |
166 | |
} |
167 | |
|
168 | |
|
169 | |
} |