Search in sources :

Example 1 with PackageJson

use of org.eclipse.n4js.external.libraries.PackageJson in project n4js by eclipse.

the class NpmPackageToProjectAdapter method adaptPackages.

/**
 * Adapts npm packages in provided folder to the N4JS project structure. Only package folders which match requested
 * packages are adapted. It is expected that npm flattens packages structures, therefore it is assumed that other
 * folders are dependencies (also transitive) of the requested packages.
 *
 * Requested npm packages already look like N4JS projects (i.e. have N4MF manifest file), those packages are not
 * adapted (proper structure is assumed), but they will be returned in to the caller to allow further processing
 * (i.e. passing them to the builder).
 *
 * Returned set of N4JS project folders will not include those installed by the npm but without matching names in
 * provided set of expected packages. Those packages are treated as transitive dependencies and are not return to
 * the caller.
 *
 * @param namesOfPackagesToAdapt
 *            names of the expected packages
 * @return pair of overall adaptation status and folders of successfully adapted npm packages
 */
public Pair<IStatus, Collection<File>> adaptPackages(Iterable<String> namesOfPackagesToAdapt) {
    final File nodeModulesFolder = new File(installLocationProvider.getTargetPlatformNodeModulesLocation());
    final Collection<String> names = newHashSet(namesOfPackagesToAdapt);
    final Collection<File> adaptedProjects = newHashSet();
    final File[] packageRoots = nodeModulesFolder.listFiles(packageName -> names.contains(packageName.getName()));
    final MultiStatus status = statusHelper.createMultiStatus("Status of adapting npm packages");
    final File n4jsdsFolder = getNpmsTypeDefinitionsFolder();
    for (File packageRoot : packageRoots) {
        try {
            PackageJson packageJson = getPackageJson(packageRoot);
            final File manifest = new File(packageRoot, N4MF_MANIFEST);
            // looks like n4js project skip adaptation
            if (manifest.exists() && manifest.isFile()) {
                if (!names.remove(packageRoot.getName())) {
                    throw new IOException("Unexpected error occurred while adapting '" + packageRoot.getName() + "' npm package into N4JS format.");
                }
                adaptedProjects.add(packageRoot);
            } else {
                if (manifest.isDirectory()) {
                    throw new IOException("The manifest location is occupied by the folder '" + manifest + "'.");
                }
                manifest.createNewFile();
                try {
                    String mainModule = computeMainModule(packageRoot);
                    generateManifestContent(packageRoot, packageJson, mainModule, manifest);
                    if (!names.remove(packageRoot.getName())) {
                        throw new IOException("Unexpected error occurred while adapting '" + packageRoot.getName() + "' npm package into N4JS format.");
                    }
                    adaptedProjects.add(packageRoot);
                } catch (final Exception e) {
                    try {
                        FileDeleter.delete(manifest);
                    } catch (IOException ioe) {
                        // Intentionally swallowed to get the original cause.
                        LOGGER.error("Error while trying to clean up corrupted " + manifest + " file.", e);
                    }
                    throw e;
                }
            }
            if (n4jsdsFolder != null && adaptedProjects.contains(packageRoot)) {
                addTypeDefinitions(packageRoot, packageJson, manifest, n4jsdsFolder);
            }
        } catch (final Exception e) {
            status.merge(statusHelper.createError("Unexpected error occurred while adapting '" + packageRoot.getName() + "' npm package into N4JS format.", e));
        }
    }
    return pair(status, adaptedProjects);
}
Also used : MultiStatus(org.eclipse.core.runtime.MultiStatus) IOException(java.io.IOException) PackageJson(org.eclipse.n4js.external.libraries.PackageJson) File(java.io.File) IOException(java.io.IOException) LightweightException(org.eclipse.n4js.utils.LightweightException)

Example 2 with PackageJson

use of org.eclipse.n4js.external.libraries.PackageJson in project n4js by eclipse.

the class NpmManager method refreshInstalledNpmPackage.

