Git Product home page Git Product logo

mezzanine's Introduction

Mezzanine

An annotation processor that allows you to read static UTF-8 files synchronously.

What does this do?

Android apps often need to read in a default configuration file on startup and change functionality based on the contents of the configuration file. A convenient, frequently used way to store this configuration file is in assets, and then to read out of this file on startup. This can mean doing expensive disk I/O on the main thread, which increases startup times. You can get around the additional I/O by pasting the contents of the file into a String constant, but this can make code review difficult.

Mezzanine solves this problem by generating a class at compile time which stores the file contents in a String constant. This is loaded by the class loader, along with your code, when the application is started, rather than requiring additional disk I/O after startup. It acts as an intermediary between files and your running Java code. Instead of needing to paste the contents of the file into the constant yourself, you can store it in its own file and maintain proper separation. For instance, if you are executing JavaScript using a WebView or some library like duktape, you can store the files in .js files without the additional annoyance of needing to perform disk I/O.

Note: There is a hard limit set by the javac compiler where String constants cannot exceed 65535 bytes in size.

Usage

allprojects {
    repositories {
        bintray()
    }
}

Android/Java

dependencies {
  def mezzanineVersion = '1.1.1'
  compile "com.anthonycr.mezzanine:mezzanine:$mezzanineVersion"
  annotationProcessor "com.anthonycr.mezzanine:mezzanine-compiler:$mezzanineVersion"
}

android {
  defaultConfig {
    javaCompileOptions {
      annotationProcessorOptions {
        arguments = [
          "mezzanine.projectPath", project.rootDir
        ]
      }
    }
  }
}

Kotlin

apply plugin: 'kotlin-kapt'

dependencies {
  def mezzanineVersion = '1.1.1'
  compile "com.anthonycr.mezzanine:mezzanine:$mezzanineVersion"
  kapt "com.anthonycr.mezzanine:mezzanine-compiler:$mezzanineVersion"
}

kapt {
    arguments {
        arg("mezzanine.projectPath", project.rootDir)
    }
}

Java

plugins {
    id 'net.ltgt.apt' version '0.10'
}

dependencies {
    def mezzanineVersion = '1.1.1'
    compile "com.anthonycr.mezzanine:mezzanine:$mezzanineVersion"
    apt "com.anthonycr.mezzanine:mezzanine-compiler:$mezzanineVersion"
}

gradle.projectsEvaluated {
  tasks.withType(JavaCompile) {
    aptOptions.processorArgs = [
      "mezzanine.projectPath", project.rootDir
    ]
  }
}

API

  • @FileStream(String path): the path to the file relative to the project root.
  • mezzanine.projectPath annotation processing argument is used to determine the absolute path of the project root. If not provided, mezzanine will use the root accessible to the javac instance.
  • Create an interface with one method with no parameters and a return type of String.
  • Annotate the interface with @FileStream and pass the path to the file as the value in the annotation.
  • Consume the generated implementation of the interface to get the file as a string.
  • Files are assumed to be encoded as UTF-8.
  • Files must be less than 65kB, otherwise compilation will fail.

Sample

Kotlin

@FileStream("path/from/root/to/file.json")
interface MyFileReader {

    fun readMyFile(): String

}
...

val fileReader = MezzanineGenerator.MyFileReader()

val fileContents = fileReader.readMyFile()

println("File contents: $fileContents")

Java

@FileStream("path/from/root/to/file.json")
public interface MyFileReader {

    String readMyFile();

}
...

MyFileReader fileReader = new MezzanineGenerator.MyFileReader();

String fileContents = fileReader.readMyFile();

System.out.println("File contents: " + fileContents);

License

Copyright 2017 Anthony Restaino

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.

mezzanine's People

Contributors

anthonycr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mezzanine's Issues

Update generated files without rebuild

When I change the source file it is not generated anew unless I rebuild the project.
Is there a way to update generated files without rebuilding the whole project?

An idea to fix Sring size limit

Description

There is a way to fix the String constant size limitation by simply using multiple Strings to store a large file. Hope this will be helpful.

File not found error should specify path

Summary

When the processor emits an error saying "File does not exist", it should also specify the path at which it cannot find the file. This would allow for better debugging ability.

Project root is sometimes wrong

Description

Usually, the java process is run from the root of the gradle project. The path that is provided via the @FileStream annotation is then appended to that root. This works as expected usually, except in certain occasions where the java process is oddly started in a different folder (usually some folder in the gradle cache). This causes a build error.

Solution

  • Add an optional annotation processing argument which specifies the path of the root project.

Solution details

  • The name of the argument mezzanine.projectPath
  • Value that should be passed to the argument mezzanine.projectPath = project.rootDir.path
  • Default value will use current process root Paths.get("").
  • If a file not found error occurs, include the new option in the error message.

Warning -> Info in gradle task output

Summary

Type: Feature Enhancement
The gradle task output currently shows as a warning but isn't any indication of something that could be wrong. Therefore it might be better suited as an "info".

screen shot 2018-04-02 at 4 00 59 pm

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.