From cf9125cf31fb5b58be273395726c21041517d64f Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 22 May 2024 00:06:27 +0200 Subject: refactor meson into a reusable format that can easily be copied --- flake.lock | 6 +-- meson.build | 5 +++ meson_options.txt | 13 ++++++- src/cli.vala | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/cli.vala.in | 107 ----------------------------------------------------- src/config.vala.in | 6 +++ src/meson.build | 89 +++++++++++++++++++++++--------------------- src/proxy.vala | 1 - 8 files changed, 179 insertions(+), 155 deletions(-) create mode 100644 src/cli.vala delete mode 100644 src/cli.vala.in create mode 100644 src/config.vala.in diff --git a/flake.lock b/flake.lock index 0c0646b..13f566b 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1715961556, - "narHash": "sha256-+NpbZRCRisUHKQJZF3CT+xn14ZZQO+KjxIIanH3Pvn4=", + "lastModified": 1716137900, + "narHash": "sha256-sowPU+tLQv8GlqtVtsXioTKeaQvlMz/pefcdwg8MvfM=", "owner": "nixos", "repo": "nixpkgs", - "rev": "4a6b83b05df1a8bd7d99095ec4b4d271f2956b64", + "rev": "6c0b7a92c30122196a761b440ac0d46d3d9954f1", "type": "github" }, "original": { diff --git a/meson.build b/meson.build index 17ba93e..8fabdb4 100644 --- a/meson.build +++ b/meson.build @@ -11,4 +11,9 @@ project( ], ) +assert( + get_option('lib') or get_option('cli'), + 'Either lib or cli option must be set to true.', +) + subdir('src') diff --git a/meson_options.txt b/meson_options.txt index 0966cdf..f110242 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -1,2 +1,11 @@ -option('typelib', type: 'boolean', value: true, description: 'Needed files for runtime bindings') -option('cli_client', type: 'boolean', value: true, description: 'cli client for notifd') +option( + 'lib', + type: 'boolean', + value: true, +) + +option( + 'cli', + type: 'boolean', + value: true, +) diff --git a/src/cli.vala b/src/cli.vala new file mode 100644 index 0000000..e9001f5 --- /dev/null +++ b/src/cli.vala @@ -0,0 +1,107 @@ +static bool help; +static bool version; +static bool daemonize; +static bool list; +static string invoke; +static int close_n; +static int get_n; + +const OptionEntry[] options = { + { "version", 'v', OptionFlags.NONE, OptionArg.NONE, ref version, null, null }, + { "help", 'h', OptionFlags.NONE, OptionArg.NONE, ref help, null, null }, + { "daemon", 'd', OptionFlags.NONE, OptionArg.NONE, ref daemonize, null, null }, + { "list", 'l', OptionFlags.NONE, OptionArg.NONE, ref list, null, null }, + { "invoke", 'i', OptionFlags.NONE, OptionArg.STRING, ref invoke, null, null }, + { "close", 'c', OptionFlags.NONE, OptionArg.INT, ref close_n, null, null }, + { "get", 'g', OptionFlags.NONE, OptionArg.INT, ref get_n, null, null }, + { null }, +}; + +int main(string[] argv) { + try { + var opts = new OptionContext(); + opts.add_main_entries(options, null); + opts.set_help_enabled(false); + opts.set_ignore_unknown_options(false); + opts.parse(ref argv); + } catch (OptionError err) { + printerr (err.message); + return 1; + } + + if (help) { + print("Cli client for astal-notifd\n\n"); + print("Usage:\n"); + print(" %s [flags]\n\n", argv[0]); + print("Flags:\n"); + print(" -h, --help Print this help and exit\n"); + print(" -v, --version Print version number and exit\n"); + print(" -l, --list Print every notification and exit\n"); + print(" -d, --daemonize Watch for new notifications\n"); + print(" -i, --invoke Invoke a notification action\n"); + print(" -c, --close Close a notification by its id\n"); + print(" -g, --get Print a notification by its id\n"); + return 0; + } + + var loop = new MainLoop(); + var notifd = new AstalNotifd.Notifd(); + + if (version) { + print(AstalNotifd.VERSION); + return 0; + } + + if (list) { + var cache = Environment.get_user_cache_dir() + "/astal/notifd/notifications.json"; + if (FileUtils.test(cache, FileTest.EXISTS)) { + try { + uint8[] json; + File.new_for_path(cache).load_contents(null, out json, null); + stdout.printf("%s", (string)json); + } catch (Error err) { + stderr.printf("failed to load cache: %s", err.message); + } + } + return 0; + } + + if (daemonize) { + notifd.notified.connect((id) => { + stdout.printf("%s\n", notifd.get_notification_json(id)); + }); + } + + if (invoke != null) { + if (!invoke.contains(":")) { + stderr.printf("invoke format needs to be :"); + return 1; + } + + var split = invoke.split(":"); + var n_id = int.parse(split[0]); + var a_id = split[1]; + + notifd.active.connect(() => { + notifd.get_notification(n_id).invoke(a_id); + loop.quit(); + }); + } + + if (close_n > 0) { + notifd.active.connect(() => { + notifd.get_notification(close_n).dismiss(); + loop.quit(); + }); + } + + if (get_n > 0) { + notifd.active.connect(() => { + stdout.printf("%s", notifd.get_notification(get_n).to_json_string()); + loop.quit(); + }); + } + + loop.run(); + return 0; +} diff --git a/src/cli.vala.in b/src/cli.vala.in deleted file mode 100644 index f49a056..0000000 --- a/src/cli.vala.in +++ /dev/null @@ -1,107 +0,0 @@ -static bool help; -static bool version; -static bool daemonize; -static bool list; -static string invoke; -static int close_n; -static int get_n; - -private const OptionEntry[] options = { - { "version", 'v', OptionFlags.NONE, OptionArg.NONE, ref version, null, null }, - { "help", 'h', OptionFlags.NONE, OptionArg.NONE, ref help, null, null }, - { "daemon", 'd', OptionFlags.NONE, OptionArg.NONE, ref daemonize, null, null }, - { "list", 'l', OptionFlags.NONE, OptionArg.NONE, ref list, null, null }, - { "invoke", 'i', OptionFlags.NONE, OptionArg.STRING, ref invoke, null, null }, - { "close", 'c', OptionFlags.NONE, OptionArg.INT, ref close_n, null, null }, - { "get", 'g', OptionFlags.NONE, OptionArg.INT, ref get_n, null, null }, - { null }, -}; - -int main(string[] argv) { - try { - var opts = new OptionContext(); - opts.add_main_entries(options, null); - opts.set_help_enabled(false); - opts.set_ignore_unknown_options(false); - opts.parse(ref argv); - } catch (OptionError err) { - printerr (err.message); - return 1; - } - - if (help) { - print("Cli client for astal-notifd\n\n"); - print("Usage:\n"); - print(" %s [flags]\n\n", argv[0]); - print("Flags:\n"); - print(" -h, --help Print this help and exit\n"); - print(" -v, --version Print version number and exit\n"); - print(" -l, --list Print every notification and exit\n"); - print(" -d, --daemonize Watch for new notifications\n"); - print(" -i, --invoke Invoke a notification action\n"); - print(" -c, --close Close a notification by its id\n"); - print(" -g, --get Print a notification by its id\n"); - return 0; - } - - var loop = new MainLoop(); - var notifd = new AstalNotifd.Notifd(); - - if (version) { - print("@VERSION@"); - return 0; - } - - if (list) { - var cache = Environment.get_user_cache_dir() + "/astal/notifd/notifications.json"; - if (FileUtils.test(cache, FileTest.EXISTS)) { - try { - uint8[] json; - File.new_for_path(cache).load_contents(null, out json, null); - stdout.printf("%s", (string)json); - } catch (Error err) { - stderr.printf("failed to load cache: %s", err.message); - } - } - return 0; - } - - if (daemonize) { - notifd.notified.connect((id) => { - stdout.printf("%s\n", notifd.get_notification_json(id)); - }); - } - - if (invoke != null) { - if (!invoke.contains(":")) { - stderr.printf("invoke format needs to be :"); - return 1; - } - - var split = invoke.split(":"); - var n_id = int.parse(split[0]); - var a_id = split[1]; - - notifd.active.connect(() => { - notifd.get_notification(n_id).invoke(a_id); - loop.quit(); - }); - } - - if (close_n > 0) { - notifd.active.connect(() => { - notifd.get_notification(close_n).dismiss(); - loop.quit(); - }); - } - - if (get_n > 0) { - notifd.active.connect(() => { - stdout.printf("%s", notifd.get_notification(get_n).to_json_string()); - loop.quit(); - }); - } - - loop.run(); - return 0; -} diff --git a/src/config.vala.in b/src/config.vala.in new file mode 100644 index 0000000..7db3963 --- /dev/null +++ b/src/config.vala.in @@ -0,0 +1,6 @@ +namespace AstalNotifd { + const int MAJOR_VERSION = @MAJOR_VERSION@; + const int MINOR_VERSION = @MINOR_VERSION@; + const int MICRO_VERSION = @MICRO_VERSION@; + const string VERSION = "@VERSION@"; +} diff --git a/src/meson.build b/src/meson.build index e965647..b1d326c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,8 +1,19 @@ version_split = meson.project_version().split('.') api_version = version_split[0] + '.' + version_split[1] -notifd_gir = 'AstalNotifd-' + api_version + '.gir' -notifd_typelib = 'AstalNotifd-' + api_version + '.typelib' -notifd_so = 'libastal-notifd.so.' + meson.project_version() +gir = 'AstalNotifd-' + api_version + '.gir' +typelib = 'AstalNotifd-' + api_version + '.typelib' +so = 'libastal-notifd.so.' + meson.project_version() + +config = configure_file( + input: 'config.vala.in', + output: 'config.vala', + configuration: { + 'VERSION': meson.project_version(), + 'MAJOR_VERSION': version_split[0], + 'MINOR_VERSION': version_split[1], + 'MICRO_VERSION': version_split[2], + }, +) deps = [ dependency('glib-2.0'), @@ -12,64 +23,58 @@ deps = [ dependency('gdk-pixbuf-2.0'), ] -sources = files( +sources = [ + config, 'daemon.vala', 'notifd.vala', - 'proxy.vala', 'notification.vala', -) + 'proxy.vala', +] -libnotifd = library( - meson.project_name(), - sources, - dependencies: deps, - vala_header: meson.project_name() + '.h', - vala_vapi: meson.project_name() + '.vapi', - vala_gir: notifd_gir, - version: meson.project_version(), - install: true, - install_dir: [true, true, true, true], -) +if get_option('lib') + lib = library( + meson.project_name(), + sources, + dependencies: deps, + vala_header: meson.project_name() + '.h', + vala_vapi: meson.project_name() + '.vapi', + vala_gir: gir, + version: meson.project_version(), + install: true, + install_dir: [true, true, true, true], + ) -import('pkgconfig').generate( - description: 'libastal-notifd', - libraries: libnotifd, - name: meson.project_name(), - filebase: meson.project_name() + '-' + api_version, - version: meson.project_version(), - subdirs: meson.project_name(), - requires: 'gio-2.0', - install_dir: get_option('libdir') / 'pkgconfig', -) + import('pkgconfig').generate( + description: 'libastal-notifd', + libraries: lib, + name: meson.project_name(), + filebase: meson.project_name() + '-' + api_version, + version: meson.project_version(), + subdirs: meson.project_name(), + requires: deps, + install_dir: get_option('libdir') / 'pkgconfig', + ) -if get_option('typelib') custom_target( - notifd_typelib, + typelib, command: [ find_program('g-ir-compiler'), '--output', '@OUTPUT@', '--shared-library', get_option('prefix') / get_option('libdir') / '@PLAINNAME@', - meson.current_build_dir() / notifd_gir, + meson.current_build_dir() / gir, ], - input: libnotifd, - output: notifd_typelib, - depends: libnotifd, + input: lib, + output: typelib, + depends: lib, install: true, install_dir: get_option('libdir') / 'girepository-1.0', ) endif -if get_option('cli_client') - cli = configure_file( - input: 'cli.vala.in', - output: 'cli.vala', - configuration: { - 'VERSION': meson.project_version(), - }, - ) +if get_option('cli') executable( meson.project_name(), - [cli, sources], + ['cli.vala', sources], dependencies: deps, install: true, ) diff --git a/src/proxy.vala b/src/proxy.vala index 1f5870d..b5686fe 100644 --- a/src/proxy.vala +++ b/src/proxy.vala @@ -9,7 +9,6 @@ internal interface IDaemon : Object { public signal void notified(uint id); public signal void resolved(uint id, ClosedReason reason); - public signal void action_invoked(uint id, string action); public abstract void emit_notified(uint id); public abstract void emit_resolved(uint id, ClosedReason reason); -- cgit v1.2.3