private IStatus refreshInstalledNpmPackage(final String packageName, final boolean performGitPull, final IProgressMonitor monitor) {
    final SubMonitor progress = SubMonitor.convert(monitor, 2);
    logger.logInfo(LINE_SINGLE);
    final String taskName = "Refreshing type definitions for '" + packageName + "' npm package...";
    logger.logInfo(taskName);
    progress.setTaskName(taskName);
    try {
        final URI uri = getAllNpmProjectsMapping().get(packageName);
        if (null == uri) {
            // No project with the given package name. Nothing to do.
            return statusHelper.OK();
        }
        final File definitionsFolder = npmPackageToProjectAdapter.getNpmsTypeDefinitionsFolder(performGitPull);
        if (null == definitionsFolder) {
            // No definitions are available at the moment.
            return statusHelper.OK();
        }
        if (performGitPull) {
            performGitPull(progress.newChild(1));
        }
        final File packageRoot = new File(uri);
        final PackageJson packageJson = npmPackageToProjectAdapter.getPackageJson(packageRoot);
        final File manifest = new File(packageRoot, N4MF_MANIFEST);
        if (!manifest.isFile()) {
            String message = "Cannot locate N4JS manifest for '" + packageName + "' package at '" + manifest + "'.";
            final IStatus error = statusHelper.createError(message);
            logger.logError(error);
        }
        final IStatus status = npmPackageToProjectAdapter.addTypeDefinitions(packageRoot, packageJson, manifest, definitionsFolder);
        if (status.isOK()) {
            logger.logInfo("Successfully refreshed the type definitions for '" + packageName + "' npm package.");
            logger.logInfo(LINE_SINGLE);
        } else {
            logger.logError(status);
        }
        return status;
    } catch (final IOException e) {
        final String message = "Error while refreshing the definitions for '" + packageName + "' npm package.";
        final IStatus error = statusHelper.createError(message, e);
        logger.logError(error);
        return error;
    } finally {
        monitor.done();
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException) PackageJson(org.eclipse.n4js.external.libraries.PackageJson) URI(java.net.URI) File(java.io.File)

Example 3 with PackageJson

use of org.eclipse.n4js.external.libraries.PackageJson in project n4js by eclipse.

the class N4jscBase method cloneGitRepositoryAndInstallNpmPackages.

/**
 * depends on the checks done in {@link #checkTargetPlatformConfigurations}
 */
private void cloneGitRepositoryAndInstallNpmPackages() throws ExitCodeException {
    checkState(installLocationProvider instanceof HeadlessTargetPlatformInstallLocationProvider);
    HeadlessTargetPlatformInstallLocationProvider locationProvider = (HeadlessTargetPlatformInstallLocationProvider) installLocationProvider;
    if (installMissingDependencies) {
        try {
            // pull n4jsd to install location
            java.net.URI gitRepositoryLocation = locationProvider.getTargetPlatformLocalGitRepositoryLocation();
            Path localClonePath = new File(gitRepositoryLocation).toPath();
            hardReset(gitLocationProvider.getGitLocation().getRepositoryRemoteURL(), localClonePath, gitLocationProvider.getGitLocation().getRemoteBranch(), true);
            pull(localClonePath);
            // generate n4tp file for NpmManager to use
            PackageJson packageJson = TargetPlatformFactory.createN4Default();
            java.net.URI platformLocation = locationProvider.getTargetPlatformInstallLocation();
            File packageJsonFile = new File(new File(platformLocation), PackageJson.PACKAGE_JSON);
            try {
                if (!packageJsonFile.exists()) {
                    packageJsonFile.createNewFile();
                }
                try (PrintWriter pw = new PrintWriter(packageJsonFile)) {
                    pw.write(packageJson.toString());
                    pw.flush();
                    locationProvider.setTargetPlatformFileLocation(packageJsonFile.toURI());
                }
            } catch (IOException e) {
                throw new ExitCodeException(EXITCODE_CONFIGURATION_ERROR, "Error while consuming target platform file.", e);
            }
        } catch (Exception e) {
            locationProvider.setTargetPlatformFileLocation(null);
            locationProvider.setTargetPlatformInstallLocation(null);
            if (e instanceof ExitCodeException)
                throw e;
            Throwables.propagateIfPossible(e);
        }
    }
}
Also used : Path(java.nio.file.Path) HeadlessTargetPlatformInstallLocationProvider(org.eclipse.n4js.external.HeadlessTargetPlatformInstallLocationProvider) IOException(java.io.IOException) PackageJson(org.eclipse.n4js.external.libraries.PackageJson) File(java.io.File) N4JSCompileException(org.eclipse.n4js.generator.headless.N4JSCompileException) CmdLineException(org.kohsuke.args4j.CmdLineException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

File (java.io.File)3 IOException (java.io.IOException)3 PackageJson (org.eclipse.n4js.external.libraries.PackageJson)3 PrintWriter (java.io.PrintWriter)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 IStatus (org.eclipse.core.runtime.IStatus)1 MultiStatus (org.eclipse.core.runtime.MultiStatus)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1 HeadlessTargetPlatformInstallLocationProvider (org.eclipse.n4js.external.HeadlessTargetPlatformInstallLocationProvider)1 N4JSCompileException (org.eclipse.n4js.generator.headless.N4JSCompileException)1 LightweightException (org.eclipse.n4js.utils.LightweightException)1 CmdLineException (org.kohsuke.args4j.CmdLineException)1