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