mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-11 16:05:44 +01:00
added search method to IOUtil
This commit is contained in:
@@ -29,27 +29,20 @@
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
|
||||
package sonia.scm.installer;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.io.Command;
|
||||
import sonia.scm.io.CommandResult;
|
||||
import sonia.scm.io.SimpleCommand;
|
||||
import sonia.scm.repository.HgConfig;
|
||||
import sonia.scm.repository.HgRepositoryHandler;
|
||||
import sonia.scm.util.IOUtil;
|
||||
import sonia.scm.util.SystemUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -61,10 +54,6 @@ public abstract class AbstractHgInstaller implements HgInstaller
|
||||
/** Field description */
|
||||
public static final String DIRECTORY_REPOSITORY = "repositories";
|
||||
|
||||
/** the logger for AbstractHgInstaller */
|
||||
private static final Logger logger = LoggerFactory
|
||||
.getLogger(AbstractHgInstaller.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
@@ -91,104 +80,15 @@ public abstract class AbstractHgInstaller implements HgInstaller
|
||||
@Override
|
||||
public void install(HgConfig config) throws IOException
|
||||
{
|
||||
File repoDirectory = new File(baseDirectory, DIRECTORY_REPOSITORY.concat(
|
||||
File.separator).concat(HgRepositoryHandler.TYPE_NAME));
|
||||
File repoDirectory = new File(
|
||||
baseDirectory,
|
||||
DIRECTORY_REPOSITORY.concat(File.separator).concat(
|
||||
HgRepositoryHandler.TYPE_NAME));
|
||||
|
||||
IOUtil.mkdirs(repoDirectory);
|
||||
config.setRepositoryDirectory(repoDirectory);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO check for windows
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* @param cmd
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
protected String search(String[] path, String cmd)
|
||||
{
|
||||
String cmdPath = null;
|
||||
|
||||
try
|
||||
{
|
||||
Command command = new SimpleCommand(cmd, "--version");
|
||||
CommandResult result = command.execute();
|
||||
|
||||
if (result.isSuccessfull())
|
||||
{
|
||||
cmdPath = cmd;
|
||||
}
|
||||
}
|
||||
catch (IOException ex)
|
||||
{}
|
||||
|
||||
if (cmdPath == null)
|
||||
{
|
||||
for (String pathPart : path)
|
||||
{
|
||||
List<String> extensions = getExecutableSearchExtensions();
|
||||
File file = findFileByExtension(pathPart, cmd, extensions);
|
||||
if (file != null)
|
||||
{
|
||||
cmdPath = file.getAbsolutePath();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cmdPath != null)
|
||||
{
|
||||
if (logger.isInfoEnabled())
|
||||
{
|
||||
logger.info("found {} at {}", cmd, cmdPath);
|
||||
}
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("could not find {}", cmd);
|
||||
}
|
||||
|
||||
return cmdPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of file extensions to use when searching for executables.
|
||||
* The list is in priority order, with the highest priority first.
|
||||
*/
|
||||
protected List<String> getExecutableSearchExtensions()
|
||||
{
|
||||
List<String> extensions;
|
||||
if (SystemUtil.isWindows())
|
||||
{
|
||||
extensions = Arrays.asList(".exe");
|
||||
}
|
||||
else
|
||||
{
|
||||
extensions = Arrays.asList("");
|
||||
}
|
||||
return extensions;
|
||||
}
|
||||
|
||||
private File findFileByExtension(String parentPath, String cmd,
|
||||
List<String> potentialExtensions)
|
||||
{
|
||||
File file = null;
|
||||
for (String potentialExtension : potentialExtensions)
|
||||
{
|
||||
String fileName = cmd.concat(potentialExtension);
|
||||
File potentialFile = new File(parentPath, fileName);
|
||||
if (potentialFile.exists())
|
||||
{
|
||||
file = potentialFile;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** Field description */
|
||||
|
||||
@@ -35,10 +35,8 @@ package sonia.scm.installer;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.repository.HgConfig;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -52,29 +50,6 @@ import java.io.IOException;
|
||||
public class UnixHgInstaller extends AbstractHgInstaller
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
private static final String[] PATH = new String[]
|
||||
{
|
||||
|
||||
// default path
|
||||
"/usr/bin",
|
||||
|
||||
// manually installed
|
||||
"/usr/local/bin",
|
||||
|
||||
// mac ports
|
||||
"/opt/local/bin",
|
||||
|
||||
// opencsw
|
||||
"/opt/csw/bin"
|
||||
};
|
||||
|
||||
/** the logger for UnixHgInstaller */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(UnixHgInstaller.class);
|
||||
|
||||
//~--- constructors ---------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
@@ -100,8 +75,8 @@ public class UnixHgInstaller extends AbstractHgInstaller
|
||||
public void install(HgConfig config) throws IOException
|
||||
{
|
||||
super.install(config);
|
||||
config.setHgBinary(search(PATH, "hg"));
|
||||
config.setPythonBinary(search(PATH, "python"));
|
||||
config.setHgBinary(IOUtil.search("hg"));
|
||||
config.setPythonBinary(IOUtil.search("python"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -320,7 +320,7 @@ public class WindowsHgInstaller extends AbstractHgInstaller
|
||||
|
||||
if (python == null)
|
||||
{
|
||||
python = search(new String[0], "python");
|
||||
python = IOUtil.search(new String[0], "python");
|
||||
}
|
||||
|
||||
return python;
|
||||
|
||||
@@ -38,6 +38,9 @@ package sonia.scm.util;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import sonia.scm.io.Command;
|
||||
import sonia.scm.io.CommandResult;
|
||||
import sonia.scm.io.SimpleCommand;
|
||||
import sonia.scm.io.ZipUnArchiver;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
@@ -52,6 +55,9 @@ import java.io.OutputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
@@ -59,6 +65,26 @@ import java.io.Writer;
|
||||
public class IOUtil
|
||||
{
|
||||
|
||||
/** Field description */
|
||||
public static final String DEFAULT_CHECKPARAMETER = "--version";
|
||||
|
||||
/** Field description */
|
||||
public static final String[] DEFAULT_PATH = new String[]
|
||||
{
|
||||
|
||||
// default path
|
||||
"/usr/bin",
|
||||
|
||||
// manually installed
|
||||
"/usr/local/bin",
|
||||
|
||||
// mac ports
|
||||
"/opt/local/bin",
|
||||
|
||||
// opencsw
|
||||
"/opt/csw/bin"
|
||||
};
|
||||
|
||||
/** Field description */
|
||||
private static final Logger logger =
|
||||
LoggerFactory.getLogger(IOUtil.class.getName());
|
||||
@@ -260,6 +286,144 @@ public class IOUtil
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param cmd
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String search(String cmd)
|
||||
{
|
||||
return search(DEFAULT_PATH, cmd, DEFAULT_CHECKPARAMETER);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* @param cmd
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String search(String[] path, String cmd)
|
||||
{
|
||||
return search(path, cmd, DEFAULT_CHECKPARAMETER);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO check for windows
|
||||
*
|
||||
*
|
||||
*
|
||||
* @param path
|
||||
* @param cmd
|
||||
* @param checkParameter
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static String search(String[] path, String cmd, String checkParameter)
|
||||
{
|
||||
String cmdPath = null;
|
||||
|
||||
try
|
||||
{
|
||||
Command command = new SimpleCommand(cmd, checkParameter);
|
||||
CommandResult result = command.execute();
|
||||
|
||||
if (result.isSuccessfull())
|
||||
{
|
||||
cmdPath = cmd;
|
||||
}
|
||||
}
|
||||
catch (IOException ex) {}
|
||||
|
||||
if (cmdPath == null)
|
||||
{
|
||||
for (String pathPart : path)
|
||||
{
|
||||
List<String> extensions = getExecutableSearchExtensions();
|
||||
File file = findFileByExtension(pathPart, cmd, extensions);
|
||||
|
||||
if (file != null)
|
||||
{
|
||||
cmdPath = file.getAbsolutePath();
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cmdPath != null)
|
||||
{
|
||||
if (logger.isInfoEnabled())
|
||||
{
|
||||
logger.info("found {} at {}", cmd, cmdPath);
|
||||
}
|
||||
}
|
||||
else if (logger.isWarnEnabled())
|
||||
{
|
||||
logger.warn("could not find {}", cmd);
|
||||
}
|
||||
|
||||
return cmdPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method description
|
||||
*
|
||||
*
|
||||
* @param parentPath
|
||||
* @param cmd
|
||||
* @param potentialExtensions
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static File findFileByExtension(String parentPath, String cmd,
|
||||
List<String> potentialExtensions)
|
||||
{
|
||||
File file = null;
|
||||
|
||||
for (String potentialExtension : potentialExtensions)
|
||||
{
|
||||
String fileName = cmd.concat(potentialExtension);
|
||||
File potentialFile = new File(parentPath, fileName);
|
||||
|
||||
if (potentialFile.exists())
|
||||
{
|
||||
file = potentialFile;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
//~--- get methods ----------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns a list of file extensions to use when searching for executables.
|
||||
* The list is in priority order, with the highest priority first.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static List<String> getExecutableSearchExtensions()
|
||||
{
|
||||
List<String> extensions;
|
||||
|
||||
if (SystemUtil.isWindows())
|
||||
{
|
||||
extensions = Arrays.asList(".exe");
|
||||
}
|
||||
else
|
||||
{
|
||||
extensions = Arrays.asList("");
|
||||
}
|
||||
|
||||
return extensions;
|
||||
}
|
||||
|
||||
//~--- inner classes --------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user