Profile: Load profile registered in registry
This commit is contained in:
3
NeoLang/example/profile.nl
Normal file
3
NeoLang/example/profile.nl
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
profile-shell: {
|
||||||
|
bell: true
|
||||||
|
}
|
@ -10,4 +10,10 @@ class NeoLangGroupNode(val attributes: Array<NeoLangAttributeNode>) : NeoLangBas
|
|||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "NeoLangGroupNode { attrs: $attributes }"
|
return "NeoLangGroupNode { attrs: $attributes }"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun emptyNode() : NeoLangGroupNode {
|
||||||
|
return NeoLangGroupNode(arrayOf())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -6,8 +6,13 @@ import io.neolang.runtime.type.NeoLangArray
|
|||||||
import io.neolang.runtime.type.NeoLangValue
|
import io.neolang.runtime.type.NeoLangValue
|
||||||
|
|
||||||
class ConfigVisitor : IVisitorCallback {
|
class ConfigVisitor : IVisitorCallback {
|
||||||
|
private var rootContext: NeoLangContext? = null
|
||||||
private var currentContext: NeoLangContext? = null
|
private var currentContext: NeoLangContext? = null
|
||||||
|
|
||||||
|
fun getRootContext(): NeoLangContext {
|
||||||
|
return rootContext!!
|
||||||
|
}
|
||||||
|
|
||||||
fun getContext(contextPath: Array<String>): NeoLangContext {
|
fun getContext(contextPath: Array<String>): NeoLangContext {
|
||||||
var context = getCurrentContext()
|
var context = getCurrentContext()
|
||||||
contextPath.forEach {
|
contextPath.forEach {
|
||||||
@ -27,6 +32,7 @@ class ConfigVisitor : IVisitorCallback {
|
|||||||
|
|
||||||
override fun onStart() {
|
override fun onStart() {
|
||||||
currentContext = NeoLangContext("global")
|
currentContext = NeoLangContext("global")
|
||||||
|
rootContext = currentContext
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFinish() {
|
override fun onFinish() {
|
||||||
|
@ -21,6 +21,10 @@ import io.neoterm.frontend.session.shell.ShellProfile
|
|||||||
object NeoInitializer {
|
object NeoInitializer {
|
||||||
fun init(context: Context) {
|
fun init(context: Context) {
|
||||||
NLog.init(context)
|
NLog.init(context)
|
||||||
|
initComponents()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun initComponents() {
|
||||||
ComponentManager.registerComponent(ConfigureComponent::class.java)
|
ComponentManager.registerComponent(ConfigureComponent::class.java)
|
||||||
ComponentManager.registerComponent(CodeGenComponent::class.java)
|
ComponentManager.registerComponent(CodeGenComponent::class.java)
|
||||||
ComponentManager.registerComponent(ColorSchemeComponent::class.java)
|
ComponentManager.registerComponent(ColorSchemeComponent::class.java)
|
||||||
|
@ -1,8 +1,20 @@
|
|||||||
package io.neoterm.component.profile
|
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
|
* @author kiva
|
||||||
*/
|
*/
|
||||||
abstract class NeoProfile {
|
abstract class NeoProfile : CodeGenObject {
|
||||||
abstract val profileMetaName: String
|
abstract val profileMetaName: String
|
||||||
|
|
||||||
|
abstract fun onProfileLoaded(visitor: ConfigVisitor): Boolean
|
||||||
|
|
||||||
|
override fun getCodeGenerator(parameter: CodeGenParameter): ICodeGenerator {
|
||||||
|
return NeoProfileGenerator(parameter)
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,7 +1,11 @@
|
|||||||
package io.neoterm.component.profile
|
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.component.NeoComponent
|
||||||
|
import io.neoterm.frontend.config.NeoConfigureFile
|
||||||
import io.neoterm.frontend.config.NeoTermPath
|
import io.neoterm.frontend.config.NeoTermPath
|
||||||
|
import io.neoterm.frontend.logging.NLog
|
||||||
import io.neoterm.frontend.session.shell.ShellProfile
|
import io.neoterm.frontend.session.shell.ShellProfile
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@ -20,11 +24,39 @@ class ProfileComponent : NeoComponent {
|
|||||||
profileRegistry.remove(metaName)
|
profileRegistry.remove(metaName)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkForFiles() {
|
inline fun <reified T: NeoProfile> loadConfigure(file: File): T {
|
||||||
val profileDir = File(NeoTermPath.PROFILE_PATH)
|
return loadConfigure(file, T::class.java) as T
|
||||||
if (!profileDir.exists()) {
|
}
|
||||||
profileDir.mkdirs()
|
|
||||||
|
@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() {
|
override fun onServiceInit() {
|
||||||
@ -37,4 +69,11 @@ class ProfileComponent : NeoComponent {
|
|||||||
override fun onServiceObtained() {
|
override fun onServiceObtained() {
|
||||||
checkForFiles()
|
checkForFiles()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun checkForFiles() {
|
||||||
|
val profileDir = File(NeoTermPath.PROFILE_PATH)
|
||||||
|
if (!profileDir.exists()) {
|
||||||
|
profileDir.mkdirs()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package io.neoterm.frontend.session.shell
|
package io.neoterm.frontend.session.shell
|
||||||
|
|
||||||
|
import io.neolang.visitor.ConfigVisitor
|
||||||
import io.neoterm.component.color.ColorSchemeComponent
|
import io.neoterm.component.color.ColorSchemeComponent
|
||||||
import io.neoterm.component.font.FontComponent
|
import io.neoterm.component.font.FontComponent
|
||||||
import io.neoterm.component.profile.NeoProfile
|
import io.neoterm.component.profile.NeoProfile
|
||||||
@ -32,20 +33,27 @@ class ShellProfile : NeoProfile() {
|
|||||||
var profileColorScheme: String
|
var profileColorScheme: String
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val fontComp = ComponentManager.getComponent<FontComponent>()
|
// val fontComp = ComponentManager.getComponent<FontComponent>()
|
||||||
val colorComp = ComponentManager.getComponent<ColorSchemeComponent>()
|
// val colorComp = ComponentManager.getComponent<ColorSchemeComponent>()
|
||||||
|
//
|
||||||
|
// profileFont = fontComp.getCurrentFontName()
|
||||||
|
// profileColorScheme = colorComp.getCurrentColorSchemeName()
|
||||||
|
|
||||||
profileFont = fontComp.getCurrentFontName()
|
profileFont = ""
|
||||||
profileColorScheme = colorComp.getCurrentColorSchemeName()
|
profileColorScheme = ""
|
||||||
|
|
||||||
loginShell = NeoPreference.getLoginShellPath()
|
// loginShell = NeoPreference.getLoginShellPath()
|
||||||
initialCommand = NeoPreference.getInitialCommand()
|
// initialCommand = NeoPreference.getInitialCommand()
|
||||||
enableBell = NeoPreference.isBellEnabled()
|
// enableBell = NeoPreference.isBellEnabled()
|
||||||
enableVibrate = NeoPreference.isVibrateEnabled()
|
// enableVibrate = NeoPreference.isVibrateEnabled()
|
||||||
enableExecveWrapper = NeoPreference.isExecveWrapperEnabled()
|
// enableExecveWrapper = NeoPreference.isExecveWrapperEnabled()
|
||||||
enableSpecialVolumeKeys = NeoPreference.isSpecialVolumeKeysEnabled()
|
// enableSpecialVolumeKeys = NeoPreference.isSpecialVolumeKeysEnabled()
|
||||||
enableAutoCompletion = NeoPreference.isAutoCompletionEnabled()
|
// enableAutoCompletion = NeoPreference.isAutoCompletionEnabled()
|
||||||
enableBackButtonBeMappedToEscape = NeoPreference.isBackButtonBeMappedToEscapeEnabled()
|
// enableBackButtonBeMappedToEscape = NeoPreference.isBackButtonBeMappedToEscapeEnabled()
|
||||||
enableExtraKeys = NeoPreference.isExtraKeysEnabled()
|
// enableExtraKeys = NeoPreference.isExtraKeysEnabled()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onProfileLoaded(visitor: ConfigVisitor): Boolean {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -2,6 +2,9 @@ package io.neoterm
|
|||||||
|
|
||||||
import io.neoterm.component.color.NeoColorScheme
|
import io.neoterm.component.color.NeoColorScheme
|
||||||
import io.neoterm.component.extrakey.NeoExtraKey
|
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 org.junit.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@ -35,7 +38,7 @@ class ConfigureFileTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val extraKey = NeoExtraKey()
|
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("programs: ${extraKey.programNames}")
|
||||||
println("version: ${extraKey.version}")
|
println("version: ${extraKey.version}")
|
||||||
println("with-default: ${extraKey.withDefaultKeys}")
|
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
|
@Test
|
||||||
fun configureFileTest() {
|
fun configureFileTest() {
|
||||||
colorConfigureTest()
|
colorConfigureTest()
|
||||||
extraKeyConfigureTest()
|
extraKeyConfigureTest()
|
||||||
|
profileConfigureTest()
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package io.neoterm
|
package io.neoterm
|
||||||
|
|
||||||
|
import io.neoterm.component.NeoInitializer
|
||||||
import io.neoterm.component.codegen.CodeGenComponent
|
import io.neoterm.component.codegen.CodeGenComponent
|
||||||
import io.neoterm.component.color.ColorSchemeComponent
|
import io.neoterm.component.color.ColorSchemeComponent
|
||||||
import io.neoterm.component.completion.CompletionComponent
|
import io.neoterm.component.completion.CompletionComponent
|
||||||
@ -15,13 +16,6 @@ import io.neoterm.frontend.component.ComponentManager
|
|||||||
*/
|
*/
|
||||||
object TestInitializer {
|
object TestInitializer {
|
||||||
fun init() {
|
fun init() {
|
||||||
ComponentManager.registerComponent(ConfigureComponent::class.java)
|
NeoInitializer.initComponents()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user