Hi,
I've got a curious problem...
As part of our build process, I need to copy one directory
full of files recursively to another location. I can think of
several ways to approach this problem, but none of them
are quite satisfactory.
1. As far as I can tell by reading and experience, the
<copy> task will flatten a directory of files into the "todir"
location. For instance, this task:
<copy todir="d:/temp/bar/" overwrite="true" verbose="true"
flatten="false">
<fileset>
<includes name="d:\temp\foo\**" />
</fileset>
</copy>
...quite happily puts all the files in the /temp/foo hierarchy, no
matter how deeply nested, e.g.:
testXCopy:
[copy] Copying 4 files to d:/temp/bar/.
[copy] Copying d:\temp\foo\bar.txt to d:\temp\bar\bar.txt.
[copy] Copying d:\temp\foo\taz.txt to d:\temp\bar\taz.txt.
[copy] Copying d:\temp\foo\foo2\taz2.txt to d:\temp\bar\taz2.txt.
[copy] Copying d:\temp\foo\foo2\bar2.txt to d:\temp\bar\bar2.txt.
BUILD SUCCEEDED
It seems to me that the presence of a "flatten" attribute would imply a
non-flattening behavior otherwise. Apparently I'm mistaken here?
2. Since that didn't work, I turned to the old standby, "<exec ... "xcopy"
...>
So I tried this:
<target name="testXCopy">
<exec
program="C:\winnt\system32\xcopy.exe"
commandline="d:\temp\foo d:\temp\bar /s/e/q/y"
/>
</target>
This target runs to success at completion, but no files get copied. None.
In verbose mode, the command line will be printed to the screen, and if I
grab that generated command line and paste it into a command window,
the command works fine. Just not inside NAnt.
I have tried simpler things with XCOPY. For instance, this code produces
a help message when the program starts:
<exec
program="C:\winnt\system32\attrib.exe"
commandline="/?"
/>
But this code produces only silence:
<exec
program="C:\winnt\system32\xcopy.exe"
commandline="/?"
/>
I know it sees XCOPY.EXE because if I rename the XCOPY executable,
I get a "not found" error.
I have also tried using a surrounding shell:
<exec
program="c:\winnt\system32\cmd.exe"
commandline="/c xcopy d:\temp\foo d:\temp\bar /s/e/q/y"
/>
Again, nothing.
3. It's possible to recurse directly in NAnt; for instance, I have a
recursive
target that can invoke _another_ target on every directory or file it finds
on
a tree walk. I use it to update assembly files in a large solution, for
instance.
But copying files this way seems like it would be really slow.
Finally, I created a solution that worked, using the duct tape of NAnt
coding:
the C# script.
I call it like this:
<property name="xcopy.from" value="d:\temp\foo"/>
<property name="xcopy.to" value="d:\temp\bar"/>
<call target="xcopy"/>
And here's the tested, reusable task. (Yes, I do TDD in NAnt :-) :
<!--
===============================================
Function: xcopy
Execute the XCOPY utility correctly
Property: xcopy.from
Source of files
Property: xcopy.to
Destination for files
===============================================
-->
<target name="xcopy">
<echo message="[xcopy] Copying ${xcopy.from} to ${xcopy.to}."/>
<script language="C#">
<imports>
<import name="System.Diagnostics"/>
</imports>
<code>
<![CDATA[
public static void ScriptMain(Project project) {
System.Diagnostics.Process process = new
System.Diagnostics.Process();
process.EnableRaisingEvents=false;
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName=@"c:\winnt\system32\xcopy.exe";
process.StartInfo.Arguments=
project.Properties["xcopy.from"] + " "
+
project.Properties["xcopy.to" ] + "
/s/e/y";
process.StartInfo.WindowStyle =
ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
}
]]>
</code>
</script>
<echo message="[xcopy] Copying completed."/>
</target>
Please forgive the excessive indenting; it looks much better in Visual
Studio.
I am more than interested in any NAnt guru who would like to point out
the error of my ways. This does seem like a lot of work to copy a
directory;
its only saving grace may be that I copy a _lot_ of directories around, so
it's nice to have a reusable task that does exactly what it should and
nothing more.
Comments?
Best wishes,
-greg
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Nant-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nant-users