working build scripts

This commit is contained in:
Henry Hiles 2026-01-24 13:44:16 +00:00
commit 5c6440894b
No known key found for this signature in database
6 changed files with 78 additions and 101 deletions

View file

@ -1,8 +1,11 @@
import "dart:io";
import "package:hooks/hooks.dart"; import "package:hooks/hooks.dart";
import "package:code_assets/code_assets.dart"; import "package:code_assets/code_assets.dart";
import "package:path/path.dart";
Future<void> main(List<String> args) => build(args, (input, output) async { Future<void> main(List<String> args) => build(args, (input, output) async {
final buildDir = input.packageRoot.resolve("build/");
if (await File(buildDir.resolve("lock").toFilePath()).exists()) return;
final targetOS = input.config.code.targetOS; final targetOS = input.config.code.targetOS;
String libFileName; String libFileName;
switch (targetOS) { switch (targetOS) {
@ -19,16 +22,29 @@ Future<void> main(List<String> args) => build(args, (input, output) async {
throw UnsupportedError("Unsupported OS: $targetOS"); throw UnsupportedError("Unsupported OS: $targetOS");
} }
final generatedFile = join("src", "third_party", "gomuks.g.dart"); final gomuksBuildDir = buildDir.resolve("gomuks/");
final libFile = gomuksBuildDir.resolve(libFileName);
print("Building Gomuks shared library $libFileName...");
final result = await Process.run("go", [
"build",
"-o",
libFile.path,
"-buildmode=c-shared",
], workingDirectory: gomuksBuildDir.resolve("source/pkg/ffi/").toFilePath());
if (result.exitCode != 0) {
throw Exception("Failed to build Gomuks shared library\n${result.stderr}");
}
final generatedFile = "src/third_party/gomuks.g.dart";
output.assets.code.add( output.assets.code.add(
CodeAsset( CodeAsset(
package: "nexus", package: "nexus",
name: generatedFile, name: generatedFile,
linkMode: DynamicLoadingBundled(), linkMode: DynamicLoadingBundled(),
file: input.packageRoot.resolve(join("build", "gomuks", libFileName)), file: libFile,
), ),
); );
output.dependencies.add( output.dependencies.add(input.packageRoot.resolve("lib/$generatedFile"));
input.packageRoot.resolve(join("lib", generatedFile)),
);
}); });

View file

@ -46,23 +46,14 @@ class ClientController extends Notifier<int> {
return handle; return handle;
} }
(int requestId, Map<String, dynamic> response) sendCommand( void sendCommand(String command, Map<String, dynamic> data) {
String command, // final response = GomuksSubmitCommand(
Map<String, dynamic> data, // state,
) { // command.toNativeUtf8().cast<Char>(),
final responsePtr = calloc<GomuksBuffer>(); // data.toGomuksBuffer(),
try { // );
final requestId = GomuksSubmitCommand(
state,
command.toNativeUtf8().cast<Char>(),
data.toGomuksBuffer(),
responsePtr,
);
return (requestId, responsePtr.ref.toJson()); // return response.buf; TODO
} finally {
calloc.free(responsePtr);
}
} }
static final provider = NotifierProvider<ClientController, int>( static final provider = NotifierProvider<ClientController, int>(

View file

@ -6,6 +6,11 @@ import "package:ffi/ffi.dart";
import "package:nexus/src/third_party/gomuks.g.dart"; import "package:nexus/src/third_party/gomuks.g.dart";
extension GomuksBufferToJson on GomuksBorrowedBuffer { extension GomuksBufferToJson on GomuksBorrowedBuffer {
Uint8List toBytes() {
if (base == nullptr || length <= 0) return Uint8List(0);
return base.asTypedList(length);
}
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {
final bytes = toBytes(); final bytes = toBytes();
if (bytes.isEmpty) return {}; if (bytes.isEmpty) return {};
@ -14,21 +19,21 @@ extension GomuksBufferToJson on GomuksBorrowedBuffer {
} }
extension JsonToGomuksBuffer on Map<String, dynamic> { extension JsonToGomuksBuffer on Map<String, dynamic> {
GomuksBuffer toGomuksBuffer() { // GomuksBorrowedBuffer toGomuksBuffer() {
final jsonString = json.encode(this); // final jsonString = json.encode(this);
final bytes = utf8.encode(jsonString); // final bytes = utf8.encode(jsonString);
final dataPtr = calloc<Uint8>(bytes.length); // final dataPtr = calloc<Uint8>(bytes.length);
dataPtr.asTypedList(bytes.length).setAll(0, bytes); // dataPtr.asTypedList(bytes.length).setAll(0, bytes);
final bufPtr = calloc<GomuksBuffer>(); // final bufPtr = calloc<GomuksBuffer>();
bufPtr.ref.base = dataPtr; // bufPtr.ref.base = dataPtr;
bufPtr.ref.length = bytes.length; // bufPtr.ref.length = bytes.length;
final bufByValue = bufPtr.ref; // final bufByValue = bufPtr.ref;
calloc.free(bufPtr); // calloc.free(bufPtr);
return bufByValue; // return bufByValue;
} // }
} }

View file

@ -105,7 +105,6 @@ class App extends ConsumerWidget {
"username": "quadradical", "username": "quadradical",
"password": "Quadmarad1!", "password": "Quadmarad1!",
}); });
debugPrint("$response");
return Placeholder(); return Placeholder();
}, },
// .betterWhen( // .betterWhen(

