# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""File references to important output files from the rule.

These file references can be used across the bundling logic, but there must be only 1 action
registered to generate these files.
"""

load(
    "@build_bazel_rules_apple//apple/bundling:bundling_support.bzl",
    "bundling_support",
)
load(
    "@build_bazel_rules_apple//apple/internal:intermediates.bzl",
    "intermediates",
)
load(
    "@bazel_skylib//lib:paths.bzl",
    "paths",
)

def _archive(ctx):
    """Returns a file reference for this target's archive."""

    # TODO(kaipi): Look into removing this rule implicit output and just return it using
    # DefaultInfo.
    return ctx.outputs.archive

def _archive_root_path(ctx):
    """Returns the path to a directory reference for this target's archive root."""

    # TODO(b/65366691): Migrate this to an actual tree artifact.
    archive_root_name = paths.replace_extension(_archive(ctx).path, "_archive-root")
    return archive_root_name

def _binary(ctx):
    """Returns a file reference for the binary that will be packaged into this target's archive. """
    return intermediates.file(
        ctx.actions,
        ctx.label.name,
        bundling_support.bundle_name(ctx),
    )

def _infoplist(ctx):
    """Returns a file reference for this target's Info.plist file."""

    # TODO(b/73349137): Revert to using intermediate artifacts.
    return ctx.actions.declare_file("{}-Info.plist".format(ctx.label.name))

outputs = struct(
    archive = _archive,
    archive_root_path = _archive_root_path,
    binary = _binary,
    infoplist = _infoplist,
)

@@ -1961,16 +1961,142 @@
 tifacts.
+ It uses a rare format for the name%0A    # because there are clients that have genrules that generate APPNAME-Info.plist files.
 %0A    ret
@@ -2129,11 +2129,20 @@
 le(%22
+_
 %7B%7D-
+Private-
 Info

Rename Info.plist output to a private one to avoid collisions with clients that might have genrules that output APPNAME-Info.plist files.