-
-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
vala: Port Vala to Meson #969
base: main
Are you sure you want to change the base?
Changes from all commits
7f6e023
0672cbb
7172d06
7826298
4696398
fc77803
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,79 @@ | ||
import Gio from "gi://Gio"; | ||
import dbus_previewer from "../../Previewer/DBusPreviewer.js"; | ||
import { decode } from "../../util.js"; | ||
import { createTmpDir, copy } from "../../util.js"; | ||
import { setupValaProject } from "./vala.js"; | ||
|
||
let ready_to_build = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused about why this is needed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did this to know if the Vala builddir was already setup (as it needs a meson file and the workbench API). This could be done checking if a build file (like |
||
const session_build_dir = await createTmpDir("vala"); | ||
|
||
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 = session_build_dir | ||
.get_child(meson_builddir) | ||
.get_child("libworkbenchcode.so"); | ||
|
||
async function compile() { | ||
let args; | ||
if (!ready_to_build) { | ||
try { | ||
await setupValaProject(session_build_dir); | ||
ready_to_build = true; | ||
} catch (err) { | ||
console.error(err); | ||
return false; | ||
} | ||
} | ||
|
||
try { | ||
const [contents] = await file_vala.load_contents_async(null); | ||
const code = decode(contents); | ||
args = getValaCompilerArguments(code); | ||
} catch (error) { | ||
console.debug(error); | ||
return; | ||
await copy( | ||
"main.vala", | ||
file, | ||
session_build_dir, | ||
Gio.FileCopyFlags.OVERWRITE | Gio.FileCopyFlags.ALL_METADATA, | ||
); | ||
|
||
// TODO: Do not run setup if the build directory is already | ||
// configured | ||
const meson_launcher = new Gio.SubprocessLauncher(); | ||
meson_launcher.set_cwd(session_build_dir.get_path()); | ||
const meson_setup = meson_launcher.spawnv([ | ||
"meson", | ||
"setup", | ||
meson_builddir, | ||
]); | ||
|
||
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_clean = meson_launcher.spawnv([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why cleaning? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When replacing the file in the build dir, meson will not detect any changes on the file, thus not compiling anything and just running the project right away. This is a problem when a user has two open projects and working on them simultaneously, so I went for cleaning the build files and recompiling. I thought this approach was OK since we don't really compile much. |
||
"meson", | ||
"compile", | ||
"--clean", | ||
"-C", | ||
meson_builddir, | ||
]); | ||
|
||
await valac.wait_async(null); | ||
await meson_clean.wait_async(null); | ||
if (!meson_clean.get_successful()) { | ||
return false; | ||
} | ||
|
||
const meson_compile = meson_launcher.spawnv([ | ||
"meson", | ||
"compile", | ||
"-C", | ||
meson_builddir, | ||
]); | ||
|
||
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() { | ||
|
@@ -60,11 +90,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(" ") || []; | ||
} | ||
Comment on lines
-63
to
-70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
install_data(['template/meson.build', 'template/workbench.vala'], | ||
install_dir : join_paths(pkgdatadir, 'langs/vala/template')) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||||||||||||||||||||||||||||||||
project('demo', ['c', 'vala'], | ||||||||||||||||||||||||||||||||||||
version: '0.1', | ||||||||||||||||||||||||||||||||||||
meson_version: '>= 0.59.0', | ||||||||||||||||||||||||||||||||||||
default_options: [ 'warning_level=2', 'werror=false', ], | ||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
i18n = import('i18n') | ||||||||||||||||||||||||||||||||||||
gnome = import('gnome') | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
source_files = [ | ||||||||||||||||||||||||||||||||||||
'main.vala', | ||||||||||||||||||||||||||||||||||||
'workbench.vala' | ||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
cc = meson.get_compiler('c') | ||||||||||||||||||||||||||||||||||||
m_dep = cc.find_library('m', required : false) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
add_project_arguments( | ||||||||||||||||||||||||||||||||||||
'-shared', | ||||||||||||||||||||||||||||||||||||
'-fpic', | ||||||||||||||||||||||||||||||||||||
language: 'c' | ||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
dependencies = [ | ||||||||||||||||||||||||||||||||||||
m_dep, | ||||||||||||||||||||||||||||||||||||
dependency('libadwaita-1'), | ||||||||||||||||||||||||||||||||||||
dependency('shumate-1.0'), | ||||||||||||||||||||||||||||||||||||
dependency('libportal-gtk4'), | ||||||||||||||||||||||||||||||||||||
dependency('libsoup-3.0'), | ||||||||||||||||||||||||||||||||||||
dependency('webkitgtk-6.0'), | ||||||||||||||||||||||||||||||||||||
dependency('gstreamer-1.0'), | ||||||||||||||||||||||||||||||||||||
dependency('gtksourceview-5'), | ||||||||||||||||||||||||||||||||||||
dependency('json-glib-1.0'), | ||||||||||||||||||||||||||||||||||||
Comment on lines
+26
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||
] | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
library('workbenchcode', source_files, | ||||||||||||||||||||||||||||||||||||
dependencies: dependencies, | ||||||||||||||||||||||||||||||||||||
install: true, | ||||||||||||||||||||||||||||||||||||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,22 +2,16 @@ import Gio from "gi://Gio"; | |
|
||
import { isValaAvailable } 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 (!isValaAvailable()) return; | ||
|
||
const { file, buffer, code_view } = document; | ||
|
||
const api_file = Gio.File.new_for_path(pkg.pkgdatadir).get_child( | ||
"workbench.vala", | ||
); | ||
api_file.copy( | ||
file.get_parent().get_child("workbench.vala"), | ||
Gio.FileCopyFlags.OVERWRITE, | ||
null, | ||
null, | ||
); | ||
// VLS needs the project to be already setup once it starts, | ||
// otherwise it won't pick it up later. | ||
setupValaProject(file.get_parent()).catch(console.error); | ||
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible to move the meson and workbench.vala files under a subdir? to avoid cluttering the project and we might need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I think this is possible. Would that subdir still be inside the project folder? Or do you mean saving just the |
||
|
||
const lspc = createLSPClient({ | ||
lang: getLanguage("vala"), | ||
|
@@ -44,3 +38,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, | ||
), | ||
]); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -211,3 +211,25 @@ export async function copy(filename, source_dir, dest_dir, flags) { | |
|
||
return dest_file; | ||
} | ||
|
||
const tmp_dirs = {}; | ||
|
||
export async function createTmpDir(filename) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I'll check them out! |
||
if (filename in tmp_dirs) { | ||
return tmp_dirs[filename]; | ||
} | ||
|
||
const tmp_dir = Gio.File.new_for_path( | ||
GLib.build_filenamev([GLib.get_tmp_dir(), filename]), | ||
); | ||
|
||
try { | ||
tmp_dir.make_directory(null); | ||
tmp_dirs[filename] = tmp_dir; | ||
} catch (err) { | ||
console.error(err); | ||
return null; | ||
} | ||
|
||
return tmp_dir; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.