Search in sources :

Example 1 with Version

use of org.eclipse.n4js.utils.Version in project n4js by eclipse.

the class BinariesValidator method validate.

/**
 * Validates the availability, accessibility and version of the given binary. Returns with a status representing the
 * outcome of the validation process.
 *
 * @param binary
 *            the binary to validate.
 * @return a status representing the outcome of the validation process.
 */
public IStatus validate(final Binary binary) {
    IStatus simpleValidation = validateBinaryFile(binary);
    if (!simpleValidation.isOK())
        return simpleValidation;
    final File file = new File(binary.getBinaryAbsolutePath());
    if (!file.canExecute()) {
        return error(binary, "Cannot execute '" + binary.getLabel() + "' binary at: " + file + ".");
    }
    final ProcessResult result = commandFactory.checkBinaryVersionCommand(binary).execute();
    if (!result.isOK()) {
        return error(binary, "Expected exit code 0 when checking version of '" + binary.getLabel() + "' got " + result.getExitCode() + "' instead.\n" + result.getStdErr());
    }
    if (LOGGER.isDebugEnabled()) {
        final String stdErrString = result.getStdErr();
        if (!stdErrString.isEmpty())
            LOGGER.debug(stdErrString);
    }
    final String stdOutString = result.getStdOut();
    final Version currentVersion = Version.createFromString(stdOutString.trim());
    if (!Version.isValid(currentVersion)) {
        return error(binary, "Cannot find current version of '" + binary.getLabel() + "' binary. Output was: " + stdOutString);
    } else {
        final Version minimumVersion = binary.getMinimumVersion();
        if (0 < minimumVersion.compareTo(currentVersion)) {
            return error(binary, "The required minimum version of '" + binary.getLabel() + "' is '" + minimumVersion + "'. Currently configured version is '" + currentVersion + "'.");
        }
        return OK_STATUS;
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Version(org.eclipse.n4js.utils.Version) ProcessResult(org.eclipse.n4js.utils.process.ProcessResult) File(java.io.File)

Example 2 with Version

use of org.eclipse.n4js.utils.Version in project n4js by eclipse.

the class NpmPackageToProjectAdapter method addTypeDefinitions.

/**
 * Add type definitions (N4JSDs) to the npm package. Types are added only if matching version is found.
 *
 * This method suppresses any potential issues as adding type definitions to some npm package does not affect
 * overall npm usage. Still, errors are {@link #LOGGER logged} to help troubleshooting potential issues and returns
 * with an {@link IStatus status} instance that represents the problem if any.
 *
 * @param packageRoot
 *            npm package folder.
 * @param packageJson
 *            {@link TargetPlatformFactory package.json} of that package.
 * @param manifest
 *            file that will be adjusted according to manifest fragments.
 * @param definitionsFolder
 *            root folder for npm type definitions.
 *
 * @return a status representing the outcome of performed the operation.
 */
IStatus addTypeDefinitions(File packageRoot, PackageJson packageJson, File manifest, File definitionsFolder) {
    String packageName = packageRoot.getName();
    File packageN4JSDsRoot = new File(definitionsFolder, packageName);
    if (!(packageN4JSDsRoot.exists() && packageN4JSDsRoot.isDirectory())) {
        LOGGER.info("No type definitions found for '" + packageRoot + "' npm package at '" + packageN4JSDsRoot + "'" + (!packageN4JSDsRoot.isDirectory() ? " (which is not a directory)" : "") + ".");
        return statusHelper.OK();
    }
    String packageJsonVersion = packageJson.version;
    Version packageVersion = Version.createFromString(packageJsonVersion);
    String[] list = packageN4JSDsRoot.list();
    Set<Version> availableTypeDefinitionsVersions = new HashSet<>();
    for (int i = 0; i < list.length; i++) {
        String version = list[i];
        Version availableTypeDefinitionsVersion = Version.createFromString(version);
        if (!Version.MISSING.equals(availableTypeDefinitionsVersion)) {
            availableTypeDefinitionsVersions.add(availableTypeDefinitionsVersion);
        }
    }
    Version closestMatchingVersion = Version.findClosestMatching(availableTypeDefinitionsVersions, packageVersion);
    if (Version.MISSING.equals(closestMatchingVersion)) {
        String details = "";
        if (availableTypeDefinitionsVersions.isEmpty()) {
            details = " Cannot find any type definitions for  '" + packageName + "'.";
        } else if (1 == availableTypeDefinitionsVersions.size()) {
            final Version head = availableTypeDefinitionsVersions.iterator().next();
            details = " Type definitions are available only in version : " + head + ".";
        } else {
            final String versions = Iterables.toString(availableTypeDefinitionsVersions);
            details = " Type definitions are available only in versions : " + versions + ".";
        }
        logger.logInfo("Type definitions for '" + packageName + "' npm package in version " + packageVersion + " are not available." + details);
        return statusHelper.OK();
    }
    if (!(definitionsFolder.exists() && definitionsFolder.isDirectory())) {
        final String message = "Cannot find type definitions folder for '" + packageName + "' npm package for version '" + closestMatchingVersion + "'.";
        LOGGER.error(message);
        return statusHelper.createError(message);
    }
    File packageVersionedN4JSDProjectRoot = new File(packageN4JSDsRoot, closestMatchingVersion.toString());
    try {
        Path sourcePath = packageVersionedN4JSDProjectRoot.toPath();
        Path targetPath = packageRoot.toPath();
        FileCopier.copy(sourcePath, targetPath, COPY_N4JSD_PREDICATE);
    } catch (IOException e) {
        final String message = "Error while trying to update type definitions content for '" + packageName + "' npm package.";
        LOGGER.error(message);
        return statusHelper.createError(message, e);
    }
    // adjust manifest according to type definitions manifest fragments
    try {
        File[] manifestFragments = prepareManifestFragments(packageVersionedN4JSDProjectRoot, packageRoot);
        return adjustManifest(manifest, manifestFragments);
    } catch (IOException e) {
        final String message = "Error while trying to prepare manifest fragments for '" + packageName + "' npm package.";
        LOGGER.error(message);
        return statusHelper.createError(message, e);
    }
}
Also used : Path(java.nio.file.Path) Version(org.eclipse.n4js.utils.Version) IOException(java.io.IOException) File(java.io.File) HashSet(java.util.HashSet) Sets.newHashSet(com.google.common.collect.Sets.newHashSet)

Example 3 with Version

use of org.eclipse.n4js.utils.Version in project n4js by eclipse.

the class NodeVersionTest method testNodeJsVersion.

/**
 * Simply checks if the node version found by default matches the required version. Prints out path if this fails.
 */
@Test
public void testNodeJsVersion() {
    final ProcessResult result = commandFactory.checkBinaryVersionCommand(nodeJsBinary).execute();
    final Version currentVersion = Version.createFromString(result.getStdOut().trim());
    assertTrue("Version of node in " + nodeJsBinary.getBinaryAbsolutePath() + ": " + currentVersion + ",  need at least " + NodeBinariesConstants.NODE_MIN_VERSION, currentVersion.compareTo(NodeBinariesConstants.NODE_MIN_VERSION) >= 0);
}
Also used : Version(org.eclipse.n4js.utils.Version) ProcessResult(org.eclipse.n4js.utils.process.ProcessResult) Test(org.junit.Test)

Example 4 with Version

use of org.eclipse.n4js.utils.Version in project n4js by eclipse.

the class NodeVersionTest method testNPMVersion.

/**
 * Simply checks if the npm version found by default matches the required version. Prints out path if this fails.
 */
@Test
public void testNPMVersion() {
    final ProcessResult result = commandFactory.checkBinaryVersionCommand(npmBinary).execute();
    final Version currentVersion = Version.createFromString(result.getStdOut().trim());
    assertTrue("Version of node in " + npmBinary.getBinaryAbsolutePath() + ": " + currentVersion + ",  need at least " + NodeBinariesConstants.NPM_MIN_VERSION, currentVersion.compareTo(NodeBinariesConstants.NPM_MIN_VERSION) >= 0);
}
Also used : Version(org.eclipse.n4js.utils.Version) ProcessResult(org.eclipse.n4js.utils.process.ProcessResult) Test(org.junit.Test)

Aggregations

Version (org.eclipse.n4js.utils.Version)4 ProcessResult (org.eclipse.n4js.utils.process.ProcessResult)3 File (java.io.File)2 Test (org.junit.Test)2 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 IStatus (org.eclipse.core.runtime.IStatus)1