diff --git a/src/please.go b/src/please.go index 5f72bd82df..104bbccd77 100644 --- a/src/please.go +++ b/src/please.go @@ -107,6 +107,7 @@ var opts struct { Rebuild bool `long:"rebuild" description:"To force the optimisation and rebuild one or more targets."` NoDownload bool `long:"nodownload" hidden:"true" description:"Don't download outputs after building. Only applies when using remote build execution."` Download bool `long:"download" hidden:"true" description:"Force download of all outputs regardless of original target spec. Only applies when using remote build execution."` + OutDir string `long:"out_dir" optional:"true" description:"Copies build output to given directory"` Args struct { Targets []core.BuildLabel `positional-arg-name:"targets" description:"Targets to build"` } `positional-args:"true" required:"true"` @@ -456,7 +457,24 @@ var opts struct { var buildFunctions = map[string]func() int{ "build": func() int { success, state := runBuild(opts.Build.Args.Targets, true, false, false) - return toExitCode(success, state) + if !success || opts.Build.OutDir == "" { + return toExitCode(success, state) + } + for _, label := range state.ExpandOriginalLabels() { + target := state.Graph.TargetOrDie((label)) + for _, out := range target.Outputs() { + from := filepath.Join(target.OutDir(), out) + fm, err := os.Lstat(from) + if err != nil { + log.Fatalf("Failed to get file mode on build output files: %s", err) + } + err = fs.CopyFile(from, filepath.Join(opts.Build.OutDir, target.PackageDir(), out), fm.Mode()) + if err != nil { + log.Fatalf("Failed to output build to provided directory: %s", err) + } + } + } + return 0 }, "hash": func() int { if opts.Hash.Update { diff --git a/test/build_outdir/BUILD b/test/build_outdir/BUILD new file mode 100644 index 0000000000..03d172aa1c --- /dev/null +++ b/test/build_outdir/BUILD @@ -0,0 +1,8 @@ +subinclude("//test/build_defs") + +please_repo_e2e_test( + name = "out_dir_test", + expected_output = {"bar/foo/testy": "wibble wibble wibble"}, + plz_command = "plz build //foo:testy --out_dir=bar", + repo = "test_repo", +) diff --git a/test/build_outdir/test_repo/.plzconfig b/test/build_outdir/test_repo/.plzconfig new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/build_outdir/test_repo/foo/BUILD_FILE b/test/build_outdir/test_repo/foo/BUILD_FILE new file mode 100644 index 0000000000..9a5a418235 --- /dev/null +++ b/test/build_outdir/test_repo/foo/BUILD_FILE @@ -0,0 +1,4 @@ +text_file( + name = "testy", + content = "wibble wibble wibble", +) \ No newline at end of file