Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a code style formater for contributing. #88

Open
glung opened this issue Dec 16, 2016 · 4 comments
Open

Add a code style formater for contributing. #88

glung opened this issue Dec 16, 2016 · 4 comments

Comments

@glung
Copy link
Contributor

glung commented Dec 16, 2016

  • configurable with a gradle task
  • instruction in the contribution file
  • suggested code style attached
  • when done, reformat all the code and push

_idea-codestyle.xml.zip

@NikoYuwono
Copy link
Contributor

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

@NikoYuwono
Copy link
Contributor

@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
Copy link
Contributor Author

glung commented Jan 19, 2017

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.

  • commit: .idea-codestyle.xml
  • add the following gradle task
task setupCodeStyle << {
    def template = file(".idea-codestyle.xml")
    def ideaPrefDirs = file(System.env['HOME'] + '/Library/Preferences/').listFiles().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 {
        file("$it.absolutePath/codestyles").with { it.mkdir(); it }
    }.forEach {
        ln(template, 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
Copy link
Contributor

NikoYuwono commented Jan 20, 2017

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 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants