Skip to content

Commit

Permalink
Move "inline parser assistance" to "junk" package for some incubation
Browse files Browse the repository at this point in the history
  • Loading branch information
hugithordarson committed Oct 21, 2024
1 parent 161b7f9 commit 276f73c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@
import java.util.Map.Entry;
import java.util.Objects;

import ng.appserver.NGActionResults;
import ng.appserver.NGApplication;
import ng.appserver.NGApplication.NGElementNotFoundException;
import ng.appserver.NGAssociation;
import ng.appserver.NGAssociationFactory;
import ng.appserver.NGComponentReference;
import ng.appserver.NGContext;
import ng.appserver.NGElement;
import ng.appserver.NGRequest;
import ng.appserver.NGResponse;
import ng.appserver.elements.NGDynamicGroup;
import ng.appserver.elements.NGHTMLBareString;
import ng.appserver.elements.NGHTMLCommentString;
import ng.appserver.templating.NGDeclaration.NGBindingValue;
import x.junk.NGElementNotFoundElement;

/**
* Serves as a bridge between the "new and old world" for template parsing
Expand Down Expand Up @@ -63,27 +60,7 @@ private static NGElement toDynamicElement( final NGDynamicHTMLTag tag ) throws N
// Very experimental functionality at the moment and definitely does not belong with the parser part of the framework.
// But since it's definitely something we want to add ad some point, I'm keeping it for reference
// Hugi 2024-10-19
return new NGElement() {
@Override
public void appendToResponse( NGResponse response, NGContext context ) {
String s = """
<a href="%s" style="padding: 10px; border: 2px solid rgba(50,50,200,0.6); box-shadow: 4px 4px 1px red; background-color: rgba(0,0,200,0.5); border-radius: 4px; text-decoration: none; color: white">
Can't find an element/component '<strong>%s</strong>'. Would you like to create it?
</a>
""".formatted( context.componentActionURL(), tag.declaration().type() );
response.appendContentString( s );
};

@Override
public NGActionResults invokeAction( NGRequest request, NGContext context ) {

if( context.currentElementIsSender() ) {
System.out.println( "Let's create that component!" );
}

return NGElement.super.invokeAction( request, context );
}
};
return new NGElementNotFoundElement( tag.declaration().type() );
}
}

Expand Down
96 changes: 96 additions & 0 deletions ng-appserver/src/main/java/x/junk/NGElementNotFoundElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package x.junk;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.Objects;

import ng.appserver.NGActionResults;
import ng.appserver.NGApplication;
import ng.appserver.NGContext;
import ng.appserver.NGElement;
import ng.appserver.NGRequest;
import ng.appserver.NGResponse;

public class NGElementNotFoundElement implements NGElement {

private String _type;

public NGElementNotFoundElement( final String type ) {
_type = type;
}

@Override
public void appendToResponse( NGResponse response, NGContext context ) {
String s = """
<a href="%s" style="padding: 10px; border: 2px solid rgba(50,50,200,0.6); box-shadow: 4px 4px 1px red; background-color: rgba(0,0,200,0.5); border-radius: 4px; text-decoration: none; color: white">
Can't find an element/component '<strong>%s</strong>'. Would you like to create it?
</a>
""".formatted( context.componentActionURL() + "?elementName=" + _type, _type );

response.appendContentString( s );
};

@Override
public NGActionResults invokeAction( NGRequest request, NGContext context ) {

if( context.currentElementIsSender() ) {
System.out.println( "Let's create that component!" );

final String packageName = NGApplication.application().getClass().getPackageName() + ".elements";
final String elementName = request.formValueForKey( "elementName" );
final String elementString = """
package %s;
import ng.appserver.NGContext;
import ng.appserver.NGElement;
import ng.appserver.NGResponse;
/**
* Your new element
*/
public class %s implements NGElement {
@Override
public void appendToResponse( final NGResponse response, final NGContext context ) {
response.appendContentString( "Congratulations on your new element!" );
}
}
""".formatted( packageName, elementName, elementName );

final Path root = Paths.get( "." ).normalize().toAbsolutePath();
final Path dirPath = Paths.get( root.toString(), "src", "main", "java", "smu", "elements" ); // FIXME: Split the package name elements into corresponding directories
final Path filePath = Paths.get( dirPath.toString(), elementName + ".java" ); // FIXME: Split the package name elements into corresponding directories

System.out.println( "Creating new dynamic element at %s".formatted( filePath ) );

try {
Files.createDirectories( dirPath );
Files.write( filePath, elementString.getBytes(), StandardOpenOption.CREATE_NEW );
touch( Paths.get( root.toString(), ".project" ) );
}
catch( IOException e ) {
throw new UncheckedIOException( e );
}
}

return NGElement.super.invokeAction( request, context );
}

public static void touch( final Path path ) throws IOException {
Objects.requireNonNull( path, "path is null" );
try {
Files.createFile( path );
}
catch( FileAlreadyExistsException e ) {
Files.setLastModifiedTime( path, FileTime.from( Instant.now() ) );
}
}
};

0 comments on commit 276f73c

Please sign in to comment.