-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.zig
More file actions
69 lines (67 loc) · 1.92 KB
/
Copy pathexample.zig
File metadata and controls
69 lines (67 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const prefix = "example/";
const filenames = [_][]const u8{
"example/example/debug/apps.html",
"example/example/debug/hyprland.html",
"example/example/debug/index.html",
"example/example/debug/network.html",
"example/example/debug/notifd.html",
"example/example/debug/screenshot.html",
"example/example/debug/style.css",
"example/example/debug/test-open.html",
"example/example/debug/tray.html",
"example/example/alias.json",
"example/example/bar.html",
"example/example/bongocat.html",
"example/example/dock.html",
"example/example/extra.js",
"example/example/showkeys.html",
"example/example/traymenu.html",
"example/config.json",
"example/notify.html",
};
const std = @import("std");
const fs = std.fs;
const File = struct {
name: []const u8,
bytes: []const u8,
};
pub const files = blk: {
var f: [filenames.len]File = undefined;
for (filenames, 0..) |name, i| {
f[i].name = name[prefix.len..];
f[i].bytes = @embedFile(name);
}
break :blk f;
};
fn writeFile(dir: fs.Dir, file: File) !void {
if (fs.path.dirname(file.name)) |dirName| {
try dir.makePath(dirName);
}
const f = try dir.createFile(file.name, .{});
defer f.close();
try f.writeAll(file.bytes);
}
pub fn write(dirPath: []const u8) !void {
var dir: fs.Dir = undefined;
if (fs.path.isAbsolute(dirPath)) {
try mkdir(dirPath);
dir = try fs.openDirAbsolute(dirPath, .{});
} else {
var cwd = fs.cwd();
try cwd.makePath(dirPath);
dir = try cwd.openDir(dirPath, .{});
}
defer dir.close();
for (files) |file| {
try writeFile(dir, file);
}
}
fn mkdir(path: []const u8) !void {
std.debug.assert(fs.path.isAbsolute(path));
fs.accessAbsolute(path, .{}) catch {
const dir = fs.path.dirname(path).?;
try mkdir(dir);
try fs.makeDirAbsolute(path);
return;
};
}