Giter VIP home page Giter VIP logo

fakebundle's Introduction

FakeBundle

๐Ÿ—„ Use Resources in your Swift Package Manager executable

Description

As Swift Package Manager does not support Resources or Bundle for Swift executables, I needed a way to integrate generic file resources (including folder structure) in my binary.

FakeBundle takes an input folder and generates a single file of code, which can be used to export the complete folder, or single files, onto the file system during runtime.

Usage with Mint ๐ŸŒฑ

$ mint run zweigraf/FakeBundle fakebundle --input ./Resources --output ./Resources.swift

Use Case

Say you have a folder of templates but want to distribute your app as a single binary (or otherwise simplify installation). You could add all of these templates to your code as strings, but maintaining this gets cumbersome. In this case you could run FakeBundle as a pre-compile script phase and generate a class that contains all of your templates, automatically. During runtime you can then export the templates back to the file system or use them directly from code.

Exported Code

My main use case was directly exporting the whole input folder back onto the file system. This can be done like this (Resources here is the name of the top level input folder):

Resources().export(to: <path>)

This will create the Resources folder in <path> on the disk and export all children recursively into it. Single files can also be exported, but getting a reference to them is right now quite annoying (traversing through children).

Currently you cannot easily access files directly.

More complicated use cases

Resources().children.forEach {
    if $0.isDirectory {
        // Special directory handling
    } else if let file = $0 as? File {
        if file.filename == "MyImage.png", 
            let data = file.contents,
            let image = UIImage(data: data) {
                // You now have an image
        }
    }
}

Types

The generated code conforms to these protocols (which are included in the generated resources file):

protocol FileType {
    var isDirectory: Bool { get }
    var filename: String { get }
    func export(to path: String) throws
}
protocol File: FileType {
    var contentsBase64: String { get }
}
extension File {
    var isDirectory: Bool {
        return false
    }
    var contents: Data? {
        return Data(base64Encoded: contentsBase64)
    }

    func export(to path: String) throws {
        guard let contents = contents else { return }
        let originalUrl = URL(fileURLWithPath: path)
        let myUrl = originalUrl.appendingPathComponent(filename)
        try contents.write(to: myUrl)
    }
}
protocol Directory: FileType {
    var children: [FileType] { get }
}
extension Directory {
    var isDirectory: Bool {
        return true
    }
    func export(to path: String) throws {
        let originalUrl = URL(fileURLWithPath: path)
        let myUrl = originalUrl.appendingPathComponent(filename)
        try FileManager.default.createDirectory(at: myUrl, withIntermediateDirectories: true, attributes: nil)
        try children.forEach { try $0.export(to: myUrl.path) }
    }
}

License

FakeBundle is licensed under MIT.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.