Chocolatey 是一个基于Nuget的Windows包管理工具,截止到我更新此文章的时候,一共有3.7k多个独立包。本文旨在介绍Chocolatey这一工具的安装和用法,希望能有更多人享受到该工具的便利之处。 安装chocolateyCMD打开一个具有管理员权限的命令行窗口,执行如下命令: @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https:///install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin PowerShell打开一个具有管理员权限的PowerShell窗口,执行如下命令: iex ((new-object net.webclient).DownloadString('https:///install.ps1')) 使用chocolatey安装包安装包十分的容易,只需要打开管理员权限的命令行即可: choco install <package> 卸载包卸载也同样十分简单,打开管理员权限命令行,执行: choco uninstall <package> 更新包更新包则需要使用如下命令: choco upgrade <package> 为chocolatey做贡献Chocolatey的一个特点就是社区化的环境,每一个人都可以向它提交自己喜爱的包。因为Chocolatey总是从官方下载所需要的包,所以避免了授权等问题。当然,本来收费的软件,同样需要收费,比如JetBrains系列的产品。 注册APIKey提交包则会略为复杂一点,首先你需要在 https:/// 注册一个账号,并获取跟你账户绑定的唯一APIKey。然后在命令行中执行如下命令: choco apikey -k <your key here> -s https:// 新建软件包在命令行中运行 choco new <package name here> # 为方便理解,此处使用test做包名
则当前目录下就会自动生成一个名为 test.nuspec tools |--chocolateyinstall.ps1 |--chocolateyuninstall.ps1 |--ReadMe.md 完善信息修改test.nuspec自动生成的描述文件在需要填写的位置都有大写英文注释,只需要按照要求一一填写即可,下面我列举一份已经通过审核的Datagrip的描述文件作为参考: <?xml version="1.0" encoding="utf-8"?> <!-- Do not remove this test for UTF-8: if “Ω” doesn’t appear as greek uppercase omega letter enclosed in quotation marks, you should use an editor that supports UTF-8, not this one. --> <package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd"> <metadata> <!-- Read this before publishing packages to : https://github.com/chocolatey/chocolatey/wiki/CreatePackages --> <id>datagrip</id> <title>DataGrip</title> <version>1.0.1</version> <authors>JetBrains</authors> <owners>Xuanwo</owners> <summary>Your Swiss Army Knife for Databases and SQL</summary> <description>## Intelligent query console Allows you to execute queries in different modes and provides local history that keeps track of all your activity and protects you from losing your work. ......(省略) </description> <projectUrl>https://www./datagrip/</projectUrl> <packageSourceUrl>https://download./datagrip/datagrip-1.0.1.exe</packageSourceUrl> <!--<projectSourceUrl></projectSourceUrl> <docsUrl></docsUrl> <mailingListUrl></mailingListUrl>--> <bugTrackerUrl>https://youtrack./issues/DBE</bugTrackerUrl> <tags>datagrip admin jetbrains trail 30days</tags> <copyright>Commercial</copyright> <!--<licenseUrl></licenseUrl>--> <requireLicenseAcceptance>false</requireLicenseAcceptance> <!--<iconUrl>http://cdn./__REPLACE_YOUR_REPO__/master/icons/datagrip.png</iconUrl>--> <!--<dependencies> <dependency id="" version="__VERSION__" /> <dependency id="" /> </dependencies>--> <releaseNotes>https://confluence./display/DBE/DataGrip+1.0.1+Release+Notes</releaseNotes> <!--<provides></provides>--> </metadata> <files> <file src="tools\**" target="tools" /> </files> </package>
有一个比较坑的地方是 补充chocolateyinstall.ps1
下面同样列举一份已经填写完成的样例: $packageName= 'datagrip' $installerType = 'EXE' $toolsDir = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)" $url = 'https://download./datagrip/datagrip-1.0.1.exe' $url64 = $url $silentArgs = '/S' $validExitCodes = @(0) Install-ChocolateyPackage "$packageName" "$installerType" "$silentArgs" "$url" "$url64" -validExitCodes $validExitCodes 需要注意的是,填写完毕之后,一定要在Powershell中运行下列代码以删除所有的注释文字: $f='c:\path\to\thisFile.ps1' gc $f | ? {$_ -notmatch "^\s*#"} | % {$_ -replace '(^.*?)\s*?[^``]#.*','$1'} | Out-File $f+".~" -en utf8; mv -fo $f+".~" $f 打包上传
所有的信息填写完成后,在 如果有重要的信息没有填写,choco将会报错,并红字提示: LicenseUrl cannot be empty. ProjectUrl cannot be empty. PackageSourceUrl cannot be empty. 如果没有问题,则会有如下提示: Attempting to build package from 'datagrip.nuspec'. Successfully created package 'datagrip.1.0.1.nupkg'
之后执行 |
|