Is there a way to create the GSettings schema during installation in Vala?

I am trying to create an application using Vala that uses Glib.Settings. I don't want my application to crash if there is no schema or key in it. I already figured out that I can't catch errors ( How to handle errors when using Glib.Settings in Vala? ), So I need to create schemas somehow when installing the program, otherwise it will crash. I don't want to ask the user to write something like

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

      

in the terminal, so I need to do it inside the program.

So the question is, is there any way I can compile the schema in my program?

+4


source to share


1 answer


Vala itself will not be responsible for compiling your schematics; it depends on your build system (like CMake or Meson). When your application is packaged, the packaging system will use your build system to build the package.

For your build system to compile them, you need to include your schemas as an XML file, for example:

<?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>

      

Then on your build system install the schema files. For example, in Meson:



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')

      

In Meson, you can also add post_install.py

to compile schematics when installed with a build system, making development easier:

#!/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])

      

0


source







All Articles