88import org .antlr .v4 .test .runtime .*;
99import org .antlr .v4 .test .runtime .states .CompiledState ;
1010import org .antlr .v4 .test .runtime .states .GeneratedState ;
11- import org .stringtemplate .v4 .ST ;
1211
1312import java .io .File ;
14- import java .io .IOException ;
1513import java .nio .file .Files ;
16- import java .nio .file .Paths ;
1714
1815import static org .antlr .v4 .test .runtime .FileUtils .writeFile ;
19- import static org .antlr .v4 .test .runtime .RuntimeTestUtils .TempDirectory ;
16+ import static org .antlr .v4 .test .runtime .RuntimeTestUtils .isWindows ;
2017
2118public class TscRunner extends RuntimeRunner {
2219
2320 /* TypeScript runtime is the same as JavaScript runtime */
2421 private final static String NORMALIZED_JAVASCRIPT_RUNTIME_PATH = getRuntimePath ("JavaScript" ).replace ('\\' , '/' );
22+ private final static String NPM_EXEC = "npm" + (isWindows () ? ".cmd" : "" );
23+ private final static String TSC_EXEC = "tsc" + (isWindows () ? ".cmd" : "" );
2524
2625 @ Override
2726 public String getLanguage () {
2827 return "TypeScript" ;
2928 }
3029
30+
3131 @ Override
3232 protected void initRuntime () throws Exception {
33- installTsc ();
34- npmLinkRuntime ();
33+ synchronized (this .getClass ()) {
34+ installTsc ();
35+ npmLinkRuntime ();
36+ }
3537 }
3638
3739 private void installTsc () throws Exception {
38- Processor .run (new String [] {"npm" , "--silent" , "install" , "-g" , "typescript" }, NORMALIZED_JAVASCRIPT_RUNTIME_PATH );
40+ Processor .run (new String [] {NPM_EXEC , "--silent" , "install" , "-g" , "typescript" }, null );
3941 }
4042
4143 private void npmLinkRuntime () throws Exception {
42- File dir = new File (NORMALIZED_JAVASCRIPT_RUNTIME_PATH );
43- if (!dir .exists ())
44- throw new RuntimeException ("Can't locate JavaScript runtime!" );
45- Processor .run (new String [] {"npm" , "--silent" , "link" }, NORMALIZED_JAVASCRIPT_RUNTIME_PATH );
44+ Processor .run (new String [] {NPM_EXEC , "--silent" , "install" }, NORMALIZED_JAVASCRIPT_RUNTIME_PATH );
45+ Processor .run (new String [] {NPM_EXEC , "--silent" , "link" }, NORMALIZED_JAVASCRIPT_RUNTIME_PATH );
4646 }
4747
4848 @ Override
@@ -98,7 +98,7 @@ private void npmInstall() throws Exception {
9898 }
9999
100100 private void npmInstallNormally () throws Exception {
101- Processor .run (new String [] {"npm" , "--silent" , "install" }, getTempDirPath ());
101+ Processor .run (new String [] {NPM_EXEC , "--silent" , "install" }, getTempDirPath ());
102102 }
103103
104104 private void npmInstallUsingCache () throws Exception {
@@ -115,7 +115,7 @@ private void linkNodeModulesToNodeModulesCache() throws Exception {
115115
116116 private void createNodeModulesCache () throws Exception {
117117 if (cached_node_modules_dir == null ) {
118- Processor .run (new String [] {"npm" , "--silent" , "install" }, getTempDirPath ());
118+ Processor .run (new String [] {NPM_EXEC , "--silent" , "install" }, getTempDirPath ());
119119 String parentDir = Files .createTempDirectory ("TscRunner-cached-node_modules-" ).toFile ().getAbsolutePath ();
120120 Processor .run (new String [] {"mv" , "node_modules" , parentDir }, getTempDirPath ());
121121 cached_node_modules_dir = parentDir + "/node_modules" ;
@@ -131,15 +131,16 @@ private void checkValidNodeModulesCache() {
131131 }
132132
133133 private void npmLinkAntlr4 () throws Exception {
134- Processor .run (new String [] {"npm" , "--silent" , "link" , "antlr4" }, getTempDirPath ());
134+ Processor .run (new String [] {NPM_EXEC , "--silent" , "link" , "antlr4" }, getTempDirPath ());
135135 }
136136
137- private void tscCompile () throws Exception {
138- Processor .run (new String [] {"tsc" , "--project " , "tsconfig.json " }, getTempDirPath ());
137+ private void npmUnlinkAntlr4 () throws Exception {
138+ Processor .run (new String [] {NPM_EXEC , "--silent " , "unlink" , "antlr4 " }, getTempDirPath ());
139139 }
140140
141- @ Override
142- protected void addExtraRecognizerParameters (ST template ) {
143- template .add ("runtimePath" , NORMALIZED_JAVASCRIPT_RUNTIME_PATH );
141+ private void tscCompile () throws Exception {
142+ Processor .run (new String [] {TSC_EXEC , "--project" , "tsconfig.json" }, getTempDirPath ());
144143 }
144+
145+
145146}
0 commit comments