有没有办法在Vala中安装时创建GSettings架构?

ser*_*off 5 vala gsettings

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

glib-compile-schemas /usr/share/glib-2.0/schemas/
Run Code Online (Sandbox Code Playgroud)

在终端,所以我需要在程序内完成.

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

小智 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>
Run Code Online (Sandbox Code Playgroud)

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

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')
Run Code Online (Sandbox Code Playgroud)

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

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')
Run Code Online (Sandbox Code Playgroud)