Permalink
Browse files

Create a package for Windows via chocolatey

--
Reviewed-on: #1742
MOS_MIGRATED_REVID=134283632
  • Loading branch information...
1 parent 6bc0c7c commit a27949e27d4335216c28e77c41d078e338b5d4fb @petemounce petemounce committed with meteorcloudy Sep 26, 2016
View
@@ -92,3 +92,4 @@ Philipp Wollermann <philwo@google.com>
Jie Yu <yujie.jay@gmail.com>
Andrew Z Allen <me@andrewzallen.com>
Ming Zhao <mzhao@luminatewireless.com>
+Peter Mounce <pete@neverrunwithscissors.com>
@@ -0,0 +1,6 @@
+*.nupkg
+*.zip
+bazel.nuspec
+chocolateyinstall.ps1
+tools/*.exe
+tools/LICENSE.txt
@@ -0,0 +1,91 @@
+## Summary
+How do I create packages? See https://chocolatey.org/docs/create-packages
+
+If you are submitting packages to the community feed (https://chocolatey.org)
+always try to ensure you have read, understood and adhere to the create
+packages wiki link above.
+
+## Automatic Packaging Updates?
+Consider making this package an automatic package, for the best
+maintainability over time. Read up at https://chocolatey.org/docs/automatic-packages
+
+## Shim Generation
+Any executables you include in the package or download (but don't call
+install against using the built-in functions) will be automatically shimmed.
+
+This means those executables will automatically be included on the path.
+Shim generation runs whether the package is self-contained or uses automation
+scripts.
+
+By default, these are considered console applications.
+
+If the application is a GUI, you should create an empty file next to the exe
+named 'name.exe.gui' e.g. 'bob.exe' would need a file named 'bob.exe.gui'.
+See https://chocolatey.org/docs/create-packages#how-do-i-set-up-shims-for-applications-that-have-a-gui
+
+If you want to ignore the executable, create an empty file next to the exe
+named 'name.exe.ignore' e.g. 'bob.exe' would need a file named
+'bob.exe.ignore'.
+See https://chocolatey.org/docs/create-packages#how-do-i-exclude-executables-from-getting-shims
+
+## Self-Contained?
+If you have a self-contained package, you can remove the automation scripts
+entirely and just include the executables, they will automatically get shimmed,
+which puts them on the path. Ensure you have the legal right to distribute
+the application though. See https://chocolatey.org/docs/legal.
+
+You should read up on the Shim Generation section to familiarize yourself
+on what to do with GUI applications and/or ignoring shims.
+
+## Automation Scripts
+You have a powerful use of Chocolatey, as you are using PowerShell. So you
+can do just about anything you need. Choco has some very handy built-in
+functions that you can use, these are sometimes called the helpers.
+
+### Built-In Functions
+https://chocolatey.org/docs/helpers-reference
+
+A note about a couple:
+* Get-BinRoot - this is a horribly named function that doesn't do what new folks think it does. It gets you the 'tools' root, which by default is set to 'c:\tools', not the chocolateyInstall bin folder - see https://chocolatey.org/docs/helpers-get-tools-location
+* Install-BinFile - used for non-exe files - executables are automatically shimmed... - see https://chocolatey.org/docs/helpers-install-bin-file
+* Uninstall-BinFile - used for non-exe files - executables are automatically shimmed - see https://chocolatey.org/docs/helpers-uninstall-bin-file
+
+### Getting package specific information
+Use the package parameters pattern - see https://chocolatey.org/docs/how-to-parse-package-parameters-argument
+
+### Need to mount an ISO?
+https://chocolatey.org/docs/how-to-mount-an-iso-in-chocolatey-package
+
+
+### Environment Variables
+Chocolatey makes a number of environment variables available (You can access any of these with $env:TheVariableNameBelow):
+
+ * TEMP = Overridden to the CacheLocation, but may be the same as the original TEMP folder
+ * ChocolateyInstall = Top level folder where Chocolatey is installed
+ * chocolateyPackageName = The name of the package, equivalent to the id in the nuspec (0.9.9+)
+ * chocolateyPackageVersion = The version of the package, equivalent to the version in the nuspec (0.9.9+)
+ * chocolateyPackageFolder = The top level location of the package folder
+
+#### Advanced Environment Variables
+The following are more advanced settings:
+
+ * chocolateyPackageParameters = (0.9.8.22+)
+ * CHOCOLATEY_VERSION = The version of Choco you normally see. Use if you are 'lighting' things up based on choco version. (0.9.9+)
+ - Otherwise take a dependency on the specific version you need.
+ * chocolateyForceX86 = If available and set to 'true', then user has requested 32bit version. (0.9.9+)
+ - Automatically handled in built in Choco functions.
+ * OS_PLATFORM = Like Windows, OSX, Linux. (0.9.9+)
+ * OS_VERSION = The version of OS, like 6.1 something something for Windows. (0.9.9+)
+ * OS_NAME = The reported name of the OS. (0.9.9+)
+ * IS_PROCESSELEVATED = Is the process elevated? (0.9.9+)
+
+#### Experimental Environment Variables
+The following are experimental or use not recommended:
+
+ * OS_IS64BIT = This may not return correctly - it may depend on the process the app is running under (0.9.9+)
+ * CHOCOLATEY_VERSION_PRODUCT = the version of Choco that may match CHOCOLATEY_VERSION but may be different (0.9.9+)
+ - it's based on git describe
+ * IS_ADMIN = Is the user an administrator? But doesn't tell you if the process is elevated. (0.9.9+)
+ * chocolateyInstallOverride = Not for use in package automation scripts. (0.9.9+)
+ * chocolateyInstallArguments = the installer arguments meant for the native installer. You should use chocolateyPackageParameters intead. (0.9.9+)
+
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Read this before creating packages: https://chocolatey.org/docs/create-packages -->
+<!-- It is especially important to read the above link to understand additional requirements when publishing packages to the community feed aka dot org (https://chocolatey.org/packages). -->
+
+<!-- Test your packages in a test environment: https://github.com/chocolatey/chocolatey-test-environment -->
+
+<!--
+This is a nuspec. It mostly adheres to https://docs.nuget.org/create/Nuspec-Reference. Chocolatey uses a special version of NuGet.Core that allows us to do more than was initially possible. As such there are certain things to be aware of:
+
+* the package xmlns schema url may cause issues with nuget.exe
+* Any of the following elements can ONLY be used by choco tools - projectSourceUrl, docsUrl, mailingListUrl, bugTrackerUrl, packageSourceUrl, provides, conflicts, replaces
+* nuget.exe can still install packages with those elements but they are ignored. Any authoring tools or commands will error on those elements
+-->
+
+<!-- You can embed software files directly into packages, as long as you are not bound by distribution rights. -->
+<!-- * If you are an organization making private packages, you probably have no issues here -->
+<!-- * If you are releasing to the community feed, you need to consider distribution rights. -->
+<package xmlns="http://schemas.microsoft.com/packaging/2015/06/nuspec.xsd">
+ <metadata>
+ <!-- == PACKAGE SPECIFIC SECTION == -->
+ <!-- This section is about this package, although id and version have ties back to the software -->
+ <!-- id is lowercase and if you want a good separator for words, use '-', not '.'. Dots are only acceptable as suffixes for certain types of packages, e.g. .install, .portable, .extension, .template -->
+ <!-- If the software is cross-platform, attempt to use the same id as the debian/rpm package(s) if possible. -->
+ <id>bazel</id>
+ <!-- version should MATCH as closely as possible with the underlying software -->
+ <!-- Is the version a prerelease of a version? https://docs.nuget.org/create/versioning#creating-prerelease-packages -->
+ <!-- Note that unstable versions like 0.0.1 can be considered a released version, but it's possible that one can release a 0.0.1-beta before you release a 0.0.1 version. If the version number is final, that is considered a released version and not a prerelease. -->
+ <version>$tvVersion</version>
+ <!-- <packageSourceUrl>Where is this Chocolatey package located (think GitHub)? packageSourceUrl is highly recommended for the community feed</packageSourceUrl>-->
+ <packageSourceUrl>https://github.com/bazelbuild/bazel/tree/master/scripts/packages/chocolatey</packageSourceUrl>
+ <!-- owners is a poor name for maintainers of the package. It sticks around by this name for compatibility reasons. It basically means you. -->
+ <owners>Peter Mounce</owners>
+
+ <!-- ============================== -->
+
+ <!-- == SOFTWARE SPECIFIC SECTION == -->
+ <!-- This section is about the software itself -->
+ <title>bazel (Install)</title>
+ <authors>The Bazel Authors</authors>
+ <!-- projectUrl is required for the community feed -->
+ <projectUrl>https://bazel.io</projectUrl>
+ <iconUrl>https://cdn.rawgit.com/bazelbuild/bazel/master/site/images/bazel-logo.png</iconUrl>
+ <copyright>Copyright 2016 The Bazel Authors. All rights reserved.</copyright>
+ <!-- If there is a license Url available, it is is required for the community feed -->
+ <licenseUrl>https://github.com/bazelbuild/bazel/tree/master/LICENSE.txt</licenseUrl>
+ <requireLicenseAcceptance>true</requireLicenseAcceptance>
+ <projectSourceUrl>https://github.com/bazelbuild/bazel.git</projectSourceUrl>
+ <docsUrl>https://www.bazel.io/versions/master/docs/getting-started.html</docsUrl>
+ <mailingListUrl>https://groups.google.com/forum/#!forum/bazel-discuss</mailingListUrl>
+ <bugTrackerUrl>https://github.com/bazelbuild/bazel/issues</bugTrackerUrl>
+ <tags>bazel build automation</tags>
+ <summary>Correct, reproducible, fast builds for everyone</summary>
+ <description>Build software of any size, quickly and reliably, just as engineers do at Google.
+
+Bazel is Google's own build tool, now publicly available in Beta.
+
+Bazel has built-in support for building both client and server software, including client applications for both Android and iOS platforms. It also provides an extensible framework that you can use to develop your own build rules.
+
+## Package parameters
+
+Supply like `--params="/option:value ..."` ([see docs for --params](https://github.com/chocolatey/choco/wiki/CommandsInstall#options-and-switches))
+
+* `msys2Path` (optional, defaults to c:\tools\msys64) - override this if msys2 is installed elsewhere.
+</description>
+ <releaseNotes>https://github.com/bazelbuild/bazel/releases/tag/$tvVersion</releaseNotes>
+ <!-- =============================== -->
+
+ <!-- Specifying dependencies and version ranges? https://docs.nuget.org/create/versioning#specifying-version-ranges-in-.nuspec-files -->
+ <!--<dependencies>
+ <dependency id="" version="__MINIMUM_VERSION__" />
+ <dependency id="" version="[__EXACT_VERSION__]" />
+ <dependency id="" version="[_MIN_VERSION_INCLUSIVE, MAX_VERSION_INCLUSIVE]" />
+ <dependency id="" version="[_MIN_VERSION_INCLUSIVE, MAX_VERSION_EXCLUSIVE)" />
+ <dependency id="" />
+ <dependency id="chocolatey-uninstall.extension" />
+ </dependencies>-->
+ <dependencies>
+ <dependency id="jdk8" version="[8.0.102,)"/>
+ <dependency id="msys2" version="[20150916.0.1,)"/>
+ <dependency id="python2" version="[2.7.11,3.0)"/>
+ </dependencies>
+ <!-- chocolatey-uninstall.extension - If supporting 0.9.9.x (or below) and including a chocolateyUninstall.ps1 file to uninstall an EXE/MSI, you probably want to include chocolatey-uninstall.extension as a dependency. Please verify whether you are using a helper function from that package. -->
+
+ <!--<provides>NOT YET IMPLEMENTED</provides>-->
+ <!--<conflicts>NOT YET IMPLEMENTED</conflicts>-->
+ <!--<replaces>NOT YET IMPLEMENTED</replaces>-->
+ </metadata>
+ <files>
+ <!-- this section controls what actually gets packaged into the Chocolatey package -->
+ <file src="tools\**" target="tools" />
+ <!--Building from Linux? You may need this instead: <file src="tools/**" target="tools" />-->
+ </files>
+</package>
@@ -0,0 +1,52 @@
+param(
+ [string] $version = "0.3.1",
+ [switch] $isRelease
+)
+
+$tvVersion = $version
+$tvFilename = "bazel_$($version)_windows_x86_64.zip"
+if ($isRelease) {
+ $tvUri = "https://github.com/bazelbuild/bazel/releases/download/$($version)/$($tvFilename)"
+} else {
+ $tvUri = "http://localhost:8000/$($tvFilename)"
+}
+write-host "download uri: $($tvUri)"
+
+rm -force -ErrorAction SilentlyContinue ./*.nupkg
+rm -force -ErrorAction SilentlyContinue ./*.zip
+rm -force -ErrorAction SilentlyContinue ./bazel.nuspec
+rm -force -ErrorAction SilentlyContinue ./tools/chocolateyinstall.ps1
+
+Add-Type -A System.IO.Compression.FileSystem
+$outputDir = "$pwd/../../../output"
+$zipFile = "$pwd/$($tvFilename)"
+write-host "Creating zip package with $outputDir/bazel.exe: $zipFile"
+Compress-Archive -Path "$outputDir/bazel.exe" -DestinationPath $zipFile
+$tvChecksum = (get-filehash $zipFile -algorithm sha256).Hash
+write-host "zip sha256: $tvChecksum"
+
+$nuspecTemplate = get-content "bazel.nuspec.template" | out-string
+$nuspecExpanded = $ExecutionContext.InvokeCommand.ExpandString($nuspecTemplate)
+add-content -value $nuspecExpanded -path bazel.nuspec
+
+$installerScriptTemplate = get-content "chocolateyinstall.ps1.template" | out-string
+$installerScriptExpanded = $ExecutionContext.InvokeCommand.ExpandString($installerScriptTemplate)
+$installerScriptExpanded = $installerScriptExpanded -replace "ps_var_","$"
+$installerScriptExpanded = $installerScriptExpanded -replace "escape_char","``"
+add-content -value $installerScriptExpanded -path ./tools/chocolateyinstall.ps1
+
+$uninstallerScriptTemplate = get-content "chocolateyuninstall.ps1.template" | out-string
+$uninstallerScriptExpanded = $ExecutionContext.InvokeCommand.ExpandString($uninstallerScriptTemplate)
+$uninstallerScriptExpanded = $uninstallerScriptExpanded -replace "ps_var_","$"
+$uninstallerScriptExpanded = $uninstallerScriptExpanded -replace "escape_char","``"
+add-content -value $uninstallerScriptExpanded -path ./tools/chocolateyuninstall.ps1
+
+write-host "Copying LICENSE.txt from repo-root to tools directory"
+$licenseHeader = @"
+From: https://github.com/bazelbuild/bazel/blob/master/LICENSE.txt
+
+"@
+add-content -value $licenseHeader -path "./tools/LICENSE.txt"
+add-content -value (get-content "../../../LICENSE.txt") -path "./tools/LICENSE.txt"
+
+choco pack ./bazel.nuspec
@@ -0,0 +1,69 @@
+ps_var_ErrorActionPreference = 'Stop'; # stop on all errors
+ps_var_packageName = 'bazel'
+ps_var_url = "$tvUri"
+ps_var_checksum = "$tvChecksum"
+ps_var_checksumType = 'sha256'
+ps_var_url64bit = ps_var_url
+ps_var_checksum64 = "$tvChecksum"
+ps_var_checksumType64 = ps_var_checksumType
+ps_var_toolsDir = Split-Path -parent ps_var_MyInvocation.MyCommand.Definition
+ps_var_packageDir = Split-Path -parent ps_var_toolsDir
+ps_var_binRoot = (Get-ToolsLocation) -replace "\\", "/"
+write-host "package directory: ps_var_packageDir"
+write-host "destination directory: ps_var_packageDir"
+write-host "url: ps_var_url"
+write-host "url64bit: ps_var_url64bit"
+
+Install-ChocolateyZipPackage -PackageName "ps_var_packageName" escape_char
+ -Url "ps_var_url" escape_char
+ -Checksum "ps_var_checksum" escape_char
+ -ChecksumType "ps_var_checksumType" escape_char
+ -Url64bit "ps_var_url64bit" escape_char
+ -Checksum64 "ps_var_checksum64" escape_char
+ -Checksum64Type "ps_var_checksumType64" escape_char
+ -UnzipLocation "ps_var_packageDir"
+
+write-host "Ensure that msys2 dll is present in PATH to allow bazel to be run from non-msys2 shells"
+
+# stolen from docs: https://github.com/chocolatey/choco/wiki/How-To-Parse-PackageParameters-Argument
+ps_var_msys2Path = "c:\tools\msys64"
+if (ps_var_packageParameters)
+{
+ ps_var_match_pattern = "\/(?<option>([a-zA-Z]+)):(?<value>([escape_char"'])?([a-zA-Z0-9- _\\:\.]+)([escape_char"'])?)|\/(?<option>([a-zA-Z]+))"
+ ps_var_option_name = 'option'
+ ps_var_value_name = 'value'
+
+ if (ps_var_packageParameters -match ps_var_match_pattern)
+ {
+ ps_var_results = ps_var_packageParameters | Select-String ps_var_match_pattern -AllMatches
+ ps_var_results.matches | % {
+ ps_var_arguments.Add(
+ ps_var__.Groups[ps_var_option_name].Value.Trim(),
+ ps_var__.Groups[ps_var_value_name].Value.Trim())
+ }
+ }
+ else
+ {
+ Throw "Package Parameters were found but were invalid (REGEX Failure)"
+ }
+
+ if (ps_var_arguments.ContainsKey("msys2Path")) {
+ ps_var_msys2Path = ps_var_arguments["msys2Path"]
+ Write-Host "msys2Path Argument Found: ps_var_msys2Path"
+ }
+}
+Install-ChocolateyPath -PathToInstall "ps_var_msys2Path\usr\bin" -PathType "Machine"
+
+write-host @"
+bazel installed to ps_var_packageDir
+
+To use it in msys2, you should add that to your msys2 PATH:
+ export PATH=ps_var_(ps_var_packageDir):escape_charps_var_PATH
+
+You also need, in your msys2 environment:
+ export JAVA_HOME="escape_charps_var_(ls -d C:/Program\ Files/Java/jdk* | sort | tail -n 1)escape_char"
+ export BAZEL_SH=c:/tools/msys64/usr/bin/bash.exe
+ export BAZEL_PYTHON=c:/tools/python2/python.exe
+
+See also https://bazel.io/docs/windows.html
+"@
@@ -0,0 +1,10 @@
+write-host @"
+bazel is being uninstalled.
+
+You should now:
+* Remove "<msys>/usr/bin" from your PATH
+
+Sorry this isn't automatic; there are no helper functions available within chocolatey (as of v0.10.0) to do these steps as part of the uninstall. See:
+* https://github.com/chocolatey/choco/issues/310
+* https://github.com/chocolatey/package-validator/issues/148
+"@
@@ -0,0 +1,23 @@
+param(
+ [string] $version = "0.3.1"
+)
+
+choco uninstall bazel --force -y
+
+choco install "./bazel.$($version).nupkg" --verbose --debug --force -y
+
+if ($LASTEXITCODE -ne 0)
+{
+ write-error "`$LASTEXITCODE was not zero. Inspect the output from choco install above."
+ exit 1
+}
+
+write-host @"
+The package should have installed without errors.
+
+Now:
+* open an msys2 shell
+* Make sure your environment is accurate (see ``./tools/chocolateyinstall.ps1`` output)
+* run ``bazel version`` in that msys2 shell
+* ... and you should get a version number back
+"@
@@ -0,0 +1,17 @@
+VERIFICATION
+Verification is intended to assist the Chocolatey moderators and community
+in verifying that this package's contents are trustworthy.
+
+Download the zip file from github releases
+Take its sha256
+If it matches what is in package's chocolateyinstall.ps1, it's authentic.
+
+Example:
+
+```
+invoke-webrequest https://github.com/bazelbuild/bazel/releases/0.3.1/bazel_0.3.1_windows_x86_64.zip -outfile bazel_0.3.1_windows_x86_64.zip
+get-filehash bazel_0.3.1_windows_x86_64.zip -algorithm sha256
+# compare that to what's in <package>/tools/chocolateyinstall.ps1
+```
+
+See also package build script `<repo>/scripts/packages/chocolatey/buildAndInstall.ps1`.
@@ -0,0 +1,20 @@
+write-host "Attempting to stop any running bazel processes, to allow upgrade"
+try
+{
+ $running = get-process bazel
+}
+catch
+{
+ write-host "No running bazel processes to stop"
+ $running = @()
+}
+
+if ($running)
+{
+ write-host "Stopping bazel processes"
+ foreach($p in $running)
+ {
+ stop-process $p
+ write-verbose "Stopped $($p.ProcessName) $($p.Id)"
+ }
+}
Oops, something went wrong.

0 comments on commit a27949e

Please sign in to comment.