Skip to content
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

wip: support processing string catalogs (xcstrings) #2645

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions apple/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ bzl_library(
"//apple/internal/resource_actions:plist",
"//apple/internal/resource_actions:png",
"//apple/internal/resource_actions:texture_atlas",
"//apple/internal/resource_actions:xcstrings",
],
)

Expand Down
38 changes: 38 additions & 0 deletions apple/internal/partials/support/resources_support.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,43 @@ def _xibs(
processed_origins = processed_origins,
)

def _xcstrings(
*,
actions,
apple_mac_toolchain_info,
files,
output_discriminator,
parent_dir,
platform_prerequisites,
rule_label,
**_kwargs):
"""Process xcstrings files."""
lproj_files = []
processed_origins = {}
for file in files.to_list():
basename = paths.replace_extension(file.basename, "")
out_path = paths.join("xcstrings", parent_dir or "", basename)
out_dir = intermediates.directory(
actions = actions,
target_name = rule_label.name,
output_discriminator = output_discriminator,
dir_name = out_path,
)
processed_origins[out_dir.short_path] = [file.short_path]
resource_actions.compile_xcstrings(
actions = actions,
input_file = file,
output_dir = out_dir,
platform_prerequisites = platform_prerequisites,
xctoolrunner = apple_mac_toolchain_info.xctoolrunner,
)
lproj_files.append(out_dir)

return struct(
files = [(processor.location.resource, parent_dir, depset(lproj_files))],
processed_origins = processed_origins,
)

