/* * Copyright 2008 Codehaus * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.codehaus.mojo.nsis; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import java.text.MessageFormat; import java.util.Date; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.project.MavenProject; import org.codehaus.plexus.util.IOUtil; import org.codehaus.plexus.util.StringUtils; /** * Generate a <code>project.nsh</code> containing NSIS variables that represent the common * POM values. * * <p> * The resulting <code>target/project.nsh</code> (configurable) can then be loaded * into the main <code>setup.nsi</code> script via the include command. * </p> * * <pre> * !include target\project.nsh * </pre> * * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a> * @version $Id$ * * @goal generate-project * @phase generate-sources */ public class NsisGenerateProjectMojo extends AbstractMojo { /** * The name of the project script. * * @parameter expression="${nsis.project.script}" * default-value="${project.build.directory}/project.nsh" * @required */ private File projectScript; /** * The maven project itself. * * @parameter expression="${project}" * @required * @readonly */ private MavenProject project; public void execute() throws MojoExecutionException, MojoFailureException { FileWriter filewriter = null; MessageWriter out = null; try { File parentDir = projectScript.getParentFile(); if ( ( parentDir != null ) && ( parentDir.exists() == false ) ) { if ( parentDir.mkdirs() == false ) { throw new MojoFailureException( "Unable to create parent directory " + parentDir.getAbsolutePath() + " for setup script file " + projectScript.getAbsolutePath() ); } } filewriter = new FileWriter( projectScript ); out = new MessageWriter( filewriter ); out.println( "; Template for project details" ); out.println( "; Generated by {0} from pom.xml version {1}", System.getProperty( "user.name" ), project .getVersion() ); out.println( "; on date {0,date}, time {0,time}", new Date() ); out.println( "" ); out.println( "!define PROJECT_BASEDIR \"{0}\"", project.getBasedir() ); out.println( "!define PROJECT_BUILD_DIR \"{0}\"", project.getBuild().getDirectory() ); out.println( "!define PROJECT_FINAL_NAME \"{0}\"", project.getBuild().getFinalName() ); // TODO: have variable for license? // TODO: how do we deal with multiple licenses? out.println( "!define PROJECT_GROUP_ID \"{0}\"", project.getGroupId() ); out.println( "!define PROJECT_ARTIFACT_ID \"{0}\"", project.getArtifactId() ); out.println( "!define PROJECT_NAME \"{0}\"", project.getName() ); out.println( "!define PROJECT_VERSION \"{0}\"", project.getVersion() ); if ( StringUtils.isNotEmpty( project.getUrl() ) ) { out.println( "!define PROJECT_URL \"{0}\"", project.getUrl() ); } if ( project.getOrganization() != null ) { out.println( "!define PROJECT_ORGANIZATION_NAME \"{0}\"", project.getOrganization().getName() ); out.println( "!define PROJECT_ORGANIZATION_URL \"{0}\"", project.getOrganization().getUrl() ); out.println( "!define PROJECT_REG_KEY \"SOFTWARE\\{0}\\{1}\\{2}\"", new Object[] { project.getOrganization().getName(), project.getName(), project.getVersion() } ); out.println( "!define PROJECT_REG_UNINSTALL_KEY " + "\"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{0} {1}\"", project.getName(), project .getVersion() ); out.println( "!define PROJECT_STARTMENU_FOLDER \"{0}\\{1}\\{2} {3}\"", new Object[] { "${SMPROGRAMS}", project.getOrganization().getName(), project.getName(), project.getVersion() } ); } else { out.println( "; The project organization section is missing from your pom.xml" ); } } catch ( IOException e ) { throw new MojoExecutionException( "Unable to generate project script " + projectScript.getAbsolutePath(), e ); } finally { IOUtil.close( out ); IOUtil.close( filewriter ); } } class MessageWriter extends PrintWriter { public MessageWriter( Writer out ) { super( out ); } public void println( String pattern, Object arguments[] ) { println( MessageFormat.format( pattern, arguments ) ); } public void println( String pattern, Object arg1 ) { println( pattern, new Object[] { arg1 } ); } public void println( String pattern, Object arg1, Object arg2 ) { println( pattern, new Object[] { arg1, arg2 } ); } } }