Giter VIP home page Giter VIP logo

paraphrase's Introduction

Paraphrase

DEPRECATED! Use https://github.com/cashapp/paraphrase instead!

An experimental Gradle plugin which generates compile-safe format string builders.

Don't write your format strings like this: ๐Ÿ‘Ž

<string name="greeting">Hello, %1$s! My name is %2$s.</string>

Don't suffer through positional argument formatting like this: ๐Ÿ‘Ž

CharSequence greeting = res.getText(R.string.greeting, "GitHub user", "Jake Wharton");

When you inevitably change the format string you will get a crash at runtime. Or even worse, a silent malformed replacement!

Do write your format strings like this: ๐Ÿ‘

<string name="greeting">Hello, {other_name}! My name is {my_name}.</string>

Do enjoy formatting them like this: ๐Ÿ‘

CharSequence greeting = Phrase.greeting()
    .other_name("GitHub user")
    .my_name("Jake Wharton")
    .build(this);

Now what happens when you change the format?

<string name="greeting">Hello! My name is {my_name}.</string>
Example.java:34: error: cannot find symbol
      .other_name("GitHub user")
      ^
  symbol:   method other_name(String)
  location: class Phrase_greeting

Build failures instead of runtime crashes! ๐Ÿ‘ ๐Ÿ‘ ๐Ÿ‘

This library is experimental, under-tested, and could really use your help.

Give it a whirl and send some pull requests! Inspired by Phrase (which you should use if this plugin fails you miserably) and a genius comment by Mark Carter on its announcement.

Usage

Note: Currently only deployed as 1.0.0-SNAPSHOT for experimentation. Subject to API changes. A release will come soon once I verify the code isn't absolutely terrible.

Apply the plugin in your build.gradle:

buildscript {
  repositories {
    mavenCentral()
    maven url: 'http://oss.sonatype.org/content/repositories/snapshots/'
  }
  dependencies {
    classpath 'com.android.tools.build:gradle:0.8.+'
    classpath 'com.jakewharton.paraphrase:paraphrase:1.0.0-SNAPSHOT'
  }
}

apply plugin: 'android'
apply plugin: 'paraphrase'

To Do

  • Compile-time validation of format string problems.
  • Verify spannable strings actually work...
  • Test fixtures that fail.
  • Tests for generated AbstractPhrase.
  • Emit string resource XML comment as method Javadoc.

License

Copyright 2014 Jake Wharton

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.

paraphrase's People

Contributors

danielkutik avatar jakewharton 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  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

paraphrase's Issues

Thoughts on performance

Scenario:

We have following string:

<string name="greeting">Hello, {name}!</string>

What would happen if we would use Phrase like this ...

for(String name : listOfNames) {
  CharSequence greeting = Phrase.greeting()
    .name("GitHub user")
    .build(this);
}

... in a loop, let's say with 10,000 names.
Every time we call greeting() a new instance of the class Phrase_greeting is constructed, that would leave us with a lot of classes to clean up afterwards. Wouldn't it make sense to keep the instance somehow?

Add compatibility for up-to-date gradle versions

Currently gradle version seems to be limited to 0.8+ via com.android.tools.build:gradle:0.8.+. I would love to use it in my current android project which uses a recent gradle version (1.2.3 as of today).

  1. Do you have any plans to support recent gradle versions?
  2. Anyway thx for contributing this lib! ๐Ÿ‘

Migrate Android Gradle Plugin From 0.8 to 0.9 ?

Recently, gradle plugin for Android bumps 0.8 to 0.9 with the release of Android Studio 5.0.

When I try to integrate paraphrase in my project which is using 0.9, I can't build it. This is my build.gradle file.

buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'com.jakewharton.paraphrase:paraphrase:1.0.0-SNAPSHOT'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'android'
apply plugin: 'paraphrase'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }

        debug {
        }
    }

    signingConfigs {
        debug {
        }
    }

    dependencies {
        compile 'com.android.support:support-v4:19.0.1'
        compile 'com.android.support:appcompat-v7:+'
        compile 'com.jakewharton:butterknife:4.0.1'
        compile 'com.squareup.picasso:picasso:2.2.0'
        compile project(':libs:utils:library')
        compile fileTree(dir: 'libs', include: ['*.jar'])
    }
}

Android Studio gives this error

09:55:35 Gradle 'MyProj' project refresh failed:
         Build script error, unsupported Gradle DSL method found: 'maven()'!
         Possible causes could be:  
         - you are using Gradle version where the method is absent 
         - you didn't apply Gradle plugin which provides the method
         - or there is a mistake in a build script

I like to know if there's something wrong with my build.gradle or I guess it's because paraphrase is built with 0.8 and there's a few changes between 0.8 and 0.9 according to the Migration Guide.

I understand this is 1.0.0-SNAPSHOT so any plan for the migration and stable release ?

Thanks for your awesome work!

Gradle import causes closeable error

I am trying to bring the library in on the latest Android Studio with Gradle 0.9.+ but I suffer from a project refresh failed cause by

         Cause: com.google.common.io.Closeables.closeQuietly(Ljava/io/Closeable;)V

my build script is as easy as it comes

buildscript {
    repositories {
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'com.jakewharton.paraphrase:paraphrase:1.0.0-SNAPSHOT'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

Convert underscore based String format variable names to camel case methods?

What do you think of something like --

<string name="greeting">Hello, {other_name}! My name is {my_name}.</string>

generating this --

CharSequence greeting = Phrase.greeting()
    .otherName("GitHub user")
    .myName("Jake Wharton")
    .build(this);

From what I've seen, stuff like this is pretty common to do for JSON <=> Java conversion and I think it would make for nicer Java code.

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.