在Android上一尘不染

Setting up spotless is quite easy, let’s take a look at what we did at Collect by WeTransfer to have a setup that is as much ready-to-go as possible.

设置spotless非常容易,让我们看一下我们在WeTransfer的 Collect中所做的事情,以使设置尽可能多地准备就绪

less什么是一尘不染? (🔍 What is spotless?)

Is a code formatter: we can define rules and it will apply them every time we execute it.

是一个代码格式化程序:我们可以定义规则,并且每次执行时都会应用它们。

It’s pretty useful in automating fixes for pretty simple (and common) formatting mistakes as in spaces, newlines, removing unnecessary imports, etc.

在自动修复非常简单(常见)的格式错误(如空格,换行符,删除不必要的导入等)时,此功能非常有用。

You can find more info at https://github.com/diffplug/spotles

您可以在https://github.com/diffplug/spotles上找到更多信息。

🛠设置 (🛠 Setup)

The setup is pretty simple (you can follow the instructions in the GitHub repo as well): add the necessary dependencies, install ktlint on your machine and define a spotless block in your project level build.gradle file and inside that block define whatever rules you need for your codebase.

设置非常简单(您也可以按照GitHub存储库中的说明进行操作):添加必要的依赖项,在计算机上安装ktlint ,并在项目级别的 build.gradle文件中定义一个spotless build.gradle块,并在该块内定义您要使用的任何规则需要您的代码库。

Something like this:

像这样:

If we want to do something nicer, we can create a dedicated gradle file and apply it to the project one. It is as simple as creating a spotless.gradle file (you name it), adding it to the root directory of our project and then, again, in the project level build.gradle file we add

如果我们想做更好的事情,我们可以创建一个专用的gradle文件并将其应用于项目。 这就像创建一个spotless.gradle文件(将其命名)一样简单,将其添加到我们项目的根目录中,然后再次在项目级别的build.gradle文件中添加

apply from: "$project.rootDir/spotless.gradle"

so our project level build.gradle file will look something like

所以我们的项目级别的build.gradle文件看起来像

Now we have spotless configured on our machine and we can run it, to that simply run the command:

现在,我们已经在机器上配置了spotless ,并且可以运行它,只需运行以下命令即可:

./gradlew spotlessApply

Note that due to this line in our configuration

请注意,由于我们配置中的这一行

ratchetFrom 'origin/develop'

it will run only on modified files (in relation to the develop branch in our case)

它将仅在修改后的文件上运行(在我们的案例中,相对于develop分支)

Want to try how is working?

想尝试如何运作?

Just add a class (or modify an existing one) as follows:

只需添加一个类(或修改现有的类),如下所示:

Image for post

run ./gradlew spotlessApply and it should become:

运行./gradlew spotlessApply ,它应该变成:

Image for post

⏲让我们在正确的时间运行 (⏲ Let’s make it run at the right time)

When could we run spotless ? Ideally before a commit, and that’s easy to achieve thanks to Git hooks. We will be looking more specifically at the pre-commit hooks.

我们什么时候可以spotless ? 理想情况下,在提交之前,使用Git hooks可以轻松实现 我们将更具体地关注预提交挂钩。

As stated in the documentation, we can just modify (or create)the desired file (pre-commit in our case) under the folder .git/hooks and make it executable.

如文档中所述,我们只需修改(或创建) .git/hooks文件夹下的所需文件(在我们的情况下为pre-commit ),然后使其可执行即可。

so a simple bash script like this:

所以像下面这样一个简单的bash脚本:

will do the job.

会做的工作。

Wait, do we have to configure this script for each machine that will pull the project? Well… Yes.And we have two options here: going around gently forcing our lovely colleagues to configure that script or automate this process too 🤖.I went for the latter :)

等待,我们是否必须为将提取项目的每台计算机配置此脚本? 好吧...是的。我们这里有两个选择:慢慢地强迫我们可爱的同事配置该脚本或使该过程也自动化🤖。我选择了后者:)

