All Projects → zweigraf → FakeBundle

zweigraf / FakeBundle

Licence: MIT license
🗄 Use Resources in your Swift Package Manager executable

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to FakeBundle

Archery
Declare all your project's metadata and what you can do with it in one single place.
Stars: ✭ 47 (+261.54%)
Mutual labels:  swift-package-manager, mint
PKU-Lessons-Summary
北京大学软件与微电子学院硕士生课程知识点、作业等汇总【Summary of Knowledge Points and Assignments of Peking University Integrated Circuit Major Courses】
Stars: ✭ 31 (+138.46%)
Mutual labels:  resources
embd-go
embd-go is an embeddable command-line tool for embedding data files in Go source code, specially crafted for easy use with `go generate`.
Stars: ✭ 24 (+84.62%)
Mutual labels:  resources
python-resources-for-earth-sciences
A Curated List of Python Resources for Earth Sciences
Stars: ✭ 159 (+1123.08%)
Mutual labels:  resources
Conduit
Robust Swift networking for web APIs
Stars: ✭ 52 (+300%)
Mutual labels:  swift-package-manager
feedback
বাংলার প্রোগ্রামিং নেটওয়ার্ক
Stars: ✭ 21 (+61.54%)
Mutual labels:  resources
chameleon-sdk
Chameleon Software Development Kit
Stars: ✭ 12 (-7.69%)
Mutual labels:  bundle
web-development-learning-resources
💪 Resources to become a senior web developer
Stars: ✭ 21 (+61.54%)
Mutual labels:  resources
SwiftCurrent
A library for managing complex workflows in Swift
Stars: ✭ 286 (+2100%)
Mutual labels:  swift-package-manager
Front-End-Checklist
🗂 O Front-End Checklist perfeito para websites modernos e desenvolvedores meticulosos
Stars: ✭ 157 (+1107.69%)
Mutual labels:  resources
resources api
Flask API for programming and cyber security learning resources
Stars: ✭ 63 (+384.62%)
Mutual labels:  resources
awesome-agtech
A curated list of technology companies, resources, and tools in the agricultural field.
Stars: ✭ 43 (+230.77%)
Mutual labels:  resources
MLLineChart
A simple Line Chart Lib
Stars: ✭ 28 (+115.38%)
Mutual labels:  swift-package-manager
rollit
🌯 Zero config js library bundling using rollup with support for Vue
Stars: ✭ 24 (+84.62%)
Mutual labels:  bundle
IphpFileStoreBundle
Symfony 2 file upload for doctrine entities
Stars: ✭ 51 (+292.31%)
Mutual labels:  bundle
swift-package-info
Swift CLI tool that provides information about a Swift Package
Stars: ✭ 65 (+400%)
Mutual labels:  swift-package-manager
Awesome-Workstations
A curated list of Awesome WFH computer and desk setups!
Stars: ✭ 35 (+169.23%)
Mutual labels:  resources
bhamtech
A community-currated collection of tech resources, projects, and other things related for Birmingham, AL
Stars: ✭ 23 (+76.92%)
Mutual labels:  resources
esp8266
esp8266 resources.
Stars: ✭ 17 (+30.77%)
Mutual labels:  resources
Data-Science
Free self-taught educational resources for Data Science! I'm currently learning Data Science. I build this repository for helping myself. But if it helps you anyhow, feel free to star it!
Stars: ✭ 35 (+169.23%)
Mutual labels:  resources

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].