load("//ReactNative:DEFS.bzl", "rn_android_library", "rn_android_resource", "rn_prebuilt_jar")

# This is a bit messy and hopefully a temporary thing
# The problem is that Gradle extracts appcompat resources into app namespace, com.facebook.react
# While BUCK behaves properly and extracts them into android.support.v7.appcompat package.
# We want to support both Gradle and BUCK builds so we hack a bit how BUCK extracts resources.
# Besides that we still need JAVA classes from appcompat-v7.aar, that is why rn_android_library
# extracts classes.jar but the trick is that we can't take full appcompat.aar because resources
# extracted from it by BUCK would conflict with resources we use under Gradelified package
# All this mumbo jumbo will go away after t10182713

rn_android_library(
    name = "appcompat",
    exported_deps = [
        ":classes-for-react-native",
    ],
    visibility = [
        "PUBLIC",
    ],
    deps = [
        ":res-for-appcompat",
    ],
)

# still used by appcompat library internally, so we need both during the build
rn_android_resource(
    name = "res-for-appcompat",
    package = "android.support.v7.appcompat",
    res = ":res-unpacker-cmd",
    visibility = ["//ReactAndroid/..."],
)

rn_prebuilt_jar(
    name = "classes-for-react-native",
    binary_jar = ":classes-unpacker-cmd",
    visibility = ["//ReactAndroid/..."],
)

genrule(
    name = "classes-unpacker-cmd",
    out = "classes.jar",
    cmd = "$(exe :aar-unpacker) $(location :appcompat-binary-aar) classes.jar $OUT",
)

genrule(
    name = "res-unpacker-cmd",
    out = "res",
    cmd = "$(exe :aar-unpacker) $(location :appcompat-binary-aar) res/ $OUT",
    visibility = ["//ReactAndroid/..."],
)

python_binary(
    name = "aar-unpacker",
    main = "aar-unpacker.py",
)

remote_file(
    name = "appcompat-binary-aar",
    sha1 = "7d659f671541394a8bc2b9f909950aa2a5ec87ff",
    url = "mvn:com.android.support:appcompat-v7:aar:23.0.1",
)
