GitRoot

Craft your forge, Build your project, Grow your community freely
  1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
  2//
  3// SPDX-License-Identifier: MIT
  4
  5import { JSON } from "json-as";
  6import { __server } from "../server";
  7import {
  8  Call,
  9  CallRes,
 10  Commit,
 11  ConfPlugin,
 12  File,
 13  PluginSdkType,
 14  Report,
 15} from "./index";
 16
 17export function init(
 18  repoNamePtr: i32,
 19  repoNameSize: i32,
 20  initKindPtr: i32,
 21  initKindSize: i32,
 22  confPtr: i32,
 23  confSize: i32,
 24): void {
 25  const repoName = String.UTF8.decodeUnsafe(repoNamePtr, repoNameSize);
 26  const serializedConf = String.UTF8.decodeUnsafe(confPtr, confSize);
 27  const initKind = String.UTF8.decodeUnsafe(initKindPtr, initKindSize);
 28  if (__server && __server!.plugin) {
 29    __server!.plugin!.init(repoName, initKind, serializedConf);
 30  }
 31}
 32
 33export function startCommit(commitPtr: i32, commitSize: i32): void {
 34  if (__server && __server!.plugin) {
 35    const commit = JSON.parse<Commit>(
 36      String.UTF8.decodeUnsafe(commitPtr, commitSize),
 37    );
 38    __server!.plugin!.startCommit(commit);
 39  }
 40}
 41
 42export function addFile(ptr: i32, size: i32): void {
 43  if (__server && __server!.plugin) {
 44    const file = JSON.parse<File>(String.UTF8.decodeUnsafe(ptr, size));
 45    __server!.plugin!.addFile(file);
 46  }
 47}
 48
 49export function modFile(ptr: i32, size: i32): void {
 50  if (__server && __server!.plugin) {
 51    const file = JSON.parse<File>(String.UTF8.decodeUnsafe(ptr, size));
 52    __server!.plugin!.modFile(file);
 53  }
 54}
 55
 56export function delFile(ptr: i32, size: i32): void {
 57  if (__server && __server!.plugin) {
 58    const file = JSON.parse<File>(String.UTF8.decodeUnsafe(ptr, size));
 59    __server!.plugin!.delFile(file);
 60  }
 61}
 62
 63export function endCommit(commitPtr: i32, commitSize: i32): void {
 64  if (__server && __server!.plugin) {
 65    const commit = JSON.parse<Commit>(
 66      String.UTF8.decodeUnsafe(commitPtr, commitSize),
 67    );
 68    __server!.plugin!.endCommit(commit);
 69  }
 70}
 71
 72export function finish(): void {
 73  if (__server && __server!.plugin) {
 74    __server!.plugin!.finish();
 75  }
 76}
 77
 78export function report(jsonPtr: i32, jsonSize: i32): void {
 79  if (__server && __server!.plugin) {
 80    const report = JSON.parse<Report>(
 81      String.UTF8.decodeUnsafe(jsonPtr, jsonSize),
 82    );
 83    __server!.reportToPlugin(report);
 84  }
 85}
 86
 87export function call(jsonPtr: i32, jsonSize: i32): string {
 88  if (__server) {
 89    const call = JSON.parse<Call>(String.UTF8.decodeUnsafe(jsonPtr, jsonSize));
 90    const callRes = new CallRes(new Map(), "method not found");
 91    const callback = __server!.getExportedFuncs(call.name);
 92    if (callback != null) {
 93      const res = callback(call.args);
 94      callRes.res = res;
 95    }
 96    return JSON.stringify(callRes);
 97  }
 98  return "";
 99}
100
101export function pluginConf(): string {
102  if (__server) {
103    const conf = new ConfPlugin(
104      __server!.run,
105      "0.5.0",
106      PluginSdkType.Assemblyscript,
107    );
108    return JSON.stringify<ConfPlugin>(conf);
109  }
110  return "{}";
111}
112
113export function malloc(size: usize): usize {
114  const ptr = __new(size, 1);
115  // @ts-ignore
116  __pin(ptr);
117  return ptr;
118}
119
120export function free(ptr: usize): void {
121  // @ts-ignore
122  __unpin(ptr);
123}