Write your package for Chocolatey

Chocolatey — a package Manager for windows greatly simplifies the procedure of software installation, and as much as it saves time. Wonder about it already wrote on Habre, and has even been shown how to create a package for it, but there was more manual work, I want to show how to simplify this.
Start
If you don't have chocolatey yourself, fix it:
the
@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin
Closing and re-opening the console to refreshed data for a variable.
Chocolatey installed, now install the packages we need:
the
cinst warmup
cinst git
cinst nuget.commandline
Closing and re-opening the console to refreshed data for a variable.
Go to the folder with the installed сhocolatey, clone the repository with templates and proceed to our templates.
the
cd %ChocolateyInstall%
git clone https://github.com/chocolatey/chocolateytemplates.git
cd chocolateytemplates\_templates
Podredaktirovat a few standard settings
the
-
the
- list your name, it will be used as the author of package
thewarmup addTextReplacement __CHOCO_PKG_OWNER_NAME__ "Your Name"
the - Your repo with the packages on github. Specify only the name of the account and repository. Example: Claud/chocolatey-packages
thewarmup addTextReplacement __CHOCO_PKG_OWNER_REPO__ "Your Repository"
the - Then we need to create links to our templates. By default they are created in the directory
C:\CODE\_templates
I don't like it, so I changed it to their own. If you're with me in agreement on this matter, then open config, which is stored here:
c:\Chocolatey\lib\warmup.*\bin\warmup.exe.Config
finds theC:\CODE\_templates
and change to its directory.
Now you can activate the templates from which we will make our packages (basically, a link is created to the directory with the desired pattern). To do this, in the directory
%ChocolateyInstall%\chocolateytemplates\_templates
will execute the command (I have only runs under admin
):the
addTemplateFolder warmup chocolatey "%CD%\chocolatey"
Go to the website chocolatey.org are registered, go to your profile click
“Your key is hidden for privacy click to show.“ See three commands only one: set API Key (if you will swear that nuget.exe not found, remove extension .exe).
this preparation it is now possible to proceed with the mass production of the packages
The official documentation recommends naming the new packages in lower case separating words with “-” or just omitting the spaces. Also, it is advisable to use search package with the right program and if not, to do your own.
Storage source will use the following structure:.
the
packages |- workrave |- src |- tools |- workrave.nuspec | - ..... |- build |- package-2
Being in the directory
packages
is running:the
warmup chocolatey workrave
In the directory
workrave
creates a src
and move the contents of workrave
.Open the file
workrave.nuspec
. This is a common xml
, with the description of our package. Title tag speaks for itself: title
— the name of the program, which will be displayed in the package list on the website chocolatey.org; author
— the Creator of the program you placed in the package.... The fun is in the file
tools\chocolateyInstall.ps1
Code
#NOTE: Please remove any commented lines to tidy up prior to releasing the package, including this one
$packageName = 'workrave' # arbitrary name for the package, used in messages
$installerType = 'EXE_MSI_OR_MSU' #only one of these: exe, msi, msu
$url = 'URL_HERE' # download url
$url64 = 'URL_x64_HERE' # 64bit URL here or remove - if installer decides, then use $url
$silentArgs = 'SILENT_ARGS_HERE' # "/s /s /q /Q /quiet /silent /SILENT /verysilent flags" # try any of these to get the silent installer #msi is always /quiet
# main helpers - these have error handling tucked into them already
# installer, will assert administrative rights
# if removing $url64, please remove from here
Install-ChocolateyPackage "$packageName" "$installerType" "$silentArgs" "$url" "$url64" -validExitCodes $validExitCodes
# download and unpack a zip file
# if removing $url64, please remove from here
Install-ChocolateyZipPackage "$packageName" "$url" "$(Split-Path -parent $MyInvocation.MyCommand.Definition)" "$url64"
#try { #error handling is only necessary if you need to do anything in addition to/instead of the main helpers
# other helpers - using any of these means you want to uncomment the error handling up top and at bottom.
# downloader that the main helpers use to download items
# if removing $url64, please remove from here
#Get-ChocolateyWebFile "$packageName" 'DOWNLOAD_TO_FILE_FULL_PATH' "$url" "$url64"
# installer, will assert administrative rights - used by Install-ChocolateyPackage
#Install-ChocolateyInstallPackage "$packageName" "$installerType" "$silentArgs" '_FULLFILEPATH_' -validExitCodes $validExitCodes
# unzips the a file to the specified location - auto overwrites existing content
#Get-ChocolateyUnzip "FULL_LOCATION_TO_ZIP.zip" "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
# Runs processes asserting UAC, will assert administrative rights - used by Install-ChocolateyInstallPackage
#Start-ChocolateyProcessAsAdmin 'STATEMENTS_TO_RUN' 'Optional_Application_If_Not_Powershell' -validExitCodes $validExitCodes
# add specific folders to the path - any executables found in the chocolatey package folder will already be on the path. This is used in addition to that or for cases when a native installer doesn't add things to the path.
#Install-ChocolateyPath 'LOCATION_TO_ADD_TO_PATH' 'User_OR_Machine' # Machine will assert administrative rights
# add specific files as shortcuts to the desktop
#$target = Join-Path $MyInvocation.MyCommand.Definition "$($packageName).exe"
#Install-ChocolateyDesktopLink $target
# ------- ADDITIONAL SETUP -------#
# make sure to uncomment the error handling if you have additional setup to do
#$processor = Get-WmiObject Win32_Processor
#$is64bit = $processor.AddressWidth -eq 64
# the following is all part of error handling
#Write-ChocolateySuccess "$packageName"
#} catch {
#Write-ChocolateyFailure "$packageName" "$($_.Exception.Message)"
#throw
#}
There are three blocks:
the
-
the
- Variables. the
- Two simple installers. the
Install-ChocolateyPackage
— downloads the exe file and starts the installation with the desired keys. The most running team in most cases it should be enough. theInstall-ChocolateyZipPackage
downloads the zip file and unpacks it in the specified directory.
the try / catch
— in case of difficult installation process. Say download zip archive, unzip it and run from it .exe file.
For our test package code given to this kind
Code after
#NOTE: Please remove any commented lines to tidy up prior to releasing the package, including this one
$packageName = 'workrave' # arbitrary name for the package, used in messages
$installerType = 'exe' #only one of these: exe, msi, msu
$url = 'http://softlayer-ams.dl.sourceforge.net/project/workrave/workrave/1.10.1/workrave-win32-v1.10.1-installer.exe' # download url
$silentArgs = '/verysilent flags /norestart' # "/s /s /q /Q /quiet /silent /SILENT /verysilent flags" # try any of these to get the silent installer #msi is always /quiet
$validExitCodes = @(0) #please insert other valid exit codes here, exit codes for ms http://msdn.microsoft.com/en-us/library/aa368542(VS.85).aspx
# main helpers - these have error handling tucked into them already
# installer, will assert administrative rights
# if removing $url64, please remove from here
Install-ChocolateyPackage "$packageName" "$installerType" "$silentArgs" "$url" -validExitCodes $validExitCodes
# download and unpack a zip file
In workrave for silent installation use the keys
/verysilent flags /norestart
. The list of the most common keys in a public installer is specified in the description to the variable $silentArgs
. You can also read this: unattended.sourceforge.net/installers.phpYou can now build the package, test it, and pour on chocolatey.org. To facilitate this process, I wrote 4 simple .bat file, copy-paste code, I will not, you can download from github.
the
-
the
- 1-build-package.bat — assemble pack the
- 2-test-package.bat — performs a local installation of the package to test. the
- 3-push-package.bat — floods the packet on chocolatey.org the
- 4-helper.bat — wrapper on the first three. Collector to run, and on a local installation and push, asked to do or not.
I.e. to build our package just launched the first three .bat file in order or 4-th.
That's all.
the
-
the
- github.com/Claud/chocolatey-packages/tree/master/workrave what happened. the
- github.com/Claud/chocolatey-packages — my repository with packages. the
- github.com/Claud/chocolatey-packages/blob/master/smartgit-with-jre/src/tools/chocolateyInstall.ps1 — an example of how to act when the installation file is distributed in zip archive. the
- github.com/chocolatey/chocolatey/wiki/CreatePackages details on building packages. Although there is more about the General rules. the
- github.com/chocolatey/chocolatey/wiki/CreatePackagesQuickStart — about the Assembly, but in a shortened form. the
- unattended.sourceforge.net/installers.php — about the keys for silent installation. the
- chocolatey.org project website.
About errors please write to PM.