diff --git a/ddterm/app/init.js b/ddterm/app/init.js index 79e02f8c..c749e38b 100644 --- a/ddterm/app/init.js +++ b/ddterm/app/init.js @@ -30,5 +30,7 @@ gi_require({ 'Gdk': '3.0', 'Pango': '1.0', 'Vte': '2.91', + // BEGIN ESM 'Handy': '1', + // END ESM }); diff --git a/ddterm/app/init.legacy.js b/ddterm/app/init.legacy.js deleted file mode 100644 index 20128834..00000000 --- a/ddterm/app/init.legacy.js +++ /dev/null @@ -1,33 +0,0 @@ -/* - Copyright © 2024 Aleksandr Mezin - - This file is part of ddterm GNOME Shell extension. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -import Gettext from 'gettext'; - -import { metadata, dir } from './meta.js'; -import { gi_require } from './dependencies.js'; - -Gettext.bindtextdomain(metadata['gettext-domain'], dir.get_child('locale').get_path()); -Gettext.textdomain(metadata['gettext-domain']); - -gi_require({ - 'Gtk': '3.0', - 'Gdk': '3.0', - 'Pango': '1.0', - 'Vte': '2.91', -}); diff --git a/ddterm/app/meson.build b/ddterm/app/meson.build index 604761ec..b58dcc7f 100644 --- a/ddterm/app/meson.build +++ b/ddterm/app/meson.build @@ -5,9 +5,11 @@ app_js_files = files( 'dependencies.js', 'extensiondbus.js', 'heapdump.js', + 'init.js', 'meta.js', 'notebook.js', 'pcre2.js', + 'prefsdialog.js', 'resources.js', 'search.js', 'settings.js', @@ -26,20 +28,15 @@ app_ui_files = files( ) foreach app_file : app_js_files + app_ui_files + files('dependencies.json', 'style.css') - pack += fs.copyfile( - app_file, + pack += custom_target( + command: preprocess_command, + input: app_file, + output: fs.name(app_file), install: true, install_dir: extension_dir / 'ddterm' / 'app', ) endforeach -pack += fs.copyfile( - get_option('esm') ? 'prefsdialog.js' : 'prefsdialog.legacy.js', - 'prefsdialog.js', - install: true, - install_dir: extension_dir / 'ddterm' / 'app', -) - pack += fs.copyfile( get_option('esm') ? 'gtktheme.js' : 'gtktheme.legacy.js', 'gtktheme.js', @@ -47,13 +44,6 @@ pack += fs.copyfile( install_dir: extension_dir / 'ddterm' / 'app', ) -pack += fs.copyfile( - get_option('esm') ? 'init.js' : 'init.legacy.js', - 'init.js', - install: true, - install_dir: extension_dir / 'ddterm' / 'app', -) - subdir('icons') foreach app_ui_file : app_ui_files diff --git a/ddterm/app/prefsdialog.js b/ddterm/app/prefsdialog.js index 302543be..bf0bd2c5 100644 --- a/ddterm/app/prefsdialog.js +++ b/ddterm/app/prefsdialog.js @@ -1,5 +1,5 @@ /* - Copyright © 2023 Aleksandr Mezin + Copyright © 2024 Aleksandr Mezin This file is part of ddterm GNOME Shell extension. @@ -17,14 +17,52 @@ along with this program. If not, see . */ +import GLib from 'gi://GLib'; import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; import Gettext from 'gettext'; -import { metadata } from './meta.js'; +import { metadata, dir } from './meta.js'; +// BEGIN ESM import { PrefsWidget } from '../pref/widget.js'; +// END ESM + +// BEGIN !ESM +const [fakeext_import_path] = GLib.filename_from_uri( + GLib.Uri.resolve_relative(import.meta.url, 'fakeext', GLib.UriFlags.NONE) +); + +imports.searchPath.unshift(fakeext_import_path); + +const Me = imports.misc.extensionUtils.getCurrentExtension(); + +function make_dir_importer(file) { + /* Like extensionUtils.installImporter() */ + + const old_search_path = imports.searchPath.slice(); + + imports.searchPath = [file.get_parent().get_path()]; + + try { + return imports[file.get_basename()]; + } finally { + imports.searchPath = old_search_path; + } +} + +/* + * fake current extension object to make `Me.imports` and `Me.dir` + * work in application context + */ +Object.assign(Me, { + imports: make_dir_importer(dir), + dir, + path: dir.get_path(), + metadata, +}); +// END !ESM export const PrefsDialog = GObject.registerClass({ Properties: { @@ -46,10 +84,19 @@ export const PrefsDialog = GObject.registerClass({ this.set_default_size(640, 576); this.set_icon_name('preferences-system'); - const widget = new PrefsWidget({ + let widget; + // BEGIN ESM + widget = new PrefsWidget({ + settings: this.settings, + gettext_context, + }); + // END ESM + // BEGIN !ESM + widget = new Me.imports.ddterm.pref.widget.PrefsWidget({ settings: this.settings, gettext_context, }); + // END !ESM const content_area = this.get_content_area(); diff --git a/ddterm/app/prefsdialog.legacy.js b/ddterm/app/prefsdialog.legacy.js deleted file mode 100644 index 9ba5c47e..00000000 --- a/ddterm/app/prefsdialog.legacy.js +++ /dev/null @@ -1,94 +0,0 @@ -/* - Copyright © 2024 Aleksandr Mezin - - This file is part of ddterm GNOME Shell extension. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -import GLib from 'gi://GLib'; -import GObject from 'gi://GObject'; -import Gio from 'gi://Gio'; -import Gtk from 'gi://Gtk'; - -import Gettext from 'gettext'; - -import { metadata, dir } from './meta.js'; - -const [fakeext_import_path] = GLib.filename_from_uri( - GLib.Uri.resolve_relative(import.meta.url, 'fakeext', GLib.UriFlags.NONE) -); - -imports.searchPath.unshift(fakeext_import_path); - -const Me = imports.misc.extensionUtils.getCurrentExtension(); - -function make_dir_importer(file) { - /* Like extensionUtils.installImporter() */ - - const old_search_path = imports.searchPath.slice(); - - imports.searchPath = [file.get_parent().get_path()]; - - try { - return imports[file.get_basename()]; - } finally { - imports.searchPath = old_search_path; - } -} - -/* - * fake current extension object to make `Me.imports` and `Me.dir` - * work in application context - */ -Object.assign(Me, { - imports: make_dir_importer(dir), - dir, - path: dir.get_path(), - metadata, -}); - -export const PrefsDialog = GObject.registerClass({ - Properties: { - 'settings': GObject.ParamSpec.object( - 'settings', - '', - '', - GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY, - Gio.Settings - ), - }, -}, class PrefsDialog extends Gtk.Dialog { - _init(params) { - super._init(params); - - const gettext_context = Gettext.domain(metadata['gettext-domain']); - - this.set_title(gettext_context.gettext('Preferences')); - this.set_default_size(640, 576); - this.set_icon_name('preferences-system'); - - const widget = new Me.imports.ddterm.pref.widget.PrefsWidget({ - settings: this.settings, - gettext_context, - }); - - const content_area = this.get_content_area(); - - if (content_area.append) - content_area.append(widget); - else - content_area.pack_start(widget, true, true, 0); - } -}); diff --git a/ddterm/app/resources.js b/ddterm/app/resources.js index 59f1fa9f..442edae7 100644 --- a/ddterm/app/resources.js +++ b/ddterm/app/resources.js @@ -38,8 +38,9 @@ export function get_resource_binary(file_or_relative_url) { export function get_resource_text(file_or_relative_url) { const bytes = get_resource_binary(file_or_relative_url); - - return globalThis.TextDecoder - ? new TextDecoder().decode(bytes) - : imports.byteArray.toString(bytes); + // BEGIN !ESM + if (!globalThis.TextDecoder) + return imports.byteArray.toString(bytes); + // END !ESM + return new TextDecoder().decode(bytes); } diff --git a/ddterm/pref/animation.js b/ddterm/pref/animation.js index b1a89942..741c0cf9 100644 --- a/ddterm/pref/animation.js +++ b/ddterm/pref/animation.js @@ -21,13 +21,12 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; - import { bind_sensitive, bind_widgets, insert_settings_actions, - set_scale_value_format + set_scale_value_format, + ui_file_uri } from './util.js'; export const AnimationWidget = GObject.registerClass({ diff --git a/ddterm/pref/behavior.js b/ddterm/pref/behavior.js index ad39976e..92be8d92 100644 --- a/ddterm/pref/behavior.js +++ b/ddterm/pref/behavior.js @@ -21,8 +21,7 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { bind_widget, insert_settings_actions } from './util.js'; +import { bind_widget, insert_settings_actions, ui_file_uri } from './util.js'; export const BehaviorWidget = GObject.registerClass({ GTypeName: 'DDTermPrefsBehavior', diff --git a/ddterm/pref/colors.js b/ddterm/pref/colors.js index 268fa73e..2a835497 100644 --- a/ddterm/pref/colors.js +++ b/ddterm/pref/colors.js @@ -23,13 +23,12 @@ import Gio from 'gi://Gio'; import Gdk from 'gi://Gdk'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; - import { bind_sensitive, bind_widget, insert_settings_actions, - set_scale_value_format + set_scale_value_format, + ui_file_uri } from './util.js'; function show_dialog(parent_window, message, message_type = Gtk.MessageType.ERROR) { diff --git a/ddterm/pref/command.js b/ddterm/pref/command.js index cacc599f..49a7d4eb 100644 --- a/ddterm/pref/command.js +++ b/ddterm/pref/command.js @@ -21,8 +21,7 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { bind_widget, insert_settings_actions } from './util.js'; +import { bind_widget, insert_settings_actions, ui_file_uri } from './util.js'; export const CommandWidget = GObject.registerClass({ GTypeName: 'DDTermPrefsCommand', diff --git a/ddterm/pref/compatibility.js b/ddterm/pref/compatibility.js index 763acca2..ce214854 100644 --- a/ddterm/pref/compatibility.js +++ b/ddterm/pref/compatibility.js @@ -21,8 +21,7 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { bind_widgets } from './util.js'; +import { bind_widgets, ui_file_uri } from './util.js'; export const CompatibilityWidget = GObject.registerClass({ GTypeName: 'DDTermPrefsCompatibility', diff --git a/ddterm/pref/meson.build b/ddterm/pref/meson.build index abeca31f..5300c4d0 100644 --- a/ddterm/pref/meson.build +++ b/ddterm/pref/meson.build @@ -19,15 +19,23 @@ pref_copy_files = [] foreach pref_file : pref_files if get_option('esm') - pref_copy_files += fs.copyfile( - pref_file, + pref_copy_files += custom_target( + command: preprocess_command, + input: pref_file, + output: fs.name(pref_file), install: true, install_dir: extension_dir / 'ddterm' / 'pref', ) else - pref_copy_files += custom_target( - command: gjs_translate_esm, + preproc = custom_target( + command: preprocess_command, input: pref_file, + output: fs.name(pref_file) + '.sed', + ) + + pref_copy_files += custom_target( + command: [gjs_translate_esm, '-o', '@OUTPUT@', '-d', meson.project_build_root(), '@INPUT@'], + input: preproc, output: fs.name(pref_file), install: true, install_dir: extension_dir / 'ddterm' / 'pref', @@ -35,13 +43,6 @@ foreach pref_file : pref_files endif endforeach -pref_copy_files += fs.copyfile( - get_option('esm') ? 'resources.js' : 'resources.legacy.js', - 'resources.js', - install: true, - install_dir: extension_dir / 'ddterm' / 'pref', -) - pack += pref_copy_files subdir('ui') diff --git a/ddterm/pref/panelicon.js b/ddterm/pref/panelicon.js index 022a889a..dfaa7b2d 100644 --- a/ddterm/pref/panelicon.js +++ b/ddterm/pref/panelicon.js @@ -21,8 +21,7 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { insert_settings_actions } from './util.js'; +import { insert_settings_actions, ui_file_uri } from './util.js'; export const PanelIconWidget = GObject.registerClass({ GTypeName: 'DDTermPrefsPanelIcon', diff --git a/ddterm/pref/positionsize.js b/ddterm/pref/positionsize.js index 99a9cc6d..02b1ac11 100644 --- a/ddterm/pref/positionsize.js +++ b/ddterm/pref/positionsize.js @@ -21,8 +21,15 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { bind_widget, insert_settings_actions, set_scale_value_format } from './util.js'; +import { + bind_widget, + insert_settings_actions, + set_scale_value_format, + ui_file_uri +} from './util.js'; +// BEGIN ESM +import { DisplayConfig } from '../util/displayconfig.js'; +// END ESM export const PositionSizeWidget = GObject.registerClass({ GTypeName: 'DDTermPrefsPositionSize', @@ -53,10 +60,10 @@ export const PositionSizeWidget = GObject.registerClass({ insert_settings_actions(this, this.settings, ['window-monitor']); + // BEGIN !ESM const destroy_cancel = new Gio.Cancellable(); this.connect('destroy', () => destroy_cancel.cancel()); - /* On GNOME versions before 45, the current module isn't ESM - but displayconfig.js is */ import('../util/displayconfig.js').then(displayconfig => { destroy_cancel.set_error_if_cancelled(); @@ -72,6 +79,20 @@ export const PositionSizeWidget = GObject.registerClass({ display_config.update_async(); }); + // END !ESM + // BEGIN ESM + const display_config = new DisplayConfig({ + dbus_connection: Gio.DBus.session, + }); + + this.connect('destroy', () => display_config.unwatch()); + + display_config.connect('notify::monitors', () => { + this.update_monitors(display_config.monitors); + }); + + display_config.update_async(); + // END ESM bind_widget(this.settings, 'window-monitor-connector', this.monitor_combo); diff --git a/ddterm/pref/resources.js b/ddterm/pref/resources.js deleted file mode 100644 index de88c184..00000000 --- a/ddterm/pref/resources.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - Copyright © 2024 Aleksandr Mezin - - This file is part of ddterm GNOME Shell extension. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -import GLib from 'gi://GLib'; -import Gtk from 'gi://Gtk'; - -export function ui_file_uri(name) { - return GLib.Uri.resolve_relative( - import.meta.url, - `ui/gtk${Gtk.get_major_version()}/${name}`, - GLib.UriFlags.NONE - ); -} diff --git a/ddterm/pref/resources.legacy.js b/ddterm/pref/resources.legacy.js deleted file mode 100644 index 3d6f5c0a..00000000 --- a/ddterm/pref/resources.legacy.js +++ /dev/null @@ -1,29 +0,0 @@ -/* - Copyright © 2024 Aleksandr Mezin - - This file is part of ddterm GNOME Shell extension. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -'use strict'; - -const { Gtk } = imports.gi; -const Me = imports.misc.extensionUtils.getCurrentExtension(); - -function ui_file_uri(name) { - return `${Me.dir.get_uri()}/ddterm/pref/ui/gtk${Gtk.get_major_version()}/${name}`; -} - -/* exported ui_file_uri */ diff --git a/ddterm/pref/scrolling.js b/ddterm/pref/scrolling.js index 713ddcf0..053bb16e 100644 --- a/ddterm/pref/scrolling.js +++ b/ddterm/pref/scrolling.js @@ -21,8 +21,7 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { bind_sensitive, bind_widget, insert_settings_actions } from './util.js'; +import { bind_sensitive, bind_widget, insert_settings_actions, ui_file_uri } from './util.js'; export const ScrollingWidget = GObject.registerClass({ GTypeName: 'DDTermPrefsScrolling', diff --git a/ddterm/pref/shortcuts.js b/ddterm/pref/shortcuts.js index 81f7d42e..2df9fad7 100644 --- a/ddterm/pref/shortcuts.js +++ b/ddterm/pref/shortcuts.js @@ -22,8 +22,7 @@ import Gio from 'gi://Gio'; import Gdk from 'gi://Gdk'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { bind_sensitive, insert_settings_actions } from './util.js'; +import { bind_sensitive, insert_settings_actions, ui_file_uri } from './util.js'; const IS_GTK3 = Gtk.get_major_version() === 3; diff --git a/ddterm/pref/tabs.js b/ddterm/pref/tabs.js index 8d3dd475..3e432eea 100644 --- a/ddterm/pref/tabs.js +++ b/ddterm/pref/tabs.js @@ -21,8 +21,12 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; -import { bind_widgets, insert_settings_actions, set_scale_value_format } from './util.js'; +import { + bind_widgets, + insert_settings_actions, + set_scale_value_format, + ui_file_uri +} from './util.js'; export const TabsWidget = GObject.registerClass({ GTypeName: 'DDTermPrefsTabs', diff --git a/ddterm/pref/text.js b/ddterm/pref/text.js index dc0e0466..65216da0 100644 --- a/ddterm/pref/text.js +++ b/ddterm/pref/text.js @@ -21,13 +21,12 @@ import GObject from 'gi://GObject'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; -import { ui_file_uri } from './resources.js'; - import { bind_sensitive, bind_widget, bind_widgets, - insert_settings_actions + insert_settings_actions, + ui_file_uri } from './util.js'; export const TextWidget = GObject.registerClass({ diff --git a/ddterm/pref/util.js b/ddterm/pref/util.js index 87cf0d40..f3bef15a 100644 --- a/ddterm/pref/util.js +++ b/ddterm/pref/util.js @@ -17,9 +17,14 @@ along with this program. If not, see . */ +import GLib from 'gi://GLib'; import Gio from 'gi://Gio'; import Gtk from 'gi://Gtk'; +// BEGIN !ESM +const Me = imports.misc.extensionUtils.getCurrentExtension(); +// END !ESM + export function set_scale_value_format(scale, format) { const formatter = (_, value) => format.format(value); @@ -83,3 +88,17 @@ export function insert_settings_actions(widget, settings, keys) { widget.insert_action_group('settings', group); return group; } + +export function ui_file_uri(name) { + // BEGIN ESM + return GLib.Uri.resolve_relative( + import.meta.url, + `ui/gtk${Gtk.get_major_version()}/${name}`, + GLib.UriFlags.NONE + ); + // END ESM + // BEGIN !ESM + // eslint-disable-next-line no-unreachable -- eslint doesn't understand our "preprocessor" + return `${Me.dir.get_uri()}/ddterm/pref/ui/gtk${Gtk.get_major_version()}/${name}`; + // END !ESM +} diff --git a/ddterm/shell/meson.build b/ddterm/shell/meson.build index 2c8a3a87..295d55ef 100644 --- a/ddterm/shell/meson.build +++ b/ddterm/shell/meson.build @@ -18,15 +18,23 @@ shell_files = files( foreach shell_file : shell_files if get_option('esm') - pack += fs.copyfile( - shell_file, + pack += custom_target( + command: preprocess_command, + input: shell_file, + output: fs.name(shell_file), install: true, install_dir: extension_dir / 'ddterm' / 'shell', ) else - pack += custom_target( - command: gjs_translate_esm, + preproc = custom_target( + command: preprocess_command, input: shell_file, + output: fs.name(shell_file) + '.sed', + ) + + pack += custom_target( + command: [gjs_translate_esm, '-o', '@OUTPUT@', '-d', meson.project_build_root(), '@INPUT@'], + input: preproc, output: fs.name(shell_file), install: true, install_dir: extension_dir / 'ddterm' / 'shell', diff --git a/ddterm/shell/subprocess.js b/ddterm/shell/subprocess.js index 629e3aa4..d792861f 100644 --- a/ddterm/shell/subprocess.js +++ b/ddterm/shell/subprocess.js @@ -130,13 +130,12 @@ class TeeLogCollector { async collect() { await this._promise; - - if (globalThis.TextDecoder) { - const decoder = new TextDecoder(); - return this._collected.map(line => decoder.decode(line)).join('\n'); - } else { + // BEGIN !ESM + if (!globalThis.TextDecoder) return this._collected.map(line => imports.byteArray.toString(line)).join('\n'); - } + // END !ESM + const decoder = new TextDecoder(); + return this._collected.map(line => decoder.decode(line)).join('\n'); } } diff --git a/ddterm/shell/wlclipboard.js b/ddterm/shell/wlclipboard.js index 7d110719..9da7972a 100644 --- a/ddterm/shell/wlclipboard.js +++ b/ddterm/shell/wlclipboard.js @@ -35,10 +35,16 @@ export function is_wlclipboard(win) { try { const [, bytes] = GLib.file_get_contents(`/proc/${pid}/cmdline`); const argv0_bytes = bytes.slice(0, bytes.indexOf(0)); - const argv0 = globalThis.TextDecoder - ? new TextDecoder().decode(argv0_bytes) - : imports.byteArray.toString(argv0_bytes); - + let argv0; + // BEGIN !ESM + if (!globalThis.TextDecoder) + argv0 = imports.byteArray.toString(argv0_bytes); + else + argv0 = new TextDecoder().decode(argv0_bytes); + // END !ESM + // BEGIN ESM + argv0 = new TextDecoder().decode(argv0_bytes); + // END ESM return ['wl-copy', 'wl-paste'].includes(GLib.path_get_basename(argv0)); } catch { return false; diff --git a/meson.build b/meson.build index 29ddd70d..bcd9e7e6 100644 --- a/meson.build +++ b/meson.build @@ -92,7 +92,15 @@ gjs_config.set('GJS', gjs.full_path()) gtk3_builder_tool = find_program('gtk-builder-tool') -gjs_translate_esm = [gjs, '-m', files('tools' / 'translate-esm.js'), '-o', '@OUTPUT@', '@INPUT@'] +sed = find_program('sed') + +if get_option('esm') + preprocess_command = [output_capture, sed, '-f', files('tools/preproc_esm.sin'), '@INPUT@'] +else + preprocess_command = [output_capture, sed, '-f', files('tools/preproc_legacy.sin'), '@INPUT@'] +endif + +gjs_translate_esm = [gjs, '-m', files('tools' / 'translate-esm.js')] subdir('bin') subdir('schemas') diff --git a/tools/preproc_esm.sin b/tools/preproc_esm.sin new file mode 100644 index 00000000..aa2eccd1 --- /dev/null +++ b/tools/preproc_esm.sin @@ -0,0 +1 @@ +/\/\/ BEGIN !ESM/,/\/\/ END !ESM/d diff --git a/tools/preproc_legacy.sin b/tools/preproc_legacy.sin new file mode 100644 index 00000000..28989a69 --- /dev/null +++ b/tools/preproc_legacy.sin @@ -0,0 +1 @@ +/\/\/ BEGIN ESM/,/\/\/ END ESM/d diff --git a/tools/translate-esm.js b/tools/translate-esm.js index eee3deda..8628e73d 100755 --- a/tools/translate-esm.js +++ b/tools/translate-esm.js @@ -24,9 +24,6 @@ import Gio from 'gi://Gio'; import System from 'system'; -const me_url = GLib.Uri.resolve_relative(import.meta.url, '..', GLib.UriFlags.NONE); -const me_url_parsed = GLib.Uri.parse(me_url, GLib.UriFlags.NONE); - class AstError extends Error { constructor(message, node) { if (node?.loc) { @@ -43,7 +40,7 @@ class AstError extends Error { } } -function translate(file) { +function translate(file, root_url) { const [, bytes] = file.load_contents(null); const text = globalThis.TextDecoder ? new TextDecoder().decode(bytes) @@ -115,7 +112,7 @@ function translate(file) { case 'file': return translate_path( module_uri_parsed.get_path(), - me_url_parsed.get_path(), + root_url.get_path(), 'Me.imports', node ); @@ -269,6 +266,15 @@ app.add_main_option( null ); +app.add_main_option( + 'base-dir', + 'd'.charCodeAt(0), + GLib.OptionFlags.NONE, + GLib.OptionArg.STRING, + 'Base/root directory', + '.' +); + app.connect('handle-local-options', (_, options) => { const files = options.lookup(GLib.OPTION_REMAINING, 'as', true); @@ -285,7 +291,9 @@ app.connect('handle-local-options', (_, options) => { const input_file = Gio.File.new_for_commandline_arg(files[0]); try { - const translated = translate(input_file); + const base_dir = GLib.canonicalize_filename(options.lookup('base-dir', 's'), null); + const base_uri = GLib.filename_to_uri(base_dir, null); + const translated = translate(input_file, GLib.Uri.parse(base_uri, GLib.UriFlags.NONE)); const output_path = options.lookup('output', 's'); if (output_path) {