Profile: Load profile registered in registry

This commit is contained in:
zt515 2017-12-15 21:24:47 +08:00
parent 63ced09c99
commit bc57308c00
9 changed files with 119 additions and 27 deletions

View File

@ -0,0 +1,3 @@
profile-shell: {
bell: true
}

View File

@ -10,4 +10,10 @@ class NeoLangGroupNode(val attributes: Array<NeoLangAttributeNode>) : NeoLangBas
override fun toString(): String {
return "NeoLangGroupNode { attrs: $attributes }"
}
companion object {
fun emptyNode() : NeoLangGroupNode {
return NeoLangGroupNode(arrayOf())
}
}
}

View File

@ -6,8 +6,13 @@ import io.neolang.runtime.type.NeoLangArray
import io.neolang.runtime.type.NeoLangValue
class ConfigVisitor : IVisitorCallback {
private var rootContext: NeoLangContext? = null
private var currentContext: NeoLangContext? = null
fun getRootContext(): NeoLangContext {
return rootContext!!
}
fun getContext(contextPath: Array<String>): NeoLangContext {
var context = getCurrentContext()
contextPath.forEach {
@ -27,6 +32,7 @@ class ConfigVisitor : IVisitorCallback {
override fun onStart() {
currentContext = NeoLangContext("global")
rootContext = currentContext
}
override fun onFinish() {

View File

@ -21,6 +21,10 @@ import io.neoterm.frontend.session.shell.ShellProfile
object NeoInitializer {
fun init(context: Context) {
NLog.init(context)
initComponents()
}
fun initComponents() {
ComponentManager.registerComponent(ConfigureComponent::class.java)
ComponentManager.registerComponent(CodeGenComponent::class.java)
ComponentManager.registerComponent(ColorSchemeComponent::class.java)

View File

@ -1,8 +1,20 @@
package io.neoterm.component.profile
import io.neolang.visitor.ConfigVisitor
import io.neoterm.component.codegen.CodeGenParameter
import io.neoterm.component.codegen.generator.ICodeGenerator
import io.neoterm.component.codegen.impl.NeoProfileGenerator
import io.neoterm.component.codegen.model.CodeGenObject
/**
* @author kiva
*/
abstract class NeoProfile {
abstract class NeoProfile : CodeGenObject {
abstract val profileMetaName: String
abstract fun onProfileLoaded(visitor: ConfigVisitor): Boolean
override fun getCodeGenerator(parameter: CodeGenParameter): ICodeGenerator {
return NeoProfileGenerator(parameter)
}
}

View File

@ -1,7 +1,11 @@
package io.neoterm.component.profile
import io.neoterm.component.config.ConfigureComponent
import io.neoterm.frontend.component.ComponentManager
import io.neoterm.frontend.component.NeoComponent
import io.neoterm.frontend.config.NeoConfigureFile
import io.neoterm.frontend.config.NeoTermPath
import io.neoterm.frontend.logging.NLog
import io.neoterm.frontend.session.shell.ShellProfile
import java.io.File
@ -20,11 +24,39 @@ class ProfileComponent : NeoComponent {
profileRegistry.remove(metaName)
}
private fun checkForFiles() {
val profileDir = File(NeoTermPath.PROFILE_PATH)
if (!profileDir.exists()) {
profileDir.mkdirs()
inline fun <reified T: NeoProfile> loadConfigure(file: File): T {
return loadConfigure(file, T::class.java) as T
}
@Suppress("UNCHECKED_CAST")
fun loadConfigure(file: File, clazz: Class<out NeoProfile>): NeoProfile {
val loaderService = ComponentManager.getComponent<ConfigureComponent>()
val configure: NeoConfigureFile?
try {
configure = loaderService.newLoader(file).loadConfigure()
if (configure == null) {
throw RuntimeException("Parse configuration failed.")
}
} catch (e: Exception) {
NLog.e("Profile", "Failed to load profile: ${file.absolutePath}: ${e.localizedMessage}")
throw e
}
val visitor = configure.getVisitor()
val rootContext = visitor.getRootContext()
val profileClass = rootContext.children
.mapNotNull { profileRegistry[it.contextName] }
.singleOrNull()
if (profileClass != null) {
val profile = profileClass.newInstance()
profile.onProfileLoaded(visitor)
return profile
}
throw IllegalArgumentException("No profile registry for ${clazz.simpleName} found")
}
override fun onServiceInit() {
@ -37,4 +69,11 @@ class ProfileComponent : NeoComponent {
override fun onServiceObtained() {
checkForFiles()
}
private fun checkForFiles() {
val profileDir = File(NeoTermPath.PROFILE_PATH)
if (!profileDir.exists()) {
profileDir.mkdirs()
}
}
}

View File

@ -1,5 +1,6 @@
package io.neoterm.frontend.session.shell
import io.neolang.visitor.ConfigVisitor
import io.neoterm.component.color.ColorSchemeComponent
import io.neoterm.component.font.FontComponent
import io.neoterm.component.profile.NeoProfile
@ -32,20 +33,27 @@ class ShellProfile : NeoProfile() {
var profileColorScheme: String
init {
val fontComp = ComponentManager.getComponent<FontComponent>()
val colorComp = ComponentManager.getComponent<ColorSchemeComponent>()
// val fontComp = ComponentManager.getComponent<FontComponent>()
// val colorComp = ComponentManager.getComponent<ColorSchemeComponent>()
//
// profileFont = fontComp.getCurrentFontName()
// profileColorScheme = colorComp.getCurrentColorSchemeName()
profileFont = fontComp.getCurrentFontName()
profileColorScheme = colorComp.getCurrentColorSchemeName()
profileFont = ""
profileColorScheme = ""
loginShell = NeoPreference.getLoginShellPath()
initialCommand = NeoPreference.getInitialCommand()
enableBell = NeoPreference.isBellEnabled()
enableVibrate = NeoPreference.isVibrateEnabled()
enableExecveWrapper = NeoPreference.isExecveWrapperEnabled()
enableSpecialVolumeKeys = NeoPreference.isSpecialVolumeKeysEnabled()
enableAutoCompletion = NeoPreference.isAutoCompletionEnabled()
enableBackButtonBeMappedToEscape = NeoPreference.isBackButtonBeMappedToEscapeEnabled()
enableExtraKeys = NeoPreference.isExtraKeysEnabled()
// loginShell = NeoPreference.getLoginShellPath()
// initialCommand = NeoPreference.getInitialCommand()
// enableBell = NeoPreference.isBellEnabled()
// enableVibrate = NeoPreference.isVibrateEnabled()
// enableExecveWrapper = NeoPreference.isExecveWrapperEnabled()
// enableSpecialVolumeKeys = NeoPreference.isSpecialVolumeKeysEnabled()
// enableAutoCompletion = NeoPreference.isAutoCompletionEnabled()
// enableBackButtonBeMappedToEscape = NeoPreference.isBackButtonBeMappedToEscapeEnabled()
// enableExtraKeys = NeoPreference.isExtraKeysEnabled()
}
override fun onProfileLoaded(visitor: ConfigVisitor): Boolean {
return true
}
}

View File

@ -2,6 +2,9 @@ package io.neoterm
import io.neoterm.component.color.NeoColorScheme
import io.neoterm.component.extrakey.NeoExtraKey
import io.neoterm.component.profile.ProfileComponent
import io.neoterm.frontend.component.ComponentManager
import io.neoterm.frontend.session.shell.ShellProfile
import org.junit.Test
import java.io.File
@ -35,7 +38,7 @@ class ConfigureFileTest {
}
val extraKey = NeoExtraKey()
if (extraKey.loadConfigure(File("/Users/kiva/Documents/NeoTerm/app/src/main/assets/eks/vim.nl"))) {
if (extraKey.loadConfigure(File("app/src/main/assets/eks/vim.nl"))) {
println("programs: ${extraKey.programNames}")
println("version: ${extraKey.version}")
println("with-default: ${extraKey.withDefaultKeys}")
@ -43,9 +46,26 @@ class ConfigureFileTest {
}
}
@Test
fun profileConfigureTest() {
try {
TestInitializer.init()
} catch (ignore: Throwable) {
}
ComponentManager.registerComponent(ProfileComponent::class.java)
val profileComp = ComponentManager.getComponent<ProfileComponent>()
profileComp.registerProfile(ShellProfile.PROFILE_META_NAME, ShellProfile::class.java)
val comp = ComponentManager.getComponent<ProfileComponent>()
val profile = comp.loadConfigure<ShellProfile>(File("NeoLang/example/profile.nl"))
}
@Test
fun configureFileTest() {
colorConfigureTest()
extraKeyConfigureTest()
profileConfigureTest()
}
}

View File

@ -1,5 +1,6 @@
package io.neoterm
import io.neoterm.component.NeoInitializer
import io.neoterm.component.codegen.CodeGenComponent
import io.neoterm.component.color.ColorSchemeComponent
import io.neoterm.component.completion.CompletionComponent
@ -15,13 +16,6 @@ import io.neoterm.frontend.component.ComponentManager
*/
object TestInitializer {
fun init() {
ComponentManager.registerComponent(ConfigureComponent::class.java)
ComponentManager.registerComponent(CodeGenComponent::class.java)
ComponentManager.registerComponent(ColorSchemeComponent::class.java)
ComponentManager.registerComponent(FontComponent::class.java)
ComponentManager.registerComponent(UserScriptComponent::class.java)
ComponentManager.registerComponent(ExtraKeysComponent::class.java)
ComponentManager.registerComponent(CompletionComponent::class.java)
ComponentManager.registerComponent(PackageComponent::class.java)
NeoInitializer.initComponents()
}
}