📜 ⬆️ ⬇️

We write your package for Chocolatey



Chocolatey - batch manager for windows greatly facilitates the installation of programs, and how much it saves time. They already wrote about this miracle in Habré and even showed how to write my own package for it, but there was more manual work, but I want to show how to simplify this thing a little.

Getting started


If you have not installed chocolatey itself, fix this:

@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%systemdrive%\chocolatey\bin 

Close and reopen the console to refresh data on variables.
')
Chocolatey installed, now install the packages that will be useful to us:

 cinst warmup cinst git cinst nuget.commandline 

Close and reopen the console to refresh data on variables.

Go to the folder with established chocolatey, clone the repository with templates, and go to our templates.

 cd %ChocolateyInstall% git clone https://github.com/chocolatey/chocolateytemplates.git cd chocolateytemplates\_templates 

Edit some of the default settings.



Now you can activate templates, from which we will make our packages (in essence, a link is created to the directory with the desired template). To do this, being in the %ChocolateyInstall%\chocolateytemplates\_templates execute the command (I run only from under admin ):

 warmup addTemplateFolder chocolatey "%CD%\chocolatey" 

Go to the website chocolatey.org , register, go to your profile, click-
“Your key is hidden for privacy, click to show.“ We see three commands, we execute only the first one: install the API Key (if you swear that nuget.exe is not found, then remove the extension - .exe).

On this with the preparation of everything, now you can break the mass release of packages


Official documentation recommends naming all new packages in lower case by separating words with a “-” or simply omitting spaces. Also, at first it is recommended to use the search package with the desired program, and only if there is none, make your own.

For storage of source codes we will use the following structure :.

 packages
   | - workrave
     | - src
       | - tools
       | - workrave.nuspec
       | - ... ..
     | - build
   | - package-2

Being in the packages directory we execute:

 warmup chocolatey workrave 

In the workrave folder that workrave create a src and transfer the contents of the workrave .

Open the file workrave.nuspec . This is the usual xml , with the description of our package. The name of the tags speaks for itself: title - the name of the program, which will be displayed in the list of packages on the website chocolatey.org; author is the creator of the program you are putting into the package ....

The most interesting is in the file tools\chocolateyInstall.ps1

Code to
 #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" # 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" "$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 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 #} 


Here are three blocks:



For our test package, we bring the code to this form.

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 /norestart' # "/s /S /q /Q /quiet /silent /SILENT /VERYSILENT" # 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 


The workrave uses the /verysilent /norestart keys for silent installation. The list of the most common keys in public installers is given in the description of the $silentArgs variable. You can also read this: unattended.sourceforge.net/installers.php

Now you can build a package, test it, and upload it to chocolatey.org. To facilitate this process, I wrote 4 unpretentious .bat files, I will not copy-paste the code, you can download them from github .



Those. to build our package, simply run the first three .bat files in order or 4th.

That's all.



For errors, please write to the LAN.

Source: https://habr.com/ru/post/223373/


All Articles