Git Product home page Git Product logo

codegen's Introduction

Codegen

This is simple code generator that uses Stencil as template language and as input you can provide a context using a YAML, JSON or PLIST file.

Codegen is helpful for the cases that you have to autogenerate code, but as input, you can't use your source code. For instance, you can have a YAML with a list of feature flags and from there, autogenerate all the related boilerplate code. Check Word of the Day example to learn more about that.

If you can use your source code as input to autogenerate the code, you should try Sourcery.

Stencil is a simple and powerful template language for Swift, you can find more information here.

Codegen also uses StencilSwiftKit, that library adds more Tags and Filters to Stencil.

Installation

  • Building with Swift Package Manager:

You can clone the project and then run:

$ swift build -c release
$ cd .build/release
$ cp -f codegen /usr/local/bin/codegen

Alternatively, you can run $ sh install.sh from the root folder.

  • Homebrew

🚧coming soon 🚧

Usage

codegen-base-example

Codegen is a command line tool. You have to provide three parameters:

  • --input or -i A path to the file input.
  • --template or -t A path to the stencil template.
  • --output or -o A path for the autogenerated codoe.

$ codegen -i fruits.yaml -t template.stencil -o fruits.html

As input you can provide a YAML, JSON or PLIST file, the extension is used to know how to parse the file.

Alternatively, instead of a file, you can provide as input a dictionary.

$ codegen -i '{"fruits": ["Apple", "Organge", "Lemon"]}' -t template.stencil -o fruits.html

By default, existing autogenerated files are not overriden. If you you want to override files everytime that you run codegen, include --override flag.

File annotations

In case that you want to generate multiple files from one template or just set a custom name for the autogenerated file based on the input, you can use file annotations.

Imagine that you have an input YAML of people:

codegen-file-annotations-example

Having this input.yaml

people:
  - name: "Pablo"
    city: "Barcelona"
    profession: "Software Engineer"
  - name: "John"
    city: "New York"
    profession: "Graphic Designer"
  - name: "Akira"
    city: "Tokyo"
    profession: "Writer"

If you want to generate one output file for each person, you can create a template like:

{% for person in people %}
// codegen:file:begin:{{ person.name }}.md
{{ person.name }} is from {{ person.city }} and works as {{ person.profession }}.
// codegen:file:end
{% endfor %}

That will generate three files:

Pablo.md

Pablo is from Barcelona and works as a Software Engineer.

John.md

John is from New York and works as a Graphic Designer.

Akira.md

Akira is from Tokyo and works as a Writer.

Additionaly, you can define some directory as part of the file name:

// codegen:file:begin:{{ person.name }}/data.md

If the directory doesn't exist, it will be created.

You can also define a common root folder for all autogenerated annotated files when you execute the codegen command:

$ codegen -i people.yml -t template.stencil -o People

In that case, files will be generated with this directory structure:

People/Pablo/data.md
People/John/data.md
People/Akira/data.md

codegen's People

Contributors

pablo-e-romero avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.