Kotlin library and command line tool for generation of directory with files from template (on Handlebars) and configuration.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/icerockdev/shaper/master/install-shaper.sh)"
repositories {
mavenCentral()
}
dependencies {
implementation("dev.icerock.tools:shaper-core:0.5.1")
}
You can use generator by CLI app. Configuration stored in yaml file in this case:
globalParams:
packageName: dev.icerock
files:
- pathTemplate: 'build.gradle.kts'
contentTemplateName: build.gradle.kts
templateParams:
dependencies:
- dep1
- dep2
- pathTemplate: 'src/main/kotlin/{{packagePath packageName}}/Test.kt'
contentTemplateName: Test.kt
templateParams:
packageName: dev.icerock.test
To run CLI:
shaper-cli --help
Core is kotlin library, for integration in cli-app, idea plugin, gradle plugin, etc.
Using is simple:
import dev.icerock.tools.shaper.core.TemplateConfig
import dev.icerock.tools.shaper.core.Shaper
// describe files configuration for generator
val buildGradleFile = TemplateConfig.FileConfig(
// path of output file
pathTemplate = "build.gradle.kts",
// name of template file used for content of file
contentTemplateName = "build.gradle.kts",
// params that will be passed into template
templateParams = mapOf("dependencies" to listOf("dep1", "dep2"))
)
val sourceCodeFile = TemplateConfig.FileConfig(
// path also support templates and got all params
pathTemplate = "src/main/kotlin/{{packagePath packageName}}/Test.kt",
contentTemplateName = "Test.kt",
templateParams = mapOf("packageName" to "dev.icerock.test")
)
// describe generator config
val config = TemplateConfig(
// params that will be passed into template of each files.
// can be overriden by params of file
globalParams = mapOf("packageName" to "dev.icerock"),
// list of files that should be generated
files = listOf(buildGradleFile, sourceCodeFile)
)
// create generator with configuration
val shaper = Shaper(config)
// execute generation into build/test directory
shaper.execute(output = "build/test")
Templates will be loaded by priority:
- search at working dir by name
- search at template repositories from
~/.shaper/config.yaml
- search at resources of ClassLoader by name
Here sample templates:
build.gradle.kts.hbs
:
Test.kt.hbs
:
You can include/exclude files using global parameters in config file.
-
Add global parameter(s) that can have value
true
orfalse
. -
Add
{{incl ...}}
helper with the specified parameter in file path and/or file name.
globalParams:
...
addTests: false
addDefaultTest: false
files:
- pathTemplate: '{{incl addTests}}src/main/kotlin/package/{{incl addDefaultTest}}DefaultTest.kt'
contentTemplateName: DefaultTest.kt
In this example, file DefaultTest.kt
will be added only if both parameters addFragment
and addDefaultTest
are set to true
.
For use partials you can put your .hbs
files in includes
directories (multi-nesting support).
In config yaml file:
globalParams:
...
files:
...
outputs:
...
includes:
- partials
- layouts
partials/sub-partials/type.hbs
layouts/hello.hbs
And use the partials in templates:
In config yaml file:
globalParams:
items:
- name: "name1"
type: "type1"
...
- name: "name2"
type: "type2"
...
filterByAllOf
filters items
by all pair key-value (AND condition), for example:
filterByOneOf
filters items
by one of pair key-value (OR condition), for example:
containsAllOf
checks for all pair key-value (AND condition) in items
, for example:
containsOneOf
checks for one of pair key-value (OR condition) in items
, for example:
containsKey
checks for exist key in items
, for example:
containsValue
checks for exist value in items
, for example:
currentTimestamp
returns the current UNIX time, for example:
currentDateTime
returns the current date and/or time in a specified format, for example:
renderPartial
render partial template like {{> paramTemplate paramName="test"}}
and return it value, for example:
Copyright 2021 IceRock MAG Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.