5

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?

Community
  • 1
  • 1
serge1peshcoff
  • 4,342
  • 11
  • 45
  • 76
  • How is your program being built? You can add the rule to make, automake, cmake or what have you, but it varies depending on which you are using. – apmasell Nov 05 '14 at 11:13
  • @apmasell I'm not using any build system now and am building it through script that uses valac, but I'm planning to do it on cmake. – serge1peshcoff Nov 05 '14 at 15:02
  • You have to do it as part of CMake's install process. – apmasell Nov 05 '14 at 20:47
  • @apmasell and another question: is there any way to do this while installing the program not through cmake? I'm planning to do .deb packaging for it, can I do this while installing the package? – serge1peshcoff Nov 06 '14 at 07:14
  • For a Debian package, you do not want to run it as there is a trigger that does this automatically when the package is installed or uninstalled. – apmasell Nov 06 '14 at 11:45

1 Answers1

3

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