-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Michiel Borkent <michielborkent@gmail.com>
- Loading branch information
Showing
16 changed files
with
619 additions
and
121 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Babashka Process Test Resources | ||
|
||
For test coverage, we need executables that emit their path and their current working directory. | ||
For example, on Windows: | ||
``` | ||
> .\print-dirs.bat | ||
exepath: Z:\babashka\process\test-resources\print-dirs.bat | ||
workdir: Z:\babashka\process\test-resources | ||
> cd .. | ||
>test-resources\print-dirs | ||
exepath: Z:\babashka\process\test-resources\print-dirs.exe | ||
workdir: Z:\babashka\process | ||
``` | ||
|
||
This is straightforward for Linux/macOS `.sh`, Windows `.cmd` and Windows `.bat` variants, but for the Windows `.exe` variant, we need to create a binary. | ||
|
||
## Generating a Windows .exe | ||
|
||
It doesn't matter how `print-dirs.exe` is created; it won't need to be recreated often (we commit it to source control). | ||
|
||
Since it generates relatively small binaries very quickly for any architecture, we've built a `print-dirs.exe` using Go. | ||
Source is in [print-dirs.go](print-dirs.go) | ||
|
||
### Minimal Build | ||
Presuming you have [Go](https://go.dev/) installed, from this dir: | ||
|
||
```shell | ||
GOOS=windows GOARCH=amd64 go build -o print-dirs.exe print-dirs.go | ||
``` | ||
|
||
For me, this generated a 1.9mb `print-dirs.exe`. | ||
|
||
### Build a Smaller Exe | ||
Since this `.exe` is checked into version control, I opted to create a smaller binary by adding the following `-ldflags` to strip debug info: | ||
|
||
```shell | ||
GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o print-dirs.exe print-dirs.go | ||
``` | ||
|
||
For me, the generated `print-dirs.exe` is now 1.3mb. | ||
To further shrink down the binary, you can use [UPX](https://upx.github.io/) (which is also cross-platform): | ||
|
||
``` | ||
upx --ultra-brute print-dirs.exe | ||
``` | ||
|
||
And now `print-dirs.exe` is 457kb. | ||
|
||
### Building With Docker | ||
If you don't want to install Go and UPX on your dev box but have docker installed, you can build from a docker image like so: | ||
|
||
``` shell | ||
docker run --rm \ | ||
-v "$PWD":/src \ | ||
-w /src \ | ||
devopsworks/golang-upx:latest \ | ||
bash -c 'GOOS=windows GOARCH=amd64 \ | ||
go build -ldflags "-s -w" -o print-dirs.exe print-dirs.go && | ||
upx --ultra-brute print-dirs.exe' | ||
``` | ||
|
||
This is ultimately the command I ran to create our Windows .exe. | ||
|
||
## What about Windows .com? | ||
|
||
These days, `.com` executables are rare. | ||
|
||
I could not find an easy way to create one. | ||
Our tests use `print-dirs.exe` copied to `print-dirs.com` to support `.com` test cases. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@echo off | ||
echo exepath: %~dpnx0 | ||
echo workdir: %cd% |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@echo off | ||
echo exepath: %~dpnx0 | ||
echo workdir: %cd% |
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func main() { | ||
executable, _ := os.Executable() | ||
fmt.Fprintln(os.Stdout,"exepath:", executable) | ||
cwd, _ := os.Getwd() | ||
fmt.Fprintln(os.Stdout,"workdir:", cwd) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Write-Output "exepath: $($MyInvocation.MyCommand.Path)" | ||
Write-Output "workdir: $(Get-Location)" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env sh | ||
echo "exepath: $(readlink -f "$0")" | ||
echo "workdir: $(pwd)" |
Oops, something went wrong.