Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9668108

Browse files
committedDec 12, 2024·
Removed allowed languages notion, now preffered language can be obtained automatically
- Completely removed `denyJavaScript` logic, now you cannot compile everything with TypeScript until it's not directly stated in config. - On flip side, you can disallow use of TypeScript. If project contains only JavaScript files or/and declarations, toolchain will try to build it even if TypeScript language is specified directly.
1 parent 14d31eb commit 9668108

File tree

2 files changed

+47
-49
lines changed

2 files changed

+47
-49
lines changed
 

‎toolchain/toolchain/python/icmtoolchain/script_build.py

+45-46
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,39 @@
44

55
from . import GLOBALS, PROPERTIES
66
from .includes import Includes
7-
from .shell import abort, debug, error, info, warn
8-
from .utils import (copy_directory, copy_file, ensure_directory, remove_tree,
9-
request_tool, request_typescript)
7+
from .shell import debug, error, info, warn
8+
from .utils import (RuntimeCodeError, copy_directory, copy_file,
9+
ensure_directory, remove_tree, request_typescript,
10+
walk_all_files)
1011

1112
VALID_SOURCE_TYPES = ("main", "launcher", "preloader", "instant", "custom", "library")
1213
VALID_RESOURCE_TYPES = ("resource_directory", "gui", "minecraft_resource_pack", "minecraft_behavior_pack")
1314

1415

15-
def get_allowed_languages() -> List[str]:
16-
allowed_languages = list()
17-
18-
if len(GLOBALS.MAKE_CONFIG.get_filtered_list("sources", "language", ("typescript"))) > 0 or GLOBALS.PREFERRED_CONFIG.get_value("denyJavaScript", False):
19-
if request_typescript() == "typescript":
20-
allowed_languages.append("typescript")
21-
# Otherwise check tsc directly, allowing dynamically rebuilding references
22-
elif request_tool("tsc"):
23-
allowed_languages.append("typescript")
24-
25-
if not GLOBALS.PREFERRED_CONFIG.get_value("denyJavaScript", False):
26-
allowed_languages.append("javascript")
27-
28-
if len(allowed_languages) == 0:
29-
abort("TypeScript is required, if you want to build legacy JavaScript, change `denyJavaScript` property in your 'make.json' or 'toolchain.json' config.")
30-
31-
return allowed_languages
32-
3316
def build_all_scripts(watch: bool = False) -> int:
3417
GLOBALS.MOD_STRUCTURE.cleanup_build_target("script_source")
3518
GLOBALS.MOD_STRUCTURE.cleanup_build_target("script_library")
3619

20+
if request_typescript(only_check=True) and not exists(GLOBALS.TOOLCHAIN_CONFIG.get_path("toolchain/declarations")):
21+
warn("Not found 'toolchain/declarations', in most cases build will be failed, please install it via tasks.")
22+
3723
overall_result = 0
3824
for source in GLOBALS.MAKE_CONFIG.get_value("sources", list()):
39-
if "source" not in source or "language" not in source or "type" not in source:
40-
error(f"Skipped invalid source json {source!r}, it might contain `source`, `type` and `language` properties!")
25+
if "source" not in source or "type" not in source:
26+
error(f"Invalid source json {source!r}, it might contain `source` and `type` properties!")
4127
overall_result = 1
4228
continue
4329
if source["type"] not in VALID_SOURCE_TYPES:
4430
error(f"Invalid script `type` in source: {source['type']}, it might be one of {VALID_SOURCE_TYPES}.")
4531
overall_result = 1
32+
if "language" in source:
33+
if not source["language"] in ("javascript", "typescript"):
34+
error(f"Invalid source `language` property: {source['language']}, it should be 'javascript' or 'typescript'!")
35+
overall_result = 1
4636
if overall_result != 0:
4737
return overall_result
4838

