我正在尝试创建一个具有嵌套包的模板。
这是我的模板代码。
// root/global.xml.ftl
<?xml version="1.0"?>
<globals>
<global id="simpleLayoutName" value="${layoutName}"/>
<global id="excludeMenu" type="boolean" value="true"/>
<global id="resOut" value="${resDir}"/>
<global id="srcOut" value="${srcDir}/${slashedPackageName(packageName)}"/>
<#include "../common/common_globals.xml.ftl" />
</globals>
// root/recipe.xml.ftl
<?xml version="1.0"?>
<recipe>
<#if generateLayout>
<#include "../common/recipe_simple.xml.ftl" />
<open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml"/>
</#if>
<instantiate from="root/src/app_package/SimpleFragment.kt.ftl"
to="${escapeXmlAttribute(srcOut)}/${activityClass}Fragment.kt"/>
</recipe>
// root/template.xml
<?xml version="1.0"?>
<template
format="5"
revision="1"
name="MVVM Activity"
minApi="9"
minBuildApi="14"
description="Creates a new empty activity that uses MVVM Pattern">
<category value="Activity" />
<formfactor value="Mobile" />
<parameter
id="generateLayout"
name="Generate Layout File"
type="boolean"
default="true"
help="If true, a layout file will be generated" />
<parameter
id="layoutName"
name="Layout Name"
type="string"
constraints="layout|unique|nonempty"
suggest="${activityToLayout(activityClass)}"
default="activity_main"
visibility="generateLayout"
help="The name of the layout to create for the activity" />
<parameter
id="packageName"
name="Package name"
type="string"
constraints="package"
default="com.mycompany.myapp" />
<!-- 128x128 thumbnails relative to template.xml -->
<thumbs>
<!-- default thumbnail is required -->
<thumb>template_MVVM_activity.png</thumb>
</thumbs>
<globals file="globals.xml.ftl" />
<execute file="recipe.xml.ftl" />
</template>
// root/src/app_package/SimpleFragment.kt.ftl
package ${packageName}
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class ${fragmentClass}Fragment : Fragment() {
// TODO: Implement by lazy ViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// TODO: Init or setup view, viewmodel related, etc anything to setup here
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// TODO: Init or setup view, viewmodel related, etc anything to setup here
}
}
但是,我在这里找不到用于创建目录的任何功能:http://freemarker.apache.org/docs
好像我在这里缺少什么,有没有人这样做来实现在输出目录中创建包。
我很了解recipe.xml.ftl
可以实例化新文件,但是我仍然没有尝试创建目录,这可能吗? from
属性值应添加什么?
答案 0 :(得分:0)
根据此article。默认情况下,您无需任何额外的操作即可执行此操作。只需将新软件包指向recipe.xml.ftl
,例如:
<instantiate from="root/src/main/java/model/Simple.kt.ftl"
to="${escapeXmlAttribute(srcOut)}/model/${modelName}.kt"/>
to
元素将在目标目录内创建新的model
包。