Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Is there a way to generate .bundle file for iOS project? #1483

Closed
wdsunny opened this issue Aug 24, 2017 · 3 comments
Closed

Is there a way to generate .bundle file for iOS project? #1483

wdsunny opened this issue Aug 24, 2017 · 3 comments
Labels
P3 Priority 3

Comments

@wdsunny
Copy link

wdsunny commented Aug 24, 2017

I have a project contains several resource files such as png、xib and plist etc. I want to generate the output files as a static library: .a file and a resource bundle file: .bundle file.

I check the apple_resource and apple_library rule, it seems that it can only generate the single .a static library file, but the apple_resource doesn't generate any output file. Is there a way to do that?

@scottrice
Copy link
Contributor

If you want to include a bundle which already exists in your binary, then you can do so by adding an apple_resource rule with dirs that includes a path to your .bundle folder. This of course assumes that you have the bundle on disk already and that you want to place that in your binary exactly as is.

If some of the contents of the bundle are unknown though (like they are the output of another build step), then putting together a bundle is a bit more difficult. Buck currently doesn't have anything set up, although since a .bundle is just a directory you could create a genrule to do what you want.

Something like this:

genrule(
  name = 'SomeOutput.bundle',
  out = 'SomeOutput.bundle',
  bash = 'some_script_to_put_files_in_a_folder.sh $OUT $SRCS',
  srcs = [
    # All the files you want in your bundle
  ],
)

My bash-foo is weak, but theres probably a bash one-liner to copy all sources to the out folder. Anyway, a genrule like that will let you generate a bundle and then you can build it with buck build //path/to/buck/file:SomeOutput.bundle.

There is also the apple_bundle rule, but it is meant for the bundle which contains binaries (like .framework, .app, or .appex bundles). If you are just looking for a folder which can hold your files, a genrule is easiest.

@acecilia
Copy link

acecilia commented Oct 8, 2019

This is the way I managed:

    # here, name is created as <module_name> + "Resources"
    genrule_name = name + "GenRule"
    native.genrule(
        name = genrule_name,
        srcs = files,
        out = name + ".bundle",
        cmd = "mkdir $OUT && cp $SRCS $OUT",
    )
    native.apple_resource(
        name = name,
        dirs = [":" + genrule_name],
    )

And then in the code you access the resources as follows:

    let moduleName = String(reflecting: SomeClass.self).components(separatedBy:".").first!
    let resourceBundleName = moduleName + "Resources"
    let bundleURL = Bundle.main.url(forResource: resourceBundleName, withExtension: "bundle")!
    let bundle = Bundle(url: bundleURL)!
    let textFilePath = bundle.url(forResource: "text_file", withExtension: "txt")

For more details see https://github.com/acecilia/BazelVSBuckSample

@hendych
Copy link

hendych commented Nov 14, 2019

I don't really understand with this genrules
But this is my approach:

# Creates resource bundle
apple_binary(
    name = "noop",
    srcs = ["main.c"],
    deps = [
        ':Assets',
    ]
)

apple_asset_catalog(
    name = "Assets",
    dirs = ["Resources/Images.xcassets"],
)

apple_bundle(
    name = "ModuleResourcesBundle",
    binary = ':noop',
    info_plist = 'Resources/Info.plist',
    extension = 'bundle',
    deps = [
        ':AssetXib',
    ],
)

apple_resource(
    name = "AssetXib",
    visibility = ["PUBLIC"],
    dirs = [],
    files = glob([
        "src/**/*.xib",
    ]),
)

apple_resource(
    name = "ModuleResources",
    dirs = [":ModuleResourcesBundle"]
)

# My module
apple_library(
    ....
    ....
    deps = [
        ":ModuleResources",
    ]
)

This creates ModuleResources.bundle but with noop executable.
I think this is easier

@v-jizhang v-jizhang added the P3 Priority 3 label Mar 18, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
P3 Priority 3
Projects
None yet
Development

No branches or pull requests

5 participants