49-
allowed_languages = get_allowed_languages()
50-
if "typescript" in allowed_languages and not exists(GLOBALS.TOOLCHAIN_CONFIG.get_path("toolchain/declarations")):
51-
warn("Not found 'toolchain/declarations', in most cases build will be failed, please install it via tasks.")
52-
53-
overall_result += build_composite_project(allowed_languages) \
54-
if not watch else watch_composite_project(allowed_languages)
39+
overall_result += build_composite_project() if not watch else watch_composite_project()
5540
return overall_result
5641

5742
def rebuild_build_target(source, target_path: str) -> str:
@@ -80,7 +65,7 @@ def do_sorting(a: Dict[Any, Any], b: Dict[Any, Any]) -> int:
8065
lb = b["type"] == "library"
8166
return 0 if la == lb else -1 if la else 1
8267

83-
def compute_and_capture_changed_scripts(allowed_languages: List[str] = ["typescript"]) -> Tuple[List[Tuple[str, str, str]], List[Tuple[str, str, str]], List[Tuple[Includes, str, str]], List[Tuple[str, str]]]:
68+
def compute_and_capture_changed_scripts() -> Tuple[List[Tuple[str, str, str]], List[Tuple[str, str, str]], List[Tuple[Includes, str, str]], List[Tuple[str, str]]]:
8469
composite = list()
8570
computed_composite = list()
8671
includes = list()
@@ -89,18 +74,34 @@ def compute_and_capture_changed_scripts(allowed_languages: List[str] = ["typescr
8974
for source in sorted(GLOBALS.MAKE_CONFIG.get_value("sources", list()), key=cmp_to_key(do_sorting)):
9075
make = source["includes"] if "includes" in source else ".includes"
9176
preffered_language = source["language"] if "language" in source else None
92-
language = preffered_language if preffered_language \
93-
and source["language"] in allowed_languages else allowed_languages[0]
9477

9578
for source_path in GLOBALS.MAKE_CONFIG.get_paths(source["source"]):
9679
if not exists(source_path):
97-
warn(f"* Skipped non-existing source {source['source']!r}!", sep="")
80+
warn(f"* Skipped non-existing source {GLOBALS.MAKE_CONFIG.get_relative_path(source_path)!r}!")
9881
continue
9982

10083
# Supports assembling directories, JavaScript and TypeScript
101-
if not (isdir(source_path) or source_path.endswith(".js") or source_path.endswith(".ts")):
84+
preffered_javascript = source_path.endswith(".js")
85+
preffered_typescript = source_path.endswith(".ts")
86+
if not (isdir(source_path) or preffered_javascript or preffered_typescript):
87+
warn(f"Unsupported script {GLOBALS.MAKE_CONFIG.get_relative_path(source_path)!r}, it should be directory with includes or Java/TypeScript file!")
10288
continue
10389

90+
language = preffered_language or ("javascript" if preffered_javascript else "typescript" if preffered_typescript else None)
91+
typescript_directory = False
92+
if isdir(source_path):
93+
try:
94+
def walk(file: str) -> None:
95+
if file.endswith(".ts") and not file.endswith(".d.ts"):
96+
raise RuntimeError()
97+
walk_all_files(source_path, walk)
98+
except RuntimeError:
99+
typescript_directory = True
100+
language = language or ("typescript" if typescript_directory else "javascript")
101+
102+
if language == "typescript" and (preffered_typescript or typescript_directory) and not request_typescript():
103+
raise RuntimeCodeError(255, "It is not possible to compile TypeScript without having TypeScript Compiler, please install Node.js and do installation again.")
104+
104105
# Using template <sourceName>.<extension> -> <sourceName>, e.g. main.js -> main
105106
if "target" not in source:
106107
target_path = basename(source_path)
@@ -124,7 +125,7 @@ def compute_and_capture_changed_scripts(allowed_languages: List[str] = ["typescr
124125
if isdir(source_path):
125126
include = Includes.invalidate(source_path, make)
126127
# Computing in any case, tsconfig normalises environment usage
127-
if include.compute(destination_path, "typescript" if "typescript" in allowed_languages and not appending_library else "javascript"):
128+
if include.compute(destination_path, "typescript" if not appending_library else "javascript"):
128129
includes.append((
129130
include,
130131
destination_path,
@@ -191,21 +192,20 @@ def copy_build_targets(composite: List[Tuple[str, str, str]], includes: List[Tup
191192

192193
GLOBALS.BUILD_STORAGE.save()
193194

194-
def build_composite_project(allowed_languages: List[str] = ["typescript"]) -> int:
195+
def build_composite_project() -> int:
195196
overall_result = 0
196197

197-
composite, computed_composite, includes, computed_includes = \
198-
compute_and_capture_changed_scripts(allowed_languages)
198+
composite, computed_composite, includes, computed_includes = compute_and_capture_changed_scripts()
199199

200-
if "typescript" in allowed_languages:
200+
if request_typescript(only_check=True):
201201
GLOBALS.WORKSPACE_COMPOSITE.flush()
202202
for included in includes:
203203
if not GLOBALS.MAKE_CONFIG.get_value("project.useReferences", False) or included[2] == "javascript":
204204
overall_result += included[0].build(included[1], included[2])
205205
if overall_result != 0:
206206
return overall_result
207207

208-
if "typescript" in allowed_languages \
208+
if request_typescript(only_check=True) \
209209
and (GLOBALS.MAKE_CONFIG.get_value("project.composite", True) \
210210
or GLOBALS.MAKE_CONFIG.get_value("project.useReferences", False)):
211211

@@ -250,23 +250,22 @@ def build_composite_project(allowed_languages: List[str] = ["typescript"]) -> in
250250
GLOBALS.MOD_STRUCTURE.update_build_config_list("compile")
251251
return overall_result
252252

253-
def watch_composite_project(allowed_languages: List[str] = ["typescript"]) -> int:
254-
if not "typescript" in allowed_languages:
255-
error("Watching is not supported for legacy JavaScript!")
253+
def watch_composite_project() -> int:
254+
if not request_typescript():
255+
error("* Watching is not supported for legacy JavaScript!")
256256
return 1
257257
overall_result = 0
258258

259259
# Recomputing existing changes before watching, changes here doesn't make sence
260260
# since it will be recomputed after watching interruption
261-
compute_and_capture_changed_scripts(allowed_languages)
261+
compute_and_capture_changed_scripts()
262262
GLOBALS.WORKSPACE_COMPOSITE.flush()
263263
GLOBALS.WORKSPACE_COMPOSITE.watch()
264264
GLOBALS.MOD_STRUCTURE.cleanup_build_target("script_source")
265265
GLOBALS.MOD_STRUCTURE.cleanup_build_target("script_library")
266266
GLOBALS.WORKSPACE_COMPOSITE.reset()
267267

268-
composite, computed_composite, includes, computed_includes = \
269-
compute_and_capture_changed_scripts(allowed_languages)
268+
composite, computed_composite, includes, computed_includes = compute_and_capture_changed_scripts()
270269

271270
for included in includes:
272271
if not GLOBALS.MAKE_CONFIG.get_value("project.useReferences", False) or included[2] == "javascript":

‎toolchain/toolchain/python/icmtoolchain/task.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,8 @@ def task_watch_scripts() -> int:
214214
description="Overrides the contents of 'tsconfig.json' based on script files."
215215
)
216216
def task_update_includes() -> int:
217-
from .script_build import (compute_and_capture_changed_scripts,
218-
get_allowed_languages)
219-
compute_and_capture_changed_scripts(get_allowed_languages())
217+
from .script_build import compute_and_capture_changed_scripts
218+
compute_and_capture_changed_scripts()
220219
GLOBALS.WORKSPACE_COMPOSITE.flush()
221220
return 0
222221

0 commit comments

Comments
 (0)
Please sign in to comment.