From bee07a0c96d5f95111ffc4f43a352d2cf8f8a8b0 Mon Sep 17 00:00:00 2001 From: Mahdi Sharifi Date: Fri, 20 Oct 2023 13:30:46 +0330 Subject: [PATCH] Automatically install pnpm in the builder --- .github/workflows/test-run.yml | 2 +- .gitignore | 3 ++- bench/bench-with-builtin.js | 2 +- build.zig | 25 +++++++++++++++++++++++-- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test-run.yml b/.github/workflows/test-run.yml index faf2074..29a5a6a 100644 --- a/.github/workflows/test-run.yml +++ b/.github/workflows/test-run.yml @@ -11,6 +11,6 @@ jobs: - name: Install npm run: sudo apt-get install -y curl unzip && curl -fsSL https://fnm.vercel.app/install | bash && export PATH="/$HOME/.local/share/fnm:$PATH" && eval "`fnm env`" && fnm install v20.6.1 && fnm use v20.6.1 && npm -g i npm@latest - name: Install Deno and Bun - run: npm install -g npm@latest; npm install -g bun deno-bin pnpm + run: npm install -g npm@latest; npm install -g bun deno-bin - name: Run tests run: zig build test \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2b134b4..4722605 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ **/pnpm-lock.yaml **/package-lock.json bench/faker/data/* -!bench/faker/data/README.md \ No newline at end of file +!bench/faker/data/README.md +perf.data \ No newline at end of file diff --git a/bench/bench-with-builtin.js b/bench/bench-with-builtin.js index 08f049c..2084191 100644 --- a/bench/bench-with-builtin.js +++ b/bench/bench-with-builtin.js @@ -5,7 +5,7 @@ import { fileURLToPath } from "url"; import { Kivi } from "../src/drivers/js/index.js"; import { isNotNodeJS, isBun } from "../src/drivers/js/runtime.js"; -const benchmarkRepeat = 100; +const benchmarkRepeat = 5; const average = (arr) => arr.reduce((a, b) => a + b, 0) / arr.length; const resolveOnEmit = (event) => { diff --git a/build.zig b/build.zig index 9482a1f..9e573c0 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,5 @@ const std = @import("std"); const cwd = std.fs.cwd(); -var optimize: std.builtin.OptimizeMode = undefined; /// Build static and shared libraries given name and path const Libs = struct { @@ -71,9 +70,27 @@ const FFI = struct { } }; +fn install_pnpm() !void { + const command_res = try std.ChildProcess.exec(.{ .allocator = std.heap.page_allocator, .argv = &[_][]const u8{ "npm", "install", "-g", "pnpm@latest" } }); + if (command_res.stderr.len > 0) { + std.debug.print("{s}\n", .{command_res.stderr}); + return error.PnpmNotFoundAndFailedToInstall; + } +} +fn install_pnpm_if_needed() !void { + const pnpm_version_command = std.ChildProcess.exec(.{ .allocator = std.heap.page_allocator, .argv = &[_][]const u8{ "pnpm", "--version" } }); + if (pnpm_version_command) |command_res| { + if (command_res.stderr.len > 0) { + try install_pnpm(); + } + } else |_| { + try install_pnpm(); + } +} + pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); - optimize = b.standardOptimizeOption(.{}); + const optimize = b.standardOptimizeOption(.{}); const target_info = try std.zig.system.NativeTargetInfo.detect(target); const arch = @tagName(target_info.target.cpu.arch); const os = @tagName(target_info.target.os.tag); @@ -122,6 +139,9 @@ pub fn build(b: *std.Build) !void { const codegen_run = b.addRunArtifact(codegen); codegen_step.dependOn(&b.addRunArtifact(codegen).step); + // Install pnpm if needed + try install_pnpm_if_needed(); + // Runs all tests const test_step = b.step("test", "Runs all tests"); test_step.dependOn(&b.addRunArtifact(core_targets.tests).step); @@ -143,6 +163,7 @@ pub fn build(b: *std.Build) !void { inline for (@typeInfo(FFI).Struct.fields) |field| { test_step.dependOn(&b.addRunArtifact(@field(ffi, field.name)).step); } + const js_driver_test_commad1 = b.addSystemCommand(&[_][]const u8{ "pnpm", "-C", "src/drivers/js", "run", "nodejs-test" }); js_driver_test_commad1.step.dependOn(drivers_build_step); test_step.dependOn(&js_driver_test_commad1.step);