diff --git a/sandbox/csharp/maven-csharp-plugin/pom.xml b/sandbox/csharp/maven-csharp-plugin/pom.xml new file mode 100644 index 0000000000..8baa9c8fb8 --- /dev/null +++ b/sandbox/csharp/maven-csharp-plugin/pom.xml @@ -0,0 +1,52 @@ + + + 4.0.0 + + org.apache.maven.plugins + maven-plugin-parent + 2.0-beta-1-SNAPSHOT + + maven-csharp-plugin + maven-plugin + csharp-plugin + 1.0-SNAPSHOT + 2005 + M2 csharp plugin + + + + Gilles Dodinet + gdodinet@karmicsoft.com + Karmic Software Research + + + + + + org.apache.maven + maven-artifact + 2.0-beta-1-SNAPSHOT + + + org.apache.maven + maven-project + 2.0-beta-1-SNAPSHOT + + + plexus + plexus-compiler-api + 1.2 + + + ant + ant + 1.6.5 + + + ant + ant-nodeps + 1.6.5 + + + + diff --git a/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/compiler/CSharpCompiler.java b/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/compiler/CSharpCompiler.java new file mode 100644 index 0000000000..9d2be58e91 --- /dev/null +++ b/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/compiler/CSharpCompiler.java @@ -0,0 +1,157 @@ +package org.apache.plugins.csharp.compiler; + +/* + * Copyright 2005 The Apache Software Foundation. + * + * 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. + */ + +import org.apache.plugins.csharp.helpers.AntBuildListener; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.optional.dotnet.CSharp; +import org.codehaus.plexus.compiler.AbstractCompiler; +import org.codehaus.plexus.compiler.CompilerConfiguration; + +import java.io.File; +import java.util.List; +import java.util.Map; + +/** + * @author Gilles Dodinet + * @version $Id$ + */ +public class CSharpCompiler + extends AbstractCompiler +{ + private File basedir; + + private AntBuildListener antBuildListener; + + private String references; + + public List compile( CompilerConfiguration config ) + throws Exception + { + + CSharp csc = createCompiler( config ); + + csc.execute(); + + return null; + } + + private CSharp createCompiler( CompilerConfiguration config ) + throws Exception + { + Map compilerOptions = config.getCompilerOptions(); + + CSharp csc = new CSharp(); + + Project antProject = new Project(); + antProject.setBaseDir( basedir ); + + if ( antBuildListener == null ) + { + antBuildListener = new AntBuildListener(); + } + + antProject.addBuildListener( antBuildListener ); + + csc.setProject( antProject ); + + csc.setOptimize( getBooleanOption( compilerOptions, "optimize", true ) ); + csc.setUnsafe( getBooleanOption( compilerOptions, "unsafe", false ) ); + csc.setIncremental( getBooleanOption( compilerOptions, "incremental", false ) ); + csc.setFullPaths( getBooleanOption( compilerOptions, "fullpaths", true ) ); + csc.setWarnLevel( getIntOption( compilerOptions, "warnLevel", 4 ) ); + csc.setDebug( getBooleanOption( compilerOptions, "debug", true ) ); + + csc.setMainClass( (String) compilerOptions.get( "mainClass" ) ); + + String type = (String) compilerOptions.get( "type" ); + + csc.setTargetType( type ); + csc.setReferences( references ); + + File destDir = new File( config.getOutputLocation() ); + if ( !destDir.exists() ) + { + destDir.mkdirs(); + } + csc.setDestDir( destDir ); + + String destFileName = (String) compilerOptions.get( "destFile" ); + if ( destFileName == null ) + { + destFileName = (String) compilerOptions.get( "mainClass" ); + } + csc.setDestFile( new File( destDir, destFileName + "." + getTypeExtension( type ) ) ); + + return csc; + } + + /** + * @param type + * @return + * @throws Exception + */ + private String getTypeExtension( String type ) + throws Exception + { + if ( "exe".equals( type ) || "winexe".equals( type ) ) + { + return "exe"; + } + if ( "library".equals( type ) || "module".equals( type ) ) + { + return "dll"; + } + throw new Exception( "Unrecognized type" ); + } + + private boolean getBooleanOption( Map options, String optionName, boolean defaultValue ) + { + Boolean optionValue = (Boolean) options.get( optionName ); + return optionValue != null ? optionValue.booleanValue() : defaultValue; + } + + private int getIntOption( Map options, String optionName, int defaultValue ) + { + Integer optionValue = (Integer) options.get( optionName ); + return optionValue != null ? optionValue.intValue() : defaultValue; + } + + /** + * @param basedir + */ + public void setBasedir( File basedir ) + { + this.basedir = basedir; + } + + /** + * @param antBuildListener The antBuildListener to set. + */ + public void setAntBuildListener( AntBuildListener antBuildListener ) + { + this.antBuildListener = antBuildListener; + } + + /** + * @param additionalModules The additionalModules to set. + */ + public void setReferences( String additionalModules ) + { + this.references = additionalModules; + } +} diff --git a/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/compiler/CSharpCompilerMojo.java b/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/compiler/CSharpCompilerMojo.java new file mode 100644 index 0000000000..e3b0119d2e --- /dev/null +++ b/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/compiler/CSharpCompilerMojo.java @@ -0,0 +1,365 @@ +package org.apache.plugins.csharp.compiler; + +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.project.MavenProject; +import org.apache.plugins.csharp.helpers.AntBuildListener; +import org.codehaus.plexus.compiler.CompilerConfiguration; + +import java.io.File; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/* + * Copyright 2005 The Apache Software Foundation. + * + * 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. + */ + + +/** + * @author Gilles Dodinet + * @version $Id$ + * @description Compiles c# sources + * @goal compile + * @phase compile + * @requiresDependencyResolution compile + */ +public class CSharpCompilerMojo + extends AbstractMojo +{ + + /** + * @parameter + */ + private String debug = Boolean.TRUE.toString(); + + /** + * @parameter + */ + private String optimize = Boolean.TRUE.toString(); + + /** + * @parameter + */ + private String unsafe = Boolean.FALSE.toString(); + + /** + * @parameter + */ + private String incremental = Boolean.FALSE.toString(); + + /** + * @parameter + */ + private String fullPaths = Boolean.TRUE.toString(); + + /** + * @parameter + */ + private String warnLevel = "4"; + + /** + * @parameter + */ + private String mainClass; + + /** + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + + /** + * @parameter expression="${project.build.outputDirectory}" + * @required + * @readonly + */ + private String outputDirectory; + + /** + * @parameter + */ + private String destFile; + + /** + * @parameter + * @required + */ + private String type = "library"; + + /** + * @parameter expression="${project.compileSourceRoots}" + * @required + * @readonly + */ + private List compileSourceRoots; + + public void execute() + throws MojoExecutionException + { + CompilerConfiguration config = new CompilerConfiguration(); + + Map compilerOptions = new HashMap(); + + config.setOutputLocation( outputDirectory ); + + config.setSourceLocations( compileSourceRoots ); + + compilerOptions.put( "optimize", Boolean.valueOf( optimize ) ); + compilerOptions.put( "unsafe", Boolean.valueOf( unsafe ) ); + compilerOptions.put( "incremental", Boolean.valueOf( incremental ) ); + compilerOptions.put( "fullPaths", Boolean.valueOf( fullPaths ) ); + compilerOptions.put( "warnLevel", Integer.valueOf( warnLevel ) ); + compilerOptions.put( "mainClass", mainClass ); + compilerOptions.put( "destFile", destFile ); + + //until handlers ready + compilerOptions.put( "type", type ); + //compilerOptions.put("type", project.getPackaging()); + + config.setCompilerOptions( compilerOptions ); + CSharpCompiler compiler = new CSharpCompiler(); + compiler.setBasedir( project.getBasedir() ); + compiler.setAntBuildListener( new AntBuildListener( this.getLog() ) ); + + String artifactList = ""; + Set artifacts = project.getArtifacts(); + int u = 0; + for ( Iterator i = artifacts.iterator(); i.hasNext(); ) + { + Artifact artifact = (Artifact) i.next(); + if ( "dll".equals( artifact.getType() ) ) + { + + File file = artifact.getFile(); + + artifactList += file.getAbsolutePath(); + if ( u < artifacts.size() - 1 ) + { + artifactList += ":"; + } + + } + u++; + } + compiler.setReferences( artifactList ); + + try + { + compiler.compile( config ); + } + catch ( Exception e ) + { + throw new MojoExecutionException( "Error compiling C# sources", e ); + } + + } + + /** + * @return Returns the debug. + */ + public String getDebug() + { + return debug; + } + + /** + * @param debug The debug to set. + */ + public void setDebug( String debug ) + { + this.debug = debug; + } + + /** + * @return Returns the fullPaths. + */ + public String getFullPaths() + { + return fullPaths; + } + + /** + * @param fullPaths The fullPaths to set. + */ + public void setFullPaths( String fullPaths ) + { + this.fullPaths = fullPaths; + } + + /** + * @return Returns the incremental. + */ + public String getIncremental() + { + return incremental; + } + + /** + * @param incremental The incremental to set. + */ + public void setIncremental( String incremental ) + { + this.incremental = incremental; + } + + /** + * @return Returns the mainClass. + */ + public String getMainClass() + { + return mainClass; + } + + /** + * @param mainClass The mainClass to set. + */ + public void setMainClass( String mainClass ) + { + this.mainClass = mainClass; + } + + /** + * @return Returns the optimize. + */ + public String getOptimize() + { + return optimize; + } + + /** + * @param optimize The optimize to set. + */ + public void setOptimize( String optimize ) + { + this.optimize = optimize; + } + + /** + * @return Returns the outputDirectory. + */ + public String getOutputDirectory() + { + return outputDirectory; + } + + /** + * @param outputDirectory The outputDirectory to set. + */ + public void setOutputDirectory( String outputDirectory ) + { + this.outputDirectory = outputDirectory; + } + + /** + * @return Returns the compileSourceRoots. + */ + public List getCompileSourceRoots() + { + return compileSourceRoots; + } + + /** + * @param compileSourceRoots The compileSourceRoots to set. + */ + public void setCompileSourceRoots( List compileSourceRoots ) + { + this.compileSourceRoots = compileSourceRoots; + } + + /** + * @return Returns the project. + */ + public MavenProject getProject() + { + return project; + } + + /** + * @param project The project to set. + */ + public void setProject( MavenProject project ) + { + this.project = project; + } + + /** + * @return Returns the unsafe. + */ + public String getUnsafe() + { + return unsafe; + } + + /** + * @param unsafe The unsafe to set. + */ + public void setUnsafe( String unsafe ) + { + this.unsafe = unsafe; + } + + /** + * @return Returns the warnLevel. + */ + public String getWarnLevel() + { + return warnLevel; + } + + /** + * @param warnLevel The warnLevel to set. + */ + public void setWarnLevel( String warnLevel ) + { + this.warnLevel = warnLevel; + } + + /** + * @return Returns the destFile. + */ + public String getDestFile() + { + return destFile; + } + + /** + * @param destFile The destFile to set. + */ + public void setDestFile( String destFile ) + { + this.destFile = destFile; + } + + /** + * @return Returns the type. + */ + public String getType() + { + return type; + } + + /** + * @param type The type to set. + */ + public void setType( String type ) + { + this.type = type; + } +} diff --git a/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/helpers/AntBuildListener.java b/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/helpers/AntBuildListener.java new file mode 100644 index 0000000000..546294b4dd --- /dev/null +++ b/sandbox/csharp/maven-csharp-plugin/src/main/java/org/apache/plugins/csharp/helpers/AntBuildListener.java @@ -0,0 +1,92 @@ +package org.apache.plugins.csharp.helpers; + +/* + * Copyright 2005 The Apache Software Foundation. + * + * 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. + */ + +import org.apache.maven.plugin.logging.Log; +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.BuildListener; +import org.apache.tools.ant.Project; + + +/** + * @author Gilles Dodinet + * @version $Id$ + */ +public final class AntBuildListener + implements BuildListener +{ + private Log log; + + public AntBuildListener() + { + } + + public AntBuildListener( Log log ) + { + this.log = log; + } + + public void buildFinished( BuildEvent arg0 ) + { + } + + public void buildStarted( BuildEvent arg0 ) + { + } + + public void targetStarted( BuildEvent arg0 ) + { + } + + public void targetFinished( BuildEvent arg0 ) + { + } + + public void taskStarted( BuildEvent arg0 ) + { + } + + public void taskFinished( BuildEvent arg0 ) + { + } + + public void messageLogged( BuildEvent e ) + { + if ( log == null ) + { + System.out.println( e.getMessage() ); + return; + } + switch ( e.getPriority() ) + { + case Project.MSG_DEBUG: + log.debug( e.getMessage() ); + break; + case Project.MSG_INFO : + log.info( e.getMessage() ); + break; + case Project.MSG_WARN : + log.warn( e.getMessage() ); + break; + case Project.MSG_ERR : + log.error( e.getMessage() ); + break; + default : + break; + } + } +} diff --git a/sandbox/csharp/maven-csharp-plugin/src/main/resources/META-INF/plexus/components.xml b/sandbox/csharp/maven-csharp-plugin/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 0000000000..6a55f4a830 --- /dev/null +++ b/sandbox/csharp/maven-csharp-plugin/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,34 @@ + + + + org.apache.maven.artifact.handler.ArtifactHandler + dll + org.apache.maven.artifact.handler.DefaultArtifactHandler + + dll + dll + + + + org.apache.maven.artifact.handler.ArtifactHandler + exe + org.apache.maven.artifact.handler.DefaultArtifactHandler + + exe + exe + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + dotnetexe + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + csharp:compile + install:install + deploy:deploy + + + + + diff --git a/sandbox/csharp/maven-csharp-test/pom.xml b/sandbox/csharp/maven-csharp-test/pom.xml new file mode 100644 index 0000000000..57e63b34b9 --- /dev/null +++ b/sandbox/csharp/maven-csharp-test/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + org.apache.maven + maven-csharp-plugin-test + dotnetexe + csharp-plugin-test + 1.0-SNAPSHOT + 2005 + M2 csharp plugin Test + + + + Gilles Dodinet + gdodinet@karmicsoft.com + Karmic Software Research + + + + + src/main/csharp + + +true + maven-csharp-plugin + 1.0-SNAPSHOT + + Com.Test.Test2 + Test2 + exe + + + + + + diff --git a/sandbox/csharp/maven-csharp-test/src/main/csharp/Test.cs b/sandbox/csharp/maven-csharp-test/src/main/csharp/Test.cs new file mode 100644 index 0000000000..7375895cfe --- /dev/null +++ b/sandbox/csharp/maven-csharp-test/src/main/csharp/Test.cs @@ -0,0 +1,17 @@ + + +using System; +using System.Text; + +namespace Com.Test { + public class Test2 { + public int MyProp { + get { return 1; } + } + + [STAThread] + public static void Main(string[] args) { + System.Console.WriteLine("HELLO WORLD"); + } + } +}