Skip to content

Commit 4284690

Browse files
committed
Switch for different variants in AspectEditMoveCommand
1 parent c1e05b8 commit 4284690

File tree

3 files changed

+74
-22
lines changed

3 files changed

+74
-22
lines changed

core/esmf-aspect-model-serializer/src/main/java/org/eclipse/esmf/aspectmodel/serializer/AspectSerializer.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public String apply( final Aspect aspect ) {
7272
}
7373

7474
/**
75-
* Serializes all files of an Aspect Model to their respective source locations
75+
* Serializes all files of an Aspect Model to their respective source locations.
76+
* <b>Attention:</b> This method does not check validity of paths or existance of files and will overwrite without further checks.
7677
*
7778
* @param aspectModel the Aspect Model
7879
*/
@@ -81,11 +82,13 @@ public void write( final AspectModel aspectModel ) {
8182
}
8283

8384
/**
84-
* Writes the content of an Aspect Model file to its defined source location
85+
* Returns a URL for the Aspect Model file if it can be determined.
8586
*
86-
* @param aspectModelFile the Aspect Model file
87+
* @param aspectModelFile the input Aspect Model file
88+
* @return the Aspect Model file's location as URL
89+
* @throws SerializationException if the file has no source location or the source location URI is no URL
8790
*/
88-
public void write( final AspectModelFile aspectModelFile ) {
91+
public URL aspectModelFileUrl( final AspectModelFile aspectModelFile ) {
8992
if ( aspectModelFile.sourceLocation().isEmpty() ) {
9093
throw new SerializationException( "Aspect Model file has no source location" );
9194
}
@@ -98,13 +101,23 @@ public void write( final AspectModelFile aspectModelFile ) {
98101
throw new SerializationException( "Aspect Model file can only be written to locations given by URLs" );
99102
}
100103

104+
return url;
105+
}
106+
107+
/**
108+
* Writes the content of an Aspect Model file to its defined source location
109+
*
110+
* @param aspectModelFile the Aspect Model file
111+
*/
112+
public void write( final AspectModelFile aspectModelFile ) {
113+
final URL url = aspectModelFileUrl( aspectModelFile );
101114
final Function<URI, OutputStream> protocolHandler = protocolHandlers.get( url.getProtocol() );
102115
if ( protocolHandler == null ) {
103116
throw new SerializationException( "Don't know how to write " + url.getProtocol() + " URLs: " + url );
104117
}
105118

106119
final String content = aspectModelFileToString( aspectModelFile );
107-
try ( final OutputStream out = protocolHandler.apply( uri ) ) {
120+
try ( final OutputStream out = protocolHandler.apply( aspectModelFile.sourceLocation().get() ) ) {
108121
out.write( content.getBytes( StandardCharsets.UTF_8 ) );
109122
} catch ( final IOException exception ) {
110123
throw new SerializationException( exception );

tools/samm-cli/src/main/java/org/eclipse/esmf/AbstractCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
import org.apache.commons.io.FilenameUtils;
4747

4848
public abstract class AbstractCommand implements Runnable {
49-
private Path modelsRootForFile( final File file ) {
49+
protected Path modelsRootForFile( final File file ) {
5050
return file.toPath().getParent().getParent().getParent();
5151
}
5252

tools/samm-cli/src/main/java/org/eclipse/esmf/aspect/edit/AspectEditMoveCommand.java

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.nio.file.Paths;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Optional;
2223
import java.util.stream.Collectors;
2324

2425
import org.eclipse.esmf.AbstractCommand;
@@ -34,7 +35,9 @@
3435
import org.eclipse.esmf.aspectmodel.edit.ChangeReport;
3536
import org.eclipse.esmf.aspectmodel.edit.ChangeReportFormatter;
3637
import org.eclipse.esmf.aspectmodel.edit.change.MoveElementToExistingFile;
38+
import org.eclipse.esmf.aspectmodel.edit.change.MoveElementToNewFile;
3739
import org.eclipse.esmf.aspectmodel.serializer.AspectSerializer;
40+
import org.eclipse.esmf.aspectmodel.urn.AspectModelUrn;
3841
import org.eclipse.esmf.exception.CommandException;
3942
import org.eclipse.esmf.metamodel.AspectModel;
4043
import org.eclipse.esmf.metamodel.ModelElement;
@@ -111,16 +114,26 @@ public class AspectEditMoveCommand extends AbstractCommand {
111114

112115
@Override
113116
public void run() {
114-
final File file = new File( targetFile );
115-
if ( file.exists() ) {
116-
if ( targetNamespace == null ) {
117+
final String input = parentCommand.parentCommand.getInput();
118+
119+
if ( targetNamespace == null ) {
120+
final File targetFileRelativeToInput = getInputFile( input ).toPath().getParent().resolve( targetFile ).toFile();
121+
if ( targetFileRelativeToInput.exists() ) {
117122
moveElementToExistingFile();
118123
} else {
119-
moveElementToOtherNamespaceExistingFile();
124+
moveElementToNewFile();
120125
}
121126
} else {
122-
if ( targetNamespace == null ) {
123-
moveElementToNewFile();
127+
final File inputFile = getInputFile( input );
128+
final AspectModelUrn targetNamespaceUrn = AspectModelUrn.from( targetNamespace )
129+
.getOrElseThrow( () -> new CommandException( "Target namespace is invalid: " + targetNamespace ) );
130+
final File targetFileInNewNamespace = modelsRootForFile( inputFile )
131+
.resolve( targetNamespaceUrn.getNamespaceMainPart() )
132+
.resolve( targetNamespaceUrn.getVersion() )
133+
.resolve( targetFile )
134+
.toFile();
135+
if ( targetFileInNewNamespace.exists() ) {
136+
moveElementToOtherNamespaceExistingFile();
124137
} else {
125138
moveElementToOtherNamespaceNewFile();
126139
}
@@ -131,7 +144,21 @@ public void run() {
131144
* Supports the case {@code samm aspect Aspect.ttl edit move :MyAspect newFile.ttl}
132145
*/
133146
private void moveElementToNewFile() {
134-
// TODO
147+
final String input = parentCommand.parentCommand.getInput();
148+
final AspectModel aspectModel = loadAspectModelOrFail( input, customResolver );
149+
150+
// Do refactoring
151+
final ModelElement modelElement = determineModelElementToMove( aspectModel );
152+
final URI targetFileUri = getInputFile( input ).toPath().getParent().resolve( targetFile ).toUri();
153+
final List<String> headerCommentForNewFile = copyHeader
154+
? modelElement.getSourceFile().headerComment()
155+
: List.of();
156+
final Change move = new MoveElementToNewFile( modelElement, headerCommentForNewFile, Optional.of( targetFileUri ) );
157+
final AspectChangeContext changeContext = performRefactoring( aspectModel, move );
158+
159+
// Check & write changes to file system
160+
checkFilesystemConsistency( changeContext );
161+
performFileSystemWrite( changeContext );
135162
}
136163

137164
/**
@@ -160,13 +187,27 @@ private void moveElementToExistingFile() {
160187
final AspectModel aspectModel = AspectModelBuilder.merge( sourceAspectModel, targetAspectModel );
161188

162189
// Find the loaded AspectModelFile that corresponds to the input targetFile
190+
final AspectModelFile targetAspectModelFile = determineTargetAspectModelFile( aspectModel );
191+
192+
// Do refactoring
193+
final ModelElement modelElement = determineModelElementToMove( aspectModel );
194+
final Change move = new MoveElementToExistingFile( modelElement, targetAspectModelFile );
195+
final AspectChangeContext changeContext = performRefactoring( aspectModel, move );
196+
197+
// Check & write changes to file system
198+
checkFilesystemConsistency( changeContext );
199+
performFileSystemWrite( changeContext );
200+
}
201+
202+
private AspectModelFile determineTargetAspectModelFile( final AspectModel aspectModel ) {
163203
final URI targetFileUri = getInputFile( targetFile ).toURI();
164-
final AspectModelFile targetAspectModelFile = aspectModel.files().stream()
204+
return aspectModel.files().stream()
165205
.filter( file -> file.sourceLocation().map( uri -> uri.equals( targetFileUri ) ).orElse( false ) )
166206
.findFirst()
167207
.orElseThrow( () -> new CommandException( "Could not determine target file" ) );
208+
}
168209

169-
// Determine the URN for the element to move
210+
private ModelElement determineModelElementToMove( final AspectModel aspectModel ) {
170211
final List<ModelElement> potentialElements = aspectModel.elements().stream()
171212
.filter( element -> !element.isAnonymous() )
172213
.filter( element -> element.urn().toString().endsWith( elementName ) )
@@ -183,24 +224,22 @@ private void moveElementToExistingFile() {
183224
+ "\nPlease use the element's full URN." );
184225
System.exit( 1 );
185226
}
227+
return potentialElements.get( 0 );
228+
}
186229

187-
// Perform the refactoring
230+
private AspectChangeContext performRefactoring( final AspectModel aspectModel, final Change change ) {
188231
final AspectChangeContextConfig config = AspectChangeContextConfigBuilder.builder()
189232
.detailedChangeReport( details )
190233
.build();
191234
final AspectChangeContext changeContext = new AspectChangeContext( config, aspectModel );
192-
final ModelElement modelElement = potentialElements.get( 0 );
193-
final Change move = new MoveElementToExistingFile( modelElement, targetAspectModelFile );
194-
final ChangeReport changeReport = changeContext.applyChange( move );
235+
final ChangeReport changeReport = changeContext.applyChange( change );
195236
if ( dryRun ) {
196237
System.out.println( "Changes to be performed" );
197238
System.out.println( "=======================" );
198239
System.out.println( ChangeReportFormatter.INSTANCE.apply( changeReport, config ) );
199240
System.exit( 0 );
200241
}
201-
202-
checkFilesystemConsistency( changeContext );
203-
performFileSystemWrite( changeContext );
242+
return changeContext;
204243
}
205244

206245
private void performFileSystemWrite( final AspectChangeContext changeContext ) {

0 commit comments

Comments
 (0)