|
| 1 | +package com.minres.coredsl.validator |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.util.concurrent.Callable |
| 5 | +import org.apache.log4j.Logger |
| 6 | +import picocli.CommandLine.Parameters |
| 7 | +import picocli.CommandLine.Option |
| 8 | +import picocli.CommandLine |
| 9 | +import com.google.inject.Inject |
| 10 | +import com.google.inject.Provider |
| 11 | +import org.eclipse.emf.ecore.resource.ResourceSet |
| 12 | +import org.eclipse.xtext.validation.IResourceValidator |
| 13 | +import org.eclipse.xtext.generator.GeneratorDelegate |
| 14 | +import org.eclipse.xtext.generator.JavaIoFileSystemAccess |
| 15 | +import org.eclipse.emf.mwe.utils.ProjectMapping |
| 16 | +import org.eclipse.emf.mwe.utils.StandaloneSetup |
| 17 | +import org.eclipse.xtext.resource.XtextResourceSet |
| 18 | +import org.eclipse.xtext.resource.XtextResource |
| 19 | +import org.eclipse.xtext.diagnostics.Severity; |
| 20 | +import org.eclipse.xtext.validation.Issue; |
| 21 | +import org.eclipse.xtext.validation.CheckMode; |
| 22 | +import org.eclipse.xtext.util.CancelIndicator; |
| 23 | +import org.eclipse.emf.common.util.URI |
| 24 | +import org.eclipse.xtext.parser.ParseException |
| 25 | +import org.eclipse.xtext.generator.GeneratorContext |
| 26 | +import org.eclipse.xtext.generator.IFileSystemAccess |
| 27 | +import java.lang.reflect.MalformedParametersException |
| 28 | +import org.apache.log4j.Level |
| 29 | +import org.eclipse.emf.ecore.resource.Resource |
| 30 | +import org.eclipse.emf.ecore.resource.Resource.Diagnostic |
| 31 | +import com.minres.coredsl.CoreDslStandaloneSetup |
| 32 | +import java.util.ArrayList |
| 33 | + |
| 34 | +class Main implements Callable<Integer> { |
| 35 | + |
| 36 | + @Parameters(paramLabel="input_file", arity="1..*", description="one ore more files to process") |
| 37 | + public File[] files; |
| 38 | + |
| 39 | + @Option(names=#["-r", "--repository"], description="one ore more repository directories") |
| 40 | + public String[] repositories; |
| 41 | + |
| 42 | + @Option(names=#["-v", "--verbose"], description="verbose output") |
| 43 | + public Boolean verbose = false; |
| 44 | + |
| 45 | + def static main(String[] args) { |
| 46 | + val ret = new CommandLine(new Main()).execute(args); |
| 47 | + System.exit(ret) |
| 48 | + } |
| 49 | + |
| 50 | + override Integer call() throws Exception { |
| 51 | + Logger.rootLogger.level = verbose ? Level.DEBUG : Level.INFO |
| 52 | + val injector = new CoreDslValidatorSetup().createInjectorAndDoEMFRegistration |
| 53 | + injector.getInstance(ValidatorMain).run(this) |
| 54 | + return 0; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +class ValidatorMain { |
| 59 | + |
| 60 | + static val logger = Logger.getLogger(typeof(ValidatorMain)); |
| 61 | + |
| 62 | + @Inject Provider<ResourceSet> resourceSetProvider |
| 63 | + |
| 64 | + @Inject IResourceValidator validator |
| 65 | + |
| 66 | + def run(Main params) { |
| 67 | + params.repositories.forEach[repository, idx| |
| 68 | + val projectMapping = new ProjectMapping |
| 69 | + projectMapping.projectName = "Repository:"+idx |
| 70 | + projectMapping.path = repository |
| 71 | + new StandaloneSetup().addProjectMapping(projectMapping) |
| 72 | + ] |
| 73 | + try { |
| 74 | + for (file : params.files) { |
| 75 | + // Load the resource |
| 76 | + val resourceSet = resourceSetProvider.get as XtextResourceSet |
| 77 | + resourceSet.addLoadOption(XtextResource.OPTION_RESOLVE_ALL, Boolean.TRUE); |
| 78 | + val resource = resourceSet.getResource(URI.createFileURI(file.absolutePath), true) |
| 79 | + // Validate the resource |
| 80 | + val issues = new ArrayList<Issue> |
| 81 | + for (Diagnostic diag : resource.getErrors()) { |
| 82 | + val issue = new Issue.IssueImpl(); |
| 83 | + issue.setSeverity(Severity.ERROR); |
| 84 | + issue.setMessage(diag.getMessage()); |
| 85 | + issue.setLineNumber(diag.getLine()); |
| 86 | + issue.setUriToProblem(resource.getURI()); |
| 87 | + issues.add(issue); |
| 88 | + } |
| 89 | + if (issues.isEmpty()) |
| 90 | + issues += validator.validate(resource, CheckMode.ALL, CancelIndicator.NullImpl); |
| 91 | + |
| 92 | + if (!issues.empty) { |
| 93 | + val errors = issues.filter[it.severity == Severity.ERROR] |
| 94 | + val warnings = issues.filter[it.severity == Severity.WARNING] |
| 95 | + if (!errors.empty) { |
| 96 | + logger.error("Error validating " + resource.URI) |
| 97 | + issues.forEach[logger.error(it)] |
| 98 | + throw new ParseException("error validating " + resource.URI) |
| 99 | + } else if (!warnings.empty) { |
| 100 | + logger.warn("There are warnings validating " + resource.URI) |
| 101 | + issues.forEach[logger.warn(it)] |
| 102 | + } |
| 103 | + } |
| 104 | + logger.info('Code validation for ' + file + ' finished') |
| 105 | + } |
| 106 | + } catch (MalformedParametersException | IllegalArgumentException | ParseException e) { |
| 107 | + logger.error("Command line error " + e.message, e) |
| 108 | + return -1 |
| 109 | + } |
| 110 | + return 0 |
| 111 | + } |
| 112 | +} |
0 commit comments