-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: name build type
treesitter-parser
by default
For backward compatibility, we keep the `tree-sitter` module.
- Loading branch information
Showing
7 changed files
with
107 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |