diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/generator/GeneratorExceptionHandler.xtend b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/generator/GeneratorExceptionHandler.java similarity index 58% rename from plugins/org.eclipse.n4js/src/org/eclipse/n4js/generator/GeneratorExceptionHandler.xtend rename to plugins/org.eclipse.n4js/src/org/eclipse/n4js/generator/GeneratorExceptionHandler.java index 2d21c7328c..2a58482697 100644 --- a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/generator/GeneratorExceptionHandler.xtend +++ b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/generator/GeneratorExceptionHandler.java @@ -8,20 +8,20 @@ * Contributors: * NumberFour AG - Initial API and implementation */ -package org.eclipse.n4js.generator +package org.eclipse.n4js.generator; /** * Code generator exception handler. */ -class GeneratorExceptionHandler { +public class GeneratorExceptionHandler { /** - * Prints provided message to the out stream, and the stack trace of the provided cause. - * Afterwards throws new {@link GeneratorException} wrapping provided cause. + * Prints provided message to the out stream, and the stack trace of the provided cause. Afterwards throws new + * {@link GeneratorException} wrapping provided cause. */ - def handleError(String message, Throwable cause) { - println(message) - cause.printStackTrace - throw new GeneratorException( message, cause); + public void handleError(String message, Throwable cause) { + System.out.println(message); + cause.printStackTrace(); + throw new GeneratorException(message, cause); } } diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/packagejson/PackageJsonContentProvider.java b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/packagejson/PackageJsonContentProvider.java new file mode 100644 index 0000000000..50206fa4ca --- /dev/null +++ b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/packagejson/PackageJsonContentProvider.java @@ -0,0 +1,241 @@ +/** + * Copyright (c) 2016 NumberFour AG. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * NumberFour AG - Initial API and implementation + */ +package org.eclipse.n4js.packagejson; + +import static org.eclipse.n4js.packagejson.PackageJsonProperties.DEPENDENCIES; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.DEV_DEPENDENCIES; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.EXTENDED_RUNTIME_ENVIRONMENT; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTATION_ID; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTED_PROJECTS; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.N4JS; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.NAME; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.OUTPUT; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.PRIVATE; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.PROJECT_TYPE; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.PROVIDED_RUNTIME_LIBRARIES; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.REQUIRED_RUNTIME_LIBRARIES; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.SOURCES; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.TESTED_PROJECTS; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_ID; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_NAME; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.VERSION; +import static org.eclipse.n4js.packagejson.PackageJsonProperties.WORKSPACES_ARRAY; +import static org.eclipse.n4js.packagejson.PackageJsonUtils.getSourceContainerTypeStringRepresentation; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.groupBy; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.map; +import static org.eclipse.xtext.xbase.lib.IterableExtensions.toList; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.SortedMap; + +import org.eclipse.n4js.json.JSON.JSONArray; +import org.eclipse.n4js.json.JSON.JSONDocument; +import org.eclipse.n4js.json.JSON.JSONFactory; +import org.eclipse.n4js.json.JSON.JSONObject; +import org.eclipse.n4js.json.JSON.JSONStringLiteral; +import org.eclipse.n4js.json.JSON.NameValuePair; +import org.eclipse.n4js.json.model.utils.JSONModelUtils; +import org.eclipse.n4js.packagejson.projectDescription.ProjectType; +import org.eclipse.n4js.packagejson.projectDescription.SourceContainerType; + +import com.google.common.base.Optional; + +/** + * Class for providing the content of N4JS-specific package.json files. + * + * Use {@link PackageJsonBuilder} for creating package.json model instances and file content. + */ +class PackageJsonContentProvider { + + /** + * Creates and returns with the N4JS package.json {@link JSONDocument} representation based on the given arguments. + * + * @param projectName + * the N4JS project name of the project (cf. name). + * @param version + * The declared version of the project. + * @param type + * The type of the N4JS project. + * @param vendorId + * The vendorId to use. + * @param vendorName + * The name of the vendor as string. + * @param output + * The relative output folder location. + * @param extendedRE + * The optional extended runtime environment. + * @param dependencies + * A map of dependencies of the project (maps dependencies to their version constraints). + * @param providedRL + * An iterable of provided runtime libraries. + * @param requiredRL + * An iterable of required runtime libraries. + * @param implementationId + * The implementationId of the project. + * @param testedProjects + * A list of all projects that are being tested. + * @param sourceContainers + * A map of all source containers of the project. + * + * @return the N4JS package.json content as a string. + */ + static JSONDocument getModel( + Optional projectName, + Optional version, + Optional _private, + Iterable workspaces, + Optional type, + Optional vendorId, + Optional vendorName, + Optional output, + Optional extendedRE, + SortedMap dependencies, + SortedMap devDependencies, + Iterable providedRL, + Iterable requiredRL, + Optional implementationId, + Iterable implementedProjects, + Iterable testedProjects, + Map sourceContainers) { + JSONObject root = JSONFactory.eINSTANCE.createJSONObject(); + + if (projectName.isPresent()) + JSONModelUtils.addProperty(root, NAME.name, projectName.get()); + + if (version.isPresent()) + JSONModelUtils.addProperty(root, VERSION.name, version.get()); + + if (_private.isPresent()) { + JSONModelUtils.addProperty(root, PRIVATE.name, + JSONModelUtils.createBooleanLiteral(_private.get())); + } + + if (workspaces.iterator().hasNext()) { + JSONModelUtils.addProperty(root, WORKSPACES_ARRAY.name, + JSONModelUtils.createStringArray(workspaces)); + } + + // add "dependencies" section + if (!dependencies.isEmpty()) { + JSONObject dependenciesValue = createDependenciesValue(dependencies); + JSONModelUtils.addProperty(root, DEPENDENCIES.name, dependenciesValue); + } + + // add "devDependencies" section + if (!devDependencies.isEmpty()) { + JSONObject devDependenciesValue = createDependenciesValue(devDependencies); + JSONModelUtils.addProperty(root, DEV_DEPENDENCIES.name, devDependenciesValue); + } + + // create "n4js" section (will be added below iff it will be non-empty) + JSONObject n4jsRoot = JSONFactory.eINSTANCE.createJSONObject(); + + // project type + if (type.isPresent()) { + String projectTypeStr = PackageJsonUtils.getProjectTypeStringRepresentation(type.get()); + JSONModelUtils.addProperty(n4jsRoot, PROJECT_TYPE.name, projectTypeStr); + } + + // add vendor related properties + if (vendorId.isPresent()) + JSONModelUtils.addProperty(n4jsRoot, VENDOR_ID.name, vendorId.get()); + if (vendorName.isPresent()) + JSONModelUtils.addProperty(n4jsRoot, VENDOR_NAME.name, vendorName.get()); + + // add sources section + if (!sourceContainers.isEmpty()) { + JSONObject sourcesSection = JSONFactory.eINSTANCE.createJSONObject(); + JSONModelUtils.addProperty(n4jsRoot, SOURCES.name, sourcesSection); + + // add sources sub-sections + List> srcConts = new ArrayList<>(sourceContainers.entrySet()); + // sort by container type + Collections.sort(srcConts, + Comparator.comparing(e -> getSourceContainerTypeStringRepresentation(e.getKey()))); + // group by source container type + Map>> srcContsByPath = groupBy(srcConts, + e -> e.getKey()); + + // add source container sub-section for each specified source container type + for (SourceContainerType sct : srcContsByPath.keySet()) { + List> paths = srcContsByPath.get(sct); + JSONArray typeSectionArray = JSONFactory.eINSTANCE.createJSONArray(); + JSONModelUtils.addProperty(sourcesSection, + PackageJsonUtils.getSourceContainerTypeStringRepresentation(sct), typeSectionArray); + List pathLiterals = toList( + map(paths, pathEntry -> JSONModelUtils.createStringLiteral(pathEntry.getValue()))); + typeSectionArray.getElements().addAll(pathLiterals); + } + } + + // add output folder + if (output.isPresent()) + JSONModelUtils.addProperty(n4jsRoot, OUTPUT.name, output.get()); + + // add provided and required runtime libraries if given + if (providedRL.iterator().hasNext()) { + JSONModelUtils.addProperty(n4jsRoot, PROVIDED_RUNTIME_LIBRARIES.name, + JSONModelUtils.createStringArray(providedRL)); + } + if (requiredRL.iterator().hasNext()) { + JSONModelUtils.addProperty(n4jsRoot, REQUIRED_RUNTIME_LIBRARIES.name, + JSONModelUtils.createStringArray(requiredRL)); + } + + if (extendedRE.isPresent()) { + JSONModelUtils.addProperty(n4jsRoot, EXTENDED_RUNTIME_ENVIRONMENT.name, + extendedRE.get()); + } + if (implementationId.isPresent()) { + JSONModelUtils.addProperty(n4jsRoot, IMPLEMENTATION_ID.name, + implementationId.get()); + } + + if (implementedProjects.iterator().hasNext()) { + JSONModelUtils.addProperty(n4jsRoot, IMPLEMENTED_PROJECTS.name, + JSONModelUtils.createStringArray(implementedProjects)); + } + + if (testedProjects.iterator().hasNext()) { + JSONModelUtils.addProperty(n4jsRoot, TESTED_PROJECTS.name, + JSONModelUtils.createStringArray(testedProjects)); + } + + // add "n4js" section (if non-empty) + if (!n4jsRoot.getNameValuePairs().isEmpty()) { + JSONModelUtils.addProperty(root, N4JS.name, n4jsRoot); + } + + // finally serialize as JSONDocument + JSONDocument document = JSONFactory.eINSTANCE.createJSONDocument(); + document.setContent(root); + + return document; + } + + private static JSONObject createDependenciesValue(Map dependencies) { + JSONObject dependenciesValue = JSONFactory.eINSTANCE.createJSONObject(); + + List map = toList(map(dependencies.entrySet(), e -> { + NameValuePair pair = JSONFactory.eINSTANCE.createNameValuePair(); + pair.setName(e.getKey()); + pair.setValue(JSONModelUtils.createStringLiteral(e.getValue())); + return pair; + })); + dependenciesValue.getNameValuePairs().addAll(map); + return dependenciesValue; + } +} diff --git a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/packagejson/PackageJsonContentProvider.xtend b/plugins/org.eclipse.n4js/src/org/eclipse/n4js/packagejson/PackageJsonContentProvider.xtend deleted file mode 100644 index 1b36e0aa57..0000000000 --- a/plugins/org.eclipse.n4js/src/org/eclipse/n4js/packagejson/PackageJsonContentProvider.xtend +++ /dev/null @@ -1,210 +0,0 @@ -/** - * Copyright (c) 2016 NumberFour AG. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * NumberFour AG - Initial API and implementation - */ -package org.eclipse.n4js.packagejson - -import com.google.common.base.Optional -import java.util.Map -import java.util.SortedMap -import org.eclipse.n4js.json.JSON.JSONArray -import org.eclipse.n4js.json.JSON.JSONDocument -import org.eclipse.n4js.json.JSON.JSONFactory -import org.eclipse.n4js.json.JSON.JSONObject -import org.eclipse.n4js.json.model.utils.JSONModelUtils -import org.eclipse.n4js.packagejson.projectDescription.ProjectType -import org.eclipse.n4js.packagejson.projectDescription.SourceContainerType - -import static org.eclipse.n4js.packagejson.PackageJsonProperties.DEPENDENCIES -import static org.eclipse.n4js.packagejson.PackageJsonProperties.DEV_DEPENDENCIES -import static org.eclipse.n4js.packagejson.PackageJsonProperties.EXTENDED_RUNTIME_ENVIRONMENT -import static org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTATION_ID -import static org.eclipse.n4js.packagejson.PackageJsonProperties.IMPLEMENTED_PROJECTS -import static org.eclipse.n4js.packagejson.PackageJsonProperties.N4JS -import static org.eclipse.n4js.packagejson.PackageJsonProperties.NAME -import static org.eclipse.n4js.packagejson.PackageJsonProperties.OUTPUT -import static org.eclipse.n4js.packagejson.PackageJsonProperties.PRIVATE -import static org.eclipse.n4js.packagejson.PackageJsonProperties.PROJECT_TYPE -import static org.eclipse.n4js.packagejson.PackageJsonProperties.PROVIDED_RUNTIME_LIBRARIES -import static org.eclipse.n4js.packagejson.PackageJsonProperties.REQUIRED_RUNTIME_LIBRARIES -import static org.eclipse.n4js.packagejson.PackageJsonProperties.SOURCES -import static org.eclipse.n4js.packagejson.PackageJsonProperties.TESTED_PROJECTS -import static org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_ID -import static org.eclipse.n4js.packagejson.PackageJsonProperties.VENDOR_NAME -import static org.eclipse.n4js.packagejson.PackageJsonProperties.VERSION -import static org.eclipse.n4js.packagejson.PackageJsonProperties.WORKSPACES_ARRAY - -/** - * Class for providing the content of N4JS-specific package.json files. - * - * Use {@link PackageJsonBuilder} for creating package.json model instances and file content. - */ -package class PackageJsonContentProvider { - - /** - * Creates and returns with the N4JS package.json {@link JSONDocument} representation - * based on the given arguments. - * - * @param projectName the N4JS project name of the project (cf. name). - * @param version The declared version of the project. - * @param type The type of the N4JS project. - * @param vendorId The vendorId to use. - * @param vendorName The name of the vendor as string. - * @param output The relative output folder location. - * @param extendedRE The optional extended runtime environment. - * @param dependencies A map of dependencies of the project (maps dependencies to their version constraints). - * @param providedRL An iterable of provided runtime libraries. - * @param requiredRL An iterable of required runtime libraries. - * @param implementationId The implementationId of the project. - * @param testedProject A list of all projects that are being tested. - * @param sourceContainers A map of all source containers of the project. - * - * @return the N4JS package.json content as a string. - */ - package static def JSONDocument getModel( - Optional projectName, - Optional version, - Optional _private, - Iterable workspaces, - Optional type, - Optional vendorId, - Optional vendorName, - Optional output, - Optional extendedRE, - SortedMap dependencies, - SortedMap devDependencies, - Iterable providedRL, - Iterable requiredRL, - Optional implementationId, - Iterable implementedProjects, - Iterable testedProjects, - Map sourceContainers - ) { - val JSONObject root = JSONFactory.eINSTANCE.createJSONObject(); - - if (projectName.present) - JSONModelUtils.addProperty(root, NAME.name, projectName.get()); - - if (version.present) - JSONModelUtils.addProperty(root, VERSION.name, version.get()); - - if (_private.present) { - JSONModelUtils.addProperty(root, PRIVATE.name, - JSONModelUtils.createBooleanLiteral(_private.get())); - } - - if (!workspaces.empty) { - JSONModelUtils.addProperty(root, WORKSPACES_ARRAY.name, - JSONModelUtils.createStringArray(workspaces)); - } - - // add "dependencies" section - if (!dependencies.empty) { - val dependenciesValue = createDependenciesValue(dependencies); - JSONModelUtils.addProperty(root, DEPENDENCIES.name, dependenciesValue); - } - - // add "devDependencies" section - if (!devDependencies.empty) { - val devDependenciesValue = createDependenciesValue(devDependencies); - JSONModelUtils.addProperty(root, DEV_DEPENDENCIES.name, devDependenciesValue); - } - - // create "n4js" section (will be added below iff it will be non-empty) - val JSONObject n4jsRoot = JSONFactory.eINSTANCE.createJSONObject(); - - // project type - if (type.present) { - val projectTypeStr = PackageJsonUtils.getProjectTypeStringRepresentation(type.get()); - JSONModelUtils.addProperty(n4jsRoot, PROJECT_TYPE.name, projectTypeStr); - } - - // add vendor related properties - if (vendorId.present) - JSONModelUtils.addProperty(n4jsRoot, VENDOR_ID.name, vendorId.get()); - if (vendorName.present) - JSONModelUtils.addProperty(n4jsRoot, VENDOR_NAME.name, vendorName.get()); - - // add sources section - if (!sourceContainers.empty) { - val JSONObject sourcesSection = JSONFactory.eINSTANCE.createJSONObject(); - JSONModelUtils.addProperty(n4jsRoot, SOURCES.name, sourcesSection); - - // add sources sub-sections - sourceContainers.entrySet - // sort by container type - .sortBy[ e | PackageJsonUtils.getSourceContainerTypeStringRepresentation(e.key) ] - // group by source container type - .groupBy[ e | e.key ] - // add source container sub-section for each specified source container type - .forEach[containerType, paths| - val JSONArray typeSectionArray = JSONFactory.eINSTANCE.createJSONArray(); - JSONModelUtils.addProperty(sourcesSection, - PackageJsonUtils.getSourceContainerTypeStringRepresentation(containerType), typeSectionArray); - val pathLiterals = paths.map[pathEntry | JSONModelUtils.createStringLiteral(pathEntry.value) ]; - typeSectionArray.getElements().addAll(pathLiterals); - ]; - } - - // add output folder - if (output.present) - JSONModelUtils.addProperty(n4jsRoot, OUTPUT.name, output.get()); - - // add provided and required runtime libraries if given - if (!providedRL.empty) { - JSONModelUtils.addProperty(n4jsRoot, PROVIDED_RUNTIME_LIBRARIES.name, - JSONModelUtils.createStringArray(providedRL)); - } - if (!requiredRL.empty) { - JSONModelUtils.addProperty(n4jsRoot, REQUIRED_RUNTIME_LIBRARIES.name, - JSONModelUtils.createStringArray(requiredRL)); - } - - if (extendedRE.isPresent) { - JSONModelUtils.addProperty(n4jsRoot, EXTENDED_RUNTIME_ENVIRONMENT.name, - extendedRE.get()); - } - if (implementationId.isPresent) { - JSONModelUtils.addProperty(n4jsRoot, IMPLEMENTATION_ID.name, - implementationId.get()); - } - - if (!implementedProjects.empty) { - JSONModelUtils.addProperty(n4jsRoot, IMPLEMENTED_PROJECTS.name, - JSONModelUtils.createStringArray(implementedProjects)); - } - - if (!testedProjects.empty) { - JSONModelUtils.addProperty(n4jsRoot, TESTED_PROJECTS.name, - JSONModelUtils.createStringArray(testedProjects)); - } - - // add "n4js" section (if non-empty) - if (!n4jsRoot.nameValuePairs.empty) { - JSONModelUtils.addProperty(root, N4JS.name, n4jsRoot); - } - - // finally serialize as JSONDocument - val JSONDocument document = JSONFactory.eINSTANCE.createJSONDocument(); - document.setContent(root); - - return document; - } - - private def static JSONObject createDependenciesValue(Map dependencies) { - val JSONObject dependenciesValue = JSONFactory.eINSTANCE.createJSONObject(); - dependenciesValue.nameValuePairs.addAll(dependencies.entrySet.map [ e | - val pair = JSONFactory.eINSTANCE.createNameValuePair(); - pair.name = e.key; - pair.value = JSONModelUtils.createStringLiteral(e.value); - return pair; - ]); - return dependenciesValue; - } -}