From 151d06b9e19f2664e23787bf60ea46bf62cc87f2 Mon Sep 17 00:00:00 2001 From: V1K1NGbg Date: Sun, 4 Aug 2024 14:28:12 +0300 Subject: type_check and rockspec --- src/luarocks/type/rockspec.tl | 352 ++++++++++++++++++++++++++++++------------ src/luarocks/type_check.tl | 2 +- 2 files changed, 253 insertions(+), 101 deletions(-) (limited to 'src') diff --git a/src/luarocks/type/rockspec.tl b/src/luarocks/type/rockspec.tl index 4b9aecf9..365f5d3d 100644 --- a/src/luarocks/type/rockspec.tl +++ b/src/luarocks/type/rockspec.tl @@ -1,8 +1,11 @@ local record type_rockspec - + order: util.Ordering end local type_check = require("luarocks.type_check") +local util = require("luarocks.core.util") --! + +local type TableSchema = type_check.TableSchema type_rockspec.rockspec_format = "3.0" @@ -19,121 +22,271 @@ type_rockspec.rockspec_format = "3.0" -- _more (boolean) indicates that the table accepts unspecified keys and does not type-check them. -- Any other string keys that don't start with an underscore represent known keys and are type-checking tables, recursively checked. +-- local rockspec_formats, versions = type_check.declare_schemas({ +-- ["1.0"] = { +-- rockspec_format = { _type = "string" }, +-- package = { _type = "string", _mandatory = true }, +-- version = { _type = "string", _pattern = "[%w.]+-[%d]+", _mandatory = true }, +-- description = { +-- summary = { _type = "string" }, +-- detailed = { _type = "string" }, +-- homepage = { _type = "string" }, +-- license = { _type = "string" }, +-- maintainer = { _type = "string" }, +-- }, +-- dependencies = { +-- platforms = type_check.MAGIC_PLATFORMS, +-- _any = { +-- _type = "string", +-- _name = "a valid dependency string", +-- _pattern = "%s*([a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", +-- }, +-- }, +-- supported_platforms = { +-- _any = { _type = "string" }, +-- }, +-- external_dependencies = { +-- platforms = type_check.MAGIC_PLATFORMS, +-- _any = { +-- program = { _type = "string" }, +-- header = { _type = "string" }, +-- library = { _type = "string" }, +-- } +-- }, +-- source = { +-- _mandatory = true, +-- platforms = type_check.MAGIC_PLATFORMS, +-- url = { _type = "string", _mandatory = true }, +-- md5 = { _type = "string" }, +-- file = { _type = "string" }, +-- dir = { _type = "string" }, +-- tag = { _type = "string" }, +-- branch = { _type = "string" }, +-- module = { _type = "string" }, +-- cvs_tag = { _type = "string" }, +-- cvs_module = { _type = "string" }, +-- }, +-- build = { +-- platforms = type_check.MAGIC_PLATFORMS, +-- type = { _type = "string" }, +-- install = { +-- lua = { +-- _more = true +-- }, +-- lib = { +-- _more = true +-- }, +-- conf = { +-- _more = true +-- }, +-- bin = { +-- _more = true +-- } +-- }, +-- copy_directories = { +-- _any = { _type = "string" }, +-- }, +-- _more = true, +-- _mandatory = true +-- }, +-- hooks = { +-- platforms = type_check.MAGIC_PLATFORMS, +-- post_install = { _type = "string" }, +-- }, +-- }, + +-- ["1.1"] = { +-- deploy = { +-- wrap_bin_scripts = { _type = "boolean" }, +-- } +-- }, + +-- ["3.0"] = { +-- description = { +-- labels = { +-- _any = { _type = "string" } +-- }, +-- issues_url = { _type = "string" }, +-- }, +-- dependencies = { +-- _any = { +-- _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", +-- }, +-- }, +-- build_dependencies = { +-- platforms = type_check.MAGIC_PLATFORMS, +-- _any = { +-- _type = "string", +-- _name = "a valid dependency string", +-- _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", +-- }, +-- }, +-- test_dependencies = { +-- platforms = type_check.MAGIC_PLATFORMS, +-- _any = { +-- _type = "string", +-- _name = "a valid dependency string", +-- _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", +-- }, +-- }, +-- build = { +-- _mandatory = false, +-- }, +-- test = { +-- platforms = type_check.MAGIC_PLATFORMS, +-- type = { _type = "string" }, +-- _more = true, +-- }, +-- } +-- }) + local rockspec_formats, versions = type_check.declare_schemas({ ["1.0"] = { - rockspec_format = { _type = "string" }, - package = { _type = "string", _mandatory = true }, - version = { _type = "string", _pattern = "[%w.]+-[%d]+", _mandatory = true }, - description = { - summary = { _type = "string" }, - detailed = { _type = "string" }, - homepage = { _type = "string" }, - license = { _type = "string" }, - maintainer = { _type = "string" }, - }, - dependencies = { - platforms = type_check.MAGIC_PLATFORMS, - _any = { - _type = "string", - _name = "a valid dependency string", - _pattern = "%s*([a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", - }, - }, - supported_platforms = { - _any = { _type = "string" }, - }, - external_dependencies = { - platforms = type_check.MAGIC_PLATFORMS, - _any = { - program = { _type = "string" }, - header = { _type = "string" }, - library = { _type = "string" }, - } - }, - source = { - _mandatory = true, - platforms = type_check.MAGIC_PLATFORMS, - url = { _type = "string", _mandatory = true }, - md5 = { _type = "string" }, - file = { _type = "string" }, - dir = { _type = "string" }, - tag = { _type = "string" }, - branch = { _type = "string" }, - module = { _type = "string" }, - cvs_tag = { _type = "string" }, - cvs_module = { _type = "string" }, - }, - build = { - platforms = type_check.MAGIC_PLATFORMS, - type = { _type = "string" }, - install = { - lua = { - _more = true + fields = { + rockspec_format = { _type = "string" }, + package = { _type = "string", _mandatory = true }, + version = { _type = "string", _pattern = "[%w.]+-[%d]+", _mandatory = true }, + description = { + fields = { + summary = { _type = "string" }, + detailed = { _type = "string" }, + homepage = { _type = "string" }, + license = { _type = "string" }, + maintainer = { _type = "string" }, }, - lib = { - _more = true + }, + dependencies = { + fields = { + platforms = type_check.MAGIC_PLATFORMS, }, - conf = { - _more = true + _any = { + _type = "string", + _name = "a valid dependency string", + _pattern = "%s*([a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", }, - bin = { - _more = true - } }, - copy_directories = { + supported_platforms = { _any = { _type = "string" }, }, - _more = true, - _mandatory = true - }, - hooks = { - platforms = type_check.MAGIC_PLATFORMS, - post_install = { _type = "string" }, + external_dependencies = { + fields = { + platforms = type_check.MAGIC_PLATFORMS, + }, + _any = { + fields = { + program = { _type = "string" }, + header = { _type = "string" }, + library = { _type = "string" }, + } + }, + }, + source = { + _mandatory = true, + fields = { + platforms = type_check.MAGIC_PLATFORMS, + url = { _type = "string", _mandatory = true }, + md5 = { _type = "string" }, + file = { _type = "string" }, + dir = { _type = "string" }, + tag = { _type = "string" }, + branch = { _type = "string" }, + module = { _type = "string" }, + cvs_tag = { _type = "string" }, + cvs_module = { _type = "string" }, + }, + }, + build = { + fields = { + platforms = type_check.MAGIC_PLATFORMS, + type = { _type = "string" }, + install = { + fields = { + lua = { + _more = true + }, + lib = { + _more = true + }, + conf = { + _more = true + }, + bin = { + _more = true + } + } + }, + copy_directories = { + _any = { _type = "string" }, + }, + }, + _more = true, + _mandatory = true + }, + hooks = { + fields = { + platforms = type_check.MAGIC_PLATFORMS, + post_install = { _type = "string" }, + } + }, }, }, ["1.1"] = { - deploy = { - wrap_bin_scripts = { _type = "boolean" }, - } + fields = { + deploy = { + fields = { + wrap_bin_scripts = { _type = "boolean" }, + }, + } + }, }, ["3.0"] = { - description = { - labels = { - _any = { _type = "string" } + fields = { + description = { + fields = { + labels = { + _any = { _type = "string" } + }, + issues_url = { _type = "string" }, + }, }, - issues_url = { _type = "string" }, - }, - dependencies = { - _any = { - _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", + dependencies = { + _any = { + _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", + }, }, - }, - build_dependencies = { - platforms = type_check.MAGIC_PLATFORMS, - _any = { - _type = "string", - _name = "a valid dependency string", - _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", + build_dependencies = { + fields = { + platforms = type_check.MAGIC_PLATFORMS, + }, + _any = { + _type = "string", + _name = "a valid dependency string", + _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", + }, }, - }, - test_dependencies = { - platforms = type_check.MAGIC_PLATFORMS, - _any = { - _type = "string", - _name = "a valid dependency string", - _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", + test_dependencies = { + fields = { + platforms = type_check.MAGIC_PLATFORMS, + }, + _any = { + _type = "string", + _name = "a valid dependency string", + _pattern = "%s*([a-zA-Z0-9%.%-%_]*/?[a-zA-Z0-9][a-zA-Z0-9%.%-%_]*)%s*([^/]*)", + }, }, - }, - build = { - _mandatory = false, - }, - test = { - platforms = type_check.MAGIC_PLATFORMS, - type = { _type = "string" }, - _more = true, - }, + build = { + _mandatory = false, + }, + test = { + fields = { + platforms = type_check.MAGIC_PLATFORMS, + type = { _type = "string" }, + }, + _more = true, + }, + } } }) @@ -167,7 +320,7 @@ type_rockspec.order = { } } -local function check_rockspec_using_version(rockspec, globals, version) +local function check_rockspec_using_version(rockspec: {any: any}, globals: {string: any}, version: string): boolean, string local schema = rockspec_formats[version] if not schema then return nil, "unknown rockspec format " .. version @@ -189,8 +342,7 @@ end -- mismatches. -- @return boolean or (nil, string): true if type checking -- succeeded, or nil and an error message if it failed. -function type_rockspec.check(rockspec, globals) - assert(type(rockspec) == "table") +function type_rockspec.check(rockspec: {any: any}, globals: {string: any}): boolean, string local version = rockspec.rockspec_format or "1.0" local ok, err = check_rockspec_using_version(rockspec, globals, version) diff --git a/src/luarocks/type_check.tl b/src/luarocks/type_check.tl index d36debc4..1ff490b8 100644 --- a/src/luarocks/type_check.tl +++ b/src/luarocks/type_check.tl @@ -207,7 +207,7 @@ function type_check.type_check_table(version: string, tbl: {any: any}, typetbl: return true end -function type_check.check_undeclared_globals(globals: {string: any}, typetbl: {string: string}): boolean, string --! tbl and typetbl types +function type_check.check_undeclared_globals(globals: {string: any}, typetbl: TableSchema): boolean, string --! tbl and typetbl types local undeclared = {} for glob, _ in pairs(globals) do if not (typetbl[glob] or typetbl["MUST_"..glob]) then -- cgit v1.2.3-55-g6feb