rollsch
rollsch

Reputation: 2780

Conditionally set shortcut arguments in wix installer script

I have the following WIX shortcut.

 <!--Desktop shortcuts-->
  <Directory Id="DesktopFolder" Name="Desktop">
    <Component Id="CMP_DesktopShortcuts" Guid="{guidblah}">
      <Shortcut Id="Shotcut_Editor_Desktop"
                Name ="Software"
                Description="Software Description"
                Arguments="$(var.CmdLineArgs)"
                Target="blah.exe">
      </Shortcut>

      <RegistryValue Root="HKCU"
                      Key="Software\blah"
                      Name="DesktopShortcutInstalled"
                      Type="integer"
                      Value="1"
                      KeyPath="yes"
      />
    </Component>
  </Directory>

I set CmdLineArgs in my build script depending on which installer I am building. One of my build scripts has no command line arguments and hence sets CmdLineArgs to null.

I then get this error:

error CNDL0006: The Shortcut/@Arguments attribute's value cannot be an empty string. If a value is not required, simply remove the entire attribute.

How can I conditionally set Arguments only if $(var.CmdLineArgs) is not null?

Upvotes: 1

Views: 934

Answers (1)

zett42
zett42

Reputation: 27776

Use the preprocessor to conditionally compile a Shortcut element either with or without the Arguments attribute depending on the variable.

<?ifdef CmdLineArgs?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="Software"
            Description="Software Description"
            Arguments="$(var.CmdLineArgs)"
            Target="blah.exe">
    </Shortcut>
<?else?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="Software"
            Description="Software Description"
            Target="blah.exe">
    </Shortcut>
<?endif?>

Unfortunately there is some duplication here because a preprocessor condition can't be applied on the attribute level, elements are the smallest granularity.

The following is invalid XML:

<Shortcut Id="Shotcut_Editor_Desktop"
        Name ="Software"
        Description="Software Description"
    <?ifdef CmdLineArgs?>
        Arguments="$(var.CmdLineArgs)"
    <?endif?>
        Target="blah.exe">
</Shortcut>

You can get rid of some of the duplication by using preprocessor variables for the other attributes too, e. g.:

<?ifdef CmdLineArgs?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="$(var.ProductName)"
            Description="$(var.ProductDescription)"
            Arguments="$(var.CmdLineArgs)"
            Target="$(var.ProductExeFile)">
    </Shortcut>
<?else?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="$(var.ProductName)"
            Description="$(var.ProductDescription)"
            Target="$(var.ProductExeFile)">
    </Shortcut>
<?endif?>

The Id attribute is unlikely to change, so I left it as is for better readability.

Upvotes: 4

Related Questions