From 28777b60c1eb7782b6b2869037778abd81c27d9d Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Thu, 14 Mar 2024 16:28:43 +0100 Subject: [PATCH] fix: name build type `treesitter-parser` by default For backward compatibility, we keep the `tree-sitter` module. --- README.md | 2 +- fixtures/tree-sitter-ocamllex-scm-1.rockspec | 2 +- fixtures/tree-sitter-rust-scm-1.rockspec | 2 +- fixtures/tree-sitter-toml-scm-1.rockspec | 2 +- fixtures/tree-sitter-xml-scm-1.rockspec | 2 +- src/luarocks/build/tree-sitter.lua | 102 +------------------ src/luarocks/build/treesitter-parser.lua | 98 ++++++++++++++++++ 7 files changed, 107 insertions(+), 103 deletions(-) create mode 100644 src/luarocks/build/treesitter-parser.lua diff --git a/README.md b/README.md index e462798..5263f84 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,7 @@ dependencies = { build = { - type = "tree-sitter", + type = "treesitter-parser", ---@type string (required) Name of the language, e.g. "haskell". lang = "LANG", diff --git a/fixtures/tree-sitter-ocamllex-scm-1.rockspec b/fixtures/tree-sitter-ocamllex-scm-1.rockspec index 536c145..970bd83 100644 --- a/fixtures/tree-sitter-ocamllex-scm-1.rockspec +++ b/fixtures/tree-sitter-ocamllex-scm-1.rockspec @@ -18,7 +18,7 @@ dependencies = { } build = { - type = "tree-sitter", + type = "tree-sitter", -- Make sure old version still works lang = "ocamllex", sources = { "src/parser.c", "src/scanner.c" }, generate_from_grammar = true, diff --git a/fixtures/tree-sitter-rust-scm-1.rockspec b/fixtures/tree-sitter-rust-scm-1.rockspec index d04d8fc..a4c6174 100644 --- a/fixtures/tree-sitter-rust-scm-1.rockspec +++ b/fixtures/tree-sitter-rust-scm-1.rockspec @@ -18,7 +18,7 @@ dependencies = { } build = { - type = "tree-sitter", + type = "treesitter-parser", lang = "rust", sources = { "src/parser.c", "src/scanner.c" }, } diff --git a/fixtures/tree-sitter-toml-scm-1.rockspec b/fixtures/tree-sitter-toml-scm-1.rockspec index e3246ab..d845097 100644 --- a/fixtures/tree-sitter-toml-scm-1.rockspec +++ b/fixtures/tree-sitter-toml-scm-1.rockspec @@ -18,7 +18,7 @@ dependencies = { } build = { - type = "tree-sitter", + type = "treesitter-parser", lang = "toml", sources = { "src/parser.c", "src/scanner.c" }, generate_from_grammar = true, diff --git a/fixtures/tree-sitter-xml-scm-1.rockspec b/fixtures/tree-sitter-xml-scm-1.rockspec index 75734a4..c9d6105 100644 --- a/fixtures/tree-sitter-xml-scm-1.rockspec +++ b/fixtures/tree-sitter-xml-scm-1.rockspec @@ -18,7 +18,7 @@ dependencies = { } build = { - type = "tree-sitter", + type = "treesitter-parser", lang = "xml", sources = { "src/parser.c", "src/scanner.c" }, location = "xml", diff --git a/src/luarocks/build/tree-sitter.lua b/src/luarocks/build/tree-sitter.lua index 6c09438..f96f2df 100644 --- a/src/luarocks/build/tree-sitter.lua +++ b/src/luarocks/build/tree-sitter.lua @@ -1,98 +1,4 @@ ----@diagnostic disable: inject-field -local fs = require("luarocks.fs") -local builtin = require("luarocks.build.builtin") -local util = require("luarocks.util") - -local tree_sitter = {} - ----@class RockSpec ----@field package string ----@field build BuildSpec - ----@class BuildSpec ----@field type string - ----@class TreeSitterRockSpec: RockSpec ----@field type fun():string ----@field build TreeSitterBuildSpec - ----@class TreeSitterBuildSpec: BuildSpec ----@field lang string ----@field sources string[] ----@field generate_requires_npm? boolean ----@field generate_from_grammar? boolean ----@field location? string - ----@param rockspec table ----@param no_install boolean -function tree_sitter.run(rockspec, no_install) - assert(rockspec:type() == "rockspec") - ---@cast rockspec RockSpec - assert(rockspec.build.type == "tree-sitter") - ---@cast rockspec TreeSitterRockSpec - - local build = rockspec.build - - if build.generate_requires_npm and not fs.is_tool_available("npm", "npm") then - return nil, "'npm' is not installed.\n" .. rockspec.package .. " requires npm to build.\n" - end - if build.generate_from_grammar and not fs.is_tool_available("tree-sitter", "tree-sitter CLI") then - return nil, - "'tree-sitter CLI' is not installed.\n" .. rockspec.package .. " requires the tree-sitter CLI to build.\n" - end - if build.generate_from_grammar then - local js_runtime = os.getenv("TREE_SITTER_JS_RUNTIME") or "node" - local js_runtime_name = js_runtime == "node" and "Node JS" or js_runtime - if not fs.is_tool_available(js_runtime, js_runtime_name) then - return nil, - ("'%s' is not installed.\n%s requires %s to build."):format( - js_runtime, - rockspec.package, - js_runtime_name - ) - end - end - if build.location then - util.printout("Changing to directory: " .. build.location) - fs.change_dir(build.location) - end - if build.generate_from_grammar then - local cmd - if build.generate_requires_npm then - cmd = { "npm", "install" } - util.printout("Installing npm dependencies...") - if not fs.execute(table.concat(cmd, " ")) then - return nil, "Failed to install npm dependencies." - end - util.printout("Done.") - end - cmd = { "tree-sitter", "generate" } - local abi = os.getenv("TREE_SITTER_LANGUAGE_VERSION") - -- TODO: Check for tree-sitter CLI version - if abi then - table.insert(cmd, "--abi") - table.insert(cmd, abi) - end - util.printout("Generating tree-sitter sources...") - if not fs.execute(table.concat(cmd, " ")) then - return nil, "Failed to generate tree-sitter grammar." - end - util.printout("Done.") - end - local incdirs = {} - for _, source in ipairs(build.sources) do - local dir = source:match("(.-)%/") - if dir then - table.insert(incdirs, dir) - end - end - rockspec.build.modules = { - ["parser." .. build.lang] = { - sources = build.sources, - incdirs = incdirs, - }, - } - return builtin.run(rockspec, no_install) -end - -return tree_sitter +-- Sometimes, luarocks will automatically search for a rock called "luarocks-build-", +-- so the build type had to be changed to treesitter-parser. +-- This module ensures that working rockspecs that use "tree-sitter" as a build type still work. +return require("luarocks.build.treesitter-parser") diff --git a/src/luarocks/build/treesitter-parser.lua b/src/luarocks/build/treesitter-parser.lua new file mode 100644 index 0000000..dc95307 --- /dev/null +++ b/src/luarocks/build/treesitter-parser.lua @@ -0,0 +1,98 @@ +---@diagnostic disable: inject-field +local fs = require("luarocks.fs") +local builtin = require("luarocks.build.builtin") +local util = require("luarocks.util") + +local treesitter_parser = {} + +---@class RockSpec +---@field package string +---@field build BuildSpec + +---@class BuildSpec +---@field type string + +---@class TreeSitterRockSpec: RockSpec +---@field type fun():string +---@field build TreeSitterBuildSpec + +---@class TreeSitterBuildSpec: BuildSpec +---@field lang string +---@field sources string[] +---@field generate_requires_npm? boolean +---@field generate_from_grammar? boolean +---@field location? string + +---@param rockspec table +---@param no_install boolean +function treesitter_parser.run(rockspec, no_install) + assert(rockspec:type() == "rockspec") + ---@cast rockspec RockSpec + assert(rockspec.build.type == "treesitter-parser" or rockspec.build.type == "tree-sitter") + ---@cast rockspec TreeSitterRockSpec + + local build = rockspec.build + + if build.generate_requires_npm and not fs.is_tool_available("npm", "npm") then + return nil, "'npm' is not installed.\n" .. rockspec.package .. " requires npm to build.\n" + end + if build.generate_from_grammar and not fs.is_tool_available("tree-sitter", "tree-sitter CLI") then + return nil, + "'tree-sitter CLI' is not installed.\n" .. rockspec.package .. " requires the tree-sitter CLI to build.\n" + end + if build.generate_from_grammar then + local js_runtime = os.getenv("TREE_SITTER_JS_RUNTIME") or "node" + local js_runtime_name = js_runtime == "node" and "Node JS" or js_runtime + if not fs.is_tool_available(js_runtime, js_runtime_name) then + return nil, + ("'%s' is not installed.\n%s requires %s to build."):format( + js_runtime, + rockspec.package, + js_runtime_name + ) + end + end + if build.location then + util.printout("Changing to directory: " .. build.location) + fs.change_dir(build.location) + end + if build.generate_from_grammar then + local cmd + if build.generate_requires_npm then + cmd = { "npm", "install" } + util.printout("Installing npm dependencies...") + if not fs.execute(table.concat(cmd, " ")) then + return nil, "Failed to install npm dependencies." + end + util.printout("Done.") + end + cmd = { "tree-sitter", "generate" } + local abi = os.getenv("TREE_SITTER_LANGUAGE_VERSION") + -- TODO: Check for tree-sitter CLI version + if abi then + table.insert(cmd, "--abi") + table.insert(cmd, abi) + end + util.printout("Generating tree-sitter sources...") + if not fs.execute(table.concat(cmd, " ")) then + return nil, "Failed to generate tree-sitter grammar." + end + util.printout("Done.") + end + local incdirs = {} + for _, source in ipairs(build.sources) do + local dir = source:match("(.-)%/") + if dir then + table.insert(incdirs, dir) + end + end + rockspec.build.modules = { + ["parser." .. build.lang] = { + sources = build.sources, + incdirs = incdirs, + }, + } + return builtin.run(rockspec, no_install) +end + +return treesitter_parser