View file

@ -3,79 +3,36 @@ import "package:ffigen/ffigen.dart";
import "package:path/path.dart"; import "package:path/path.dart";
void main(List<String> args) async { void main(List<String> args) async {
final buildDir = Directory.fromUri( final repoDir = Directory.fromUri(
Platform.script.resolve(join("..", "build", "gomuks")), Platform.script.resolve("../build/gomuks/source"),
); );
if (!await buildDir.exists()) await buildDir.create(recursive: true); if (await repoDir.exists()) await repoDir.delete(recursive: true);
await repoDir.create(recursive: true);
final repoDir = Directory(join(buildDir.path, "source")); print("Cloning Gomuks repository...");
final cloneResult = await Process.run("git", [
"clone",
"--branch",
"tulir/ffi",
"--depth",
"1",
"https://mau.dev/gomuks/gomuks",
repoDir.path,
]);
var skipBuild = args.contains("--skip"); if (cloneResult.exitCode != 0) {
throw Exception(
final generatedSourcePath = join("src", "third_party", "gomuks.g.dart"); "Failed to clone Gomuks repository: \n${cloneResult.stderr}",
final generatedLibPath = Platform.script.resolve( );
join("..", "lib", generatedSourcePath),
);
final bindingsFile = File(generatedLibPath.toFilePath());
if (await bindingsFile.exists() && await repoDir.exists()) {
final result = await Process.run("git", [
"fetch",
"--dry-run",
], workingDirectory: repoDir.path);
if ((result.stdout as String).trim().isEmpty) {
skipBuild = true;
}
}
if (!skipBuild) {
if (await repoDir.exists()) await repoDir.delete(recursive: true);
print("Cloning Gomuks repository...");
final cloneResult = await Process.run("git", [
"clone",
"--branch",
"tulir/ffi",
"--depth",
"1",
"https://mau.dev/gomuks/gomuks",
repoDir.path,
]);
if (cloneResult.exitCode != 0) {
throw Exception(
"Failed to clone Gomuks repository: \n${cloneResult.stderr}",
);
}
for (final name in ["libgomuks.so", "libgomuks.dylib", "libgomuks.dll"]) {
final libFile = File(join(buildDir.path, name));
print("Building Gomuks shared library $name...");
final result = await Process.run("go", [
"build",
"-o",
libFile.path,
"-buildmode=c-shared",
], workingDirectory: join(repoDir.path, "pkg/ffi"));
if (result.exitCode != 0) {
throw Exception(
"Failed to build Gomuks shared library\n${result.stderr}",
);
}
}
} }
print("Generating FFI Bindings..."); print("Generating FFI Bindings...");
final packageRoot = Platform.script.resolve("../");
FfiGenerator( FfiGenerator(
output: Output( output: Output(
dartFile: packageRoot.resolve("lib/src/third_party/gomuks.g.dart"), dartFile: Platform.script.resolve("../lib/src/third_party/gomuks.g.dart"),
), ),
headers: Headers( headers: Headers(
entryPoints: [File(join(buildDir.path, "libgomuks.h")).uri], entryPoints: [File(join(repoDir.path, "pkg", "ffi", "ffi.h")).uri],
compilerOptions: ["--no-warnings"], compilerOptions: ["--no-warnings"],
), ),
functions: Functions.includeAll, functions: Functions.includeAll,

9
scripts/generate.sh Executable file
View file

@ -0,0 +1,9 @@
#!/usr/bin/env bash
pushd "$(dirname "$(readlink -f "$0")")"/.. || exit
mkdir -p build
touch build/lock
dart scripts/generate.dart
rm build/lock
popd || exit