Skip to content

Commit

Permalink
Asynchronize file copies (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp authored Feb 14, 2024
1 parent 2514485 commit 99d35b7
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/Library/Library.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export function getDemo(name) {

async function openDemo({ application, demo_name, language }) {
const demo = getDemo(demo_name);
const session = createSessionFromDemo(demo);
const session = await createSessionFromDemo(demo);

if (language) {
session.settings.set_int("code-language", language.index);
Expand Down
11 changes: 10 additions & 1 deletion src/PanelCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import Gio from "gi://Gio";
import GObject from "gi://GObject";

import { settings as global_settings, makeDropdownFlat } from "./util.js";
import { setupRustProject } from "./langs/rust/rust.js";

export default function PanelCode({ builder, previewer, settings }) {
export default function PanelCode({
builder,
previewer,
session: { settings, file },
}) {
const panel_code = builder.get_object("panel_code");
const button_code = builder.get_object("button_code");
const stack_code = builder.get_object("stack_code");
Expand Down Expand Up @@ -47,6 +52,10 @@ export default function PanelCode({ builder, previewer, settings }) {
panel.language = dropdown_code_lang.selected_item?.string;
stack_code.visible_child_name = panel.language;
previewer.useInternal().catch(console.error);

if (panel.language.toLowerCase() === "rust") {
setupRustProject(file).catch(console.error);
}
}
switchLanguage();

Expand Down
9 changes: 3 additions & 6 deletions src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,11 @@ function setColorScheme() {
setColorScheme();
settings.connect("changed::color-scheme", setColorScheme);

// We are not using async otherwise the app segfaults
// does not like opening a window in a promise
// TODO: make a reproducer and file a GJS bug
function restoreSessions() {
const sessions = getSessions();

if (sessions.length < 1) {
bootstrap();
bootstrap().catch(console.error);
} else {
sessions.forEach((session) => {
const { load } = Window({
Expand All @@ -94,15 +91,15 @@ function restoreSessions() {
}
}

function bootstrap() {
async function bootstrap() {
const first_run = settings.get_boolean("first-run");
if (!first_run) {
application.activate_action("library", null);
return;
}

const demo = getDemo("Welcome");
const session = createSessionFromDemo(demo);
const session = await createSessionFromDemo(demo);
const { load, window } = Window({
application,
session,
Expand Down
2 changes: 2 additions & 0 deletions src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ Gio._promisify(
"next_files_async",
"next_files_finish",
);

Gio._promisify(Gio.File.prototype, "copy_async", "copy_finish");
6 changes: 3 additions & 3 deletions src/langs/rust/Compiler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import dbus_previewer from "../../Previewer/DBusPreviewer.js";
import { copyDirectory, decode, encode } from "../../util.js";
import { rust_template_dir } from "./rust.js";
import { decode, encode } from "../../util.js";
import { installRustLibraries } from "./rust.js";

export default function Compiler({ session }) {
const { file } = session;
Expand All @@ -16,7 +16,7 @@ export default function Compiler({ session }) {
let savedRustcVersion;

async function compile() {
copyDirectory(rust_template_dir, file);
await installRustLibraries(file);

rustcVersion ||= await getRustcVersion();
savedRustcVersion ||= await getSavedRustcVersion({ rustcVersionFile });
Expand Down
40 changes: 39 additions & 1 deletion src/langs/rust/rust.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Gio from "gi://Gio";
import GLib from "gi://GLib";

import { createLSPClient } from "../../common.js";
import { getLanguage } from "../../util.js";
Expand Down Expand Up @@ -32,6 +33,43 @@ export function setup({ document }) {
return lspc;
}

export const rust_template_dir = Gio.File.new_for_path(
const rust_template_dir = Gio.File.new_for_path(
pkg.pkgdatadir,
).resolve_relative_path("langs/rust/template");

export async function setupRustProject(destination) {
return Promise.all([
copy("Cargo.toml", rust_template_dir, destination, Gio.FileCopyFlags.NONE),
copy("Cargo.lock", rust_template_dir, destination, Gio.FileCopyFlags.NONE),
]);
}

export async function installRustLibraries(destination) {
return Promise.all([
copy("lib.rs", rust_template_dir, destination, Gio.FileCopyFlags.OVERWRITE),
copy(
"workbench.rs",
rust_template_dir,
destination,
Gio.FileCopyFlags.OVERWRITE,
),
]);
}

async function copy(filename, source_dir, dest_dir, flags) {
const file = source_dir.get_child(filename);
try {
await file.copy_async(
dest_dir.get_child(file.get_basename()),
flags,
GLib.PRIORITY_DEFAULT,
null,
null,
null,
);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw err;
}
}
}
16 changes: 11 additions & 5 deletions src/sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
copyDirectory,
} from "./util.js";
import { languages } from "./common.js";
import { rust_template_dir } from "./langs/rust/rust.js";

export const sessions_dir = data_dir.get_child("sessions");

Expand Down Expand Up @@ -57,16 +56,16 @@ function createSession() {
return session;
}

export function createSessionFromDemo(demo) {
export async function createSessionFromDemo(demo) {
const { name, panels } = demo;

const session = createSession();
const demo_dir = demos_dir.get_child(name);

const { file, settings } = session;
copyDirectory(demo_dir, file);
copyDirectory(rust_template_dir, file);
await copyDirectory(demo_dir, file);

settings.delay();
settings.set_string("name", name);
settings.set_boolean("show-code", panels.includes("code"));
settings.set_boolean("show-style", panels.includes("style"));
Expand All @@ -76,6 +75,7 @@ export function createSessionFromDemo(demo) {
"code-language",
global_settings.get_int("recent-code-language"),
);
settings.apply();

return session;
}
Expand All @@ -84,7 +84,13 @@ export async function deleteSession(session) {
// There is no method to recursively delete a folder so we trash instead
// https://github.com/flatpak/xdg-desktop-portal/issues/630 :/
// portal.trash_file(file.get_path(), null).catch(console.error);
session.file.trash(null);
try {
session.file.trash(null);
} catch (err) {
if (!err.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.EXISTS)) {
throw err;
}
}
}

export async function saveSessionAsProject(session, destination) {
Expand Down
14 changes: 9 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,23 @@ export const demos_dir = Gio.File.new_for_path(
).resolve_relative_path("demos");

// There is no copy directory function
export function copyDirectory(source, destination) {
for (const file_info of source.enumerate_children(
"",
export async function copyDirectory(source, destination) {
const enumerator = await source.enumerate_children_async(
"standard::name",
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
GLib.PRIORITY_DEFAULT,
null,
)) {
);

for await (const file_info of enumerator) {
if (file_info.get_file_type() === Gio.FileType.DIRECTORY) continue;
const child = source.get_child(file_info.get_name());

try {
child.copy(
await child.copy_async(
destination.get_child(child.get_basename()),
Gio.FileCopyFlags.NONE,
GLib.PRIORITY_DEFAULT,
null,
null,
);
Expand Down
2 changes: 1 addition & 1 deletion src/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default function Window({ application, session }) {
const panel_code = PanelCode({
builder,
previewer,
settings,
session,
});

previewer.setPanelCode(panel_code);
Expand Down

0 comments on commit 99d35b7

Please sign in to comment.