|
28 | 28 | import java.util.Optional;
|
29 | 29 | import java.util.Set;
|
30 | 30 | import java.util.function.Consumer;
|
31 |
| -import java.util.stream.Collectors; |
32 | 31 |
|
33 | 32 | import org.eclipse.esmf.aspectmodel.edit.AspectChangeManager;
|
34 | 33 | import org.eclipse.esmf.aspectmodel.edit.AspectChangeManagerConfig;
|
|
37 | 36 | import org.eclipse.esmf.aspectmodel.edit.ChangeReportFormatter;
|
38 | 37 | import org.eclipse.esmf.aspectmodel.generator.LanguageCollector;
|
39 | 38 | import org.eclipse.esmf.aspectmodel.generator.diagram.AspectModelDiagramGenerator;
|
40 |
| -import org.eclipse.esmf.aspectmodel.loader.AspectModelLoader; |
41 |
| -import org.eclipse.esmf.aspectmodel.resolver.ExternalResolverStrategy; |
42 |
| -import org.eclipse.esmf.aspectmodel.resolver.FileSystemStrategy; |
43 |
| -import org.eclipse.esmf.aspectmodel.resolver.ModelResolutionException; |
44 |
| -import org.eclipse.esmf.aspectmodel.resolver.ResolutionStrategy; |
45 |
| -import org.eclipse.esmf.aspectmodel.resolver.fs.StructuredModelsRoot; |
46 |
| -import org.eclipse.esmf.aspectmodel.resolver.github.GitHubStrategy; |
47 | 39 | import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
|
48 |
| -import org.eclipse.esmf.aspectmodel.shacl.violation.Violation; |
49 |
| -import org.eclipse.esmf.aspectmodel.validation.services.AspectModelValidator; |
50 |
| -import org.eclipse.esmf.aspectmodel.validation.services.DetailedViolationFormatter; |
51 |
| -import org.eclipse.esmf.aspectmodel.validation.services.ViolationFormatter; |
52 | 40 | import org.eclipse.esmf.exception.CommandException;
|
53 | 41 | import org.eclipse.esmf.metamodel.Aspect;
|
54 | 42 | import org.eclipse.esmf.metamodel.AspectModel;
|
55 | 43 |
|
56 |
| -import io.vavr.control.Either; |
57 |
| -import org.apache.commons.io.FilenameUtils; |
58 |
| - |
59 | 44 | @SuppressWarnings( "UseOfSystemOutOrSystemErr" )
|
60 | 45 | public abstract class AbstractCommand implements Runnable {
|
61 |
| - protected Path modelsRootForFile( final File file ) { |
62 |
| - return file.toPath().getParent().getParent().getParent(); |
63 |
| - } |
| 46 | + private boolean details; |
| 47 | + private ResolverConfigurationMixin resolverConfig; |
64 | 48 |
|
65 |
| - protected AspectModel loadAspectModelOrFail( final String input, final ResolverConfigurationMixin resolverConfig ) { |
66 |
| - return loadAspectModelOrFail( input, resolverConfig, false ); |
| 49 | + protected void setDetails( final boolean details ) { |
| 50 | + this.details = details; |
67 | 51 | }
|
68 | 52 |
|
69 |
| - protected File getInputFile( final String modelFileName ) { |
70 |
| - final File inputFile = new File( modelFileName ); |
71 |
| - return inputFile.isAbsolute() |
72 |
| - ? inputFile |
73 |
| - : Path.of( System.getProperty( "user.dir" ) ).resolve( inputFile.toPath() ).toFile().getAbsoluteFile(); |
| 53 | + protected void setResolverConfig( final ResolverConfigurationMixin resolverConfig ) { |
| 54 | + this.resolverConfig = resolverConfig; |
74 | 55 | }
|
75 | 56 |
|
76 |
| - protected AspectModelLoader getAspectModelLoader( final Optional<File> modelFile, final ResolverConfigurationMixin resolverConfig ) { |
77 |
| - final List<ResolutionStrategy> strategies = new ArrayList<>(); |
78 |
| - if ( modelFile.isPresent() ) { |
79 |
| - strategies.add( new FileSystemStrategy( modelsRootForFile( modelFile.get().getAbsoluteFile() ) ) ); |
80 |
| - } else { |
81 |
| - strategies.add( AspectModelLoader.DEFAULT_STRATEGY.get() ); |
82 |
| - } |
83 |
| - for ( final String modelsRoot : resolverConfig.modelsRoots ) { |
84 |
| - strategies.add( new FileSystemStrategy( new StructuredModelsRoot( Path.of( modelsRoot ) ) ) ); |
85 |
| - } |
86 |
| - if ( !resolverConfig.commandLine.isBlank() ) { |
87 |
| - strategies.add( new ExternalResolverStrategy( resolverConfig.commandLine ) ); |
88 |
| - } |
89 |
| - if ( resolverConfig.gitHubResolutionOptions != null && resolverConfig.gitHubResolutionOptions.enableGitHubResolution ) { |
90 |
| - strategies.add( new GitHubStrategy( |
91 |
| - resolverConfig.gitHubResolutionOptions.gitHubName, |
92 |
| - resolverConfig.gitHubResolutionOptions.gitHubBranch, |
93 |
| - resolverConfig.gitHubResolutionOptions.gitHubDirectory ) ); |
94 |
| - } |
95 |
| - return new AspectModelLoader( strategies ); |
| 57 | + protected boolean inputIsFile( final String input ) { |
| 58 | + return new File( input ).exists(); |
96 | 59 | }
|
97 | 60 |
|
98 |
| - protected AspectModel loadAspectModelOrFail( final File modelFile, final ResolverConfigurationMixin resolverConfig, |
99 |
| - final boolean details ) { |
100 |
| - final File absoluteFile = modelFile.getAbsoluteFile(); |
101 |
| - |
102 |
| - final Either<List<Violation>, AspectModel> validModelOrViolations = new AspectModelValidator().loadModel( () -> |
103 |
| - getAspectModelLoader( Optional.of( modelFile ), resolverConfig ).load( absoluteFile ) ); |
104 |
| - if ( validModelOrViolations.isLeft() ) { |
105 |
| - final List<Violation> violations = validModelOrViolations.getLeft(); |
106 |
| - if ( details ) { |
107 |
| - System.out.println( new DetailedViolationFormatter().apply( violations ) ); |
108 |
| - } else { |
109 |
| - System.out.println( new ViolationFormatter().apply( violations ) ); |
110 |
| - } |
111 |
| - System.exit( 1 ); |
112 |
| - return null; |
113 |
| - } |
114 |
| - |
115 |
| - return validModelOrViolations.get(); |
| 61 | + protected File absoluteFile( final File inputFile ) { |
| 62 | + return inputFile.isAbsolute() |
| 63 | + ? inputFile |
| 64 | + : Path.of( System.getProperty( "user.dir" ) ).resolve( inputFile.toPath() ).toFile().getAbsoluteFile(); |
116 | 65 | }
|
117 | 66 |
|
118 |
| - protected AspectModel loadAspectModelOrFail( final String modelFileName, final ResolverConfigurationMixin resolverConfig, |
119 |
| - final boolean details ) { |
120 |
| - return loadAspectModelOrFail( getInputFile( modelFileName ), resolverConfig, details ); |
| 67 | + protected InputHandler getInputHandler( final File input ) { |
| 68 | + return new FileInputHandler( input.getAbsolutePath(), resolverConfig, details ); |
121 | 69 | }
|
122 | 70 |
|
123 |
| - protected Aspect loadAspectOrFail( final String modelFileName, final ResolverConfigurationMixin resolverConfig ) { |
124 |
| - final File inputFile = new File( modelFileName ); |
125 |
| - final AspectModel aspectModel = loadAspectModelOrFail( modelFileName, resolverConfig ); |
126 |
| - final List<Aspect> aspects = aspectModel.aspects(); |
127 |
| - if ( aspects.isEmpty() ) { |
128 |
| - throw new CommandException( new ModelResolutionException( "No Aspects were found in the model" ) ); |
129 |
| - } |
130 |
| - if ( aspects.size() == 1 ) { |
131 |
| - return aspectModel.aspect(); |
| 71 | + protected InputHandler getInputHandler( final String input ) { |
| 72 | + if ( FileInputHandler.appliesToInput( input ) ) { |
| 73 | + return new FileInputHandler( input, resolverConfig, details ); |
| 74 | + } else if ( AspectModelUrnInputHandler.appliesToInput( input ) ) { |
| 75 | + return new AspectModelUrnInputHandler( input, resolverConfig, details ); |
| 76 | + } else if ( GitHubUrlInputHandler.appliesToInput( input ) ) { |
| 77 | + return new GitHubUrlInputHandler( input, resolverConfig, details ); |
132 | 78 | }
|
133 |
| - final String expectedAspectName = FilenameUtils.removeExtension( inputFile.getName() ); |
134 |
| - return aspectModel.aspects().stream() |
135 |
| - .filter( aspect -> aspect.getName().equals( expectedAspectName ) ) |
136 |
| - .findFirst() |
137 |
| - .orElseThrow( () -> new ModelResolutionException( |
138 |
| - "Found multiple Aspects in the file " + inputFile.getAbsolutePath() + ", but none is called '" |
139 |
| - + expectedAspectName + "': " + aspects.stream().map( Aspect::getName ) |
140 |
| - .collect( Collectors.joining( ", " ) ) ) ); |
| 79 | + throw new CommandException( "Can not find file: " + input ); |
141 | 80 | }
|
142 | 81 |
|
143 |
| - protected void generateDiagram( final String inputFileName, final AspectModelDiagramGenerator.Format targetFormat, |
144 |
| - final String outputFileName, |
145 |
| - final String languageTag, final ResolverConfigurationMixin resolverConfig ) throws IOException { |
146 |
| - final Aspect aspect = loadAspectOrFail( inputFileName, resolverConfig ); |
| 82 | + protected void generateDiagram( final String input, final AspectModelDiagramGenerator.Format targetFormat, |
| 83 | + final String outputFileName, final String languageTag ) throws IOException { |
| 84 | + final Aspect aspect = getInputHandler( input ).loadAspect(); |
147 | 85 | final AspectModelDiagramGenerator generator = new AspectModelDiagramGenerator( aspect );
|
148 | 86 | final Set<AspectModelDiagramGenerator.Format> targetFormats = new HashSet<>();
|
149 | 87 | targetFormats.add( targetFormat );
|
@@ -173,8 +111,8 @@ protected OutputStream getStreamForFile( final String outputFileName ) {
|
173 | 111 | ensureDirectoryExists( outputFileName );
|
174 | 112 | try {
|
175 | 113 | return new FileOutputStream( outputFileName );
|
176 |
| - } catch ( final FileNotFoundException e ) { |
177 |
| - throw new CommandException( e ); |
| 114 | + } catch ( final FileNotFoundException exception ) { |
| 115 | + throw new CommandException( exception ); |
178 | 116 | }
|
179 | 117 | } else {
|
180 | 118 | return new ProtectedOutputStream( System.out );
|
|
0 commit comments