Home > AutoPkg > Setting custom variables in AutoPkg using the VariablePlaceholder processor
A while back, I had written a post about using custom variables in an AutoPkg recipe to set version information. Part of my earlier post references using the EndOfCheckPhase processor as a safe way to set variables because the EndOfCheckPhase processor takes no actions. However, even the EndOfCheckPhase processor isn’t completely safe because it does do something even if the processor itself takes no actions and natively sets no output variables. This is because AutoPkg uses it to figure out if it should stop checking for new information as part of a recipe’s run.
To provide a completely safe way to set custom variables in an AutoPkg recipe, I decided to write a VariablePlaceholder AutoPkg processor which truly does absolutely nothing except serve as a convenient way to set custom variables. For more details, please see below the jump.
The VariablePlaceholder processor takes no actions and does nothing. Its usefulness comes from the fact that AutoPkg will still process Arguments values if they’re defined for the VariablePlaceholder processor. The workflow in this case looks like this:
For example, if you need to set the version output variable in an AutoPkg recipe by combining two other output variables (in this example, a major_version variable and a minor_version variable), the VariablePlaceholder processor can be used to do this in a safe way.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dict> | |
<key>Processor</key> | |
<string>VariablePlaceholder</string> | |
<key>Arguments</key> | |
<dict> | |
<key>version</key> | |
<string>%major_version%.%minor_version%</string> | |
</dict> | |
</dict> |
A recipe which uses this example setup is shown below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Description</key> | |
<string>Downloads the latest version of Carousel Cloud's screensaver.</string> | |
<key>Identifier</key> | |
<string>com.github.rtrouton.download.carouselcloudscreensaver</string> | |
<key>Input</key> | |
<dict> | |
<key>NAME</key> | |
<string>Carousel</string> | |
<key>DOWNLOAD_URL</key> | |
<string>https://carousel-public-files.s3.amazonaws.com/Carousel.Cloud.Screen.Saver.dmg</string> | |
</dict> | |
<key>MinimumVersion</key> | |
<string>1.0.0</string> | |
<key>Process</key> | |
<array> | |
<dict> | |
<key>Processor</key> | |
<string>URLDownloader</string> | |
<key>Arguments</key> | |
<dict> | |
<key>url</key> | |
<string>%DOWNLOAD_URL%</string> | |
</dict> | |
</dict> | |
<dict> | |
<key>Processor</key> | |
<string>EndOfCheckPhase</string> | |
</dict> | |
<dict> | |
<key>Arguments</key> | |
<dict> | |
<key>info_path</key> | |
<string>%pathname%/Carousel Cloud.saver/Contents/Info.plist</string> | |
<key>plist_keys</key> | |
<dict> | |
<key>CFBundleVersion</key> | |
<string>minor_version</string> | |
<key>CFBundleShortVersionString</key> | |
<string>major_version</string> | |
</dict> | |
</dict> | |
<key>Processor</key> | |
<string>PlistReader</string> | |
</dict> | |
<dict> | |
<key>Processor</key> | |
<string>CodeSignatureVerifier</string> | |
<key>Arguments</key> | |
<dict> | |
<key>input_path</key> | |
<string>%pathname%/Carousel Cloud.saver</string> | |
<key>requirement</key> | |
<string>identifier "com.trms.Carousel-Cloud-Screensaver" and anchor apple generic and certificate 1[field.1.2.840.113635.100.6.2.6] /* exists */ and certificate leaf[field.1.2.840.113635.100.6.1.13] /* exists */ and certificate leaf[subject.OU] = "3WG65JKLQ8"</string> | |
</dict> | |
</dict> | |
<dict> | |
<key>Processor</key> | |
<string>com.github.rtrouton.SharedProcessors/VariablePlaceholder</string> | |
<key>Arguments</key> | |
<dict> | |
<key>version</key> | |
<string>%major_version%.%minor_version%</string> | |
</dict> | |
</dict> | |
<dict> | |
<key>Processor</key> | |
<string>EndOfCheckPhase</string> | |
</dict> | |
</array> | |
</dict> | |
</plist> |
The VariablePlaceholder processor is shown below, as well as being available via the following link:
https://github.com/rtrouton/AutoPkg_Processors/tree/main/VariablePlaceholder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/autopkg/python | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
from autopkglib import Processor | |
__all__ = ["VariablePlaceholder"] | |
class VariablePlaceholder(Processor): | |
"""This processor performs no action, but serves as a place to set variables for use by AutoPkg recipes.""" | |
input_variables = {} | |
output_variables = {} | |
description = __doc__ | |
def main(self): | |
return | |
if __name__ == "__main__": | |
PROCESSOR = VariablePlaceholder() | |
PROCESSOR.execute_shell() |
If you want to use the VariablePlaceholder processor hosted from my AutoPkg recipe repo, first verify that AutoPkg is installed on the Mac you’re using. Once verified, run the following command:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters