Skip to content

Commit 4ff5737

Browse files
mcmehrtenshaampie
andauthored
go-sh: new package, includes shfmt and gosh utils (spack#1245)
Co-authored-by: Harmen Stoppels <harmenstoppels@gmail.com>
1 parent 3aa7b7c commit 4ff5737

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Copyright Spack Project Developers. See COPYRIGHT file for details.
2+
#
3+
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
4+
5+
from spack_repo.builtin.build_systems.go import GoPackage
6+
7+
from spack.package import *
8+
9+
10+
class GoSh(GoPackage):
11+
"""A shell parser, formatter, and interpreter. Supports POSIX
12+
Shell, Bash, and mksh. Requires Go 1.23 or later."""
13+
14+
homepage = "https://github.com/mvdan/sh"
15+
url = "https://github.com/mvdan/sh/archive/refs/tags/v3.12.0.tar.gz"
16+
17+
maintainers("mcmehrtens")
18+
license("BSD-3-Clause", checked_by="mcmehrtens")
19+
20+
version("3.12.0", sha256="ac15f42feeba55af29bd07698a881deebed1cd07e937effe140d9300e79d5ceb")
21+
22+
depends_on("go@1.23:", type="build", when="@3.12.0:")
23+
24+
variant("shfmt", default=True, description="Build and install shfmt")
25+
variant("gosh", default=False, description="Build and install gosh")
26+
conflicts("~shfmt~gosh", msg="One of shfmt or gosh must be specified")
27+
28+
@property
29+
def sanity_check_is_file(self):
30+
files = []
31+
if self.spec.satisfies("+shfmt"):
32+
files.append(join_path("bin", "shfmt"))
33+
if self.spec.satisfies("+gosh"):
34+
files.append(join_path("bin", "gosh"))
35+
return files
36+
37+
def build(self, spec: Spec, prefix: Prefix) -> None:
38+
"""Runs ``go build`` in the source directory for the specified
39+
variants."""
40+
common_flags = (
41+
"-p",
42+
str(make_jobs),
43+
"-modcacherw",
44+
"-ldflags",
45+
f"-s -w -X main.version={spec.version}",
46+
)
47+
with working_dir(self.build_directory):
48+
if spec.satisfies("+shfmt"):
49+
go("build", "-o", "shfmt", *common_flags, "cmd/shfmt/main.go")
50+
if spec.satisfies("+gosh"):
51+
go("build", "-o", "gosh", *common_flags, "cmd/gosh/main.go")
52+
53+
def install(self, spec: Spec, prefix: Prefix) -> None:
54+
"""Install built binaries into prefix bin."""
55+
with working_dir(self.build_directory):
56+
mkdirp(prefix.bin)
57+
if spec.satisfies("+shfmt"):
58+
install("shfmt", prefix.bin)
59+
if spec.satisfies("+gosh"):
60+
install("gosh", prefix.bin)

0 commit comments

Comments
 (0)