Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a command over multiple lines in appveyor.yml

Tags:

yaml

appveyor

I would like to split a long build command across multiple lines in my appveyor.yml file, however I cannot get it to unwrap, so the build is failing when the first FOR command gets cut off and returns an error. I am not sure how to correctly split the lines in the .yml file so that they are reassembled inside Appveyor. How can this be done?

Here is a simplified version:

build_script:
- cmd: >-
    @echo off
    FOR %%P IN (x86,x64) DO ( ^
      FOR %%C IN (Debug,Release) DO ( ^
        msbuild ^
          /p:Configuration=%%C ^
          /p:Platform=%%P ... ^
        || EXIT 1 ^
      ) ^
    )

I want it to appear in AppVeyor as this:

@echo off
FOR %%P IN (x86,x64) DO ( FOR %%C IN (Debug,Release) DO ( msbuild /p:Configuration=%%C /p:Platform=%%P ... || EXIT 1 ) )

Extra spaces are unimportant, the important bit is that the line beginning with FOR until the final ) appears on the same line.

Note that in theory it would also be acceptable for Appveyor to see this:

@echo off
FOR %%P IN (x86,x64) DO ( ^
  FOR %%C IN (Debug,Release) DO ( ^
    msbuild ^
      /p:Configuration=%%C ^
      /p:Platform=%%P ... ^
    || EXIT 1 ^
  ) ^
)

As the Windows cmd.exe interpreter would then see the continuation markers (^) at the end of each line and treat them as one big long command, except that Appveyor does not appear to recognise the ^ marker so it sends each line to cmd.exe one at a time, instead of sending the whole multi-line block together.

This means the first option looks like the only viable solution, where the YAML is constructed such that the FOR line and everything after it is combined into a single line.

I have tried:

  • Single spacing with no extra characters at the end of each line. According to this guide, single-spaced YML lines are supposed to be unwrapped into a single line, but this does not happen with Appveyor.
  • Double-spaced lines with no extra characters at the end of each line. This is supposed to make each line a separate command, and indeed they are, as the first FOR command fails with error 255 because it is incomplete (only the FOR line is present and not the rest of the loop.)
  • Double-spaced lines terminated with ^. Appveyor only runs each line one at a time, so I get an error 255 on the first incomplete FOR command.
  • Single-spaced lines terminated with ^ as shown above. Same issue as double-spaced lines, error 255 from an incomplete FOR command.
  • Ending each line with && ^ does actually work when running separate commands (e.g. multiple msbuild statements), but this won't work with FOR loops because you can't have && without a command preceding it.

Is there a trick to splitting a single cmd command over multiple lines in appveyor.yml?

like image 241
Malvineous Avatar asked Jun 04 '16 06:06

Malvineous


2 Answers

How to split a command over multiple lines in appveyor.yml?

Here are some some syntax examples for batch, cmd, ps.

I hope these examples save you some time...

Syntax Examples

Batch

# please note the & at EOL in the next example
install:
    # Install VULKAN_SDK 
    - if not exist %VULKAN_SDK% (
       curl -L --silent --show-error --output Vulkan_SDK_Installer.exe https://sdk.lunarg.com/sdk/download/%VULKAN_VERSION%/windows/VulkanSDK-%VULKAN_VERSION%-Installer.exe?Human=true &
       Vulkan_SDK_Installer.exe /S
    )
    - dir %VULKAN_SDK%

before_build:
  - |-
    set MINGW32_ARCH=i686-w64-mingw32
  - if exist %PREFIX% set NEEDDEPENDS=rem

  # Depends
  - |-
    %NEEDDEPENDS% mkdir %PREFIX%\include\SDL2
    %NEEDDEPENDS% mkdir %PREFIX%\lib
    %NEEDDEPENDS% cd %TEMP%
    %NEEDDEPENDS% appveyor DownloadFile https://sourceforge.net/projects/gnuwin32/files/gettext/0.14.4/gettext-0.14.4-lib.zip
    %NEEDDEPENDS% mkdir gettext-0.14.4-lib
    %NEEDDEPENDS% move gettext-0.14.4-lib.zip gettext-0.14.4-lib
    %NEEDDEPENDS% cd gettext-0.14.4-lib
    %NEEDDEPENDS% 7z x gettext-0.14.4-lib.zip > nul
    %NEEDDEPENDS% copy include\* %PREFIX%\include > nul
    %NEEDDEPENDS% copy lib\* %PREFIX%\lib > nul
    %NEEDDEPENDS% cd ..

deploy_script:
  # if tagged commit, build/upload wheel
  - IF "%APPVEYOR_REPO_TAG%"=="true" IF NOT "%TESTENV%"=="check" (
      pip install twine &&
      python setup.py register &&
      twine upload -u %PYPI_USER% -p %PYPI_PASS% dist/*
    )

CMD

before_build:
    - cmd: >-     

        mkdir build

        cd .\build

        set OpenBLAS_HOME=%APPVEYOR_BUILD_FOLDER%/%MXNET_OPENBLAS_DIR%

        set OpenCV_DIR=%APPVEYOR_BUILD_FOLDER%/%MXNET_OPENCV_DIR%/build

        cmake .. -DOPENCV_DIR=%OpenCV_DIR% -DUSE_CUDA=0 -DUSE_CUDNN=0 -DUSE_NVRTC=0 -DUSE_OPENCV=1 -DUSE_OPENMP=1 -DUSE_BLAS=open -DUSE_DIST_KVSTORE=0 -G "Visual Studio 12 2013 Win64"

PS

install:
    - ps: >-

        git submodule init

        git submodule update

        if (!(Test-Path ${env:MXNET_OPENBLAS_FILE})) {

            echo "Downloading openblas from ${env:MXNET_OPENBLAS_PKG} ..."

            appveyor DownloadFile "${env:MXNET_OPENBLAS_PKG}" -FileName ${env:MXNET_OPENBLAS_FILE} -Timeout 1200000
        }

install:
      - ps: |
          Add-Type -AssemblyName System.IO.Compression.FileSystem
          if (!(Test-Path -Path "C:\maven" )) {
            (new-object System.Net.WebClient).DownloadFile('https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip', 'C:\maven-bin.zip')
            [System.IO.Compression.ZipFile]::ExtractToDirectory("C:\maven-bin.zip", "C:\maven")
          }

on_success:
  - ps: |
  if ($true)
  {
    Write-Host "Success"
  }
like image 52
Jens A. Koch Avatar answered Sep 17 '22 05:09

Jens A. Koch


Use double-quotes:

build_script:
- cmd: "
    @echo off
    FOR %%P IN (x86,x64) DO (
      FOR %%C IN (Debug,Release) DO (
        msbuild
          /p:Configuration=%%C
          /p:Platform=%%P ...
        || EXIT 1
      )
    )"

You can check yaml ref, and more precisely this example.

I was confronted to the same issue using appveyor (what a strange limitation !). Using double-quotes allowed me to leverage YAML subtlety to :

  • keep on my side an easy to read/write multi-line expression,
  • have a single-line value effectively stored application wise.

You can have a look where I use it and see how the build passes.

For other readers, please note, that the line will be collapsed, and as a consequence, need to be written with a special syntax to support that... you can't omit the || in the expression given by the OP for instance.

like image 37
vaab Avatar answered Sep 20 '22 05:09

vaab