mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
extracting findGitExecutable() method from GitExecutor to ExecutableHelper
This commit is contained in:
@@ -15,24 +15,20 @@
|
||||
*/
|
||||
package com.intellij.openapi.vcs;
|
||||
|
||||
import com.intellij.execution.configurations.PathEnvironmentVariableUtil;
|
||||
import com.intellij.execution.process.CapturingProcessHandler;
|
||||
import com.intellij.execution.process.ProcessOutput;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import com.intellij.openapi.vfs.LocalFileSystem;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class Executor {
|
||||
@@ -68,6 +64,12 @@ public class Executor {
|
||||
debug("# cd " + shortenPath(absolutePath));
|
||||
}
|
||||
|
||||
public static void debug(@NotNull String msg) {
|
||||
if (!StringUtil.isEmptyOrSpaces(msg)) {
|
||||
LOG.info(msg);
|
||||
}
|
||||
}
|
||||
|
||||
private static void cdRel(@NotNull String relativePath) {
|
||||
cdAbs(ourCurrentDir + "/" + relativePath);
|
||||
}
|
||||
@@ -262,44 +264,7 @@ public class Executor {
|
||||
return split;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
protected static String findExecutable(@NotNull String programName,
|
||||
@NotNull String unixExec,
|
||||
@NotNull String winExec,
|
||||
@NotNull Collection<String> envs) {
|
||||
String exec = findEnvValue(programName, envs);
|
||||
if (exec != null) {
|
||||
return exec;
|
||||
}
|
||||
File fileExec = PathEnvironmentVariableUtil.findInPath(SystemInfo.isWindows ? winExec : unixExec);
|
||||
if (fileExec != null) {
|
||||
return fileExec.getAbsolutePath();
|
||||
}
|
||||
throw new IllegalStateException(programName + " executable not found. " + (envs.size() > 0 ?
|
||||
"Please define a valid environment variable " +
|
||||
envs.iterator().next() +
|
||||
" pointing to the " +
|
||||
programName +
|
||||
" executable." : ""));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String findEnvValue(@NotNull String programNameForLog, @NotNull Collection<String> envs) {
|
||||
for (String env : envs) {
|
||||
String val = System.getenv(env);
|
||||
if (val != null && new File(val).canExecute()) {
|
||||
debug(String.format("Using %s from %s: %s", programNameForLog, env, val));
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void debug(@NotNull String msg) {
|
||||
if (!StringUtil.isEmptyOrSpaces(msg)) {
|
||||
LOG.info(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String shortenPath(@NotNull String path) {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2000-2016 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.testFramework.vcs;
|
||||
|
||||
import com.intellij.execution.configurations.PathEnvironmentVariableUtil;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* @author Sergey Karashevich
|
||||
*/
|
||||
public class ExecutableHelper {
|
||||
|
||||
private static final Logger LOG = Logger.getInstance(ExecutableHelper.class);
|
||||
|
||||
private static final String GIT_EXECUTABLE_ENV = "IDEA_TEST_GIT_EXECUTABLE";
|
||||
private static final String TEAMCITY_GIT_EXECUTABLE_ENV = "TEAMCITY_GIT_PATH";
|
||||
|
||||
public static String findGitExecutable() {
|
||||
return findExecutable("Git", "git", "git.exe", Arrays.asList(GIT_EXECUTABLE_ENV, TEAMCITY_GIT_EXECUTABLE_ENV));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String findExecutable(@NotNull String programName,
|
||||
@NotNull String unixExec,
|
||||
@NotNull String winExec,
|
||||
@NotNull Collection<String> envs) {
|
||||
String exec = findEnvValue(programName, envs);
|
||||
if (exec != null) {
|
||||
return exec;
|
||||
}
|
||||
File fileExec = PathEnvironmentVariableUtil.findInPath(SystemInfo.isWindows ? winExec : unixExec);
|
||||
if (fileExec != null) {
|
||||
return fileExec.getAbsolutePath();
|
||||
}
|
||||
throw new IllegalStateException(programName + " executable not found. " + (envs.size() > 0 ?
|
||||
"Please define a valid environment variable " +
|
||||
envs.iterator().next() +
|
||||
" pointing to the " +
|
||||
programName +
|
||||
" executable." : ""));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String findEnvValue(@NotNull String programNameForLog, @NotNull Collection<String> envs) {
|
||||
for (String env : envs) {
|
||||
String val = System.getenv(env);
|
||||
if (val != null && new File(val).canExecute()) {
|
||||
debug(String.format("Using %s from %s: %s", programNameForLog, env, val));
|
||||
return val;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void debug(@NotNull String msg) {
|
||||
if (!StringUtil.isEmptyOrSpaces(msg)) {
|
||||
LOG.info(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,12 +18,12 @@ package git4idea.test;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vcs.Executor;
|
||||
import com.intellij.testFramework.vcs.ExecutableHelper;
|
||||
import git4idea.repo.GitRepository;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -33,19 +33,12 @@ import static org.junit.Assert.assertFalse;
|
||||
*/
|
||||
public class GitExecutor extends Executor {
|
||||
|
||||
private static final String GIT_EXECUTABLE_ENV = "IDEA_TEST_GIT_EXECUTABLE";
|
||||
private static final String TEAMCITY_GIT_EXECUTABLE_ENV = "TEAMCITY_GIT_PATH";
|
||||
|
||||
private static final int MAX_RETRIES = 3;
|
||||
private static boolean myVersionPrinted;
|
||||
|
||||
private static String findGitExecutable() {
|
||||
return findExecutable("Git", "git", "git.exe", Arrays.asList(GIT_EXECUTABLE_ENV, TEAMCITY_GIT_EXECUTABLE_ENV));
|
||||
}
|
||||
|
||||
//using inner class to avoid extra work during class loading of unrelated tests
|
||||
public static class PathHolder {
|
||||
public static final String GIT_EXECUTABLE = findGitExecutable();
|
||||
public static final String GIT_EXECUTABLE = ExecutableHelper.findGitExecutable();
|
||||
}
|
||||
|
||||
public static String git(String command) {
|
||||
|
||||
@@ -18,6 +18,7 @@ package hg4idea.test;
|
||||
import com.intellij.openapi.application.PluginPathManager;
|
||||
import com.intellij.openapi.util.SystemInfo;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.testFramework.vcs.ExecutableHelper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.zmlx.hg4idea.execution.HgCommandResult;
|
||||
import org.zmlx.hg4idea.execution.ShellCommand;
|
||||
@@ -38,7 +39,7 @@ public class HgExecutor {
|
||||
final String programName = "hg";
|
||||
final String unixExec = "hg";
|
||||
final String winExec = "hg.exe";
|
||||
String exec = findEnvValue(programName, Collections.singletonList(HG_EXECUTABLE_ENV));
|
||||
String exec = ExecutableHelper.findEnvValue(programName, Collections.singletonList(HG_EXECUTABLE_ENV));
|
||||
if (exec != null) {
|
||||
return exec;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user