mirror of
https://github.com/modrinth/code.git
synced 2026-08-25 00:55:25 +00:00
fix saving hooks + env vars (#7176)
This commit is contained in:
@@ -10,7 +10,7 @@ import {
|
||||
import { ref, watch } from 'vue'
|
||||
|
||||
import useMemorySlider from '@/composables/useMemorySlider'
|
||||
import { get, set } from '@/helpers/settings.ts'
|
||||
import { get, parseEnvVars, serializeEnvVars, set } from '@/helpers/settings.ts'
|
||||
|
||||
const { handleError } = injectNotificationManager()
|
||||
const { formatMessage } = useVIntl()
|
||||
@@ -149,7 +149,7 @@ const messages = defineMessages({
|
||||
|
||||
const fetchSettings = await get()
|
||||
fetchSettings.launchArgs = fetchSettings.extra_launch_args.join(' ')
|
||||
fetchSettings.envVars = fetchSettings.custom_env_vars.map((x) => x.join('=')).join(' ')
|
||||
fetchSettings.envVars = serializeEnvVars(fetchSettings.custom_env_vars)
|
||||
|
||||
const settings = ref(fetchSettings)
|
||||
|
||||
@@ -164,27 +164,15 @@ watch(
|
||||
const setSettings = JSON.parse(JSON.stringify(settings.value))
|
||||
|
||||
setSettings.extra_launch_args = setSettings.launchArgs.trim().split(/\s+/).filter(Boolean)
|
||||
setSettings.custom_env_vars = setSettings.envVars
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.filter(Boolean)
|
||||
.map((x) => x.split('=').filter(Boolean))
|
||||
|
||||
if (!setSettings.hooks.pre_launch) {
|
||||
setSettings.hooks.pre_launch = null
|
||||
}
|
||||
if (!setSettings.hooks.wrapper) {
|
||||
setSettings.hooks.wrapper = null
|
||||
}
|
||||
if (!setSettings.hooks.post_exit) {
|
||||
setSettings.hooks.post_exit = null
|
||||
}
|
||||
setSettings.custom_env_vars = parseEnvVars(setSettings.envVars)
|
||||
delete setSettings.launchArgs
|
||||
delete setSettings.envVars
|
||||
|
||||
if (!setSettings.custom_dir) {
|
||||
setSettings.custom_dir = null
|
||||
}
|
||||
|
||||
await set(setSettings)
|
||||
await set(setSettings).catch(handleError)
|
||||
},
|
||||
{ deep: true },
|
||||
)
|
||||
|
||||
@@ -72,6 +72,26 @@ export type AppSettings = {
|
||||
version: number
|
||||
}
|
||||
|
||||
export function serializeEnvVars(vars: [string, string][] | undefined | null): string {
|
||||
return (vars ?? []).map(([key, value]) => `${key}=${value}`).join(' ')
|
||||
}
|
||||
|
||||
export function parseEnvVars(input: string | undefined | null): [string, string][] {
|
||||
if (!input?.trim()) {
|
||||
return []
|
||||
}
|
||||
|
||||
const vars: [string, string][] = []
|
||||
for (const entry of input.trim().split(/\s+/)) {
|
||||
const separator = entry.indexOf('=')
|
||||
if (separator <= 0) {
|
||||
continue
|
||||
}
|
||||
vars.push([entry.slice(0, separator), entry.slice(separator + 1)])
|
||||
}
|
||||
return vars
|
||||
}
|
||||
|
||||
// Get full settings object
|
||||
export async function get() {
|
||||
return (await invoke('plugin:settings|settings_get')) as AppSettings
|
||||
|
||||
@@ -11,7 +11,7 @@ import { computed, ref, watch } from 'vue'
|
||||
import { edit } from '@/helpers/instance'
|
||||
import { get } from '@/helpers/settings.ts'
|
||||
|
||||
import type { AppSettings, Hooks } from '../../../../helpers/types'
|
||||
import type { AppSettings } from '../../../../helpers/types'
|
||||
import { injectInstanceSettings } from './instance-settings-context'
|
||||
|
||||
const { handleError } = injectNotificationManager()
|
||||
@@ -26,23 +26,31 @@ const overrideHooks = ref(
|
||||
!!instance.value.hooks.wrapper ||
|
||||
!!instance.value.hooks.post_exit,
|
||||
)
|
||||
const hooks = ref(instance.value.hooks ?? globalSettings.hooks)
|
||||
|
||||
const editInstanceObject = computed(() => {
|
||||
const editInstancePatch: {
|
||||
hooks?: Hooks
|
||||
} = {}
|
||||
|
||||
// When hooks are not overridden per-instance, we want to clear them
|
||||
editInstancePatch.hooks = overrideHooks.value ? hooks.value : {}
|
||||
|
||||
return editInstancePatch
|
||||
const hooksRaw = instance.value.hooks ?? globalSettings.hooks
|
||||
const hooks = ref({
|
||||
pre_launch: hooksRaw.pre_launch ?? '',
|
||||
wrapper: hooksRaw.wrapper ?? '',
|
||||
post_exit: hooksRaw.post_exit ?? '',
|
||||
})
|
||||
|
||||
const editInstanceObject = computed(() => ({
|
||||
hooks: overrideHooks.value
|
||||
? {
|
||||
pre_launch: hooks.value.pre_launch ?? '',
|
||||
wrapper: hooks.value.wrapper ?? '',
|
||||
post_exit: hooks.value.post_exit ?? '',
|
||||
}
|
||||
: {
|
||||
pre_launch: '',
|
||||
wrapper: '',
|
||||
post_exit: '',
|
||||
},
|
||||
}))
|
||||
|
||||
watch(
|
||||
[overrideHooks, hooks],
|
||||
async () => {
|
||||
await edit(instance.value.id, editInstanceObject.value)
|
||||
await edit(instance.value.id, editInstanceObject.value).catch(handleError)
|
||||
},
|
||||
{ deep: true },
|
||||
)
|
||||
|
||||
@@ -24,7 +24,7 @@ import JavaDetectionModal from '@/components/ui/JavaDetectionModal.vue'
|
||||
import useJavaTest from '@/composables/useJavaTest'
|
||||
import useMemorySlider from '@/composables/useMemorySlider'
|
||||
import { edit, get_optimal_jre_key } from '@/helpers/instance'
|
||||
import { get } from '@/helpers/settings.ts'
|
||||
import { get, parseEnvVars, serializeEnvVars } from '@/helpers/settings.ts'
|
||||
|
||||
import type { AppSettings } from '../../../../helpers/types'
|
||||
import { injectInstanceSettings } from './instance-settings-context'
|
||||
@@ -92,9 +92,7 @@ const javaArgs = ref(
|
||||
|
||||
const overrideEnvVars = ref((instance.value.custom_env_vars?.length ?? 0) > 0)
|
||||
const envVars = ref(
|
||||
(instance.value.custom_env_vars ?? globalSettings.custom_env_vars)
|
||||
.map((x) => x.join('='))
|
||||
.join(' '),
|
||||
serializeEnvVars(instance.value.custom_env_vars ?? globalSettings.custom_env_vars),
|
||||
)
|
||||
|
||||
const overrideMemorySettings = ref(!!instance.value.memory)
|
||||
@@ -113,13 +111,7 @@ const editInstanceObject = computed(() => {
|
||||
extra_launch_args: overrideJavaArgs.value
|
||||
? javaArgs.value.trim().split(/\s+/).filter(Boolean)
|
||||
: null,
|
||||
custom_env_vars: overrideEnvVars.value
|
||||
? envVars.value
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.filter(Boolean)
|
||||
.map((x) => x.split('=').filter(Boolean))
|
||||
: null,
|
||||
custom_env_vars: overrideEnvVars.value ? parseEnvVars(envVars.value) : null,
|
||||
memory: overrideMemorySettings.value ? memory.value : null,
|
||||
}
|
||||
})
|
||||
@@ -136,7 +128,7 @@ watch(
|
||||
memory,
|
||||
],
|
||||
async () => {
|
||||
await edit(instance.value.id, editInstanceObject.value)
|
||||
await edit(instance.value.id, editInstanceObject.value).catch(handleError)
|
||||
},
|
||||
{ deep: true },
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user