def _noop(
*,
parent_dir,
Expand Down Expand Up @@ -834,5 +871,6 @@ PROVIDER_TO_FIELD_ACTION = {
"strings": (_plists_and_strings, False),
"texture_atlases": (_texture_atlases, False),
"unprocessed": (_noop, False),
"xcstrings": (_xcstrings, False),
"xibs": (_xibs, True),
}
1 change: 1 addition & 0 deletions apple/internal/providers.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ and bundled at the top level.""",
"strings": "Localization strings files.",
"texture_atlases": "Texture atlas files.",
"unprocessed": "Generic resources not mapped to the other types.",
"xcstrings": "String catalog files.",
"xibs": "XIB Interface files.",
"owners": """`depset` of (resource, owner) pairs.""",
"processed_origins": """`depset` of (processed resource, resource list) pairs.""",
Expand Down
5 changes: 5 additions & 0 deletions apple/internal/resource_actions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ load(
"//apple/internal/resource_actions:texture_atlas.bzl",
_compile_texture_atlas = "compile_texture_atlas",
)
load(
"//apple/internal/resource_actions:xcstrings.bzl",
_compile_xcstrings = "compile_xcstrings",
)

resource_actions = struct(
compile_asset_catalog = _compile_asset_catalog,
Expand All @@ -68,6 +72,7 @@ resource_actions = struct(
compile_plist = _compile_plist,
compile_storyboard = _compile_storyboard,
compile_texture_atlas = _compile_texture_atlas,
compile_xcstrings = _compile_xcstrings,
compile_xib = _compile_xib,
copy_png = _copy_png,
generate_datamodels = _generate_datamodels,
Expand Down
12 changes: 12 additions & 0 deletions apple/internal/resource_actions/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ bzl_library(
],
)

bzl_library(
name = "xcstrings",
srcs = ["xcstrings.bzl"],
visibility = [
"//apple/internal:__pkg__",
],
deps = [
"//apple/internal/utils:xctoolrunner",
"@build_bazel_apple_support//lib:apple_support",
],
)

# Consumed by bazel tests.
filegroup(
name = "for_bazel_tests",
Expand Down
36 changes: 36 additions & 0 deletions apple/internal/resource_actions/xcstrings.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""xcstrings related actions."""

load(
"@build_bazel_apple_support//lib:apple_support.bzl",
"apple_support",
)
load(
"//apple/internal/utils:xctoolrunner.bzl",
xctoolrunner_support = "xctoolrunner",
)

def compile_xcstrings(
*,
actions,
input_file,
output_dir,
platform_prerequisites,
xctoolrunner):
args = [
"xcstringstool",
"compile",
"--output-directory",
xctoolrunner_support.prefixed_path(output_dir.path),
xctoolrunner_support.prefixed_path(input_file.path),
]

apple_support.run(
actions = actions,
apple_fragment = platform_prerequisites.apple_fragment,
arguments = args,
executable = xctoolrunner,
inputs = [input_file],
mnemonic = "CompileXCStrings",
outputs = [output_dir],
xcode_config = platform_prerequisites.xcode_version_config,
)
2 changes: 2 additions & 0 deletions apple/internal/resources.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ def _bucketize_data(
bucket_name = "framework"
elif resource_short_path.endswith(".strings") or resource_short_path.endswith(".stringsdict"):
bucket_name = "strings"
elif resource_short_path.endswith(".xcstrings"):
bucket_name = "xcstrings"
elif resource_short_path.endswith(".storyboard"):
bucket_name = "storyboards"
resource_swift_module = swift_module
Expand Down
3 changes: 2 additions & 1 deletion doc/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ requirement.
<pre>
AppleResourceInfo(<a href="#AppleResourceInfo-alternate_icons">alternate_icons</a>, <a href="#AppleResourceInfo-asset_catalogs">asset_catalogs</a>, <a href="#AppleResourceInfo-datamodels">datamodels</a>, <a href="#AppleResourceInfo-framework">framework</a>, <a href="#AppleResourceInfo-infoplists">infoplists</a>, <a href="#AppleResourceInfo-metals">metals</a>,
<a href="#AppleResourceInfo-mlmodels">mlmodels</a>, <a href="#AppleResourceInfo-plists">plists</a>, <a href="#AppleResourceInfo-pngs">pngs</a>, <a href="#AppleResourceInfo-processed">processed</a>, <a href="#AppleResourceInfo-storyboards">storyboards</a>, <a href="#AppleResourceInfo-strings">strings</a>, <a href="#AppleResourceInfo-texture_atlases">texture_atlases</a>,
<a href="#AppleResourceInfo-unprocessed">unprocessed</a>, <a href="#AppleResourceInfo-xibs">xibs</a>, <a href="#AppleResourceInfo-owners">owners</a>, <a href="#AppleResourceInfo-processed_origins">processed_origins</a>, <a href="#AppleResourceInfo-unowned_resources">unowned_resources</a>)
<a href="#AppleResourceInfo-unprocessed">unprocessed</a>, <a href="#AppleResourceInfo-xcstrings">xcstrings</a>, <a href="#AppleResourceInfo-xibs">xibs</a>, <a href="#AppleResourceInfo-owners">owners</a>, <a href="#AppleResourceInfo-processed_origins">processed_origins</a>, <a href="#AppleResourceInfo-unowned_resources">unowned_resources</a>)
</pre>

Provider that propagates buckets of resources that are differentiated by type.
Expand All @@ -412,6 +412,7 @@ Provider that propagates buckets of resources that are differentiated by type.
| <a id="AppleResourceInfo-strings"></a>strings | Localization strings files. |
| <a id="AppleResourceInfo-texture_atlases"></a>texture_atlases | Texture atlas files. |
| <a id="AppleResourceInfo-unprocessed"></a>unprocessed | Generic resources not mapped to the other types. |
| <a id="AppleResourceInfo-xcstrings"></a>xcstrings | String catalog files. |
| <a id="AppleResourceInfo-xibs"></a>xibs | XIB Interface files. |
| <a id="AppleResourceInfo-owners"></a>owners | `depset` of (resource, owner) pairs. |
| <a id="AppleResourceInfo-processed_origins"></a>processed_origins | `depset` of (processed resource, resource list) pairs. |
Expand Down
1 change: 1 addition & 0 deletions examples/ios/HelloWorldSwift/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ swift_library(
],
data = [
"Resources/HelloWorldSwift.docc",
"//examples/resources:Localizable.xcstrings",
],
module_name = "Sources",
tags = ["manual"],
Expand Down
1 change: 1 addition & 0 deletions examples/ios/HelloWorldSwift/Sources/BazelApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public struct BazelApp: App {
WindowGroup {
Text("Hello World")
.accessibility(identifier: "HELLO_WORLD")
Text("Example", comment: "Example text")
}
}

Expand Down
1 change: 1 addition & 0 deletions examples/resources/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ licenses(["notice"])

exports_files([
"Launch.storyboard",
"Localizable.xcstrings",
])

filegroup(
Expand Down
24 changes: 24 additions & 0 deletions examples/resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"sourceLanguage" : "en",
"strings" : {
"Example" : {
"comment" : "Example text",
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Example"
}
},
"ja" : {
"stringUnit" : {
"state" : "translated",
"value" : "例"
}
}
}
},
},
"version" : "1.0"
}
6 changes: 4 additions & 2 deletions test/ios_application_resources_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,8 @@ objc_library(
name = "lib",
srcs = ["main.m"],
data = [
"@build_bazel_rules_apple//test/testdata/resources:localized_strings",
# "@build_bazel_rules_apple//test/testdata/resources:localized_strings",
"@build_bazel_rules_apple//test/testdata/resources:localized_xcstrings",
],
)

Expand Down Expand Up @@ -458,7 +459,8 @@ objc_library(
name = "lib",
srcs = ["main.m"],
data = [
"@build_bazel_rules_apple//test/testdata/resources:localized_strings",
# "@build_bazel_rules_apple//test/testdata/resources:localized_strings",
"@build_bazel_rules_apple//test/testdata/resources:localized_xcstrings",
],
)

Expand Down
1 change: 1 addition & 0 deletions test/smart_resource_deduplication_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ objc_library(
"@build_bazel_rules_apple//test/testdata/resources:nonlocalized.plist",
"@build_bazel_rules_apple//test/testdata/resources:sample.png",
"@build_bazel_rules_apple//test/testdata/resources:nonlocalized.strings",
"@build_bazel_rules_apple//test/testdata/resources:nonlocalized.xcstrings",
],
)

Expand Down
Loading