mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
feat(templates): add API to create file templates
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* This file is part of AndroidIDE.
|
||||
*
|
||||
* AndroidIDE is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* AndroidIDE is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.itsaky.androidide.templates.base
|
||||
|
||||
import com.itsaky.androidide.templates.FileTemplate
|
||||
import com.itsaky.androidide.templates.TemplateBuilder
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* [TemplateBuilder] implementation for building files templates.
|
||||
*
|
||||
* @property dir The directory in which the file must be created.
|
||||
* @author Akash Yadav
|
||||
*/
|
||||
class FileTemplateBuilder(val dir: File) : TemplateBuilder<FileTemplate>() {
|
||||
|
||||
override fun buildInternal(): FileTemplate {
|
||||
return FileTemplate(templateName!!, thumb!!, widgets!!, recipe!!)
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package com.itsaky.androidide.templates.base
|
||||
|
||||
import com.itsaky.androidide.templates.FileTemplate
|
||||
import com.itsaky.androidide.templates.Language
|
||||
import com.itsaky.androidide.templates.Language.Java
|
||||
import com.itsaky.androidide.templates.ModuleTemplate
|
||||
@@ -36,8 +37,6 @@ import com.itsaky.androidide.templates.R
|
||||
import com.itsaky.androidide.templates.Sdk
|
||||
import com.itsaky.androidide.templates.SpinnerWidget
|
||||
import com.itsaky.androidide.templates.TextFieldWidget
|
||||
import com.itsaky.androidide.templates.base.util.AndroidModuleTemplateConfigurator
|
||||
import com.itsaky.androidide.templates.base.util.ProjectTemplateConfigurator
|
||||
import com.itsaky.androidide.templates.base.util.moduleNameToDir
|
||||
import com.itsaky.androidide.templates.enumParameter
|
||||
import com.itsaky.androidide.templates.stringParameter
|
||||
@@ -45,6 +44,10 @@ import com.itsaky.androidide.utils.AndroidUtils
|
||||
import com.itsaky.androidide.utils.Environment
|
||||
import java.io.File
|
||||
|
||||
typealias ProjectTemplateConfigurator = ProjectTemplateBuilder.() -> Unit
|
||||
typealias ModuleTemplateConfigurator = ModuleTemplateBuilder.() -> Unit
|
||||
typealias AndroidModuleTemplateConfigurator = AndroidModuleTemplateBuilder.() -> Unit
|
||||
typealias FileTemplateConfigurator = FileTemplateBuilder.() -> Unit
|
||||
|
||||
/**
|
||||
* Setup base files for project templates.
|
||||
@@ -186,4 +189,15 @@ fun baseAndroidModule(block: AndroidModuleTemplateConfigurator): ModuleTemplate
|
||||
|
||||
block()
|
||||
}.build()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a template for a file.
|
||||
*
|
||||
* @param dir The directory in which the file will be created.
|
||||
* @param configurator The configurator to configure the template.
|
||||
* @return The [FileTemplate].
|
||||
*/
|
||||
fun baseFile(dir: File, configurator: FileTemplateConfigurator): FileTemplate {
|
||||
return FileTemplateBuilder(dir).apply(configurator).build()
|
||||
}
|
||||
@@ -24,10 +24,6 @@ import com.itsaky.androidide.templates.base.ModuleTemplateBuilder
|
||||
import com.itsaky.androidide.templates.base.ProjectTemplateBuilder
|
||||
import java.io.File
|
||||
|
||||
typealias ProjectTemplateConfigurator = ProjectTemplateBuilder.() -> Unit
|
||||
typealias ModuleTemplateConfigurator = ModuleTemplateBuilder.() -> Unit
|
||||
typealias AndroidModuleTemplateConfigurator = AndroidModuleTemplateBuilder.() -> Unit
|
||||
|
||||
/**
|
||||
* Get the asset path for base template.
|
||||
*/
|
||||
|
||||
@@ -68,8 +68,7 @@ enum class Language(val lang: String, val ext: String) {
|
||||
*/
|
||||
enum class ModuleType(val typeName: String) {
|
||||
|
||||
AndroidApp("Android Application"), AndroidLibrary("Android Library"), JavaLibrary(
|
||||
"Java library")
|
||||
AndroidApp("Android Application"), AndroidLibrary("Android Library"), JavaLibrary("Java library")
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -209,6 +208,13 @@ open class ModuleTemplate(val name: String, @StringRes templateName: Int, @Drawa
|
||||
widgets: List<Widget<*>>, recipe: TemplateRecipe
|
||||
) : Template(templateName, thumb, widgets, recipe)
|
||||
|
||||
/**
|
||||
* Template for creating a file.
|
||||
*/
|
||||
open class FileTemplate(@StringRes name: Int, @DrawableRes thumb: Int, widgets: List<Widget<*>>,
|
||||
recipe: TemplateRecipe
|
||||
) : Template(name, thumb, widgets, recipe)
|
||||
|
||||
/**
|
||||
* Base class for template builders.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user