I am trying to create an application using Vala that uses Glib.Settings. I don't want my app to crash if the schema or key in it doesn't exist. I've already understood that I can't catch errors in it (How to handle errors while using Glib.Settings in Vala?), so I need to somehow create a schema while installing the program, otherwise it will crash. I don't want to ask user to write something like
glib-compile-schemas /usr/share/glib-2.0/schemas/
in the terminal, so I need to do it within the program.
So, the question is: Can I somehow compile schema within my program?
Vala itself would not be responsible for compiling your schemas; that is up to your build system (e.g. CMake or Meson). When your app is packaged, the packaging system would then use your build system to build the package.
In order 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 in 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')
With Meson you can then also include a post_install.py
to compile the schemas when installing with the build system, which makes 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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With