🤖自动执行预提交脚本设置 (🤖 Automate pre-commit script setup)

Let’s go back to the spotless.gradle file (or wherever you choose to define your spotless configuration) and add this task at the end of the file:

让我们回到spotless.gradle文件(或您选择定义无污点配置的任何位置)并将此任务添加到文件末尾:

Let me explain in detail what’s going on there.In the first two lines

让我详细解释那里发生了什么。在前两行中

def gitHooksDirectory = new File("$project.rootDir/.git/hooks/")
if (!gitHooksDirectory.exists()) gitHooksDirectory.mkdirs()

we just make sure that the necessary directories that will hold the Git hooks are in place and, if not, create them.Then we create a new file (as said before it must be named pre-commit) and we write our script into that file.

我们只是确保将用于保存Git钩子的必要目录放置到位,如果没有,则创建它们,然后创建一个新文件(如前所述,必须将其命名为pre-commit ),然后将脚本写入其中文件。

new File("$project.rootDir/.git/hooks", "pre-commit").text = """
#!/bin/bash
echo "Running spotless check"
./gradlew spotlessApply
"""

Finally, we make that file executable.

最后,我们使该文件可执行。

"chmod +x .git/hooks/pre-commit".execute()

And we are done! We now have a fully automated code formatting check and relative fixes applied automatically! 🚀

我们完成了! 现在,我们有了全自动的代码格式检查功能,并且自动应用了相关的修复程序! 🚀

🗒注意 (🗒 Notes)

如果在spotless配置期间Gradle引发错误 (If Gradle raises an error during the spotless configuration)

Cannot add task ‘clean’ as a task with that name already exists

then just remove the clean task in your build.gradle file:

然后只需删除build.gradle文件中的clean任务:

task clean(type: Delete) {delete rootProject.buildDir}

Android Studio配置 (Android Studio configuration)

Make sure you have single class imports enabled in the settings for kotlin files (and java ones if you have java in your project)

确保在kotlin文件的设置中启用了single class imports (如果项目中包含Java,则设置为Java)

翻译自: https://medium.com/swlh/spotless-on-android-3716459cecd7

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
跳蚤市场是一种非常流行的二手交易平台,它可以让用户在平台上发布二手物品并与其他用户交换物品。为了确保跳蚤市场的使用体验和安全性,我们需要对其进行非功能需求分析。以下是基于Android的跳蚤市场设计与实现的非功能需求分析。 1. 可靠性 跳蚤市场必须保证平台的可靠性。这意味着它必须能够处理大量的交易,且不会出现任何系统故障。此外,它还需要能够保证用户数据的完整性和安全性。 2. 性能 跳蚤市场的性能也是非常重要的。它需要能够快速地响应用户请求,减少等待时间和加载时间。为了实现这一点,我们需要对系统进行优化,例如使用高效的算法和数据库设计。 3. 可扩展性 跳蚤市场需要具备可扩展性,因为我们需要在未来扩展平台的功能。例如,我们可能需要添加新的支付方式、物流服务、或者增加新的商品分类等。为了实现这一点,我们需要使用可扩展的系统设计,并且确保系统可以轻松地扩展。 4. 安全性 跳蚤市场的安全性也是非常重要的。它需要能够确保用户信息的安全和保密性,以及防止各种网络攻击,例如黑客攻击和DDoS攻击。为了实现这一点,我们需要使用安全的加密协议和数据存储技术,并且在系统中使用安全的编码实践。 5. 易用性 跳蚤市场需要具备易用性,以便用户可以轻松地使用平台。这意味着我们需要设计一个简单的用户界面,并提供清晰的操作指南和帮助文档。此外,我们还需要考虑到用户的反馈和建议,并及时对系统进行改进。 综上所述,基于Android的跳蚤市场需要具备可靠性、性能、可扩展性、安全性和易用性等非功能需求。为了实现这些需求,我们需要使用高效的系统设计、数据存储技术、安全的编码实践和用户友好的界面设计。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值