Conditionally creating a virtual directory

Nov 23, 2011 at 3:19 PM

I currently have a list for VirtualDirectories specified in a .proj file.

This .proj file is imported into my main msbuild script and the contents is currently batched , the msbuild looks a bit like this

 

<Target Name="IIS7VirtualDirectories:Create">   
   <MSBuild.ExtensionPack.Web.Iis7Website
      TaskAction="AddApplication"
      Name="%(Application.WebsiteName)"
      Applications="@(Application)"
      ContinueOnError="false"
      MachineName="$(MachineName)"/>
    
   <MSBuild.ExtensionPack.Web.Iis7Website 
     TaskAction="AddVirtualDirectory" 
     Name="%(VirtualDirectory.WebsiteName)" 
     VirtualDirectories="@(VirtualDirectory)"
     ContinueOnError="false"
     MachineName="$(MachineName)"/>
  </Target> 

 

The problem comes in that I need for the tasks to not create a virtual directory or application, should it already exist (a possibility in my scenario). I need a way to test if the app or vdir exists (I can do this with the tasks). What I am not sure how to do is how to act on each result in turn.

The applications and vdirs are pregenerated using a flat list in external code and are not aware of the target server at all so I cannot check there. Is it possible to achieve this with msbuild, either my collection modification or by other means or am I just asking too much ?

Coordinator
Nov 23, 2011 at 8:18 PM

Again, these IIS7 tasks need some attention! Already logged for you here: http://msbuildextensionpack.codeplex.com/workitem/9718

I think i go this working with some target batching. It's not pretty.

 

 <ItemGroup>
        <Application Include="/photos1">
            <PhysicalPath>C:\photos</PhysicalPath>
            <AppPool>NewAppPool100</AppPool>
            <WebsiteName>NewSite</WebsiteName>
        </Application>
        <Application Include="/photos2">
            <PhysicalPath>C:\photos2</PhysicalPath>
            <WebsiteName>NewSite</WebsiteName>
        </Application>
        <VirtualDirectory Include="/photosToo2">
            <ApplicationPath>/photos1</ApplicationPath>
            <PhysicalPath>C:\photos2</PhysicalPath>
            <WebsiteName>NewSite</WebsiteName>
        </VirtualDirectory>
        <VirtualDirectory Include="/photosToo">
            <ApplicationPath>/photos2</ApplicationPath>
            <PhysicalPath>C:\photos2</PhysicalPath>
            <WebsiteName>NewSite</WebsiteName>
        </VirtualDirectory>
    </ItemGroup>
    <Target Name="Default">
        <CallTarget Targets="AddApps"/>
        <CallTarget Targets="AddVDirs"/>
    </Target>
    <Target Name="AddApps" Inputs="@(Application)" Outputs="%(Identity)">
        <MSBuild.ExtensionPack.Web.Iis7Application TaskAction="CheckExists" Website="%(Application.WebsiteName)" Applications="%(Identity)">
            <Output TaskParameter="Exists" PropertyName="AppExists"/>
        </MSBuild.ExtensionPack.Web.Iis7Application>
        <Message Text="AppExists Exists: $(AppExists)"/>
        <MSBuild.ExtensionPack.Web.Iis7Website
           TaskAction="AddApplication"
           Name="%(Application.WebsiteName)"
           Applications="@(Application)"
           ContinueOnError="false"
           MachineName="$(MachineName)" Condition="$(AppExists) == 'false'"/>
    </Target>
    <Target Name="AddVDirs" Inputs="@(VirtualDirectory)" Outputs="%(Identity)">
        <MSBuild.ExtensionPack.Web.Iis7Website TaskAction="CheckVirtualDirectoryExists" Name="%(VirtualDirectory.WebsiteName)" VirtualDirectories="@(VirtualDirectory)">
            <Output TaskParameter="Exists" PropertyName="VDirExists"/>
        </MSBuild.ExtensionPack.Web.Iis7Website>
        <Message Text="VDirExists Exists: $(VDirExists)"/>
        <MSBuild.ExtensionPack.Web.Iis7Website
          TaskAction="AddVirtualDirectory"
          Name="%(VirtualDirectory.WebsiteName)"
          VirtualDirectories="@(VirtualDirectory)"
          ContinueOnError="false"
          MachineName="$(MachineName)" Condition="$(VDirExists) == 'false'"/>
    </Target>

Nov 23, 2011 at 10:16 PM

Thanks mike, this is really helpful, right now pretty is not my main aim, I am pretty new to advanced msbuild having only done fairly basic scripts before now with my main function being a middleware developer :) However this has helped me out understanding target batching which I was scrabbling around with making work.

Obviously a property to "skipIfExists" is nice but hey thats not there yet and anything which helps me solve my aim is good. I shall endevour to look into this and try and work my solution around your suggestion :)

 

Coordinator
Nov 23, 2011 at 10:30 PM

I guess the current design is partly down to the fact that I tend to delete websites and create them from scratch automatically on deployments. That way i'm confident of whats there. If you are open to compiling the latest code I can put in some quick unblockers for you, though longer term I'll prob re-write this whole iis7 stack.

 

Mike

Nov 25, 2011 at 12:48 PM

Yeah the problem I have is I am using msbuild to help generate a site some of which is pre-created by sharepoint, and so under certain circumstances it is still there.

I have also spotted a problem in that when creating the virtual directories on a remote machine the physical location (say H:\vdir1) is created on the local machine, this is a bug, I did create a work Item for it, I am unsure of etiquette here on the reporting of bugs, but upon examination of the source there is definately a decision made to try and either create locally or remotely but for some reason these directories are being created on the remote but also locally, this cannot be right. 

Nov 26, 2011 at 1:26 PM

@mike, I've never seen this task action AddApplication for Iis7Website.  Is this in dev?  Just saw this thread, so I might have jumped into the middle of the conversation.  

Coordinator
Nov 26, 2011 at 4:02 PM

krystan, see http://msbuildextensionpack.codeplex.com/SourceControl/changeset/changes/71808. this supports a Force property now. if you specify it it will remove the existing item and replace it with what you are adding.

flashfearless, I think its been there from the beginning.. I'd have to check the history.

 

Mike