1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package com.qulice.maven;
32
33 import com.jcabi.log.Logger;
34 import com.qulice.spi.ValidationException;
35 import java.io.File;
36 import java.util.Collection;
37 import org.apache.commons.io.FileUtils;
38 import org.apache.maven.project.MavenProject;
39 import org.cactoos.text.TextOf;
40 import org.cactoos.text.Trimmed;
41 import org.cactoos.text.UncheckedText;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public final class SvnPropertiesValidator implements MavenValidator {
59
60 @Override
61 public void validate(final MavenEnvironment env)
62 throws ValidationException {
63 if (SvnPropertiesValidator.isSvn(env.project())) {
64 final File dir = new File(env.project().getBasedir(), "src");
65 if (dir.exists()) {
66 this.validate(dir);
67 } else {
68 Logger.info(
69 this,
70 "%s directory is absent, no need to check SVN properties",
71 dir
72 );
73 }
74 } else {
75 Logger.info(this, "This is not an SVN project");
76 }
77 }
78
79
80
81
82
83
84 private void validate(final File dir) throws ValidationException {
85 final Collection<File> files = FileUtils.listFiles(
86 dir,
87 new String[] {
88 "java", "txt", "xsl", "xml", "html", "js", "css", "vm",
89 "php", "py", "groovy", "ini", "properties", "bsh", "xsd", "sql",
90 },
91 true
92 );
93 int errors = 0;
94 for (final File file : files) {
95 if (!this.valid(file)) {
96 ++errors;
97 }
98 }
99 if (errors == 0) {
100 Logger.info(
101 this,
102 "%d text files have all required SVN properties",
103 files.size()
104 );
105 } else {
106 Logger.info(
107 this,
108 "%d of %d files don't have required SVN properties",
109 errors,
110 files.size()
111 );
112 throw new ValidationException(
113 "%d files with invalid SVN properties",
114 errors
115 );
116 }
117 }
118
119
120
121
122
123
124 private static boolean isSvn(final MavenProject project) {
125 return project.getScm() != null
126 && project.getScm().getConnection() != null
127 && project.getScm().getConnection().startsWith("scm:svn");
128 }
129
130
131
132
133
134
135 private boolean valid(final File file) {
136 boolean valid = true;
137 final String style = SvnPropertiesValidator.propget(
138 file, "svn:eol-style"
139 );
140 if (!"native".equals(style)) {
141 Logger.error(
142 this,
143 "File %s doesn't have 'svn:eol-style' set to 'native': '%s'",
144 file,
145 style
146 );
147 valid = false;
148 }
149 final String keywords = SvnPropertiesValidator.propget(
150 file, "svn:keywords"
151 );
152 if (!keywords.contains("Id")) {
153 Logger.error(
154 this,
155 "File %s doesn't have 'svn:keywords' with 'Id': '%s'",
156 file,
157 keywords
158 );
159 valid = false;
160 }
161 return valid;
162 }
163
164
165
166
167
168
169
170 private static String propget(final File file, final String name) {
171 final ProcessBuilder builder = new ProcessBuilder(
172 "svn",
173 "propget",
174 name,
175 file.getAbsolutePath()
176 );
177 builder.redirectErrorStream(true);
178 try {
179 final Process process = builder.start();
180 process.waitFor();
181 return new UncheckedText(
182 new Trimmed(
183 new TextOf(
184 process.getInputStream()
185 )
186 )
187 ).asString();
188 } catch (final java.io.IOException ex) {
189 throw new IllegalArgumentException(ex);
190 } catch (final InterruptedException ex) {
191 Thread.currentThread().interrupt();
192 throw new IllegalStateException(ex);
193 }
194 }
195
196 }