5

我正在尝试使用使用 Glib.Settings 的 Vala 创建一个应用程序。如果其中的架构或键不存在,我不希望我的应用程序崩溃。我已经明白我无法捕获其中的错误(如何在 Vala 中使用 Glib.Settings 时处理错误?),所以我需要在安装程序时以某种方式创建一个模式,否则它会崩溃。我不想让用户写类似的东西

glib-compile-schemas /usr/share/glib-2.0/schemas/

在终端中,所以我需要在程序中进行。

所以,问题是:我可以在我的程序中以某种方式编译模式吗?

4

1 回答 1

3

Vala 本身不负责编译你的模式;这取决于您的构建系统(例如 CMake 或 Meson)。打包您的应用程序后,打包系统将使用您的构建系统来构建包。

为了让您的构建系统编译它们,您需要将您的模式作为 XML 文件包含在内,例如:

<?xml version="1.0" encoding="UTF-8"?>
<schemalist>
  <schema path="/com/github/yourusername/yourrepositoryname/" id="com.github.yourusername.yourrepositoryname">
    <key name="useless-setting" type="b">
      <default>false</default>
      <summary>Useless Setting</summary>
      <description>Whether the useless switch is toggled</description>
    </key>
  </schema>
</schemalist>

然后在您的构建系统中,安装架构文件。例如,在介子中:

install_data (
    'gschema.xml',
    install_dir: join_paths (get_option ('datadir'), 'glib-2.0', 'schemas'),
    rename: meson.project_name () + '.gschema.xml'
)

meson.add_install_script('post_install.py')

使用 Meson,您还可以post_install.py在使用构建系统安装时包含一个来编译模式,这使得开发更容易:

#!/usr/bin/env python3

import os
import subprocess

schemadir = os.path.join(os.environ['MESON_INSTALL_PREFIX'], 'share', 'glib-2.0', 'schemas')

# Packaging tools define DESTDIR and this isn't needed for them
if 'DESTDIR' not in os.environ:
    print('Compiling gsettings schemas...')
    subprocess.call(['glib-compile-schemas', schemadir])
于 2019-01-21T19:03:54.767 回答