diff options
author | SimonBrandhof <simon.brandhof@gmail.com> | 2013-04-05 19:19:58 +0200 |
---|---|---|
committer | SimonBrandhof <simon.brandhof@gmail.com> | 2013-04-05 19:19:58 +0200 |
commit | 28d1d3a14f28fd57cb3948d29cb7dcb53dea43c8 (patch) | |
tree | d48fcb1e7b5522db99b2463eb50b9cc7a850f898 /sonar-runner-api/src/main | |
parent | 8b6b46d0113041184676d5d878a6e0e40964149e (diff) | |
download | sonar-scanner-cli-28d1d3a14f28fd57cb3948d29cb7dcb53dea43c8.tar.gz sonar-scanner-cli-28d1d3a14f28fd57cb3948d29cb7dcb53dea43c8.zip |
Refactor initialization of dirs
Diffstat (limited to 'sonar-runner-api/src/main')
5 files changed, 268 insertions, 29 deletions
diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java new file mode 100644 index 0000000..a831d05 --- /dev/null +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Dirs.java @@ -0,0 +1,68 @@ +/* + * Sonar Runner - API + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.api; + +import org.apache.commons.io.FileUtils; + +import java.io.File; + +class Dirs { + + void init(Runner runner) { + boolean onProject = ScanProperties.SCAN_TASK.equals(runner.property(RunnerProperties.TASK, null)); + if (onProject) { + initProjectDirs(runner); + } else { + initTaskDirs(runner); + } + } + + private void initProjectDirs(Runner runner) { + String path = runner.property(ScanProperties.PROJECT_BASEDIR, "."); + File projectDir = new File(path); + if (!projectDir.isDirectory()) { + throw new IllegalStateException("Project home must be an existing directory: " + path); + } + runner.setProperty(ScanProperties.PROJECT_BASEDIR, projectDir.getAbsolutePath()); + + File workDir; + path = runner.property(RunnerProperties.WORK_DIR, ""); + if ("".equals(path.trim())) { + workDir = new File(projectDir, ".sonar"); + + } else { + workDir = new File(path); + if (!workDir.isAbsolute()) { + workDir = new File(projectDir, path); + } + } + FileUtils.deleteQuietly(workDir); + runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath()); + } + + /** + * Non-scan task + */ + private void initTaskDirs(Runner runner) { + String path = runner.property(RunnerProperties.WORK_DIR, "."); + File workDir = new File(path); + runner.setProperty(RunnerProperties.WORK_DIR, workDir.getAbsolutePath()); + } +} diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java index 03e0099..0fbd4ba 100644 --- a/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/Runner.java @@ -19,13 +19,9 @@ */ package org.sonar.runner.api; -import org.sonar.runner.impl.Constants; -import org.sonar.runner.impl.Logs; +import org.sonar.runner.impl.InternalProperties; import javax.annotation.Nullable; - -import java.nio.charset.Charset; -import java.util.Locale; import java.util.Properties; /** @@ -36,15 +32,6 @@ public abstract class Runner<T extends Runner> { private final Properties properties = new Properties(); protected Runner() { - initProperties(); - } - - private void initProperties() { - // default values - properties.put(Constants.HOST_URL, "http://localhost:9000"); - properties.put(Constants.TASK, "scan"); - properties.put(Constants.RUNNER_APP, "SonarRunner"); - properties.put(Constants.RUNNER_APP_VERSION, RunnerVersion.version()); } public Properties properties() { @@ -55,12 +42,20 @@ public abstract class Runner<T extends Runner> { /** * Declare Sonar properties, for example sonar.projectKey=>foo. + * + * @see #setProperty(String, String) */ public T addProperties(Properties p) { properties.putAll(p); return (T) this; } + /** + * Declare a Sonar property. + * + * @see RunnerProperties + * @see ScanProperties + */ public T setProperty(String key, String value) { properties.setProperty(key, value); return (T) this; @@ -74,35 +69,39 @@ public abstract class Runner<T extends Runner> { * User-agent used in the HTTP requests to the Sonar server */ public T setApp(String app, String version) { - setProperty(Constants.RUNNER_APP, app); - setProperty(Constants.RUNNER_APP_VERSION, version); + setProperty(InternalProperties.RUNNER_APP, app); + setProperty(InternalProperties.RUNNER_APP_VERSION, version); return (T) this; } public String app() { - return property(Constants.RUNNER_APP, null); + return property(InternalProperties.RUNNER_APP, null); } public String appVersion() { - return property(Constants.RUNNER_APP_VERSION, null); + return property(InternalProperties.RUNNER_APP_VERSION, null); } public void execute() { - initSourceEncoding(); + initDefaultValues(); + new SourceEncoding().init(this); + new Dirs().init(this); doExecute(); } - private void initSourceEncoding() { - String sourceEncoding = property(Constants.SOURCE_ENCODING, null); - boolean platformDependent = false; - if (sourceEncoding == null || sourceEncoding.equals("")) { - sourceEncoding = Charset.defaultCharset().name(); - platformDependent = true; - setProperty(Constants.SOURCE_ENCODING, sourceEncoding); + protected abstract void doExecute(); + + private void initDefaultValues() { + setDefaultValue(RunnerProperties.HOST_URL, "http://localhost:9000"); + setDefaultValue(RunnerProperties.TASK, "scan"); + setDefaultValue(InternalProperties.RUNNER_APP, "SonarRunner"); + setDefaultValue(InternalProperties.RUNNER_APP_VERSION, RunnerVersion.version()); + } + + private void setDefaultValue(String key, String value) { + if (!properties.containsKey(key)) { + setProperty(key, value); } - Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\"" - + (platformDependent ? " (analysis is platform dependent)" : "")); } - protected abstract void doExecute(); } diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java new file mode 100644 index 0000000..80c4a0c --- /dev/null +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/RunnerProperties.java @@ -0,0 +1,48 @@ +/* + * Sonar Runner - API + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.api; + +/** + * Mostly used properties that can be injected in {@link Runner#setProperty(String, String)}. + * See <a href="http://docs.codehaus.org/pages/viewinfo.action?pageId=194314339">documentation</a> for more properties. + * + * @since 2.2 + */ +public interface RunnerProperties { + /** + * HTTP URL of Sonar server, "http://localhost:9000" by default + */ + String HOST_URL = "sonar.host.url"; + + /** + * Task to execute, "scan" by default + */ + String TASK = "sonar.task"; + + /** + * Encoding of source and test files. By default it's the platform encoding. + */ + String SOURCE_ENCODING = "sonar.sourceEncoding"; + + /** + * Working directory containing generated reports and temporary data. + */ + String WORK_DIR = "sonar.working.directory"; +} diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java new file mode 100644 index 0000000..f41459b --- /dev/null +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/ScanProperties.java @@ -0,0 +1,83 @@ +/* + * Sonar Runner - API + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.api; + +/** + * Most commonly used properties of the task "scan". These properties are injected in {@link Runner#setProperty(String, String)}. + * See <a href="http://docs.codehaus.org/pages/viewinfo.action?pageId=194314339">documentation</a> for more properties. + * + * @since 2.2 + */ +public interface ScanProperties { + + /** + * Default task + * @see RunnerProperties#TASK + */ + String SCAN_TASK = "scan"; + + /** + * Required project key + */ + String PROJECT_KEY = "sonar.projectKey"; + + + String PROJECT_NAME = "sonar.projectName"; + + String PROJECT_VERSION = "sonar.projectVersion"; + + /** + * Optional description + */ + String PROJECT_DESCRIPTION = "sonar.projectDescription"; + + /** + * Required paths to source directories, separated by commas, for example: "srcDir1,srcDir2" + */ + String PROJECT_SOURCE_DIRS = "sonar.sources"; + + /** + * Optional paths to test directories, separated by commas, for example: "testDir1,testDir2" + */ + String PROJECT_TEST_DIRS = "sonar.tests"; + + /** + * Optional paths to binaries, for example to declare the directory of Java bytecode. Example : "binDir" + */ + String PROJECT_BINARY_DIRS = "sonar.binaries"; + + /** + * Optional comma-separated list of paths to libraries. Example : <code>path/to/library/*.jar,path/to/specific/library/myLibrary.jar,parent/*.jar</code> + */ + String PROJECT_LIBRARIES = "sonar.libraries"; + + String PROJECT_LANGUAGE = "sonar.language"; + + /** + * It becomes quickly necessary to input historical data and to highlight some events. It is possible by going for example in a subversion tag + * and use this property. Format is yyyy-MM-dd, for example 2010-12-25. + */ + String PROJECT_DATE = "sonar.projectDate"; + + /** + * Property used to specify the base directory of the project to analyse. Default is ".". + */ + String PROJECT_BASEDIR = "sonar.projectBaseDir"; +} diff --git a/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java b/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java new file mode 100644 index 0000000..8782ebd --- /dev/null +++ b/sonar-runner-api/src/main/java/org/sonar/runner/api/SourceEncoding.java @@ -0,0 +1,41 @@ +/* + * Sonar Runner - API + * Copyright (C) 2011 SonarSource + * dev@sonar.codehaus.org + * + * This program 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. + * + * This program 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 02 + */ +package org.sonar.runner.api; + +import org.sonar.runner.impl.Logs; + +import java.nio.charset.Charset; +import java.util.Locale; + +class SourceEncoding { + + void init(Runner runner) { + String sourceEncoding = runner.property(RunnerProperties.SOURCE_ENCODING, ""); + boolean platformDependent = false; + if ("".equals(sourceEncoding)) { + sourceEncoding = Charset.defaultCharset().name(); + platformDependent = true; + runner.setProperty(RunnerProperties.SOURCE_ENCODING, sourceEncoding); + } + Logs.info("Default locale: \"" + Locale.getDefault() + "\", source code encoding: \"" + sourceEncoding + "\"" + + (platformDependent ? " (analysis is platform dependent)" : "")); + } + +} |