diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-09 18:19:01 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-09 18:21:36 +0200 |
commit | e8cbc899adb18eec17aa5df5db11eea06c46c009 (patch) | |
tree | d2f7693fcbacc03a42182b98c314dcaae3e39316 /sonar-plugin-api | |
parent | 2557634d1700e935047ecb190681a6cd418cc2b1 (diff) | |
download | sonarqube-e8cbc899adb18eec17aa5df5db11eea06c46c009.tar.gz sonarqube-e8cbc899adb18eec17aa5df5db11eea06c46c009.zip |
SONAR-3677 enhanced file system
Diffstat (limited to 'sonar-plugin-api')
9 files changed, 237 insertions, 28 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java index 8584e22b07b..d33cd146ea4 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java @@ -25,7 +25,9 @@ import java.io.InputStream; /** * @since 2.6 + * @deprecated in 4.0. Replaced by {@link org.sonar.api.scan.filesystem.InputFile} */ +@Deprecated public interface InputFile { /** * The source base directory, different than the project basedir. diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java index 576d2c18cec..9e018844b19 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java @@ -34,7 +34,9 @@ import java.util.List; /** * @since 2.8 + * @deprecated in 4.0. Replaced by {@link org.sonar.api.scan.filesystem.InputFile}. */ +@Deprecated public final class InputFileUtils { private InputFileUtils() { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java index e69b13e92fa..3135b7c27cf 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileQuery.java @@ -19,12 +19,17 @@ */ package org.sonar.api.scan.filesystem; -import com.google.common.collect.Lists; +import com.google.common.base.Function; +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Collections2; +import com.google.common.collect.ListMultimap; import com.google.common.collect.Sets; +import javax.annotation.Nullable; import java.io.FileFilter; import java.util.Arrays; import java.util.Collection; +import java.util.Map; import java.util.Set; /** @@ -32,8 +37,14 @@ import java.util.Set; */ public class FileQuery { + // TODO better builders, for example FileQuery.ALL + public static FileQuery on(FileType... types) { - return new FileQuery(types); + FileQuery query = new FileQuery(); + for (FileType type : types) { + query.on(InputFile.ATTRIBUTE_TYPE, type.name().toLowerCase()); + } + return query; } public static FileQuery onSource() { @@ -44,27 +55,40 @@ public class FileQuery { return on(FileType.TEST); } - private final Set<FileType> types; - private final Set<String> languages = Sets.newLinkedHashSet(); - private final Set<String> inclusions = Sets.newLinkedHashSet(); - private final Set<String> exclusions = Sets.newLinkedHashSet(); - private final Collection<FileFilter> filters = Lists.newLinkedList(); + private final ListMultimap<String, String> attributes = ArrayListMultimap.create(); + private final Set<String> inclusions = Sets.newHashSet(); + private final Set<String> exclusions = Sets.newHashSet(); + + private FileQuery() { + } + + private FileQuery on(String attribute, String... values) { + for (String value : values) { + attributes.put(attribute, value); + } + return this; + } - private FileQuery(FileType... types) { - this.types = Sets.newHashSet(types); + public Map<String, Collection<String>> attributes() { + return attributes.asMap(); } + @Deprecated public Collection<FileType> types() { - return types; + return Collections2.transform(attributes.get(InputFile.ATTRIBUTE_TYPE), new Function<String, FileType>() { + @Override + public FileType apply(@Nullable String input) { + return input != null ? FileType.valueOf(input) : null; + } + }); } public Collection<String> languages() { - return languages; + return attributes.get(InputFile.ATTRIBUTE_LANGUAGE); } public FileQuery onLanguage(String... languages) { - this.languages.addAll(Arrays.asList(languages)); - return this; + return on(InputFile.ATTRIBUTE_LANGUAGE, languages); } public Collection<String> inclusions() { @@ -85,13 +109,14 @@ public class FileQuery { return this; } + // TODO deprecate public Collection<FileFilter> filters() { - return filters; + throw new UnsupportedOperationException("TODO"); } + // TODO deprecate ? public FileQuery withFilters(FileFilter... filters) { - this.filters.addAll(Arrays.asList(filters)); - return this; + throw new UnsupportedOperationException("TODO"); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileSystemFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileSystemFilter.java index 7d03441df56..3a215937d23 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileSystemFilter.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileSystemFilter.java @@ -30,7 +30,9 @@ import java.io.File; * <li>exclude the files which names start with Generated</li> * </ul> * @since 3.5 + * @deprecated in 4.0. Replaced by {@link InputFileFilter}. */ +@Deprecated public interface FileSystemFilter extends BatchExtension { /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java index 6600ae2099a..31f3938da22 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/FileType.java @@ -21,7 +21,9 @@ package org.sonar.api.scan.filesystem; /** * @since 3.5 + * @deprecated in 4.0. Replaced by more flexible {@link InputFile} attributes. */ +@Deprecated public enum FileType { SOURCE, TEST } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFile.java new file mode 100644 index 00000000000..c28298ef5ae --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFile.java @@ -0,0 +1,140 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.scan.filesystem; + +import com.google.common.collect.Lists; +import com.google.common.collect.Maps; +import org.apache.commons.lang.StringUtils; + +import javax.annotation.CheckForNull; +import java.io.File; +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +/** + * @since 4.0 + */ +public class InputFile implements Serializable { + + // TODO refactor attribute constants as classes or enums ? + + /** + * Path relative to module base directory. + */ + public static final String ATTRIBUTE_BASE_RELATIVE_PATH = "baseRelPath"; + + // TODO ambiguity of term "source" with sourceDir versus testDir properties + // Here it does not depend on type. + public static final String ATTRIBUTE_SOURCEDIR_PATH = "srcDirPath"; + public static final String ATTRIBUTE_SOURCE_RELATIVE_PATH = "srcRelPath"; + + public static final String ATTRIBUTE_CANONICAL_PATH = "canonicalPath"; + public static final String ATTRIBUTE_LANGUAGE = "lang"; + public static final String ATTRIBUTE_TYPE = "type"; + public static final String ATTRIBUTE_STATUS = "status"; + public static final String STATUS_SAME = "same"; + public static final String STATUS_CHANGED = "changed"; + public static final String STATUS_ADDED = "added"; + public static final String ATTRIBUTE_HASH = "checksum"; + public static final String ATTRIBUTE_EXTENSION = "extension"; + public static final String TYPE_SOURCE = "source"; + public static final String TYPE_TEST = "test"; + + // TODO limitation of persistit -> add unit test + private transient File transientFile; + private Map<String, String> attributes; + + private InputFile(File file, Map<String, String> attributes) { + this.transientFile = file; + this.attributes = attributes; + } + + /** + * Plugins should not build their own instances of {@link InputFile}. This method + * aims to be used by unit tests. + * // TODO provide builder ? + */ + public static InputFile create(File file, String baseRelativePath, Map<String, String> attributes) { + Map<String,String> copy = Maps.newHashMap(attributes); + copy.put(InputFile.ATTRIBUTE_BASE_RELATIVE_PATH, baseRelativePath); + return new InputFile(file, copy); + } + + /** + * Path from module base directory. Path is unique and identifies file within given + * <code>{@link ModuleFileSystem}</code>. File separator is the forward slash ('/'), + * even on MSWindows. + */ + public String path() { + return attribute(ATTRIBUTE_BASE_RELATIVE_PATH); + } + + public File file() { + if (transientFile == null) { + transientFile = new File(attribute(ATTRIBUTE_CANONICAL_PATH)); + } + return transientFile; + } + + public String name() { + return file().getName(); + } + + public boolean has(String attribute, String value) { + return StringUtils.equals(attributes.get(attribute), value); + } + + @CheckForNull + public String attribute(String key) { + return attributes.get(key); + } + + public Map<String, String> attributes() { + return attributes; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + InputFile inputFile = (InputFile) o; + return attribute(ATTRIBUTE_CANONICAL_PATH).equals(inputFile.attribute(ATTRIBUTE_CANONICAL_PATH)); + } + + @Override + public int hashCode() { + return path().hashCode(); + } + + public static List<File> toFiles(Iterable<InputFile> inputFiles) { + List<File> files = Lists.newArrayList(); + for (InputFile inputFile : inputFiles) { + files.add(inputFile.file()); + } + return files; + } + + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFileFilter.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFileFilter.java new file mode 100644 index 00000000000..2b166827db8 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/InputFileFilter.java @@ -0,0 +1,32 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.scan.filesystem; + +import org.sonar.api.BatchExtension; + +/** + * TODO document lifecycle -> executed when initializing project + * @since 4.0 + */ +public interface InputFileFilter extends BatchExtension { + + boolean accept(InputFile inputFile); + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java index 31882d0ac28..f8c8257141f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java @@ -20,9 +20,9 @@ package org.sonar.api.scan.filesystem; import org.sonar.api.BatchComponent; +import org.sonar.api.resources.InputFile; import javax.annotation.CheckForNull; - import java.io.File; import java.nio.charset.Charset; import java.util.List; @@ -31,6 +31,14 @@ import java.util.List; * @since 3.5 */ public interface ModuleFileSystem extends BatchComponent { + + /** + * Unique module key + * + * @since 4.0 + */ + String moduleKey(); + /** * Base directory. */ @@ -47,12 +55,14 @@ public interface ModuleFileSystem extends BatchComponent { * Source directories. Non-existing directories are excluded. * Example in Maven : ${project.basedir}/src/main/java */ + // TODO mark as dangerous to use List<File> sourceDirs(); /** * Test directories. Non-existing directories are excluded. * Example in Maven : ${project.basedir}/src/test/java */ + // TODO mark as dangerous to use List<File> testDirs(); /** @@ -72,12 +82,6 @@ public interface ModuleFileSystem extends BatchComponent { List<File> files(FileQuery query); /** - * Search for changed files. Never return null. - * @since 4.0 - */ - List<File> changedFiles(FileQuery query); - - /** * Charset of source and test files. If it's not defined, then return the platform default charset. */ Charset sourceCharset(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/SimpleModuleFileSystem.java b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/SimpleModuleFileSystem.java index 48bb0a3fcb0..4d821c0bf8e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/SimpleModuleFileSystem.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/scan/filesystem/SimpleModuleFileSystem.java @@ -42,6 +42,11 @@ public class SimpleModuleFileSystem implements ModuleFileSystem { this.buildDir = new File(baseDir, "build"); } + @Override + public String moduleKey() { + return null; + } + public File baseDir() { return baseDir; } @@ -81,11 +86,6 @@ public class SimpleModuleFileSystem implements ModuleFileSystem { return Collections.emptyList(); } - @Override - public List<File> changedFiles(FileQuery query) { - return Collections.emptyList(); - } - public Charset sourceCharset() { return Charset.forName(CharEncoding.UTF_8); } |