Skip to content

Commit

Permalink
Introduce basic support for versioned CUE modules
Browse files Browse the repository at this point in the history
  • Loading branch information
seh committed Jul 13, 2024
1 parent c4a5b82 commit 7a223cb
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
21 changes: 19 additions & 2 deletions cue/cue-run-from-runfiles
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,31 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e

function usage() {
printf "usage: %s [-i instance_path] [-m module_file] [-p package_name] cue_tool cue_subcommand extra_args_file packageless_files_file output_file [args...]\n" "$(basename "${0}")" 1>&2
printf "usage: %s [-c cue_cache_directory] [-i instance_path] [-m module_file] [-p package_name] cue_tool cue_subcommand extra_args_file packageless_files_file output_file [args...]\n" "$(basename "${0}")" 1>&2
exit 2
}

cue_cache_directory=
instance_path=
module_file=
package_name=

function parse_args() {
while getopts i:m:p: name
while getopts c:i:m:p: name
do
case "${name}" in
c) cue_cache_directory="${OPTARG}";;
i) instance_path="${OPTARG}";;
h) usage;;
m) module_file="${OPTARG}";;
p) package_name="${OPTARG}";;
?) usage;;
esac
done
if [ -n "${cue_cache_directory}" ] && [ ! -d "${cue_cache_directory}" ]; then
printf "%s: specified CUE cache directory \"%s\" does not exist\n" "$(basename "${0}" "${cue_cache_directory}")" 1>&2
exit 1
fi
if [ -n "${instance_path}" ] && [ -z "${module_file}" ]; then
printf "%s: specifying a CUE instance path requires specifying a module path\n" "$(basename "${0}")" 1>&2
exit 1
Expand Down Expand Up @@ -86,6 +92,17 @@ if [ -n "${module_file}" ]; then
cd "${module_path}"
fi

# Since we don't have access to the "HOME" environment variable here
# (barring use of the "--action-env" command-line flag), we must tell
# CUE where to store its cache explicitly.
if [ -n "${cue_cache_directory}" ]; then
export CUE_CACHE_DIR="${cue_cache_directory}"
elif [ -z "${HOME:-}" ]; then
cue_cache_directory="$(mktemp -d -t cue)"
trap "rm -rf '${cue_cache_directory}'" EXIT
export CUE_CACHE_DIR="${cue_cache_directory}"
fi

# NB: See https://stackoverflow.com/questions/7577052 for the odd
# treatment of the "packageless_file_args" array variable here,
# handling the case where the array winds up empty for lack of
Expand Down
9 changes: 7 additions & 2 deletions cue/cue.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,13 @@ or another rule that yields a CUEInstanceInfo provider.""",

_cue_toolchain_type = "//tools/cue:toolchain_type"

def _make_output_producing_action(ctx, cue_subcommand, mnemonic, description, augment_args = None, module_file = None, instance_directory_path = None, instance_package_name = None):
# TODO(seh): Consider feeding an argument for the
# "cue_cache_directory_path" parameter here by way of new rule
# attributes.
def _make_output_producing_action(ctx, cue_subcommand, mnemonic, description, augment_args = None, module_file = None, instance_directory_path = None, instance_package_name = None, cue_cache_directory_path = None):
cue_tool = ctx.toolchains[_cue_toolchain_type].cueinfo.tool
args = ctx.actions.args()
if module_file != None:
if module_file:
args.add("-m", _runfile_path(ctx, module_file))
if instance_directory_path:
args.add("-i", instance_directory_path)
Expand All @@ -520,6 +523,8 @@ def _make_output_producing_action(ctx, cue_subcommand, mnemonic, description, au
fail(msg = "CUE instance directory path provided without a module directory path")
elif instance_package_name:
fail(msg = "CUE package name provided without an instance directory path")
if cue_cache_directory_path:
args.add("-c", cue_cache_directory_path)
args.add(cue_tool.path)
args.add(cue_subcommand)
stamped_args_file = ctx.actions.declare_file("%s-stamped-args" % ctx.label.name)
Expand Down
2 changes: 2 additions & 0 deletions test/testdata/consolidated/cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module: "example.com"

language: version: "v0.9.0"
2 changes: 2 additions & 0 deletions test/testdata/hello_world/cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module: "example.com"

language: version: "v0.9.0"
2 changes: 2 additions & 0 deletions test/testdata/module/cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module: "example.com"

language: version: "v0.9.0"
2 changes: 2 additions & 0 deletions test/testdata/myservice/cue.mod/module.cue
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module: "example.com/myservice"

language: version: "v0.9.0"
6 changes: 3 additions & 3 deletions test/testdata/myservice/environments/dev.cue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import (
"example.com/myservice"
"example.com/myservice"
)

myservice.#MyDeployment & {
#Replicas: 1
}
#Replicas: 1
}

0 comments on commit 7a223cb

Please sign in to comment.