-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsconstruct
More file actions
177 lines (143 loc) · 6.69 KB
/
Copy pathsconstruct
File metadata and controls
177 lines (143 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# NVDA add-on template SCONSTRUCT file
# Copyright (C) 2012-2025 Rui Batista, Noelia Martinez, Joseph Lee
# This file is covered by the GNU General Public License.
# See the file COPYING.txt for more details.
import os
import os.path
import sys
from pathlib import Path
from collections.abc import Iterable
from typing import Any, Final
from SCons.Script import EnsurePythonVersion, Variables, BoolVariable, Environment, Copy
from SCons.Node import FS
EnsurePythonVersion(3, 10)
sys.dont_write_bytecode = True
import buildVars # NOQA: E402
def validateVersionNumber(key: str, val: str, _):
# Used to make sure version major.minor.patch are integers to comply with NV Access add-on store.
# Ignore all this if version number is not specified.
if val == "0.0.0":
return
versionNumber = val.split(".")
if len(versionNumber) < 3:
raise ValueError(f"{key} must have three parts (major.minor.patch)")
if not all([part.isnumeric() for part in versionNumber]):
raise ValueError(f"{key} (major.minor.patch) must be integers")
def expandGlobs(patterns: Iterable[str], rootdir: Path = Path(".")) -> list[FS.Entry]:
return [env.Entry(e) for pattern in patterns for e in rootdir.glob(pattern.lstrip('/'))]
def _expandTemplateMarkdown(source: Path, values: dict[str, Any]) -> Path:
target = Path(str(source).replace(".tpl.md", ".md"))
content = source.read_text(encoding="utf-8")
for k, v in values.items():
content = content.replace(f"${{{k}}}", "" if v is None else str(v))
target.write_text(content, encoding="utf-8")
return target
addonDir: Final = Path("addon/")
localeDir: Final = addonDir / "locale"
docsDir: Final = addonDir / "doc"
vars = Variables()
vars.Add("version", "The version of this build", buildVars.addon_info["addon_version"])
vars.Add("versionNumber", "Version number of the form major.minor.patch", "0.0.0", validateVersionNumber)
vars.Add(BoolVariable("dev", "Whether this is a daily development version", False))
vars.Add("channel", "Update channel for this build", buildVars.addon_info["addon_updateChannel"])
env = Environment(variables=vars, ENV=os.environ, tools=["gettexttool", "NVDATool"])
env.Append(
addon_info=buildVars.addon_info,
brailleTables=buildVars.brailleTables,
symbolDictionaries=buildVars.symbolDictionaries,
)
if env["dev"]:
from datetime import date
versionTimestamp = date.today().strftime('%Y%m%d')
version = f"{versionTimestamp}.0.0"
env["addon_info"]["addon_version"] = version
env["versionNumber"] = version
env["channel"] = "dev"
elif env["version"] is not None:
env["addon_info"]["addon_version"] = env["version"]
if "channel" in env and env["channel"] is not None:
env["addon_info"]["addon_updateChannel"] = env["channel"]
# This is necessary for further use in formatting file names.
env.Append(**env["addon_info"])
addonFile = env.File("${addon_name}-${addon_version}.nvda-addon")
addon = env.NVDAAddon(addonFile, env.Dir(addonDir), excludePatterns=buildVars.excludedFiles)
langDirs: list[FS.Dir] = [env.Dir(d) for d in env.Glob(localeDir / "*/") if d.isdir()]
# Allow all NVDA's gettext po files to be compiled in source/locale, and manifest files to be generated
moByLang: dict[str, FS.File] = {}
for dir in langDirs:
poFile = dir.File(os.path.join("LC_MESSAGES", "nvda.po"))
moTarget = env.gettextMoFile(poFile)
moFile = env.File(moTarget[0])
moByLang[dir.name] = moFile
env.Depends(moTarget, poFile)
translatedManifest = env.NVDATranslatedManifest(
dir.File("manifest.ini"), [moFile, "manifest-translated.ini.tpl"]
)
env.Depends(translatedManifest, ["buildVars.py"])
env.Depends(addon, [translatedManifest, moTarget])
pythonFiles = expandGlobs(buildVars.pythonSources)
for file in pythonFiles:
env.Depends(addon, file)
# Convert markdown files to html
# We need at least doc in English and should enable the Help button for the add-on in Add-ons Manager
if (cssFile := Path("style.css")).is_file():
cssPath = docsDir / cssFile
cssTarget = env.Command(str(cssPath), str(cssFile), Copy("$TARGET", "$SOURCE"))
env.Depends(addon, cssTarget)
# Keep support for root README.tpl.md/readme.tpl.md used by this repository.
rootReadmeTemplate = next(
(p for p in (Path("README.tpl.md"), Path("readme.tpl.md")) if p.is_file()),
None,
)
if rootReadmeTemplate is not None:
rootReadme = _expandTemplateMarkdown(rootReadmeTemplate, env["addon_info"])
else:
rootReadme = next((p for p in (Path("README.md"), Path("readme.md")) if p.is_file()), None)
if rootReadme is not None:
# Keep add-on docs filename stable regardless of root README case.
readmePath = docsDir / buildVars.baseLanguage / "readme.md"
readmeTarget = env.Command(str(readmePath), str(rootReadme), Copy("$TARGET", "$SOURCE"))
env.Depends(addon, readmeTarget)
if (rootContributing := Path("contributing.md")).is_file():
contributingPath = docsDir / buildVars.baseLanguage / "contributing.md"
contributingTarget = env.Command(
str(contributingPath),
str(rootContributing),
Copy("$TARGET", "$SOURCE"),
)
env.Depends(addon, contributingTarget)
# Keep support for localized *.tpl.md docs.
for tplMd in env.Glob(docsDir / "*/*.tpl.md"):
_expandTemplateMarkdown(Path(str(tplMd)), env["addon_info"])
for mdFile in env.Glob(docsDir / "*/*.md"):
if str(mdFile).endswith(".tpl.md"):
continue
# the title of the html file is translated based on the contents of something in the moFile for a language.
# Thus, we find the moFile for this language and depend on it if it exists.
lang = mdFile.dir.name
moFile = moByLang.get(lang)
htmlFile = env.md2html(mdFile, moFile=moFile, mdExtensions=buildVars.markdownExtensions)
env.Depends(htmlFile, mdFile)
if moFile:
env.Depends(htmlFile, moFile)
env.Depends(addon, htmlFile)
# Pot target
i18nFiles = expandGlobs(buildVars.i18nSources)
gettextvars: dict[str, str] = {
"gettext_package_bugs_address": "nvda-translations@groups.io",
"gettext_package_name": buildVars.addon_info["addon_name"],
"gettext_package_version": buildVars.addon_info["addon_version"],
}
pot = env.gettextPotFile("${addon_name}.pot", i18nFiles, **gettextvars)
env.Alias("pot", pot)
env.Depends(pot, i18nFiles)
mergePot = env.gettextMergePotFile("${addon_name}-merge.pot", i18nFiles, **gettextvars)
env.Alias("mergePot", mergePot)
env.Depends(mergePot, i18nFiles)
# Generate Manifest path
manifest = env.NVDAManifest(env.File(addonDir / "manifest.ini"), "manifest.ini.tpl")
# Ensure manifest is rebuilt if buildVars is updated.
env.Depends(manifest, "buildVars.py")
env.Depends(addon, manifest)
env.Default(addon)
env.Clean(addon, [".sconsign.dblite", "addon/doc/" + buildVars.baseLanguage + "/"])