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

时间:2014-11-05 07:50:21

标签: vala gsettings

我正在尝试使用Vala创建一个使用Glib.Settings的应用程序。如果应用程序中的架构或密钥不存在,我不希望我的应用程序崩溃。我已经理解我无法捕获它中的错误(How to handle errors while using Glib.Settings in Vala?),因此我需要在安装程序时以某种方式创建模式,否则它将崩溃。我不想让用户写一些类似

的内容
glib-compile-schemas /usr/share/glib-2.0/schemas/

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

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

1 个答案:

答案 0 :(得分:1)

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])
相关问题