views:

387

answers:

1

I have a nant script that ... 1. takes the content of disc-file 2. assigns that content to a nant property 3. and then calls sqlcmd with a -v passing in that property containg the content of the disc file 4. inside the sql script the contents of the file should be used by a stored proc.

The problem is that when the content of the file contains a space the nant build stops with a "Invalid argument" issue

Anone know a way around this ?

The top part of the nant script is ...

<?xml version="1.0"?>
<!-- the main name of this project -->
<project name="Hops" default="all">
  <!-- BuildHistory -->
  <property name="buildHistoryContents" value="" />
  <xmlpeek xpath="/" file="BuildNotes.xml" property="buildHistoryContents"></xmlpeek>
  <!-- <echo message="${buildHistoryContents}" /> -->

  <!-- ***************** -->
  <target name="ExecSql">
    <echo message="running sql script : ${SqlBuildScriptsDir}${sqlBuildFileName}" />
    <exec program="${SqlCmd}" commandline="-S ${SqlServerInstanceName} -E -d HBus -i ${SqlBuildScriptsDir}${sqlBuildFileName} -v vSchemaVersion=${buildHistoryContents}  " />
  </target>

The sql script contains the line ...

exec lsp_SchemaVersionUpsert '1.4', N'$(vSchemaVersion)'

A disc file content that works is ...

<BuildNotes>
  <Note>
    <buildVer>HasNotSpace</buildVer>
  </Note>
</BuildNotes>

A disc file content that does not works is ...

<BuildNotes>
  <Note>
    <buildVer>Has Space</buildVer>
  </Note>
</BuildNotes>

The use of all this is pass xml build comments to a table logging version build history for the db schema.

Does anyone know an alternate method or know a way through this ?

The next part, added after Phillip Keeley correcty solved first part (the SPACE Problem) I simplified the original task to simplify the question.

There is also a Quoted Attribute Problem ; xml quoted attributes cause the nant build to fail with "Invalid Argument".

eg this will cause nant to choke but removing the dt attribute will enable the nant build to succeed ...

<BuildNotes>
  <Note>
    <buildVer>1.4</buildVer>
    <dateStarted>09/24/2009 11:25:42</dateStarted>
    <Item dt="20091008" >SpacesAndNoQuotedAttribute</Item>
  </Note>
</BuildNotes>

Any ideas ... ?

A: 

Your problem is (of course) in line

<exec program="${SqlCmd}" commandline="-S ${SqlServerInstanceName} -E -d HBus -i ${SqlBuildScriptsDir}${sqlBuildFileName} -v vSchemaVersion=${buildHistoryContents}  " />

specifically, in

-v vSchemaVersion=${buildHistoryContents}

The NAnt expression replaces property ${buildHistoryContents} with the stored value--which will include any embedded spaces. Problem is, when calling SQLCMD (I"m assuming that's what ${SqlCmd} resolves to) from a command window, the values for any and all -v parameters are space delimited -- that is, the parser hits -v, reads the next characters through the "=" as the variable name, then reads all characters after the = and through the next space (or end of line) as the value to assign to the variable, and that embedded space will mess you up bigtime.

On the command line, the work-around is to wrap the variable value in quotes:

 - v MyVariable=Hello World

becomes

 - v MyVariable="Hello World"

That doesn't work here, because it's XML and you have to wrap the commandline attribute of the exec element with quotes... and embedded quotes will, once again, mess you up bigtime.

I believe the work-around here is to use XML macro substitution (I quite possibly have the formal titles of these concepts wrong) for those embedded quotes. This value should be

&quot;

Which means that the following should work:

<exec program="${SqlCmd}" commandline="-S ${SqlServerInstanceName} -E -d HBus -i ${SqlBuildScriptsDir}${sqlBuildFileName} -v vSchemaVersion=&quot;${buildHistoryContents}&quot;  " />

Please try this and see -- I may have to do something like this myself some day soon.

Philip Kelley