Skip to content

Commit 428cf06

Browse files
rule: ♻️ refactor: 改进跨数据包函数调用展开机制
- 添加 DataPackInfo provider 用于数据包信息传递 - 修改 McfunctionProcessor 接收工作区根目录参数 - 解决 Bazel 沙箱环境中项目根目录查找问题 - 修复跨数据包调用仅在 clean 后首次构建成功的问题 - 保持向后兼容性,现有依赖配置无需修改
1 parent d0221c0 commit 428cf06

2 files changed

Lines changed: 55 additions & 3 deletions

File tree

rule/mcfunction_processor/McfunctionProcessor.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class McfunctionProcessor extends Worker {
1717
private String currentPackId;
1818
private Path dataPackRoot;
1919
private List<Path> dependencyPaths = new ArrayList<>();
20+
private Path workspaceRoot;
2021

2122
public static void main(String[] args) throws Exception {
2223
new McfunctionProcessor().run();
@@ -26,22 +27,43 @@ public static void main(String[] args) throws Exception {
2627
protected int handleRequest(WorkRequest request, PrintWriter out) {
2728
var args = request.arguments();
2829
if (args.size() < 2) {
29-
out.println("Usage: McfunctionProcessor <input_file> <output_file> [dependency_files...]");
30+
out.println("Usage: McfunctionProcessor <input_file> <output_file> [workspace_root] [dependency_files...]");
3031
return 1;
3132
}
3233

3334
var inputFile = args.get(0);
3435
var outputFile = args.get(1);
3536

37+
// Optional workspace root argument (for Bazel sandbox environment)
38+
Path workspaceRoot = null;
39+
int depStartIndex = 2;
40+
41+
if (args.size() >= 3) {
42+
String thirdArg = args.get(2);
43+
// Check if the third argument looks like a path (not a dependency file)
44+
// Simple heuristic: if it contains "workspace" or doesn't end with .mcfunction or similar
45+
if (!thirdArg.endsWith(".mcfunction") && !thirdArg.endsWith(".json") && !thirdArg.endsWith(".zip")) {
46+
workspaceRoot = Paths.get(thirdArg);
47+
depStartIndex = 3;
48+
out.println("Using workspace root: " + workspaceRoot);
49+
}
50+
}
51+
3652
// 解析依赖文件路径
37-
for (int i = 2; i < args.size(); i++) {
53+
for (int i = depStartIndex; i < args.size(); i++) {
3854
Path depFilePath = Paths.get(args.get(i));
3955
Path depDataPackRoot = findDataPackRoot(depFilePath);
4056
if (depDataPackRoot != null) {
4157
dependencyPaths.add(depDataPackRoot);
4258
}
4359
}
4460

61+
// If workspace root is provided, use it as the project root
62+
if (workspaceRoot != null) {
63+
// Store workspace root for use in findProjectRoot
64+
this.workspaceRoot = workspaceRoot;
65+
}
66+
4567
try {
4668
processFile(inputFile, outputFile);
4769
} catch (IOException e) {
@@ -334,6 +356,11 @@ private List<Path> getDataPackSearchPaths() {
334356
}
335357

336358
private Path findProjectRoot() {
359+
// If workspace root is provided, use it as the project root
360+
if (workspaceRoot != null) {
361+
return workspaceRoot;
362+
}
363+
337364
if (dataPackRoot == null) {
338365
return null;
339366
}

rule/process_mcfunction.bzl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@
1414
load("@bazel_skylib//lib:paths.bzl", "paths")
1515
load("@rules_pkg//pkg:providers.bzl", "PackageFilesInfo")
1616

17+
# Provider for data pack information
18+
DataPackInfo = provider(
19+
doc = "Information about a data pack for cross-pack function resolution",
20+
fields = [
21+
"pack_id", # The ID of the pack (namespace)
22+
"data_root", # Path to the data directory root (relative to workspace)
23+
"pack_root", # Path to the pack root directory (relative to workspace)
24+
],
25+
)
26+
1727
def _owner(file):
1828
if file.owner == None:
1929
fail("File {} ({}) has no owner attribute; cannot continue".format(file, file.path))
@@ -36,6 +46,11 @@ def _process_mcfunction_impl(ctx):
3646
function_pattern = "data/%s/function" % ctx.attr.pack_id
3747
function_placement = "data/%s/functions" % ctx.attr.pack_id
3848

49+
# Get workspace root for passing to the processor
50+
# In Bazel sandbox, this helps the processor find project root
51+
# Use workspace_root if available, otherwise use workspace_name
52+
workspace_root = getattr(ctx, "workspace_root", ctx.workspace_name)
53+
3954
for src in ctx.files.srcs:
4055
if ctx.attr.keep_original_name:
4156
output_file = ctx.actions.declare_file(src.basename, sibling = src)
@@ -51,7 +66,8 @@ def _process_mcfunction_impl(ctx):
5166
args = ctx.actions.args()
5267
args.add(src)
5368
args.add(output_file)
54-
69+
# Pass workspace root as third argument to help processor find project root
70+
args.add(workspace_root)
5571
args.add_all(ctx.files.deps)
5672

5773
args.use_param_file("@%s", use_always = True)
@@ -69,12 +85,21 @@ def _process_mcfunction_impl(ctx):
6985
},
7086
)
7187

88+
# Create DataPackInfo for this pack
89+
# This allows dependent packs to get information about this pack
90+
pack_info = DataPackInfo(
91+
pack_id = ctx.attr.pack_id,
92+
data_root = "data",
93+
pack_root = ctx.label.package,
94+
)
95+
7296
return [
7397
PackageFilesInfo(
7498
attributes = {},
7599
dest_src_map = dest_src_map,
76100
),
77101
DefaultInfo(files = depset(output_files)),
102+
pack_info,
78103
]
79104

80105
process_mcfunction = rule(

0 commit comments

Comments
 (0)