soundcloud / lightcycle

LightCycle lets self-contained classes respond to Android’s lifecycle events
http://soundcloud.github.io/lightcycle/
Apache License 2.0
705 stars 55 forks source link

Add a code style formater for contributing. #88

Open glung opened 7 years ago

glung commented 7 years ago

_idea-codestyle.xml.zip

NikoYuwono commented 7 years ago

This reminds me to add contribution section in the website too! Let me add it later after this issue is resolved :)

NikoYuwono commented 7 years ago

@glung I don't know if we can use intelliJ native code formatter from gradle task or not, but I found this nice tool called spotless (https://github.com/diffplug/spotless/tree/master/plugin-gradle) that can be used for formatting code based on defined style from gradle task!

Please check it out and if you think it's good I'll try to create a PR!

glung commented 7 years ago

Hum, it seems quite a bit of configurations and adds another dependency.

In the SoundCloud listeners app setup we use the following setup. Let me know what you think about it.

def ln(File dest, File source) { exec { commandLine "ln", "-sf", dest.absolutePath, "$source.absolutePath/LightCycle.xml" } }


- add the following in CONTRIBUTING.xml

./gradlew setupCodeStyle Run ./gradlew setupCodeStyle; this will install our code formatter to all your IntelliJs From within Android Studio, go to File → Other Settings → Default Settings → Editor → Code Style. From the Scheme drop-down menu, select LightCycle

NikoYuwono commented 7 years ago

Yes, too bad Spotless will use different configuration with the native intelliJ one and will add more dependencies.

And I think I misunderstand this statement

configurable with a gradle task

I thought you want the gradle task to fix the wrong code style too!
If not, I think using the native IntelliJ code formatter with the code style you made is enough.

For the gradle task, I think it will only work in Mac environment. To make this work with Linux (unsure with Windows!) I think we will need to add ~/.<PRODUCT><VERSION> (reference) so the task will looks like this

import org.gradle.internal.os.OperatingSystem;

task setupCodeStyle << {
  def template = file(".idea-codestyle.xml")
  def currentOS = OperatingSystem.current()
  def ideaPrefDirs
  if (currentOS.isMacOsX()) {
    ideaPrefDirs = file(System.env['HOME'] + '/Library/Preferences/').listFiles()
  } else if (currentOS.isLinux() || currentOS.isUnix()) {
    ideaPrefDirs = file(System.env['HOME']).listFiles()
  } else {
    ideaPrefDirs = new File[0]
  }
  ideaPrefDirs = ideaPrefDirs.findAll {
    it.path.contains("AndroidStudio") ||
            it.path.contains("IdeaIC") ||
            it.path.contains("IntelliJIdea")
  }
  println("Installing code style template to:")
  ideaPrefDirs.forEach { println("-> $it.name") }
  ideaPrefDirs.collect {
    if (currentOS.isMacOsX()) {
      return file("$it.absolutePath/codestyles").with { it.mkdir(); it }
    } else if (currentOS.isLinux()) {
      return file("$it.absolutePath/config/codestyles").with { it.mkdir(); it }
    } else {
      return it
    }
  }.forEach {
    ln(template, it)
  }
}

This task is compatible with both MacOS and Linux (and other Unix based OS too).

Another way is we can write an .sh script and run it independently, and for Windows platform we can also write .bat script (Might be need some time to learn it first!).

Let me know what do you think about it 😃