قالب وردپرس درنا توس
Home / IOS Development / # 294: Notes with Sourcery 🔮 – Little Bites of Cocoa

# 294: Notes with Sourcery 🔮 – Little Bites of Cocoa



Subjects

Topics

We first covered Sourcery back in Bite # 292. It is a command-line tool that helps us generate Swift code from .stencil template files.

Today we will check a specific feature of Sourcery, annotations ]. Let's go diving.

Before we begin, let's do a quick "update". Tourcer for Sourcery is Returns malfiles to real Swift code. Full access to the system using SourceKitten. Useful for simple things (automate tasks like adding counts to enums), or crazy complex things (automatically generate Swift test code). Mainly, metaprogramming. Puh. Ok, keep on going.

Annotations are simply small pieces of metadata like annotating our properties, enums, enum cases and other types.

Then we can access this metadata later in our .stencil template files.

Let's try it out.

Before we begin, let's add a new Swift Enum to our project. Nothing special here so far, just plain Swift .

  enum    SpaceshipKind    {
      bag    cruiser 
      bag    frieghter 
      bag   ] destroyer 
} 

Then we add a new SpaceshipKind + Count.stencil file for Sourcery to render. We use "Tom" option in Xcode when you create a new file and then open Inspector (right side) and change the file Type ] to Swift Source so it will read (a bit) better in Xcode .

Let's finally drive sourcery :

  sourcery  source  / templates / / source / _generated / --watch --verbose

Passes in - at Sourcery tells us to run continuously and restore our templates whenever our code or templates change. Correct

The flag - verbose tells Sourcery to print a bunch of useful troubleshooting information to the console while it works. Useful first, but when we are more comfortable with Sourcery, we can probably leave this.

Whew! Now we can finally start using Annotations . Add
annotation to our enum:

  enum    SpaceshipKind    {[1
9659014] case
cruiser case frieghter case destroyer }

Then let us in our template make a count property for all our enums:

  {%    to    enum [19659040]          enums  % .} 
  extension    {{19659041] enum    name  }}    ] {
    static    var    count .    Int    {   returns [19659013] {{   enum .   cases .   count  }}  } 
} 
 {%    EndFor [19659047]% } 

This works well:

  extension    ]    
    
    
    
    
           :    Int    [   Return    3  ] 
} 

But what if we were longer together in our project? What if we had many more Enums in our code base, and wanted to exempt some of them from generating a property. [19659000] We add a new news: [[19659011] enum EngineKind {
case hyperdrive
case solsail
}

Since we added something code, sourcerys - from kicks in and rebuilds our template:

  extension    RomskipKind    [
    static    var    count :    Int    {   19659088] 3  } 
} 
  extension    EngineKind    {
    static    var    count :    Int    {   return    2  } 
} 

Let's pretend we do not need the other one to get a real estate.

There are some ways we can approach Sourcery, but this gives us a great way to learn and understand how annotations work. [19659000] We add a EngineKind to our code:

  /// sourcery: shipCount 
  enum    EngineKind    {[19659014] 
      hyperdrive 
      case    sunsail 
} 

Annotations are simply special comments that Sourcery analyzes and makes available in our .stencil painter.

The cool part is its location ready so we can put annotations over types and they will be available on these types in our .stencil painter! The same goes for annotations on properties enum cases and more!

Now, we can access this annotation in our template:

  {%    to    enum    in    types .   enums  % } 
  extension    {{[19659042] enum .   name  }}    {
    {%    ifnot    enum .   markings .             
    
          19659039].   cases .  ]  }  }    {%    endif  % } 
} 
 {%    endfor [19659045]] } 

Finally, our generated code is back to only one enum :

  extension    Spac eshipKind    {
    static    var [19659055] telle :    Int    {   return    3  } 
} 

Success!

We can also use annotations for multiple things at once with : beginning and : end tags:

  /// sourcery: start: skipCustomSetter    was    captainID :    Int 
  was    maxSpeedInParsecs :    Int 
  /// sourcery: than 

This is just the beginning. Come back next time to learn about key / value pair annotations .

Learn more about Sourcery at git.io/sourcery (also in Bite # 292)




Source link