3

I want to zip up a folder. This is easy on operating systems like Linux where it's easy for the system admin to install the command line zip tool like apt install zip. Unfortunately on Windows it's not that straight forward.

A simple workaround I've found is that I can use Compress-Archive in a powershell script like this:

Compress-Archive -Path $folder -CompressionLevel Optimal -DestinationPath $zipPath

and then invoke this with the folder and zipPath before or after project building.

To do this, my CMakeLists.txt will have a line like:

execute_process(COMMAND powershell ${powershellScriptPath} "\"${folderPathToZip}\"" "\"${outputZipPath}\"" OUTPUT_QUIET)

This works fine, except I have to allow scripts to run globally and unsafely on my computer.

I want this to just work on any Windows computer without having to require the people to do any tinkering of settings. I also don't like the idea of getting other developers to allow scripts globally and unsafely for obvious reasons. I'd like my developers to be able to open the project and press build and it all works, and this is the last thing standing in my way from making it happen.

Is there a way to be able to get this script to run by changing the way the program is invoked? Or am I stuck and have to find another way to do this? Or is there some way to whitelist this specific file automatically without the user having to do any extra steps?

The only other hacky way around this would be for me to write my own zip utility that is invoked on every build to zip up the stuff, but that is a bunch of work for something that feels so close to being operational.

Edit: I can safely make the assumption that the computer has powershell installed and is relatively modern

Water
  • 3,245
  • 3
  • 28
  • 58

1 Answers1

2

You can bypass the effective PowerShell execution policy by passing -ExecutionPolicy Bypass to the PowerShell CLI (powershell.exe in the case of Windows PowerShell):

execute_process(COMMAND powershell -ExecutionPolicy Bypass -File ${powershellScriptPath} ${folderPathToZip} ${outputZipPath} OUTPUT_QUIET)

Also note that I'm using -File so as to instruct Windows PowerShell to execute a script file, because it defaults to -Command[1], which changes the interpretation of the arguments[2]; I'm not familiar with cmake, but the hope is that it provides double-quoting around the arguments by default / as necessary, which with -File should be sufficient.


[1] Note that PowerShell Core defaults to -File.

[2] For details, see the 2nd and subsequent sections of this answer.

mklement0
  • 382,024
  • 64
  • 607
  • 775