1 package org.codehaus.mojo.gwt.shell;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FilenameUtils;
23 import org.apache.commons.lang.SystemUtils;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.model.Resource;
26 import org.apache.maven.plugin.MojoExecutionException;
27 import org.apache.maven.plugin.MojoFailureException;
28 import org.apache.maven.plugins.annotations.Component;
29 import org.apache.maven.plugins.annotations.LifecyclePhase;
30 import org.apache.maven.plugins.annotations.Mojo;
31 import org.apache.maven.plugins.annotations.Parameter;
32 import org.codehaus.plexus.util.IOUtil;
33 import org.codehaus.plexus.util.cli.StreamConsumer;
34 import org.sonatype.plexus.build.incremental.BuildContext;
35
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.OutputStreamWriter;
39 import java.nio.charset.Charset;
40
41
42
43
44
45
46
47
48
49
50 @Mojo(name = "css", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
51 public class CSSMojo
52 extends AbstractGwtShellMojo
53 {
54
55
56
57 @Parameter
58 private String[] cssFiles;
59
60
61
62
63 @Parameter
64 private String cssFile;
65
66 @Parameter(property = "project.build.sourceEncoding")
67 private String encoding;
68
69 @Component
70 private BuildContext buildContext;
71
72 @Override
73 protected boolean isGenerator() {
74 return true;
75 }
76
77 public void doExecute()
78 throws MojoExecutionException, MojoFailureException
79 {
80 setup();
81
82
83
84
85 if ( cssFiles != null )
86 {
87 for ( String file : cssFiles )
88 {
89 final String typeName = FilenameUtils.separatorsToSystem( file ).
90 substring( 0, file.lastIndexOf( '.' ) ).replace( File.separatorChar, '.' );
91 final File javaOutput =
92 new File( getGenerateDirectory(), typeName.replace( '.', File.separatorChar ) + ".java" );
93 for ( Resource resource : getProject().getResources() )
94 {
95 final File candidate = new File( resource.getDirectory(), file );
96 if ( candidate.exists() )
97 {
98 if ( buildContext.isUptodate( javaOutput, candidate ) )
99 {
100 getLog().debug( javaOutput.getAbsolutePath() + " is up to date. Generation skipped" );
101 break;
102 }
103
104 getLog().info( "Generating " + javaOutput + " with typeName " + typeName );
105 ensureTargetPackageExists( getGenerateDirectory(), typeName );
106
107
108 try
109 {
110 final StringBuilder content = new StringBuilder();
111 createJavaCommand()
112 .setMainClass( "com.google.gwt.resources.css.InterfaceGenerator" )
113 .addToClasspath( getClasspath( Artifact.SCOPE_COMPILE ) )
114 .arg( "-standalone" )
115 .arg( "-typeName" )
116 .arg( typeName )
117 .arg( "-css" )
118 .arg( candidate.getAbsolutePath() )
119 .addToClasspath( getJarFiles(GWT_DEV, false) )
120 .addToClasspath( getJarFiles(GWT_USER, false) )
121 .setOut( new StreamConsumer()
122 {
123 public void consumeLine( String line )
124 {
125 content.append( line ).append( SystemUtils.LINE_SEPARATOR );
126 }
127 } )
128 .execute();
129 if ( content.length() == 0 )
130 {
131 throw new MojoExecutionException( "cannot generate java source from file " + file + "." );
132 }
133 final OutputStreamWriter outputWriter =
134 new OutputStreamWriter( buildContext.newFileOutputStream( javaOutput ) , encoding );
135 try {
136 outputWriter.write( content.toString() );
137 } finally {
138 IOUtil.close( outputWriter );
139 }
140 }
141 catch ( IOException e )
142 {
143 throw new MojoExecutionException( "Failed to write to file: " + javaOutput, e );
144 }
145 catch ( JavaCommandException e )
146 {
147 throw new MojoExecutionException( e.getMessage(), e );
148 }
149 break;
150 }
151 }
152 }
153 }
154 }
155
156 private void setup()
157 {
158 setupGenerateDirectory();
159
160 if ( encoding == null )
161 {
162 getLog().warn( "Encoding is not set, your build will be platform dependent" );
163 encoding = Charset.defaultCharset().name();
164 }
165
166 if ( cssFiles == null && cssFile != null )
167 {
168 cssFiles = new String[] { cssFile };
169 }
170 }
171
172 private void ensureTargetPackageExists( File generateDirectory, String targetName )
173 {
174 targetName = targetName.contains( "." ) ? targetName.substring( 0, targetName.lastIndexOf( '.' ) ) : targetName;
175 String targetPackage = targetName.replace( '.', File.separatorChar );
176 getLog().debug( "ensureTargetPackageExists, targetName : " + targetName + ", targetPackage : " + targetPackage );
177 File targetPackageDirectory = new File( generateDirectory, targetPackage );
178 if ( !targetPackageDirectory.exists() )
179 {
180 targetPackageDirectory.mkdirs();
181 }
182 }
183
184 }