Properties

Last updated 2 months ago

Now that you have an idea of Templatefiles and Datafiles. In this tutorial we will extend our plugin to generate a little more complex files.

The Plugin Datafiles are stored inside AarKay/AarKayData/AarKayPlugin .

We already have a starter plugin file which we were using in the tutorials before. - AarKay/AarKayData/AarKayPlugin/Sources/AarKayMyFirstPlugin/MyFirstPlugin.Template.yml

name: MyFirstPlugin
isTemplate: true
properties:
- name|String

You must also be familiar with the property {{ name }} which we used inside the Templatefiles.

Now let's add some more properties to the template MyFirstPlugin. Let's change the contents of the file to the following

name: MyFirstPlugin
isTemplate: true
properties:
- name|String
- age|Int!|18

We already had a property name with a type String. Now we have added one more property age with a type Int!. We used an exclmation mark with the type because we want to specify a default value as 18 to it.

Let's run the command sh scripts/run.

Notice that the file Sources/AarKayMyFirstPlugin/MyFirstPlugin.swift has been modified and age has been added as a property to our templates.

Lets modify our Templatefile MyFirstPlugin.swift.stencil to the following contents

import Foundation
‚Äč
class {{ name }} {
let age: Int = {{ age }}
init() {
<aarkay init{{name}}>
</aarkay>
}
}

Now running sh scripts/run command will update our generated files with the new templatefiles.

The code inside the Template Placeholders will still remain preserved as it was.

You can now try to override the default age from the Datafile [].MyFirstPlugin.yml and run the command sh scripts/run again.

- name: Rahul
age: 27
- name: John
age: 25
- name: Peter
- name: Daniel
- name: Judah
- name: Job
- name: Josheph