Skip to content

feat: add ability to cache json responses in-repo #1696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 39 additions & 4 deletions swiftpkg/bzlmod/swift_deps.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation for `swift_deps` bzlmod extension."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("//swiftpkg/internal:bazel_repo_names.bzl", "bazel_repo_names")
load("//swiftpkg/internal:local_swift_package.bzl", "local_swift_package")
load("//swiftpkg/internal:pkginfos.bzl", "pkginfos")
Expand Down Expand Up @@ -59,11 +60,21 @@ def _declare_pkgs_from_package(module_ctx, from_package, config_pkgs, config_swi
# Get the package info.
pkg_swift = module_ctx.path(from_package.swift)
debug_path = module_ctx.path(".")
workspace_root = str(pkg_swift.dirname)

root_cached_json_directory = None
if from_package.cached_json_directory:
root_cached_json_directory = paths.join(
workspace_root,
from_package.cached_json_directory,
)

pkg_info = pkginfos.get(
module_ctx,
directory = str(pkg_swift.dirname),
directory = workspace_root,
env = env,
debug_path = str(debug_path),
cached_json_directory = root_cached_json_directory,
resolved_pkg_map = resolved_pkg_map,
collect_src_info = False,
registries_directory = registries_directory,
Expand Down Expand Up @@ -157,10 +168,18 @@ the Swift package to make it available.\
processing = to_process
to_process = []
for dep in processing:
dep_cached_json_directory = None

if from_package.cached_json_directory:
dep_cached_json_directory = paths.join(
dep.file_system.path,
from_package.cached_json_directory,
)
dep_pkg_info = pkginfos.get(
module_ctx,
directory = dep.file_system.path,
debug_path = None,
cached_json_directory = dep_cached_json_directory,
resolved_pkg_map = None,
collect_src_info = False,
)
Expand All @@ -185,7 +204,13 @@ the Swift package to make it available.\
config_pkg = config_pkgs.get(
bazel_repo_names.from_identity(dep.identity),
)
_declare_pkg_from_dependency(dep, config_pkg, from_package, config_swift_package)
_declare_pkg_from_dependency(
dep,
config_pkg,
from_package,
config_swift_package,
from_package.cached_json_directory,
)

# Add all transitive dependencies to direct_dep_repo_names if `publicly_expose_all_targets` flag is set.
for dep in all_deps_by_id.values():
Expand All @@ -199,7 +224,15 @@ the Swift package to make it available.\

return direct_dep_repo_names

def _declare_pkg_from_dependency(dep, config_pkg, from_package, config_swift_package):
def _declare_pkg_from_dependency(
dep,
config_pkg,
from_package,
config_swift_package,
cached_json_directory):
if cached_json_directory:
cached_json_directory = paths.join(cached_json_directory, dep.name)

name = bazel_repo_names.from_identity(dep.identity)
if dep.source_control:
init_submodules = None
Expand Down Expand Up @@ -236,7 +269,7 @@ def _declare_pkg_from_dependency(dep, config_pkg, from_package, config_swift_pac
dependencies_index = None,
env = from_package.env,
env_inherit = from_package.env_inherit,
init_submodules = init_submodules,
cached_json_directory = cached_json_directory, init_submodules = init_submodules,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
cached_json_directory = cached_json_directory, init_submodules = init_submodules,
cached_json_directory = cached_json_directory,
init_submodules = init_submodules,

recursive_init_submodules = recursive_init_submodules,
patch_args = patch_args,
patch_cmds = patch_cmds,
Expand All @@ -256,6 +289,7 @@ def _declare_pkg_from_dependency(dep, config_pkg, from_package, config_swift_pac
env_inherit = from_package.env_inherit,
path = dep.file_system.path,
dependencies_index = None,
cached_json_directory = cached_json_directory,
)

elif dep.registry:
Expand Down Expand Up @@ -350,6 +384,7 @@ bazel run @swift_package//:resolve
```
""",
),
"cached_json_directory": attr.string(),
"env": attr.string_dict(
doc = """\
Environment variables that will be passed to the execution environments for \
Expand Down
15 changes: 14 additions & 1 deletion swiftpkg/internal/local_swift_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,19 @@ def _local_swift_package_impl(repository_ctx):
repo_rules.write_workspace_file(repository_ctx, repo_dir)

# Generate the build file
pkg_ctx = pkg_ctxs.read(repository_ctx, repo_dir, env)
cached_json_directory = None
if repository_ctx.attr.cached_json_directory:
cached_json_directory = paths.join(
str(repository_ctx.workspace_root),
repository_ctx.attr.cached_json_directory,
)

pkg_ctx = pkg_ctxs.read(
repository_ctx,
repo_dir,
env,
cached_json_directory,
)
repo_rules.gen_build_files(repository_ctx, pkg_ctx)

return update_attrs(repository_ctx.attr, _ALL_ATTRS.keys(), {})
Expand All @@ -78,6 +90,7 @@ _ALL_ATTRS = dicts.add(
repo_rules.env_attrs,
repo_rules.swift_attrs,
_PATH_ATTRS,
{"cached_json_directory": attr.string()},
)

local_swift_package = repository_rule(
Expand Down
2 changes: 2 additions & 0 deletions swiftpkg/internal/pkg_ctxs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ def _read(
repository_ctx,
repo_dir,
env,
cached_json_directory,
resolved_pkg_map = None,
registries_directory = None,
replace_scm_with_registry = False):
pkg_info = pkginfos.get(
repository_ctx = repository_ctx,
directory = repo_dir,
env = env,
cached_json_directory = cached_json_directory,
resolved_pkg_map = resolved_pkg_map,
registries_directory = registries_directory,
replace_scm_with_registry = replace_scm_with_registry,
Expand Down
20 changes: 19 additions & 1 deletion swiftpkg/internal/pkginfos.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def _get_dump_manifest(
env = {},
working_directory = "",
debug_path = None,
cache_path = None,
registries_directory = None,
replace_scm_with_registry = False):
"""Returns a dict representing the package dump for an SPM package.
Expand All @@ -52,6 +53,10 @@ def _get_dump_manifest(
debug_path = str(repository_ctx.path("."))
debug_json_path = paths.join(debug_path, "dump.json")

cached_json_path = None
if cache_path:
cached_json_path = paths.join(cache_path, "dump.json")

args = ["swift", "package"]

if registries_directory:
Expand All @@ -66,13 +71,15 @@ def _get_dump_manifest(
env = env,
working_directory = working_directory,
debug_json_path = debug_json_path,
cached_json_path = cached_json_path,
)

def _get_desc_manifest(
repository_ctx,
env = {},
working_directory = "",
debug_path = None,
cache_path = None,
registries_directory = None,
replace_scm_with_registry = False):
"""Returns a dict representing the package description for an SPM package.
Expand All @@ -97,6 +104,10 @@ def _get_desc_manifest(
debug_path = str(repository_ctx.path("."))
debug_json_path = paths.join(debug_path, "desc.json")

cached_json_path = None
if cache_path:
cached_json_path = paths.join(cache_path, "desc.json")

args = ["swift", "package"]

if registries_directory:
Expand All @@ -112,13 +123,15 @@ def _get_desc_manifest(
env = env,
working_directory = working_directory,
debug_json_path = debug_json_path,
cached_json_path = cached_json_path,
)

def _get(
repository_ctx,
directory,
env = {},
debug_path = None,
cached_json_directory = None,
resolved_pkg_map = None,
collect_src_info = True,
registries_directory = None,
Expand Down Expand Up @@ -150,11 +163,13 @@ def _get(
if not paths.is_absolute(debug_path):
# For backwards compatibility, resolve relative to the working directory.
debug_path = paths.join(directory, debug_path)

dump_manifest = _get_dump_manifest(
repository_ctx,
env = env,
working_directory = directory,
debug_path = debug_path,
cache_path = cached_json_directory,
registries_directory = registries_directory,
replace_scm_with_registry = replace_scm_with_registry,
)
Expand All @@ -163,6 +178,7 @@ def _get(
env = env,
working_directory = directory,
debug_path = debug_path,
cache_path = cached_json_directory,
registries_directory = registries_directory,
replace_scm_with_registry = replace_scm_with_registry,
)
Expand Down Expand Up @@ -1648,7 +1664,9 @@ def _new_resource_rule_process(localization = None):

def _new_resource_from_desc_map(desc_map, pkg_path):
path = desc_map["path"]
if paths.is_absolute(path):
if path.startswith("./"):
path = path[2:]
Comment on lines +1667 to +1668
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be path.removeprefix("./")?

elif paths.is_absolute(path):
path = paths.relativize(path, pkg_path)
return _new_resource(
path = path,
Expand Down
8 changes: 8 additions & 0 deletions swiftpkg/internal/registry_swift_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,18 @@ def _registry_swift_package_impl(repository_ctx):
else:
resolved_pkg_map = dict()

cached_json_directory = None
if repository_ctx.attr.cached_json_directory:
cached_json_directory = paths.join(
str(repository_ctx.workspace_root),
repository_ctx.attr.cached_json_directory,
)

pkg_ctx = pkg_ctxs.read(
repository_ctx,
directory,
env,
cached_json_directory,
resolved_pkg_map = resolved_pkg_map,
registries_directory = registries_directory,
replace_scm_with_registry = attr.replace_scm_with_registry,
Expand Down
26 changes: 24 additions & 2 deletions swiftpkg/internal/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,39 @@ def _parsed_json_from_spm_command(
arguments,
env = {},
working_directory = "",
debug_json_path = None):
debug_json_path = None,
cached_json_path = None):
if cached_json_path:
cached_json_path_path = repository_ctx.path(cached_json_path)
if cached_json_path_path.exists:
return json.decode(
repository_ctx.read(cached_json_path_path, watch = "yes"),
)

json_str = repository_utils.exec_spm_command(
repository_ctx,
arguments,
env = env,
working_directory = working_directory,
)
).replace(working_directory, ".")

if debug_json_path:
if not paths.is_absolute(debug_json_path):
debug_json_path = paths.join(working_directory, debug_json_path)
repository_ctx.file(debug_json_path, content = json_str, executable = False)
if cached_json_path:
if debug_json_path:
tmp_path = debug_json_path
else:
tmp_path = paths.join(
str(repository_ctx.path(".")),
paths.basename(cached_json_path),
)
repository_ctx.file(tmp_path, content = json_str, executable = False)
repository_ctx.execute(["mkdir", "-p", paths.dirname(cached_json_path)])
repository_ctx.execute(["cp", tmp_path, cached_json_path])
repository_ctx.watch(cached_json_path)

return json.decode(json_str)

def _package_name(repository_ctx):
Expand Down
14 changes: 13 additions & 1 deletion swiftpkg/internal/swift_package.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Implementation for `swift_package`."""

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/build_defs/repo:git_worker.bzl", "git_repo")
load(
"@bazel_tools//tools/build_defs/repo:utils.bzl",
Expand Down Expand Up @@ -68,10 +69,18 @@ def _swift_package_impl(repository_ctx):
registries_directory = None

# Generate the build file
cached_json_directory = None
if repository_ctx.attr.cached_json_directory:
cached_json_directory = paths.join(
str(repository_ctx.workspace_root),
repository_ctx.attr.cached_json_directory,
)

pkg_ctx = pkg_ctxs.read(
repository_ctx,
directory,
env,
cached_json_directory,
registries_directory = registries_directory,
replace_scm_with_registry = replace_scm_with_registry,
)
Expand Down Expand Up @@ -212,7 +221,10 @@ _ALL_ATTRS = dicts.add(
_GIT_ATTRS,
repo_rules.env_attrs,
repo_rules.swift_attrs,
{"version": attr.string(doc = "The resolved version of the package.")},
{
"cached_json_directory": attr.string(),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a doc string.

"version": attr.string(doc = "The resolved version of the package."),
},
)

swift_package = repository_rule(
Expand Down
Loading