Use Hugo highlighting

Signed-off-by: Harsh Shandilya <msfjarvis@gmail.com>
This commit is contained in:
Harsh Shandilya 2019-12-05 23:45:49 +05:30
parent 496f5755d0
commit 7e7477fdd3
5 changed files with 78 additions and 79 deletions

View File

@ -25,7 +25,7 @@ The first part of this endeavour involved finding a new way to move static asset
To handle my two-sites-from-one-repo usecase, I setup a build staging -> publish staging -> build prod -> publish prod pipeline.
```yaml
{{< highlight yaml >}}
- name: Build staging
run: hugo --minify -DEFb=https://staging.msfjarvis.website
@ -49,7 +49,7 @@ To handle my two-sites-from-one-repo usecase, I setup a build staging -> publish
SERVER_ADDRESS: ${{ secrets.SERVER_ADDRESS }}
SERVER_DESTINATION: ${{ secrets.SERVER_DESTINATION_PROD }}
SSH_PORT: ${{ secrets.SSH_PORT }}
```
{{< / highlight >}}
You can find the `ci/deploy.sh` script [here](https://github.com/msfjarvis/msfjarvis.website/blob/src/ci/deploy.sh). It's a very basic script that sets up the SSH authentication and rsync's the built site over.

View File

@ -20,7 +20,7 @@ NB: Grab a Personal Access Token from GitHub with the `write:packages` scope. Yo
Copy the official integration step from GitHub's [guide](https://help.github.com/en/github/managing-packages-with-github-packages/configuring-gradle-for-use-with-github-packages#authenticating-with-a-personal-access-token), into your Android library's `build.gradle` / `build.gradle.kts`. If you try to run `./gradlew publish` now, you'll run into errors. We'll be fixing that shortly. \[[Commit link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/d69235577a1d4345cecb364a3a3d366bf894c5a6)\]
```diff
{{< highlight diff >}}
--- library/build.gradle
+++ library/build.gradle
@@ -1,5 +1,6 @@
@ -55,13 +55,13 @@ Copy the official integration step from GitHub's [guide](https://help.github.com
dependencies {
api deps.support.app_compat
implementation deps.kotlin.stdlib8
```
{{< / highlight >}}
#### Step 2
Switch out the `maven-publish` plugin with [this](https://github.com/wupdigital/android-maven-publish) one. It provides us an Android component that's compatible with publications and precisely what we need. \[[Commit link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/1452c4a0c15d394b73dc3384f02834788dfe1bda)\]
```diff
{{< highlight diff >}}
--- build.gradle
+++ build.gradle
@@ -14,6 +14,7 @@ buildscript {
@ -95,13 +95,13 @@ Switch out the `maven-publish` plugin with [this](https://github.com/wupdigital/
apply from: "../dependencies.gradle"
// apply from: "../bintrayconfig.gradle"
```
{{< / highlight >}}
#### Step 3
Switch to using the `android` component provided by `wup.digital.android-maven-publish`. This is the one we require to be able to upload an [AAR](https://developer.android.com/studio/projects/android-library) artifact. \[[Commit link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/7cc6fcd6ffa5774433bce76ac6929435dbbb77cc)\]
```diff
{{< highlight diff >}}
--- library/build.gradle
+++ library/build.gradle
@@ -42,7 +42,7 @@ publishing {
@ -113,15 +113,15 @@ Switch to using the `android` component provided by `wup.digital.android-maven-p
}
}
}
```
{{< / highlight >}}
#### Step 4
Every Gradle/Maven dependency's address has three attributes, a group ID, an artifact ID, and a version.
```groovy
{{< highlight groovy >}}
implementation 'com.example:my-fancy-library:1.0.0'
```
{{< / highlight >}}
Here:
@ -131,7 +131,7 @@ Here:
We'll need to configure these too. I prefer using the `gradle.properties` file for this purpose since it's very easy to access variables from it, but if you have a favorite way of configuring build properties, use that instead! \[[Commit link](https://github.com/msfjarvis/github-packages-deployment-sample/commit/cee74a5e0b3b76d1d7a2d4eb9636d80fb1db49d6)\]
```diff
{{< highlight diff >}}
--- gradle.properties
+++ gradle.properties
@@ -19,3 +19,7 @@ android.useAndroidX=true
@ -156,7 +156,7 @@ We'll need to configure these too. I prefer using the `gradle.properties` file f
}
}
}
```
{{< / highlight >}}
#### Step 5
@ -164,7 +164,7 @@ Now all that's left to do is configure GitHub Actions. Go to the Secrets menu in
Now, let's add the actual configuration that'll get Actions up and running.
```diff
{{< highlight diff >}}
--- /dev/null
+++ .github/workflows/publish_snapshot.yml
@@ -0,0 +1,13 @@
@ -181,7 +181,7 @@ Now, let's add the actual configuration that'll get Actions up and running.
+ env:
+ USERNAME: msfjarvis
+ PASSWORD: ${{ secrets.PACKAGES_TOKEN }}
```
{{< / highlight >}}
That's it! Once you push to GitHub, you'll see the [action running](https://github.com/msfjarvis/github-packages-deployment-sample/commit/42e1f6609bf9f2abe8e181296a57d86df648b4d4/checks?check_suite_id=322323808) in your repository's Actions tab and a [corresponding package](https://github.com/msfjarvis/github-packages-deployment-sample/packages/60429) in the Packages tab once the workflow finishes executing.

View File

@ -15,19 +15,19 @@ Java:
{{< highlight java >}}
class Person {
private final String name;
private final String name;
public Person(String name) {
this.name = name;
}
public Person(String name) {
this.name = name;
}
}
{{< /highlight >}}
{{< / highlight >}}
Kotlin:
{{< highlight kotlin >}}
class Person(val name: String)
{{< /highlight >}}
{{< / highlight >}}
The benefits of using Kotlin immediately start showing! But let's go over this in a sysmetatic fashion and break down each aspect of what makes Kotlin so great.
@ -41,13 +41,13 @@ The primary constructor cannot have any code so Kotlin provides something called
{{< highlight kotlin >}}
class Person(val name: String) {
init {
println("Invoking constructor!")
}
init {
println("Invoking constructor!")
}
}
val _ = Person("Matt")
{{< /highlight >}}
{{< / highlight >}}
Moving on, let's add an optional age parameter to our classes, with a default value of 18. To make it convenient to see how different constructors affect values, we're also including an implementation of the `toString` method for some classing print debugging.
@ -55,36 +55,35 @@ Java:
{{< highlight java >}}
class Person {
private final String name;
private int age = 18;
private final String name;
private int age = 18;
public Person(String name) {
this.name = name;
}
public Person(String name) {
this.name = name;
}
public Person(String name, int age) {
this(name);
this.age = age;
}
public Person(String name, int age) {
this(name);
this.age = age;
}
@Override
public String toString() {
return "Name=" + name + ",age=" + Integer.toString(age);
}
@Override
public String toString() {
return "Name=" + name + ",age=" + Integer.toString(age);
}
}
{{< /highlight >}}
{{< / highlight >}}
Kotlin:
{{< highlight kotlin >}}
class Person(val name: String, val age: Int = 18) {
override fun toString() : String {
// I'll go over string templates in a future post, hold me to it :)
return "Name=$name,age=$age"
}
override fun toString() : String {
// I'll go over string templates in a future post, hold me to it :)
return "Name=$name,age=$age"
}
}
{{< /highlight >}}
{{< / highlight >}}
Lots of new things here! Let's break them down.
@ -102,11 +101,11 @@ An important note here is that constructors with default values don't directly w
{{< highlight kotlin >}}
class Person @JvmOverloads constructor(val name: String, val age: Int = 18) {
override fun toString() : String {
return "Name=$name,age=$age"
}
override fun toString() : String {
return "Name=$name,age=$age"
}
}
{{< /highlight >}}
{{< / highlight >}}
Doing this will generate constructors similar to how we previously wrote in Java, to allow both Kotlin and Java callers to work.
@ -118,13 +117,13 @@ Java:
{{< highlight java >}}
public class Man extends Person { /* Class body */ } // Valid in Java
{{< /highlight >}}
{{< / highlight >}}
Kotlin:
{{< highlight kotlin >}}
class Man(val firstName: String) : Person(firstName) // Errors!
{{< /highlight >}}
{{< / highlight >}}
Trying it out in the Kotlin REPL
@ -138,7 +137,7 @@ Trying it out in the Kotlin REPL
error: this type is final, so it cannot be inherited from
class Man(val firstName: String) : Person(firstName)
^
{{< /highlight >}}
{{< / highlight >}}
Makes sense, since that's default for Kotlin. Let's add the `open` keyword to our definition of `Person` and try again.
@ -151,7 +150,7 @@ Makes sense, since that's default for Kotlin. Let's add the `open` keyword to ou
>>> class Man(val firstName: String) : Person(firstName)
>>> println(Man("Henry"))
Name=Henry,age=18
{{< /highlight >}}
{{< / highlight >}}
And everything works as we'd expect it to. This is a behavior change that is confusing and undesirable to a lot of people, so Kotlin provides a compiler plugin to mark all classes as `open` by default. Check out the [`kotlin-allopen`](https://kotlinlang.org/docs/reference/compiler-plugins.html#all-open-compiler-plugin) page for more information about how to configure the plugin for your needs.
@ -165,20 +164,20 @@ Java:
{{< highlight java >}}
public static final class StringUtils {
public static String normalizePath(final String str) {
return str.replace("/document/primary:", "/sdcard/");
}
public static String normalizePath(final String str) {
return str.replace("/document/primary:", "/sdcard/");
}
}
{{< /highlight >}}
{{< / highlight >}}
Kotlin:
{{< highlight kotlin >}}
object StringUtils {
// I'll cover this declaration style too. It's just the first post!
fun normalizePath(str: String): String = str.replace("/document/primary:", "/sdcard/")
// I'll cover this declaration style too. It's just the first post!
fun normalizePath(str: String): String = str.replace("/document/primary:", "/sdcard/")
}
{{< /highlight >}}
{{< / highlight >}}
A recurring pattern with Kotlin is concise code, as you can see in this case.

View File

@ -11,13 +11,13 @@ Even the variables in Kotlin are supercharged!
Let's start with a simple [data class](https://kotlinlang.org/docs/reference/data-classes.html#data-classes) and see how the variables in there behave.
``` kotlin
{{< highlight kotlin >}}
data class Student(val name: String, val age: Int, val subjects: ArrayList<String>)
```
{{< / highlight >}}
To use the variables in this class, Kotlin let's you directly use the dot notation for accessing.
``` kotlin
{{< highlight kotlin >}}
>>> val s1 = Student("Keith Hernandez", 21, arrayListOf("Mathematics", "Social Studies"))
>>> println(s1.name)
@ -27,19 +27,19 @@ Keith Hernandez
>>> println(s1) // data classes automatically generate `toString` and `hashCode`
Student(name=Keith Hernandez, age=21, subjects=[Mathematics, Social Studies])
```
{{< / highlight >}}
For Java callers, Kotlin also generates getters and setter methods.
``` java
{{< highlight java >}}
final Student s1 = new Student("Keith Hernandez", 21, arrayListOf("Mathematics", "Social Studies"));
System.out.println(s1.getName());
System.out.println(s1);
```
{{< / highlight >}}
The same properties apply to variables in non-data classes as well.
``` kotlin
{{< highlight kotlin >}}
>>> class Item(id: Int, name: String) {
@ -58,7 +58,7 @@ Line_4$Item@46fb460a
>> >
```
{{< / highlight >}}
As you can notice, the `toString` implementation is not identical to our data classes but that's a topic for another post. Back to variables!
@ -66,7 +66,7 @@ As you can notice, the `toString` implementation is not identical to our data cl
While Kotlin creates getters and setters automatically, we can customize their behaviour.
``` kotlin
{{< highlight kotlin >}}
class Item(id: Int, name: String) {
var itemId = id
var itemName = name
@ -80,18 +80,18 @@ class Item(id: Int, name: String) {
return "id=$itemId,name=$itemName"
}
}
```
{{< / highlight >}}
Let's take this for a spin in the Kotlin REPL and see how our `currentState` field behaves.
```kotlin
{{< highlight kotlin >}}
>>> val item = Item(0, "Nails")
>>> println(item)
id=0,name=Nails
>>> item.currentState = Pair(1, "Bricks")
>>> println(item)
id=1,name=Bricks
```
{{< / highlight >}}
Notice how setting a new value to currentState mutates the other variables as well? That's because of our custom setter. These setters are identical to a normal top-level function except a reference to the field in question is available as the variable `field` for manipulation.
@ -103,7 +103,7 @@ Kotlin's visiblity modifiers aren't very well explained. There's the standard `p
`inner` is a modifier that only applies to classes declared within another one. It allows you to access members of the enclosing class. A sample might help explain this better.
```kotlin
{{< highlight kotlin >}}
class Outer {
private val bar: Int = 1
inner class Inner {
@ -112,7 +112,7 @@ class Outer {
}
val demo = Outer().Inner().foo() // == 1
```
{{< / highlight >}}
The keyword `this` does not behave as some would normally expect in inner classes, go through the Kotlin documentation for `this` [here](https://kotlinlang.org/docs/reference/this-expressions.html) and I'll be happy to answer any further questions :)

View File

@ -24,7 +24,7 @@ Gradle doesn't seem to have invested any actual time in converting the original
Groovy
```groovy
{{< highlight groovy >}}
android {
compileSdkVersion 29
buildToolsVersion = '29.0.2'
@ -42,11 +42,11 @@ dependencies {
force = true
}
}
```
{{< / highlight >}}
Kotlin
```groovy
{{< highlight groovy >}}
android {
compileSdkVersion(29)
buildToolsVersion = "29.0.2"
@ -64,13 +64,13 @@ dependencies {
isForce = true
}
}
```
{{< / highlight >}}
I am definitely biased here, but this is not how an idiomatic Kotlin API looks like.
What we should have gotten
```groovy
{{< highlight groovy >}}
android {
compileSdkVersion = 29
buildToolsVersion = "29.0.2"
@ -89,7 +89,7 @@ dependencies {
force = true
}
}
```
{{< / highlight >}}
Property access syntax and discoverable variable names should have been the norm since day one for it to actually be a good Kotlin DSL.