Skip to content

Commit

Permalink
vala: Compile and run using Meson
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego-Ivan committed Jul 12, 2024
1 parent 68e1d33 commit 24ac6d3
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 43 deletions.
5 changes: 5 additions & 0 deletions src/PanelCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import GObject from "gi://GObject";
import { makeDropdownFlat, settings as global_settings } from "./util.js";
import { setupRustProject } from "./langs/rust/rust.js";
import { setupTypeScriptProject } from "./langs/typescript/typescript.js";
import { setupValaProject } from "./langs/vala/vala.js";

export default function PanelCode({
builder,
Expand Down Expand Up @@ -55,6 +56,10 @@ export default function PanelCode({
stack_code.visible_child_name = panel.language;
previewer.useInternal().catch(console.error);

if (panel.language.toLowerCase() === "vala") {
setupValaProject(file).catch(console.error);
}

if (panel.language.toLowerCase() === "rust") {
setupRustProject(file).catch(console.error);
}
Expand Down
67 changes: 27 additions & 40 deletions src/langs/vala/Compiler.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
import Gio from "gi://Gio";
import dbus_previewer from "../../Previewer/DBusPreviewer.js";
import { decode } from "../../util.js";

export default function ValaCompiler({ session }) {
const { file } = session;

const module_file = file.get_child("libworkbenchcode.so");
const file_vala = file.get_child("main.vala");
const meson_builddir = "builddir";
const module_file = file
.get_child(meson_builddir)
.get_child("libworkbenchcode.so");

async function compile() {
let args;
// TODO: Do not run setup if the build directory is already
// configured
const meson_launcher = new Gio.SubprocessLauncher();
meson_launcher.set_cwd(file.get_path());
const meson_setup = meson_launcher.spawnv([
"meson",
"setup",
meson_builddir,
]);

try {
const [contents] = await file_vala.load_contents_async(null);
const code = decode(contents);
args = getValaCompilerArguments(code);
} catch (error) {
console.debug(error);
return;
await meson_setup.wait_async(null);
const setup_result = meson_setup.get_successful();
if (!setup_result) {
return false;
}

const valac_launcher = new Gio.SubprocessLauncher();
valac_launcher.set_cwd(file.get_path());
const valac = valac_launcher.spawnv([
"valac",
file_vala.get_path(),
"--hide-internal",
"-X",
"-shared",
"-X",
"-fpic",
"--library",
"workbench",
"-o",
module_file.get_path(),
"--vapi",
"/dev/null",
...args,
const meson_compile = meson_launcher.spawnv([
"meson",
"compile",
"-C",
meson_builddir,
]);

await valac.wait_async(null);
await meson_compile.wait_async(null);
const compile_result = meson_compile.get_successful();

const result = valac.get_successful();
valac_launcher.close();
return result;
meson_launcher.close();

return compile_result;
}

async function run() {
Expand All @@ -60,11 +55,3 @@ export default function ValaCompiler({ session }) {

return { compile, run };
}

// Takes a string starting with the line
// #!/usr/bin/env -S vala workbench.vala --pkg gtk4 --pkg libadwaita-1
// and return ["--pkg", "gtk4", "--pkg", "libadwaita-1"]
// FIXME: consider using https://docs.gtk.org/glib/struct.OptionContext.html instead
function getValaCompilerArguments(text) {
return text.split("\n")[0]?.split("-S vala ")[1]?.split(" ") || [];
}
4 changes: 2 additions & 2 deletions src/langs/vala/template/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ dependencies = [
dependency('webkitgtk-6.0'),
dependency('gstreamer-1.0'),
dependency('gtksourceview-5'),
dependency('json-glib-1.0'),
]

executable('demo', source_files,
objects: 'libworkbenchcode.so',
library('workbenchcode', source_files,
dependencies: dependencies,
install: true,
)
23 changes: 22 additions & 1 deletion src/langs/vala/vala.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Gio from "gi://Gio";

import { isValaEnabled } from "../../Extensions/Extensions.js";
import { createLSPClient } from "../../common.js";
import { getLanguage } from "../../util.js";
import { getLanguage, copy } from "../../util.js";

export function setup({ document }) {
if (!isValaEnabled()) return;
Expand Down Expand Up @@ -44,3 +44,24 @@ export function setup({ document }) {

return lspc;
}

const vala_template_dir = Gio.File.new_for_path(
pkg.pkgdatadir,
).resolve_relative_path("langs/vala/template");

export async function setupValaProject(destination) {
return Promise.all([
copy(
"meson.build",
vala_template_dir,
destination,
Gio.FileCopyFlags.OVERWRITE,
),
copy(
"workbench.vala",
vala_template_dir,
destination,
Gio.FileCopyFlags.OVERWRITE,
),
]);
}

0 comments on commit 24ac6d3

Please sign in to comment.