//WARNING UNTESTED!!!
package com.arrizza.ant.taskdefs;
import java.io.*;
import org.apache.tools.ant.types.*;
import org.apache.tools.ant.taskdefs.*;
import org.apache.tools.ant.*;
public class RegAsm extends ExecTask
{
private FileSet myFileSet;
private boolean isFailOnError = true;
private boolean isVerbose = false;
private String myRegAsmFolder = null;
private String myRegAsmPath = null;
private String myExtraParameters = "/codebase /tlb";
private String myAssembly = null;
private String myAssemblyFolder = null;
private String myAssemblyPath = null;
//protected...
protected String myPath;
//constants...
protected String cFailureMessage = "*** RegAsm failed ***";
protected String cRegAsmExe = "regasm.exe";
protected String cAssemblyFolderPropertyName = "BUILD_ROOT";
protected String cRegAsmFolderPropertyName = "REGASMFOLDER";
//----------------------------------------
public void execute() //throws BuildException
{
initialize();
if (myFileSet == null)
{
register(myAssemblyPath);
}
else
registerFromFileSet();
}
//----------------------------------------
//-- the assembly name, eg. abc.dll
//-- required if fileset is not used
public void setAssembly(String theAssembly)
{
myAssembly = theAssembly;
}
//----------------------------------------
//-- where the assemblies are found
public void setAssemblyFolder(String theFolder)
{
myAssemblyFolder = theFolder;
}
//----------------------------------------
//-- additional parameters
public void setParameters(String theParms)
{
myExtraParameters = theParms;
}
//----------------------------------------
//-- where regasm is located
public void setRegAsmFolder(String theFolder)
{
myRegAsmFolder = theFolder;
}
//----------------------------------------
//-- set to false if you want to continue after an error
public void setFailonerror(boolean theFlag)
{
isFailOnError = theFlag;
}
//----------------------------------------
public void setVerbose(boolean theFlag)
{
isVerbose = theFlag;
}
//----------------------------------------
public void addTargets(FileSet set)
{
if (myFileSet != null) throw new BuildException("Only one fileset allowed");
myFileSet = set;
}
//----------------------------------------
//-- private from here on
//----------------------------------------
//----------------------------------------
private void registerFromFileSet()
{
DirectoryScanner ds = myFileSet.getDirectoryScanner(getProject());
ds.scan();
register(ds.getIncludedFiles());
}
//----------------------------------------
private void register(String[] files)
{
if (files == null) return;
for (int i = 0; i < files.length;i++)
{
register(deriveAssembly(files[i]));
}
}
//----------------------------------------
private void initialize()
{
if (myFileSet == null && myAssembly == null)
throw new BuildException("You must specify an assembly name, e.g. abc.dll");
if (myFileSet != null && myAssembly != null)
log("Note: The 'assembly' attribute is ignored when using a fileset");
initializeRegAsmFolder();
myRegAsmPath = getFullPath(myRegAsmFolder, cRegAsmExe);
initializeAssemblyFolder();
myAssemblyPath = getFullPath(myAssemblyFolder, myAssembly);
}
//----------------------------------------
private String getFullPath(String theFolder, String theFile)
{
if (theFolder == null || theFolder == "") return theFile;
return theFolder + "/" + theFile;
}
//----------------------------------------
private void initializeAssemblyFolder()
{
//if user has set it, we're done
if (myAssemblyFolder != null) return;
//if there's a property for it, create the dir from that
if (getProject().getProperty(cAssemblyFolderPropertyName) != null)
{
setAssemblyFolder(getProject().getProperty(cAssemblyFolderPropertyName));
return;
}
//none of the above, use the current directory
setAssemblyFolder("./");
}
//----------------------------------------
private void initializeRegAsmFolder()
{
//if user has set it, we're done
if (myRegAsmFolder != null) return;
//if there's a property for it, create the dir from that
if (getProject().getProperty(cRegAsmFolderPropertyName) != null)
{
setRegAsmFolder(getProject().getProperty(cRegAsmFolderPropertyName));
return;
}
//none of the above, use the current directory
setRegAsmFolder("");
}
//----------------------------------------
private String deriveAssembly(String file)
{
if (file == null) return "";
int end = file.lastIndexOf(".sln");
int begin = file.lastIndexOf("\\");
if (begin == -1) begin = file.lastIndexOf("/");
StringBuffer f = new StringBuffer(file.substring(begin + 1, end));
f.append(".dll");
return getFullPath(myAssemblyFolder, f.toString());
}
//----------------------------------------
//-- builds the command line and runs it
private void register(String theFullPath)
{
Commandline commandLine = buildCommandLine(theFullPath);
PumpStreamHandler psh = createOutStream();
log("Command= " + commandLine.toString());
try
{
runCommand(commandLine, theFullPath, psh);
}
catch (IOException e)
{
throw new BuildException("failed: " + e, e, getLocation());
}
}
//----------------------------------------
private PumpStreamHandler createOutStream()
{
return isVerbose ?
new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN)
:
new PumpStreamHandler(new ByteArrayOutputStream());
}
//----------------------------------------
//-- executes the given command line and reports any errors
private void runCommand(Commandline commandLine, String theFullpath, PumpStreamHandler theStream) throws IOException
{
if (executeCommand(commandLine, theStream) == 0) return;
String errorMessage = cFailureMessage + " Target='" + theFullpath + "'";
if (isFailOnError)
throw new BuildException(errorMessage, getLocation());
else
log(errorMessage);
}
//----------------------------------------
private int executeCommand(Commandline commandLine, PumpStreamHandler theStream) throws IOException
{
Execute exe = new Execute(theStream);
exe.setCommandline(commandLine.getCommandline());
return exe.execute();
}
//----------------------------------------
//-- create the command line
private Commandline buildCommandLine(String theAssemblyPath)
{
Commandline commandLine = new Commandline();
commandLine.setExecutable(myRegAsmPath);
commandLine.createArgument().setLine("/nologo");
commandLine.createArgument().setLine(myExtraParameters);
Path p = new Path(getProject());
p.setPath(theAssemblyPath);
commandLine.createArgument().setPath(p);
return commandLine;
}
}
|