Skip to content

Commit

Permalink
fix: name build type treesitter-parser by default
Browse files Browse the repository at this point in the history
For backward compatibility, we keep the `tree-sitter` module.
  • Loading branch information
mrcjkb committed Mar 14, 2024
1 parent 9eeb147 commit 28777b6
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 103 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies = {

build = {

type = "tree-sitter",
type = "treesitter-parser",

---@type string (required) Name of the language, e.g. "haskell".
lang = "LANG",
Expand Down
2 changes: 1 addition & 1 deletion fixtures/tree-sitter-ocamllex-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/tree-sitter-rust-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = {
}

build = {
type = "tree-sitter",
type = "treesitter-parser",
lang = "rust",
sources = { "src/parser.c", "src/scanner.c" },
}
2 changes: 1 addition & 1 deletion fixtures/tree-sitter-toml-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion fixtures/tree-sitter-xml-scm-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies = {
}

build = {
type = "tree-sitter",
type = "treesitter-parser",
lang = "xml",
sources = { "src/parser.c", "src/scanner.c" },
location = "xml",
Expand Down
102 changes: 4 additions & 98 deletions src/luarocks/build/tree-sitter.lua
Original file line number Diff line number Diff line change
@@ -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-<build.type>",
-- 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")
98 changes: 98 additions & 0 deletions src/luarocks/build/treesitter-parser.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 28777b6

Please sign in to comment.