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 { ref, watch } from 'vue'
|
||||||
|
|
||||||
import useMemorySlider from '@/composables/useMemorySlider'
|
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 { handleError } = injectNotificationManager()
|
||||||
const { formatMessage } = useVIntl()
|
const { formatMessage } = useVIntl()
|
||||||
@@ -149,7 +149,7 @@ const messages = defineMessages({
|
|||||||
|
|
||||||
const fetchSettings = await get()
|
const fetchSettings = await get()
|
||||||
fetchSettings.launchArgs = fetchSettings.extra_launch_args.join(' ')
|
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)
|
const settings = ref(fetchSettings)
|
||||||
|
|
||||||
@@ -164,27 +164,15 @@ watch(
|
|||||||
const setSettings = JSON.parse(JSON.stringify(settings.value))
|
const setSettings = JSON.parse(JSON.stringify(settings.value))
|
||||||
|
|
||||||
setSettings.extra_launch_args = setSettings.launchArgs.trim().split(/\s+/).filter(Boolean)
|
setSettings.extra_launch_args = setSettings.launchArgs.trim().split(/\s+/).filter(Boolean)
|
||||||
setSettings.custom_env_vars = setSettings.envVars
|
setSettings.custom_env_vars = parseEnvVars(setSettings.envVars)
|
||||||
.trim()
|
delete setSettings.launchArgs
|
||||||
.split(/\s+/)
|
delete setSettings.envVars
|
||||||
.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
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!setSettings.custom_dir) {
|
if (!setSettings.custom_dir) {
|
||||||
setSettings.custom_dir = null
|
setSettings.custom_dir = null
|
||||||
}
|
}
|
||||||
|
|
||||||
await set(setSettings)
|
await set(setSettings).catch(handleError)
|
||||||
},
|
},
|
||||||
{ deep: true },
|
{ deep: true },
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -72,6 +72,26 @@ export type AppSettings = {
|
|||||||
version: number
|
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
|
// Get full settings object
|
||||||
export async function get() {
|
export async function get() {
|
||||||
return (await invoke('plugin:settings|settings_get')) as AppSettings
|
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 { edit } from '@/helpers/instance'
|
||||||
import { get } from '@/helpers/settings.ts'
|
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'
|
import { injectInstanceSettings } from './instance-settings-context'
|
||||||
|
|
||||||
const { handleError } = injectNotificationManager()
|
const { handleError } = injectNotificationManager()
|
||||||
@@ -26,23 +26,31 @@ const overrideHooks = ref(
|
|||||||
!!instance.value.hooks.wrapper ||
|
!!instance.value.hooks.wrapper ||
|
||||||
!!instance.value.hooks.post_exit,
|
!!instance.value.hooks.post_exit,
|
||||||
)
|
)
|
||||||
const hooks = ref(instance.value.hooks ?? globalSettings.hooks)
|
const hooksRaw = instance.value.hooks ?? globalSettings.hooks
|
||||||
|
const hooks = ref({
|
||||||
const editInstanceObject = computed(() => {
|
pre_launch: hooksRaw.pre_launch ?? '',
|
||||||
const editInstancePatch: {
|
wrapper: hooksRaw.wrapper ?? '',
|
||||||
hooks?: Hooks
|
post_exit: hooksRaw.post_exit ?? '',
|
||||||
} = {}
|
|
||||||
|
|
||||||
// When hooks are not overridden per-instance, we want to clear them
|
|
||||||
editInstancePatch.hooks = overrideHooks.value ? hooks.value : {}
|
|
||||||
|
|
||||||
return editInstancePatch
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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(
|
watch(
|
||||||
[overrideHooks, hooks],
|
[overrideHooks, hooks],
|
||||||
async () => {
|
async () => {
|
||||||
await edit(instance.value.id, editInstanceObject.value)
|
await edit(instance.value.id, editInstanceObject.value).catch(handleError)
|
||||||
},
|
},
|
||||||
{ deep: true },
|
{ deep: true },
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import JavaDetectionModal from '@/components/ui/JavaDetectionModal.vue'
|
|||||||
import useJavaTest from '@/composables/useJavaTest'
|
import useJavaTest from '@/composables/useJavaTest'
|
||||||
import useMemorySlider from '@/composables/useMemorySlider'
|
import useMemorySlider from '@/composables/useMemorySlider'
|
||||||
import { edit, get_optimal_jre_key } from '@/helpers/instance'
|
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 type { AppSettings } from '../../../../helpers/types'
|
||||||
import { injectInstanceSettings } from './instance-settings-context'
|
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 overrideEnvVars = ref((instance.value.custom_env_vars?.length ?? 0) > 0)
|
||||||
const envVars = ref(
|
const envVars = ref(
|
||||||
(instance.value.custom_env_vars ?? globalSettings.custom_env_vars)
|
serializeEnvVars(instance.value.custom_env_vars ?? globalSettings.custom_env_vars),
|
||||||
.map((x) => x.join('='))
|
|
||||||
.join(' '),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const overrideMemorySettings = ref(!!instance.value.memory)
|
const overrideMemorySettings = ref(!!instance.value.memory)
|
||||||
@@ -113,13 +111,7 @@ const editInstanceObject = computed(() => {
|
|||||||
extra_launch_args: overrideJavaArgs.value
|
extra_launch_args: overrideJavaArgs.value
|
||||||
? javaArgs.value.trim().split(/\s+/).filter(Boolean)
|
? javaArgs.value.trim().split(/\s+/).filter(Boolean)
|
||||||
: null,
|
: null,
|
||||||
custom_env_vars: overrideEnvVars.value
|
custom_env_vars: overrideEnvVars.value ? parseEnvVars(envVars.value) : null,
|
||||||
? envVars.value
|
|
||||||
.trim()
|
|
||||||
.split(/\s+/)
|
|
||||||
.filter(Boolean)
|
|
||||||
.map((x) => x.split('=').filter(Boolean))
|
|
||||||
: null,
|
|
||||||
memory: overrideMemorySettings.value ? memory.value : null,
|
memory: overrideMemorySettings.value ? memory.value : null,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -136,7 +128,7 @@ watch(
|
|||||||
memory,
|
memory,
|
||||||
],
|
],
|
||||||
async () => {
|
async () => {
|
||||||
await edit(instance.value.id, editInstanceObject.value)
|
await edit(instance.value.id, editInstanceObject.value).catch(handleError)
|
||||||
},
|
},
|
||||||
{ deep: true },
|
{ deep: true },
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user