4
4
5
5
from . import GLOBALS , PROPERTIES
6
6
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 )
10
11
11
12
VALID_SOURCE_TYPES = ("main" , "launcher" , "preloader" , "instant" , "custom" , "library" )
12
13
VALID_RESOURCE_TYPES = ("resource_directory" , "gui" , "minecraft_resource_pack" , "minecraft_behavior_pack" )
13
14
14
15
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
-
33
16
def build_all_scripts (watch : bool = False ) -> int :
34
17
GLOBALS .MOD_STRUCTURE .cleanup_build_target ("script_source" )
35
18
GLOBALS .MOD_STRUCTURE .cleanup_build_target ("script_library" )
36
19
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
+
37
23
overall_result = 0
38
24
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!" )
41
27
overall_result = 1
42
28
continue
43
29
if source ["type" ] not in VALID_SOURCE_TYPES :
44
30
error (f"Invalid script `type` in source: { source ['type' ]} , it might be one of { VALID_SOURCE_TYPES } ." )
45
31
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
46
36
if overall_result != 0 :
47
37
return overall_result
48
38
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 ()
55
40
return overall_result
56
41
57
42
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:
80
65
lb = b ["type" ] == "library"
81
66
return 0 if la == lb else - 1 if la else 1
82
67
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 ]]]:
84
69
composite = list ()
85
70
computed_composite = list ()
86
71
includes = list ()
@@ -89,18 +74,34 @@ def compute_and_capture_changed_scripts(allowed_languages: List[str] = ["typescr
89
74
for source in sorted (GLOBALS .MAKE_CONFIG .get_value ("sources" , list ()), key = cmp_to_key (do_sorting )):
90
75
make = source ["includes" ] if "includes" in source else ".includes"
91
76
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 ]
94
77
95
78
for source_path in GLOBALS .MAKE_CONFIG .get_paths (source ["source" ]):
96
79
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} !" )
98
81
continue
99
82
100
83
# 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!" )
102
88
continue
103
89
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
+
104
105
# Using template <sourceName>.<extension> -> <sourceName>, e.g. main.js -> main
105
106
if "target" not in source :
106
107
target_path = basename (source_path )
@@ -124,7 +125,7 @@ def compute_and_capture_changed_scripts(allowed_languages: List[str] = ["typescr
124
125
if isdir (source_path ):
125
126
include = Includes .invalidate (source_path , make )
126
127
# 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" ):
128
129
includes .append ((
129
130
include ,
130
131
destination_path ,
@@ -191,21 +192,20 @@ def copy_build_targets(composite: List[Tuple[str, str, str]], includes: List[Tup
191
192
192
193
GLOBALS .BUILD_STORAGE .save ()
193
194
194
- def build_composite_project (allowed_languages : List [ str ] = [ "typescript" ] ) -> int :
195
+ def build_composite_project () -> int :
195
196
overall_result = 0
196
197
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 ()
199
199
200
- if "typescript" in allowed_languages :
200
+ if request_typescript ( only_check = True ) :
201
201
GLOBALS .WORKSPACE_COMPOSITE .flush ()
202
202
for included in includes :
203
203
if not GLOBALS .MAKE_CONFIG .get_value ("project.useReferences" , False ) or included [2 ] == "javascript" :
204
204
overall_result += included [0 ].build (included [1 ], included [2 ])
205
205
if overall_result != 0 :
206
206
return overall_result
207
207
208
- if "typescript" in allowed_languages \
208
+ if request_typescript ( only_check = True ) \
209
209
and (GLOBALS .MAKE_CONFIG .get_value ("project.composite" , True ) \
210
210
or GLOBALS .MAKE_CONFIG .get_value ("project.useReferences" , False )):
211
211
@@ -250,23 +250,22 @@ def build_composite_project(allowed_languages: List[str] = ["typescript"]) -> in
250
250
GLOBALS .MOD_STRUCTURE .update_build_config_list ("compile" )
251
251
return overall_result
252
252
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!" )
256
256
return 1
257
257
overall_result = 0
258
258
259
259
# Recomputing existing changes before watching, changes here doesn't make sence
260
260
# since it will be recomputed after watching interruption
261
- compute_and_capture_changed_scripts (allowed_languages )
261
+ compute_and_capture_changed_scripts ()
262
262
GLOBALS .WORKSPACE_COMPOSITE .flush ()
263
263
GLOBALS .WORKSPACE_COMPOSITE .watch ()
264
264
GLOBALS .MOD_STRUCTURE .cleanup_build_target ("script_source" )
265
265
GLOBALS .MOD_STRUCTURE .cleanup_build_target ("script_library" )
266
266
GLOBALS .WORKSPACE_COMPOSITE .reset ()
267
267
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 ()
270
269
271
270
for included in includes :
272
271
if not GLOBALS .MAKE_CONFIG .get_value ("project.useReferences" , False ) or included [2 ] == "javascript" :
0 commit comments