-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.bat
892 lines (797 loc) · 29.3 KB
/
build.bat
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
@echo off
setlocal enabledelayedexpansion
@rem only for interactive debugging !
set _DEBUG=0
@rem #########################################################################
@rem ## Environment setup
set _EXITCODE=0
call :env
if not %_EXITCODE%==0 goto end
call :props
if not %_EXITCODE%==0 goto end
call :args %*
if not %_EXITCODE%==0 goto end
@rem #########################################################################
@rem ## Main
if %_HELP%==1 (
call :help
exit /b !_EXITCODE!
)
if %_CLEAN%==1 (
call :clean
if not !_EXITCODE!==0 goto end
)
if %_DETEKT%==1 (
call :detekt
if not !_EXITCODE!==0 goto end
)
if %_LINT%==1 (
call :lint
if not !_EXITCODE!==0 goto end
)
if %_COMPILE%==1 (
call :compile_%_TARGET%
if not !_EXITCODE!==0 goto end
)
if %_DOC%==1 (
call :doc
if not !_EXITCODE!==0 goto end
)
if %_RUN%==1 (
call :run_%_TARGET%
if not !_EXITCODE!==0 goto end
)
if %_TEST%==1 (
call :test_%_TARGET%
if not !_EXITCODE!==0 goto end
)
goto end
@rem #########################################################################
@rem ## Subroutine
@rem output parameters: _DEBUG_LABEL, _ERROR_LABEL, _WARNING_LABEL
@rem _LANGUAGE_VERSION, _MAIN_CLASS, _EXE_FILE
:env
set _BASENAME=%~n0
set "_ROOT_DIR=%~dp0"
call :env_colors
set _DEBUG_LABEL=%_NORMAL_BG_CYAN%[%_BASENAME%]%_RESET%
set _ERROR_LABEL=%_STRONG_FG_RED%Error%_RESET%:
set _WARNING_LABEL=%_STRONG_FG_YELLOW%Warning%_RESET%:
set "_SOURCE_DIR=%_ROOT_DIR%src"
set "_MAIN_SOURCE_DIR=%_SOURCE_DIR%\main\kotlin"
set "_TARGET_DIR=%_ROOT_DIR%target"
set "_CLASSES_DIR=%_TARGET_DIR%\classes"
set "_TEST_CLASSES_DIR=%_TARGET_DIR%\test-classes"
set "_TARGET_DOCS_DIR=%_TARGET_DIR%\docs"
set "_TARGET_DIR=%_ROOT_DIR%target"
set "_CLASSES_DIR=%_TARGET_DIR%\classes"
set "_TEST_CLASSES_DIR=%_TARGET_DIR%\test-classes"
set "_TARGET_DOCS_DIR=%_TARGET_DIR%\docs"
@rem https://kotlinlang.org/docs/compatibility-guide-18.html
set _LANGUAGE_VERSION=1.8
set _JAVA_MAIN_CLASS=KotlinInterop
set __MAIN_NAME=JavaInterop
set _MAIN_CLASS=%__MAIN_NAME%Kt
set "_EXE_FILE=%_TARGET_DIR%\%__MAIN_NAME%.exe"
set _DETEKT_CMD=
if exist "%DETEKT_HOME%\bin\detekt-cli.bat" (
set "_DETEKT_CMD=%DETEKT_HOME%\bin\detekt-cli.bat"
)
set _KTLINT_CMD=
if exist "%KTLINT_HOME%\bin\ktlint.bat" (
set "_KTLINT_CMD=%KTLINT_HOME%\bin\ktlint.bat"
)
if not exist "%KOTLIN_HOME%\bin\kotlinc.bat" (
echo %_ERROR_LABEL% Kotlin installation not found 1>&2
set _EXITCODE=1
goto :eof
)
set "_KOTLIN_CMD=%KOTLIN_HOME%\bin\kotlin.bat"
set "_KOTLINC_CMD=%KOTLIN_HOME%\bin\kotlinc.bat"
set "_KOTLIN_CPATH=%KOTLIN_HOME%\lib\kotlin-stdlib.jar"
if not exist "%KOTLIN_NATIVE_HOME%\bin\kotlinc-native.bat" (
echo %_ERROR_LABEL% Kotlin Native installation not found 1>&2
set _EXITCODE=1
goto :eof
)
set "_KOTLINC_NATIVE_CMD=%KOTLIN_NATIVE_HOME%\bin\kotlinc-native.bat"
if not exist "%JAVA_HOME%\bin\javac.exe" (
echo %_ERROR_LABEL% Java SDK installation not found 1>&2
set _EXITCODE=1
goto :eof
)
set "_JAVA_CMD=%JAVA_HOME%\bin\java.exe"
set "_JAVAC_CMD=%JAVA_HOME%\bin\javac.exe"
@rem use newer PowerShell version if available
where /q pwsh.exe
if %ERRORLEVEL%==0 ( set _PWSH_CMD=pwsh.exe
) else ( set _PWSH_CMD=powershell.exe
)
goto :eof
:env_colors
@rem ANSI colors in standard Windows 10 shell
@rem see https://gist.github.com/mlocati/#file-win10colors-cmd
@rem normal foreground colors
set _NORMAL_FG_BLACK=[30m
set _NORMAL_FG_RED=[31m
set _NORMAL_FG_GREEN=[32m
set _NORMAL_FG_YELLOW=[33m
set _NORMAL_FG_BLUE=[34m
set _NORMAL_FG_MAGENTA=[35m
set _NORMAL_FG_CYAN=[36m
set _NORMAL_FG_WHITE=[37m
@rem normal background colors
set _NORMAL_BG_BLACK=[40m
set _NORMAL_BG_RED=[41m
set _NORMAL_BG_GREEN=[42m
set _NORMAL_BG_YELLOW=[43m
set _NORMAL_BG_BLUE=[44m
set _NORMAL_BG_MAGENTA=[45m
set _NORMAL_BG_CYAN=[46m
set _NORMAL_BG_WHITE=[47m
@rem strong foreground colors
set _STRONG_FG_BLACK=[90m
set _STRONG_FG_RED=[91m
set _STRONG_FG_GREEN=[92m
set _STRONG_FG_YELLOW=[93m
set _STRONG_FG_BLUE=[94m
set _STRONG_FG_MAGENTA=[95m
set _STRONG_FG_CYAN=[96m
set _STRONG_FG_WHITE=[97m
@rem strong background colors
set _STRONG_BG_BLACK=[100m
set _STRONG_BG_RED=[101m
set _STRONG_BG_GREEN=[102m
set _STRONG_BG_YELLOW=[103m
set _STRONG_BG_BLUE=[104m
@rem we define _RESET in last position to avoid crazy console output with type command
set _BOLD=[1m
set _UNDERSCORE=[4m
set _INVERSE=[7m
set _RESET=[0m
goto :eof
@rem _PROJECT_NAME, _PROJECT_URL, _PROJECT_VERSION
:props
for %%i in ("%~dp0\.") do set "_PROJECT_NAME=%%~ni"
set _PROJECT_URL=github.com/%USERNAME%/kotlin-examples
set _PROJECT_VERSION=1.0-SNAPSHOT
set "__PROPS_FILE=%_ROOT_DIR%build.properties"
if exist "%__PROPS_FILE%" (
for /f "tokens=1,* delims==" %%i in (%__PROPS_FILE%) do (
set __NAME=
set __VALUE=
for /f "delims= " %%n in ("%%i") do set __NAME=%%n
@rem line comments start with "#"
if defined __NAME if not "!__NAME:~0,1!"=="#" (
@rem trim value
for /f "tokens=*" %%v in ("%%~j") do set __VALUE=%%v
set "_!__NAME:.=_!=!__VALUE!"
)
)
if defined _project_name set _PROJECT_NAME=!_project_name!
if defined _project_url set _PROJECT_URL=!_project_url!
if defined _project_version set _PROJECT_VERSION=!_project_version!
)
goto :eof
@rem input parameter: %*
@rem output parameter(s): _CLEAN, _COMPILE, _DEBUG, _RUN, _TIMER, _VERBOSE
:args
set _CLEAN=0
set _COMPILE=0
set _DETEKT=0
set _DOC=0
set _HELP=0
set _LINT=0
set _RUN=0
set _TARGET=jvm
set _TEST=0
set _TIMER=0
set _VERBOSE=0
set __N=0
:args_loop
set "__ARG=%~1"
if not defined __ARG (
if !__N!==0 set _HELP=1
goto args_done
)
if "%__ARG:~0,1%"=="-" (
@rem option
if "%__ARG%"=="-debug" ( set _DEBUG=1
) else if "%__ARG%"=="-help" ( set _HELP=1
) else if "%__ARG%"=="-native" ( set _TARGET=native
) else if "%__ARG%"=="-timer" ( set _TIMER=1
) else if "%__ARG%"=="-verbose" ( set _VERBOSE=1
) else (
echo %_ERROR_LABEL% Unknown option "%__ARG%" 1>&2
set _EXITCODE=1
goto args_done
)
) else (
@rem subcommand
if "%__ARG%"=="clean" ( set _CLEAN=1
) else if "%__ARG%"=="compile" ( set _COMPILE=1
) else if "%__ARG%"=="detekt" ( set _DETEKT=1
) else if "%__ARG%"=="doc" ( set _DOC=1
) else if "%__ARG%"=="help" ( set _HELP=1
) else if "%__ARG%"=="lint" ( set _LINT=1
) else if "%__ARG%"=="run" ( set _COMPILE=1& set _RUN=1
) else if "%__ARG%"=="test" ( set _COMPILE=1& set _TEST=1
) else (
echo %_ERROR_LABEL% Unknown subcommand "%__ARG%" 1>&2
set _EXITCODE=1
goto args_done
)
set /a __N+=1
)
shift
goto args_loop
:args_done
set _STDOUT_REDIRECT=1^>NUL
if %_DEBUG%==1 set _STDOUT_REDIRECT=1^>CON
if %_DETEKT%==1 if not defined _DETEKT_CMD (
echo %_WARNING_LABEL% Detekt tool not found ^(disable subcommand 'detekt'^) 1>&2
set _DETEKT=0
)
if %_LINT%==1 if not defined _KTLINT_CMD (
echo %_WARNING_LABEL% KtLint tool not found ^(disable subcommand 'lint'^) 1>&2
set _LINT=0
)
if %_DEBUG%==1 (
echo %_DEBUG_LABEL% Properties : _PROJECT_NAME=%_PROJECT_NAME% _PROJECT_VERSION=%_PROJECT_VERSION% 1>&2
echo %_DEBUG_LABEL% Options : _TARGET=%_TARGET% _TIMER=%_TIMER% _VERBOSE=%_VERBOSE% 1>&2
echo %_DEBUG_LABEL% Subcommands: _CLEAN=%_CLEAN% _COMPILE=%_COMPILE% _DETEKT=%_DETEKT% _DOC=%_DOC% _LINT=%_LINT% _RUN=%_RUN% _TEST=%_TEST% 1>&2
if defined _DETEKT_CMD echo %_DEBUG_LABEL% Variables : "DETEKT_HOME=%DETEKT_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "JAVA_HOME=%JAVA_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "KOTLIN_HOME=%KOTLIN_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : "KOTLIN_NATIVE_HOME=%KOTLIN_NATIVE_HOME%" 1>&2
if defined _KTLINT_CMD echo %_DEBUG_LABEL% Variables : "KTLINT_HOME=%KTLINT_HOME%" 1>&2
echo %_DEBUG_LABEL% Variables : _LANGUAGE_VERSION=%_LANGUAGE_VERSION% _JAVA_MAIN_CLASS=%_JAVA_MAIN_CLASS% _MAIN_CLASS=%_MAIN_CLASS% 1>&2
)
if %_TIMER%==1 for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "(Get-Date)"') do set _TIMER_START=%%i
goto :eof
:help
if %_VERBOSE%==1 (
set __BEG_P=%_STRONG_FG_CYAN%
set __BEG_O=%_STRONG_FG_GREEN%
set __BEG_N=%_NORMAL_FG_YELLOW%
set __END=%_RESET%
) else (
set __BEG_P=
set __BEG_O=
set __BEG_N=
set __END=
)
echo Usage: %__BEG_O%%_BASENAME% { ^<option^> ^| ^<subcommand^> }%__END%
echo.
echo %__BEG_P%Options:%__END%
echo %__BEG_O%-debug%__END% print commands executed by this script
echo %__BEG_O%-native%__END% generate native executable
echo %__BEG_O%-timer%__END% print total execution time
echo %__BEG_O%-verbose%__END% print progress messages
echo.
echo %__BEG_P%Subcommands:%__END%
echo %__BEG_O%clean%__END% delete generated files
echo %__BEG_O%compile%__END% generate class files
echo %__BEG_O%detekt%__END% analyze Kotlin source files with %__BEG_N%Detekt%__END%
echo %__BEG_O%doc%__END% generate HTML documentation with %__BEG_N%Dokka%__END%
echo %__BEG_O%help%__END% print this help message
echo %__BEG_O%lint%__END% analyze Kotlin/Java source files with %__BEG_N%KtLint%__END%/%__BEG_N%CheckStyle%__END%
echo %__BEG_O%run%__END% execute the generated program
echo %__BEG_O%test%__END% execute unit tests
if %_VERBOSE%==0 goto :eof
echo.
echo %__BEG_P%Build tools:%__END%
echo %__BEG_O%^> ant clean run%__END%
echo %__BEG_O%^> gradle -q clean run%__END%
echo %__BEG_O%^> mvn -q clean compile exec:java%__END%
goto :eof
:clean
call :rmdir "%_TARGET_DIR%"
goto :eof
@rem input parameter: %1=directory path
:rmdir
set "__DIR=%~1"
if not exist "%__DIR%\" goto :eof
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% rmdir /s /q "%__DIR%" 1>&2
) else if %_VERBOSE%==1 ( echo Delete directory "!__DIR:%_ROOT_DIR%=!" 1>&2
)
rmdir /s /q "%__DIR%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to delete directory "!__DIR:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:detekt
for %%f in ("%~dp0\.") do set "__CONFIG_FILE=%%~dpfdetekt-config.yml"
set __DETEKT_OPTS=--language-version %_LANGUAGE_VERSION% --config "%__CONFIG_FILE%" --input "%_SOURCE_DIR%" --report "xml:%_TARGET_DIR%\detekt-report.xml"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_DETEKT_CMD%" %__DETEKT_OPTS% 1>&2
) else if %_VERBOSE%==1 ( echo Analyze Kotlin source files with Detekt 1>&2
)
call "%_DETEKT_CMD%" %__DETEKT_OPTS% %_STDERR_REDIRECT%
if not %ERRORLEVEL%==0 (
if %_DEBUG%==1 if exist "%_TARGET_DIR%\detekt-report.xml" (
set __SIZE=0
for %%f in (%_TARGET_DIR%\detekt-report.xml) do set __SIZE=%%~zf
if !__SIZE! gtr 79 type "%_TARGET_DIR%\detekt-report.xml" 1>&2
)
set _EXITCODE=1
goto :eof
)
if %_DEBUG%==1 if exist "%_TARGET_DIR%\detekt-report.xml" (
set __SIZE=0
for %%f in (%_TARGET_DIR%\detekt-report.xml) do set __SIZE=%%~zf
if !__SIZE! gtr 79 type "%_TARGET_DIR%\detekt-report.xml"
)
goto :eof
:lint
call :lint_kotlin
if not %_EXITCODE%==0 goto :eof
call :lint_java
if not %_EXITCODE%==0 goto :eof
goto :eof
:lint_kotlin
set __KTLINT_OPTS=--color --reporter=checkstyle,output="%_TARGET_DIR%\\ktlint-report.xml"
if %_DEBUG%==1 ( set __KTLINT_OPTS=--reporter=plain %__KTLINT_OPTS%
) else if %_VERBOSE%==1 ( set __KTLINT_OPTS=--reporter=plain %__KTLINT_OPTS%
)
set "__TMP_FILE=%_TARGET_DIR%\ktlint_output.txt"
if not exist "%_TARGET_DIR%" mkdir "%_TARGET_DIR%"
@rem KtLint does not support absolute path in globs.
@rem prepend ! to negate the pattern in order to check only certain locations
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_KTLINT_CMD%" %__KTLINT_OPTS% "src/**/*.kt" 2^>"%__TMP_FILE%" 1>&2
) else if %_VERBOSE%==1 ( echo Analyze Kotlin source files with Ktlint 1>&2
)
call "%_KTLINT_CMD%" %__KTLINT_OPTS% "src/**/*.kt" 2>"%__TMP_FILE%"
if not %ERRORLEVEL%==0 (
echo %_WARNING_LABEL% Ktlint error found 1>&2
if %_DEBUG%==1 ( type "%__TMP_FILE%"
) else if %_VERBOSE%==1 ( type "%__TMP_FILE%"
)
if exist "%__TMP_FILE%" del "%__TMP_FILE%"
@rem set _EXITCODE=1
goto :eof
)
goto :eof
:lint_java
call :checkstyle
if not %_EXITCODE%==0 goto :eof
goto :eof
:compile_jvm
if not exist "%_CLASSES_DIR%" mkdir "%_CLASSES_DIR%"
set "__TIMESTAMP_FILE=%_CLASSES_DIR%\.latest-build"
call :action_required "%__TIMESTAMP_FILE%" "%_SOURCE_DIR%\main\kotlin\*.kt"
if %_ACTION_REQUIRED%==1 (
call :compile_kotlin
if not !_EXITCODE!==0 goto :eof
)
call :action_required "%__TIMESTAMP_FILE%" "%_SOURCE_DIR%\main\java\*.java"
if %_ACTION_REQUIRED%==1 (
call :compile_java
if not !_EXITCODE!==0 goto :eof
)
echo. > "%__TIMESTAMP_FILE%"
goto :eof
:compile_java
set "__OPTS_FILE=%_TARGET_DIR%\javac_opts.txt"
set "__CPATH=%KOTLIN_HOME%\lib\kotlin-stdlib.jar;%_CLASSES_DIR%"
echo -cp "%__CPATH:\=\\%" -d "%_CLASSES_DIR:\=\\%" > "%__OPTS_FILE%"
set "__SOURCES_FILE=%_TARGET_DIR%\javac_sources.txt"
if exist "%__SOURCES_FILE%" del "%__SOURCES_FILE%" 1>NUL
set __N=0
for /f "delims=" %%i in ('dir /s /b "%_SOURCE_DIR%\main\java\*.java" 2^>NUL') do (
echo %%i >> "%__SOURCES_FILE%"
set /a __N+=1
)
if %__N%==0 (
echo %_WARNING_LABEL% No Java source file found 1>&2
goto :eof
) else if %__N%==1 ( set __N_FILES=%__N% Java source file
) else ( set __N_FILES=%__N% Java source files
)
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_JAVAC_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%" 1>&2
) else if %_VERBOSE%==1 ( echo Compile %__N_FILES% to directory "!_CLASSES_DIR:%_ROOT_DIR%=!" 1>&2
)
call "%_JAVAC_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to compile %__N_FILES% to directory "!_CLASSES_DIR:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:compile_kotlin
set /a __WARN_ENABLED=_VERBOSE+_DEBUG
if %__WARN_ENABLED%==0 ( set __NOWARN_OPT=-nowarn
) else ( set __NOWARN_OPT=
)
set "__OPTS_FILE=%_TARGET_DIR%\kotlinc_opts.txt"
set "__CPATH=%_CLASSES_DIR%"
echo -language-version %_LANGUAGE_VERSION% %__NOWARN_OPT% -cp "%__CPATH:\=\\%" -d "%_CLASSES_DIR:\=\\%" > "%__OPTS_FILE%"
set "__SOURCES_FILE=%_TARGET_DIR%\kotlinc_sources.txt"
if exist "%__SOURCES_FILE%" del "%__SOURCES_FILE%" 1>NUL
set __N=0
for /f "delims=" %%i in ('dir /s /b "%_MAIN_SOURCE_DIR%\*.kt" 2^>NUL') do (
echo %%i >> "%__SOURCES_FILE%"
set /a __N+=1
)
if %__N%==0 (
echo %_WARNING_LABEL% No Kotlin source files found 1>&2
goto :eof
) else if %__N%==1 ( set __N_FILES=%__N% Kotlin source file
) else ( set __N_FILES=%__N% Kotlin source files
)
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_KOTLINC_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%" 1>&2
) else if %_VERBOSE%==1 ( echo Compile %__N_FILES% to directory "!_CLASSES_DIR:%_ROOT_DIR%=!" ^(JVM^) 1>&2
)
call "%_KOTLINC_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to compile %__N_FILES% to directory "!_CLASSES_DIR:%_ROOT_DIR%=!" ^(JVM^) 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:compile_native
if not exist "%_TARGET_DIR%" mkdir "%_TARGET_DIR%"
set "__TIMESTAMP_FILE=%_TARGET_DIR%\.latest-native-build"
call :action_required "%__TIMESTAMP_FILE%" "%_SOURCE_DIR%\main\kotlin\*.kt"
if %_ACTION_REQUIRED%==0 goto :eof
set "__SOURCES_FILE=%_TARGET_DIR%\kotlinc-native_sources.txt"
if exist "%__SOURCES_FILE%" del "%__SOURCES_FILE%" 1>NUL
set __N=0
for /f "delims=" %%f in ('dir /s /b "%_SOURCE_DIR%\main\kotlin\*.kt" 2^>NUL') do (
echo %%f >> "%__SOURCES_FILE%"
set /a __N+=1
)
if %__N%==0 (
echo %_WARNING_LABEL% No Kotlin source files found 1>&2
goto :eof
) else if %__N%==1 ( set __N_FILES=%__N% Kotlin source file
) else ( set __N_FILES=%__N% Kotlin source files
)
set /a __WARN_ENABLED=_VERBOSE+_DEBUG
if %__WARN_ENABLED%==0 ( set __NOWARN_OPT=-nowarn
) else ( set __NOWARN_OPT=
)
set "__OPTS_FILE=%_TARGET_DIR%\kotlinc-native_opts.txt"
echo -language-version %_LANGUAGE_VERSION% %__NOWARN_OPT% -o "%_EXE_FILE:\=\\%" > "%__OPTS_FILE%"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_KOTLINC_NATIVE_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%" 1>&2
) else if %_VERBOSE%==1 ( echo Compile %__N_FILES% to executable "!_EXE_FILE:%_ROOT_DIR%=!" ^(native^) 1>&2
)
call "%_KOTLINC_NATIVE_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to compile %__N_FILES% to executable "!_EXE_FILE:%_ROOT_DIR%=!" ^(native^) 1>&2
set _EXITCODE=1
goto :eof
)
echo. > "%__TIMESTAMP_FILE%"
goto :eof
@rem input parameter: 1=target file 2,3,..=path (wildcards accepted)
@rem output parameter: _ACTION_REQUIRED
:action_required
set "__TARGET_FILE=%~1"
set __PATH_ARRAY=
set __PATH_ARRAY1=
:action_path
shift
set __PATH=%~1
if not defined __PATH goto action_next
set __PATH_ARRAY=%__PATH_ARRAY%,'%__PATH%'
set __PATH_ARRAY1=%__PATH_ARRAY1%,'!__PATH:%_ROOT_DIR%=!'
goto action_path
:action_next
set __TARGET_TIMESTAMP=00000000000000
for /f "usebackq" %%i in (`call "%_PWSH_CMD%" -c "gci -path '%__TARGET_FILE%' -ea Stop | select -last 1 -expandProperty LastWriteTime | Get-Date -uformat %%Y%%m%%d%%H%%M%%S" 2^>NUL`) do (
set __TARGET_TIMESTAMP=%%i
)
set __SOURCE_TIMESTAMP=00000000000000
for /f "usebackq" %%i in (`call "%_PWSH_CMD%" -c "gci -recurse -path %__PATH_ARRAY:~1% -ea Stop | sort LastWriteTime | select -last 1 -expandProperty LastWriteTime | Get-Date -uformat %%Y%%m%%d%%H%%M%%S" 2^>NUL`) do (
set __SOURCE_TIMESTAMP=%%i
)
call :newer %__SOURCE_TIMESTAMP% %__TARGET_TIMESTAMP%
set _ACTION_REQUIRED=%_NEWER%
if %_DEBUG%==1 (
echo %_DEBUG_LABEL% %__TARGET_TIMESTAMP% Target : '%__TARGET_FILE%' 1>&2
echo %_DEBUG_LABEL% %__SOURCE_TIMESTAMP% Sources: %__PATH_ARRAY:~1% 1>&2
echo %_DEBUG_LABEL% _ACTION_REQUIRED=%_ACTION_REQUIRED% 1>&2
) else if %_VERBOSE%==1 if %_ACTION_REQUIRED%==0 if %__SOURCE_TIMESTAMP% gtr 0 (
echo No action required ^(%__PATH_ARRAY1:~1%^) 1>&2
)
goto :eof
@rem output parameter: _NEWER
:newer
set __TIMESTAMP1=%~1
set __TIMESTAMP2=%~2
set __DATE1=%__TIMESTAMP1:~0,8%
set __TIME1=%__TIMESTAMP1:~-6%
set __DATE2=%__TIMESTAMP2:~0,8%
set __TIME2=%__TIMESTAMP2:~-6%
if %__DATE1% gtr %__DATE2% ( set _NEWER=1
) else if %__DATE1% lss %__DATE2% ( set _NEWER=0
) else if %__TIME1% gtr %__TIME2% ( set _NEWER=1
) else ( set _NEWER=0
)
goto :eof
@rem output parameter: _LIBS_CPATH
:libs_cpath
for /f "delims=" %%f in ("%~dp0\.") do set "__BATCH_FILE=%%~dpfcpath.bat"
if not exist "%__BATCH_FILE%" (
echo %_ERROR_LABEL% Batch file "%__BATCH_FILE%" not found 1>&2
set _EXITCODE=1
goto :eof
)
if %_DEBUG%==1 echo %_DEBUG_LABEL% "%__BATCH_FILE%" %_DEBUG% 1>&2
call "%__BATCH_FILE%" %_DEBUG%
set _LIBS_CPATH=%_CPATH%
goto :eof
:doc
if not exist "%_TARGET_DOCS_DIR%" mkdir "%_TARGET_DOCS_DIR%"
call :libs_cpath
if not %_EXITCODE%==0 goto :eof
set __JAVA_OPTS=
@rem see https://github.com/Kotlin/dokka/releases
set __ARGS=-src %_SOURCE_DIR%\main\kotlin
set __DOKKA_ARGS=-pluginsClasspath "%_DOKKA_CPATH%" -moduleName %_PROJECT_NAME% -moduleVersion %_PROJECT_VERSION%
set __DOKKA_ARGS=%__DOKKA_ARGS% -outputDir "%_TARGET_DOCS_DIR%" -sourceSet "%__ARGS%"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_JAVA_CMD%" %__JAVA_OPTS% -jar "%_DOKKA_CLI_JAR%" %__DOKKA_ARGS% 1>&2
) else if %_VERBOSE%==1 ( echo Generate HTML documentation into directory "!_TARGET_DOCS_DIR:%_ROOT_DIR%=!" 1>&2
)
call "%_JAVA_CMD%" %__JAVA_OPTS% -jar "%_DOKKA_CLI_JAR%" %__DOKKA_ARGS%
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to generate HTML documentation into directory "!_TARGET_DOCS_DIR:%_ROOT_DIR%=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:run_jvm
set "__MAIN_CLASS_FILE=%_CLASSES_DIR%\%_MAIN_CLASS:.=\%.class"
if not exist "%__MAIN_CLASS_FILE%" (
echo %_ERROR_LABEL% Kotlin main class file not found ^("!__MAIN_CLASS_FILE:%_ROOT_DIR%=!"^) 1>&2
set _EXITCODE=1
goto :eof
)
@rem call :libs_cpath
@rem if not %_EXITCODE%==0 goto :eof
set __KOTLIN_OPTS=-cp "%_CLASSES_DIR%"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_KOTLIN_CMD%" %__KOTLIN_OPTS% %_MAIN_CLASS% 1>&2
) else if %_VERBOSE%==1 ( echo Execute Kotlin main class "%_MAIN_CLASS%" 1>&2
)
call "%_KOTLIN_CMD%" %__KOTLIN_OPTS% %_MAIN_CLASS%
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to execute Kotlin main class "%_MAIN_CLASS%" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:run_native
if not exist "%_EXE_FILE%" (
echo %_ERROR_LABEL% Kotlin executable not found ^("!_EXE_FILE:%_ROOT_DIR%=!"^) 1>&2
set _EXITCODE=1
goto :eof
)
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_EXE_FILE%" 1>&2
) else if %_VERBOSE%==1 ( echo Execute Kotlin native application "!_EXE_FILE:%_ROOT_DIR%\=!" 1>&2
)
call "%_EXE_FILE%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to execute Kotlin native application "!_EXE_FILE:%_ROOT_DIR%\=!" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:run_java
set __MAIN_CLASS_FILE=%_CLASSES_DIR%\%_JAVA_MAIN_CLASS:.=\%.class
if not exist "%__MAIN_CLASS_FILE%" (
echo %_ERROR_LABEL% Java main class file not found ^(!__MAIN_CLASS_FILE:%_ROOT_DIR%=!^) 1>&2
set _EXITCODE=1
goto :eof
)
set __JAVA_OPTS=-cp "%_KOTLIN_CPATH%;%_CLASSES_DIR%"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_JAVA_CMD%" %__JAVA_OPTS% %_JAVA_MAIN_CLASS% 1>&2
) else if %_VERBOSE%==1 ( echo Execute Kotlin main class "%_JAVA_MAIN_CLASS%" 1>&2
) else ( echo.
)
call "%_JAVA_CMD%" %__JAVA_OPTS% %_JAVA_MAIN_CLASS%
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to execute Kotlin main class "%_JAVA_MAIN_CLASS%" 1>&2
set _EXITCODE=1
goto :eof
)
goto :eof
:compile_test_jvm
if not exist "%_TEST_CLASSES_DIR%" mkdir "%_TEST_CLASSES_DIR%" 1>NUL
set "__TIMESTAMP_FILE=%_TEST_CLASSES_DIR%\.latest-build"
call :action_required "%__TIMESTAMP_FILE%" "%_SOURCE_DIR%\test\kotlin\*.kt"
if %_ACTION_REQUIRED%==0 goto :eof
call :libs_cpath
if not %_EXITCODE%==0 goto :eof
set "__OPTS_FILE=%_TARGET_DIR%\test_kotlinc_opts.txt"
set "__CPATH=%_CPATH%%_CLASSES_DIR%"
echo -cp "%__CPATH:\=\\%" -d "%_TEST_CLASSES_DIR:\=\\%" > "%__OPTS_FILE%"
set "__SOURCES_FILE=%_TARGET_DIR%\test_kotlinc_sources.txt"
if exist "%__SOURCES_FILE%" del "%__SOURCES_FILE%" 1>NUL
set __N=0
for /f "delims=" %%i in ('dir /s /b "%_SOURCE_DIR%\test\kotlin\*.kt" 2^>NUL') do (
echo %%i >> "%__SOURCES_FILE%"
set /a __N+=1
)
if %__N%==0 (
echo %_WARNING_LABEL% No Kotlin test source files found 1>&2
goto :eof
) else if %__N%==1 ( set __N_FILES=%__N% Kotlin test source file
) else ( set __N_FILES=%__N% Kotlin test source files
)
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_KOTLINC_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%" 1>&2
) else if %_VERBOSE%==1 ( echo Compile %__N_FILES% into directory "!_TEST_CLASSES_DIR:%_ROOT_DIR%=!" ^(JVM^) 1>&2
)
call "%_KOTLINC_CMD%" "@%__OPTS_FILE%" "@%__SOURCES_FILE%"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to compile %__N_FILES% into directory "!_TEST_CLASSES_DIR:%_ROOT_DIR%=!" ^(JVM^) 1>&2
set _EXITCODE=1
goto :eof
)
echo. > "%__TIMESTAMP_FILE%"
goto :eof
:test_jvm
call :compile_test_jvm
if not %_EXITCODE%==0 goto :eof
call :libs_cpath
if not %_EXITCODE%==0 goto :eof
set __TEST_KOTLIN_OPTS=-classpath "%_CPATH%%_CLASSES_DIR%;%_TEST_CLASSES_DIR%"
@rem see https://github.com/junit-team/junit4/wiki/Getting-started
for /f "usebackq delims=" %%f in (`dir /s /b "%_TEST_CLASSES_DIR%\*JUnitTest.class" 2^>NUL`) do (
call :test_main_class "%%f"
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_KOTLIN_CMD%" %__TEST_KOTLIN_OPTS% org.junit.runner.JUnitCore !_TEST_MAIN_CLASS! 1>&2
) else if %_VERBOSE%==1 ( echo Execute test "!_TEST_MAIN_CLASS!" 1>&2
)
call "%_KOTLIN_CMD%" %__TEST_KOTLIN_OPTS% org.junit.runner.JUnitCore !_TEST_MAIN_CLASS!
if not !ERRORLEVEL!==0 (
echo %_ERROR_LABEL% Failed to execute test "!_TEST_MAIN_CLASS!" 1>&2
set _EXITCODE=1
goto :eof
)
)
goto :eof
@rem input parameter: %1=class file path
@rem output parameter: _TEST_MAIN_CLASS
:test_main_class
for %%i in (%~dp1) do set __PKG_NAME=%%i
for %%i in (%~n1) do set __CLS_NAME=%%i
set __PKG_NAME=!__PKG_NAME:%_TEST_CLASSES_DIR%\=!
if defined __PKG_NAME ( set "_TEST_MAIN_CLASS=!__PKG_NAME:\=.!%__CLS_NAME%"
) else ( set "_TEST_MAIN_CLASS=%__CLS_NAME%"
)
goto :eof
:test_native
echo Not yet implemented
set _EXITCODE=1
goto :eof
:checkstyle
set "__USER_KOTLIN_DIR=%USERPROFILE%\.kotlin"
if not exist "%__USER_KOTLIN_DIR%" mkdir "%__USER_KOTLIN_DIR%"
set "__XML_FILE=%__USER_KOTLIN_DIR%\java_checks.xml"
if not exist "%__XML_FILE%" call :checkstyle_xml "%__XML_FILE%"
)
set __JAR_VERSION=10.17.0
set __JAR_NAME=checkstyle-%__JAR_VERSION%-all.jar
set __JAR_URL=https://github.com/checkstyle/checkstyle/releases/download/checkstyle-%__JAR_VERSION%/%__JAR_NAME%
set "__JAR_FILE=%__USER_KOTLIN_DIR%\%__JAR_NAME%"
if exist "%__JAR_FILE%" goto checkstyle_analyze
set "__PS1_FILE=%__USER_KOTLIN_DIR%\webrequest.ps1"
if not exist "%__PS1_FILE%" call :checkstyle_ps1 "%__PS1_FILE%"
set __PS1_VERBOSE[0]=
set __PS1_VERBOSE[1]=-Verbose
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% call "%_PWSH_CMD%" -c "& '%__PS1_FILE%' -Uri '%__JAR_URL%' -Outfile '%__JAR_FILE%'" 1>&2
) else if %_VERBOSE%==1 ( echo Download file %__JAR_NAME% 1>&2
) else ( echo Download file %__JAR_NAME%
)
call "%_PWSH_CMD%" -c "& '%__PS1_FILE%' -Uri '%__JAR_URL%' -OutFile '%__JAR_FILE%' !__PS1_VERBOSE[%_VERBOSE%]!"
if not %ERRORLEVEL%==0 (
echo %_ERROR_LABEL% Failed to download file "%__JAR_NAME%" 1>&2
set _EXITCODE=1
goto :eof
)
:checkstyle_analyze
set __SOURCE_FILES=
set __N=0
for /f "delims=" %%f in ('where /r "%_SOURCE_DIR%" *.java') do (
set __SOURCE_FILES=!__SOURCE_FILES! "%%f"
set /a __N+=1
)
if %_DEBUG%==1 ( echo %_DEBUG_LABEL% "%_JAVA_CMD%" -jar "%__JAR_FILE%" -c="%__XML_FILE%" %__SOURCE_FILES% 1>&2
) else if %_VERBOSE%==1 ( echo Analyze %__N% Java source files with CheckStyle configuration "!__XML_FILE:%USERPROFILE%\=!" 1>&2
)
call "%_JAVA_CMD%" -jar "%__JAR_FILE%" -c="%__XML_FILE%" %__SOURCE_FILES% %_STDOUT_REDIRECT%
if not %ERRORLEVEL%==0 (
@rem set _EXITCODE=1
goto :eof
)
goto :eof
@rem input parameter: %1=XML file path
@rem NB. Archive checkstyle-*-all.jar contains 2 configuration files:
@rem google_checks.xml, sun_checks.xml.
:checkstyle_xml
set "__XML_FILE=%~1"
(
echo ^<?xml version="1.0"?^>
echo ^<^^!DOCTYPE module PUBLIC
echo "-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
echo "https://checkstyle.org/dtds/configuration_1_3.dtd"^>
echo.
echo ^<module name="Checker"^>
echo ^<property name="localeCountry" value="US"/^>
echo ^<property name="localeLanguage" value="en"/^>
echo ^<property name="severity" value="error"/^>
echo ^<property name="fileExtensions" value="java, properties, xml"/^>
echo ^<^^!-- See https://checkstyle.org/config_whitespace.html --^>
echo ^<module name="FileTabCharacter"/^>
echo ^<module name="TreeWalker"^>
echo ^<^^!-- See https://checkstyle.org/config_import.html --^>
echo ^<module name="AvoidStarImport"/^>
echo ^<module name="IllegalImport"/^> ^<^^!-- defaults to sun.* packages --^>
echo ^<module name="RedundantImport"/^>
echo ^<module name="UnusedImports"^>
echo ^<property name="processJavadoc" value="false"/^>
echo ^</module^>
echo ^<^^!-- See https://checkstyle.org/config_whitespace.html --^>
echo ^<module name="EmptyForIteratorPad"/^>
echo ^<module name="GenericWhitespace"/^>
echo ^<module name="MethodParamPad"/^>
echo ^<module name="NoWhitespaceAfter"/^>
echo ^<module name="NoWhitespaceBefore"/^>
echo ^<module name="OperatorWrap"/^>
echo ^<module name="ParenPad"/^>
echo ^<module name="TypecastParenPad"/^>
echo ^<module name="WhitespaceAfter"/^>
echo ^<module name="WhitespaceAround"/^>
echo ^</module^>
echo ^</module^>
) > "%__XML_FILE%"
goto :eof
@rem input parameter: %1=PS1 file path
:checkstyle_ps1
set "__PS1_FILE=%~1"
@rem see https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
@rem NB. cURL is a standard tool only from Windows 10 build 17063 and later.
(
echo Param^(
echo [Parameter^(Mandatory=$True,Position=1^)]
echo [string]$Uri,
echo [Parameter(Mandatory=$True^)]
echo [string]$OutFile
echo ^)
echo Add-Type ^@^"
echo using System.Net;
echo using System.Security.Cryptography.X509Certificates;
echo public class TrustAllCertsPolicy : ICertificatePolicy {
echo public bool CheckValidationResult^(
echo ServicePoint srvPoint, X509Certificate certificate,
echo WebRequest request, int certificateProblem^) {
echo return true;
echo }
echo }
echo ^"^@
echo $Verbose=$PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent
echo $AllProtocols=[System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
echo [System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
echo [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
echo $progressPreference='silentlyContinue'
echo Invoke-WebRequest -TimeoutSec 60 -Uri $Uri -Outfile $OutFile
) > "%__PS1_FILE%"
goto :eof
@rem output parameter: _DURATION
:duration
set __START=%~1
set __END=%~2
for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "$interval = New-TimeSpan -Start '%__START%' -End '%__END%'; Write-Host $interval"') do set _DURATION=%%i
goto :eof
@rem #########################################################################
@rem ## Cleanups
:end
if %_TIMER%==1 (
for /f "delims=" %%i in ('call "%_PWSH_CMD%" -c "(Get-Date)"') do set __TIMER_END=%%i
call :duration "%_TIMER_START%" "!__TIMER_END!"
echo Total execution time: !_DURATION! 1>&2
)
if %_DEBUG%==1 echo %_DEBUG_LABEL% _EXITCODE=%_EXITCODE% 1>&2
exit /b %_EXITCODE%
endlocal