eu.cedarsoft.devtools.modules.AddEmptySiteDescriptorModule Maven / Gradle / Ivy
The newest version!
package eu.cedarsoft.devtools.modules;
import com.google.inject.Inject;
import eu.cedarsoft.devtools.MavenMessageHandler;
import eu.cedarsoft.devtools.MavenSupport;
import eu.cedarsoft.devtools.Module;
import eu.cedarsoft.utils.ApplicationHomeAccess;
import eu.cedarsoft.utils.CmdLine;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Creates a directors for the site and adds an empty site descriptor.
*/
public class AddEmptySiteDescriptorModule implements Module {
@NotNull
private final MavenSupport mavenSupport;
@NotNull
private final ApplicationHomeAccess applicationHomeAccess;
@NotNull
@NonNls
private static final String SITE_EMPTY_XML = "site_empty.xml";
@NotNull
@NonNls
private static final String SITE_DEFAULT_XML = "site_default.xml";
@Inject
public AddEmptySiteDescriptorModule( @NotNull MavenSupport mavenSupport, @NotNull ApplicationHomeAccess applicationHomeAccess ) {
this.mavenSupport = mavenSupport;
this.applicationHomeAccess = applicationHomeAccess;
}
@NotNull
public String getDescription() {
return MavenMessageHandler.get( "module.add.empty.site.descriptor" );
}
public void process( @NotNull CmdLine cmdLine ) throws Exception {
cmdLine.out( MavenMessageHandler.get( "creating.empty.site.descriptor" ) );
File currentDir = new File( "." );
createEmptySiteXml( cmdLine, currentDir );
//Now check whether the children have poms too
for ( File childDir : currentDir.listFiles( ( FileFilter ) DirectoryFileFilter.DIRECTORY ) ) {
if ( !mavenSupport.doesPomExist( childDir ) ) {
continue;
}
createEmptySiteXml( cmdLine, childDir );
}
cmdLine.success( MavenMessageHandler.get( "module.processed.successfully" ) );
}
private void createEmptySiteXml( @NotNull CmdLine cmdLine, @NotNull File currentDir ) throws IOException {
File siteXml = mavenSupport.getSiteXml( currentDir );
if ( siteXml.exists() ) {
cmdLine.warning( MavenMessageHandler.get( "site.xml.still.exists", currentDir.getAbsolutePath() ) );
} else {
FileUtils.copyURLToFile( getSiteResource(), siteXml );
cmdLine.warning( MavenMessageHandler.get( "create.site.xml", currentDir.getAbsolutePath() ) );
//Create directories
File siteDir = mavenSupport.getSiteDir( currentDir );
new File( siteDir, MavenSupport.RESOURCES_DIR_NAME ).mkdir();
new File( siteDir, "apt" ).mkdir();
new File( siteDir, "xdoc" ).mkdir();
}
}
@NotNull
private URL getSiteResource() throws MalformedURLException {
File defaultSite = new File( applicationHomeAccess.getApplicationHome(), SITE_DEFAULT_XML );
if ( defaultSite.exists() ) {
return defaultSite.toURI().toURL();
}
return getClass().getResource( SITE_EMPTY_XML );
}
}