forked from automatic-controls/addon-dev-script
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.bat
814 lines (757 loc) · 23 KB
/
Utility.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
:: Contributors:
:: Cameron Vogt (@cvogt729)
:: BSD 3-Clause License
::
:: Copyright (c) 2022, Automatic Controls Equipment Systems, Inc.
:: All rights reserved.
::
:: Redistribution and use in source and binary forms, with or without
:: modification, are permitted provided that the following conditions are met:
::
:: 1. Redistributions of source code must retain the above copyright notice, this
:: list of conditions and the following disclaimer.
::
:: 2. Redistributions in binary form must reproduce the above copyright notice,
:: this list of conditions and the following disclaimer in the documentation
:: and/or other materials provided with the distribution.
::
:: 3. Neither the name of the copyright holder nor the names of its
:: contributors may be used to endorse or promote products derived from
:: this software without specific prior written permission.
::
:: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
:: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
:: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
:: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
:: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
:: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
:: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
:: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
:: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
:: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@echo off
:: Jump to a particular function
if "%1" EQU "--goto" (
for /f "tokens=2,* delims= " %%i in ("%*") do (
call :%%i %%j
exit /b
)
exit /b
)
setlocal EnableDelayedExpansion
:: Version control
set "version=1.0.2"
if "%1" EQU "--version" (
echo %version%
exit /b
)
title Add-On Development Utility for WebCTRL
echo Initializing...
:: This script's location for extension callback usage
set "callback=%~f0"
:: Default compilation arguments
set "compileArgs=--release 11"
:: Global settings folder
call :normalizePath settings "%~dp0."
:: License
set "license=%settings%\LICENSE"
:: External dependencies provided by WebCTRL at runtime (not packaged into the addon)
set "globalLib=%settings%\lib"
if not exist "%globalLib%" mkdir "%globalLib%"
:: JavaScript used to obfuscate the keystore password
set "obfuscate=%settings%\obfuscate.js"
:: Load the configuration file
set "config=%settings%\config.txt"
if exist "%config%" call :loadConfig
:: Determine location of JDK bin
:jdkFinder
set "jdkFound=0"
if "%JDKBin%" NEQ "" (
"%JDKBin%\java.exe" --version >nul 2>nul
if !ERRORLEVEL! EQU 0 (
set "jdkFound=1"
) else (
echo Invalid JDK location.
)
)
if "%jdkFound%" EQU "0" (
echo Enter the location of the JDK bin.
set /p "JDKBin=>"
echo.
call :saveConfig
goto :jdkFinder
)
:: Determine location of WebCTRL installation
if "%WebCTRL%" EQU "" (
for /f %%i in ('dir "%SystemDrive%\" /B /A:D ^| findstr /R /X "WebCTRL[0-9][0-9]*\.[0-9][0-9]*"') do (
set "WebCTRL=%%i"
)
if "!WebCTRL!" NEQ "" (
set "WebCTRL=%SystemDrive%\!WebCTRL!"
if exist "!WebCTRL!\webserver\*" (
echo Bound to installation !WebCTRL!
echo.
) else (
set "WebCTRL="
)
)
)
:webctrlFinder
if "%WebCTRL%" EQU "" (
echo Could not locate WebCTRL installation.
echo Please enter the installation path ^(e.g, %SystemDrive%\WebCTRL8.0^).
set /p "WebCTRL=>"
echo.
call :saveConfig
)
if not exist "%WebCTRL%\webserver\*" (
set "WebCTRL="
goto :webctrlFinder
)
:: Collect runtime dependencies from the WebCTRL installation
setlocal
set "depFolders[1]=%WebCTRL%\webserver\lib"
set "depFiles[1]=tomcat-embed-core"
set "depFolders[2]=%WebCTRL%\modules\addonsupport"
set "depFiles[2]=addonsupport-api-addon"
set "depFolders[3]=%WebCTRL%\modules\alarmmanager"
set "depFiles[3]=alarmmanager-api-addon"
set "depFolders[4]=%WebCTRL%\bin\lib"
set "depFolders2[4]=%WebCTRL%\modules\bacnet"
set "depFiles[4]=bacnet-api-addon"
set "depFolders[5]=%WebCTRL%\modules\directaccess"
set "depFiles[5]=directaccess-api-addon"
set "depFolders[6]=%WebCTRL%\modules\webaccess"
set "depFiles[6]=webaccess-api-addon"
set "depFolders[7]=%WebCTRL%\modules\xdatabase"
set "depFiles[7]=xdatabase-api-addon"
set "msg=0"
for /L %%i in (1,1,7) do (
set "exists=0"
for /F %%j in ('dir "%globalLib%" /B ^| findstr /C:"!depFiles[%%i]!"') do (
set "exists=1"
)
if "!exists!" EQU "0" (
set "msg=1"
set "file="
for /F %%j in ('dir "!depFolders[%%i]!" /B ^| findstr /C:"!depFiles[%%i]!"') do (
set "file=%%j"
)
if "!file!" EQU "" (
for /F %%j in ('dir "!depFolders2[%%i]!" /B ^| findstr /C:"!depFiles[%%i]!"') do (
set "file=%%j"
)
if "!file!" EQU "" (
echo Failed to collect dependency: !depFiles[%%i]!
) else (
copy /Y "!depFolders2[%%i]!\!file!" "%globalLib%\!file!" >nul
if !ErrorLevel!==0 (
echo Collected dependency: !depFiles[%%i]!
) else (
echo Failed to collect dependency: !depFiles[%%i]!
)
)
) else (
copy /Y "!depFolders[%%i]!\!file!" "%globalLib%\!file!" >nul
if !ErrorLevel!==0 (
echo Collected dependency: !depFiles[%%i]!
) else (
echo Failed to collect dependency: !depFiles[%%i]!
)
)
)
)
if "%msg%" EQU "1" echo.
endlocal
:: Keystore used for signing the addon
set "keystore=%settings%\keystore.jks"
:: Keypair alias in the keystore
set "alias=addon_dev"
:: Certificate file
set "certFileName=Authenticator.cer"
for /f "tokens=* delims=" %%i in ('dir /A-D /B "%settings%" ^| findstr /C:.cer') do set "certFileName=%%i"
set "certFile=%settings%\%certFileName%"
:: Retrieve the keystore password
:passwordFinder
if "!Password!" EQU "" (
echo DO NOT USE SPECIAL CHARACTERS ^"^&^^!%%;^?
echo Enter the keystore password.
set /p "pass=>"
cls
call :obfuscate Password pass
call :saveConfig
if exist "%keystore%" (
"%JDKBin%\keytool" -keystore %keystore% -storepass "!pass!" -list >nul 2>nul
if !ERRORLEVEL! NEQ 0 (
echo Incorrect password.
set "Password="
goto :passwordFinder
)
)
) else (
call :obfuscate pass Password
)
:: Create a new keystore and keypair if it doesn't already exist
set "exists=0"
if exist "%keystore%" (
"%JDKBin%\keytool" -keystore %keystore% -storepass "!pass!" -list -alias %alias% >nul 2>nul
if !ERRORLEVEL! EQU 0 (
set "exists=1"
) else (
echo Alias %alias% is not contained within the pre-existing keystore, so a new key-pair will be created.
)
)
if "%exists%" EQU "0" (
if exist "%certFile%" del /F "%certFile%" >nul 2>nul
"%JDKBin%\keytool.exe" -keystore "%keystore%" -storepass "!pass!" -genkeypair -alias %alias% -keyalg RSA -keysize 2048 -sigalg SHA512withRSA -validity 36500
echo.
)
if not exist "%certFile%" (
"%JDKBin%\keytool.exe" -keystore "%keystore%" -storepass "!pass!" -export -alias %alias% -file "%certFile%"
echo.
)
:: List of valid workspace commands
set "commands[1]=help"
set "commands[2]=build"
set "commands[3]=pack"
set "commands[4]=make"
set "commands[5]=sign"
set "commands[6]=forge"
set "commands[7]=deploy"
set "commands[8]=exec"
set "commands=8"
:: Retrieve workspace from parameter
if "%*" NEQ "" (
call :normalizePath workspace "%*."
goto :initWorkspace
)
:globalMenu
cls
echo.
echo WebCTRL Add-On Project Initializer v%version%
echo.
echo Enter the project folder to initialize.
set /p "workspace=>"
if "!workspace!" NEQ "" (
echo !workspace! | findstr /C:: >nul 2>nul
if !ERRORLEVEL! EQU 1 call :normalizePath workspace "%settings%\..\!workspace!"
goto :initWorkspace
)
goto :globalMenu
:clsHelp
echo CLS Clears the terminal.
exit /b
:initHelp
echo INIT [--new] Reinitializes the current project if no parameters are given.
echo Prompts you to initialize a new project if the '--new' flag is given.
exit /b
:help
if exist "%ext%\help.bat" (
call "%ext%\help.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo HELP Displays this message.
exit /b
)
echo.
echo Online documentation can be found at
echo https://github.com/automatic-controls/addon-dev-script/blob/main/README.md
echo.
echo GIT [ARGS] All Git commands are executed literally.
call :initHelp
call :clsHelp
setlocal
for /l %%i in (1,1,%commands%) do (
set "cmd=!commands[%%i]!"
if exist "%ext%\!cmd!.bat" (
call "%ext%\!cmd!.bat" --help
) else (
call :!cmd! --help
)
)
for %%i in ("%ext%\*.bat") do (
set prev=0
for /l %%j in (1,1,%commands%) do (
if "%%~ni" EQU "!commands[%%j]!" set prev=1
)
if !prev! EQU 0 call "%%~fi" --help
)
endlocal
echo.
exit /b
:exec
if exist "%ext%\exec.bat" (
call "%ext%\exec.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo EXEC [ARGS] Calls BUILD, PACK, SIGN, and DEPLOY. Arguments are passed to BUILD.
exit /b
)
( call :build %* ) && ( call :pack ) && ( call :sign ) && ( call :deploy )
exit /b
:forge
if exist "%ext%\forge.bat" (
call "%ext%\forge.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo FORGE [ARGS] Calls BUILD, PACK, and SIGN. Arguments are passed to BUILD.
exit /b
)
( call :build %* ) && ( call :pack ) && ( call :sign )
exit /b
:make
if exist "%ext%\make.bat" (
call "%ext%\make.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo MAKE [ARGS] Calls BUILD and PACK. Arguments are passed to BUILD.
exit /b
)
( call :build %* ) && ( call :pack )
exit /b
:deploy
if exist "%ext%\deploy.bat" (
call "%ext%\deploy.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo DEPLOY Copies the .addon archive and certificate file to the bound WebCTRL installation.
exit /b
)
if "%*" NEQ "" (
echo Unexpected parameter.
exit /b 1
)
if exist "%addonFile%" (
echo Deploying...
if not exist "%WebCTRL%\addons" mkdir "%WebCTRL%\addons" >nul 2>nul
if exist "%WebCTRL%\addons\!name!.addon" del /F "%WebCTRL%\addons\!name!.addon" >nul 2>nul
if !ERRORLEVEL! NEQ 0 (
echo Failed to overwrite !name!.addon. Please deactivate the addon in WebCTRL before attempting to redeploy.
exit /b 1
)
copy /y "%certFile%" "%WebCTRL%\addons\%certFileName%" >nul
copy /y "%addonFile%" "%WebCTRL%\addons\!name!.addon" >nul
if !ERRORLEVEL! EQU 0 (
echo Deployment successful.
exit /b 0
) else (
echo Deployment unsuccessful.
exit /b 1
)
) else (
echo Cannot deploy because !name!.addon does not exist.
exit /b 1
)
:sign
if exist "%ext%\sign.bat" (
call "%ext%\sign.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo SIGN Signs the .addon archive.
exit /b
)
if "%*" NEQ "" (
echo Unexpected parameter.
exit /b 1
)
if exist "%addonFile%" (
echo Signing...
"%JDKBin%\jarsigner.exe" -keystore "%keystore%" -storepass "!pass!" "%addonFile%" %alias% >nul
if !ERRORLEVEL! EQU 0 (
echo Signing successful.
exit /b 0
) else (
echo Signing unsuccessful.
exit /b 1
)
) else (
echo Cannot sign because !name!.addon does not exist.
exit /b 1
)
:pack
if exist "%ext%\pack.bat" (
call "%ext%\pack.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo PACK Packages all relevant files into a newly created .addon archive.
exit /b
)
if "%*" NEQ "" (
echo Unexpected parameter.
exit /b 1
)
echo Packing...
rmdir /Q /S "%classes%" >nul 2>nul
for /D %%i in ("%trackingClasses%\*") do robocopy /E "%%~fi" "%classes%" >nul 2>nul
robocopy /E "%src%" "%classes%" /XF "*.java" >nul 2>nul
copy /Y "%workspace%\LICENSE" "%root%\LICENSE" >nul 2>nul
"%JDKBin%\jar.exe" -c -M -f "%addonFile%" -C "%root%" .
if %ERRORLEVEL% EQU 0 (
echo Packing successful.
exit /b 0
) else (
echo Packing unsuccessful.
exit /b 1
)
:build
if exist "%ext%\build.bat" (
call "%ext%\build.bat" %*
exit /b
)
if /i "%*" EQU "--help" (
echo BUILD [ARGS] Compiles source code. Arguments are passed to the JAVAC compilation command.
echo Current build flags: !compileArgs!
exit /b
)
echo Indexing...
if "%*" NEQ "" (
set "compileArgs=%*"
(
echo !compileArgs!
) > "%workspaceConfig%"
rmdir /S /Q "%trackingClasses%" >nul 2>nul
)
(
echo JDK Version:
"%JDKBin%\java.exe" --version
echo.
echo Compilation Flags:
echo !compileArgs!
echo.
echo Runtime Dependencies:
for /r "%globalLib%" %%i in (*.jar) do echo %%~ni
for /r "%localLib%" %%i in (*.jar) do echo %%~ni
echo.
echo Packaged Dependencies:
for /r "%lib%" %%i in (*.jar) do echo %%~ni
) > "%depRecord%"
setlocal
set err=0
set "changes=0"
set /a index=0
for /f "tokens=1,* delims==" %%i in ('echo foreach ^($a in ^(Get-ChildItem -Path "%src%" -Recurse -Include *.java^)^){Echo ^($a.LastWriteTime.toString^(^)+"="+$a.FullName^)} ^| PowerShell -Command -') do (
set /a index+=1
set "time[!index!]=%%i"
set "file[!index!]=%%j"
set "process[!index!]=0"
)
set /a newIndex=0
if exist "%trackingRecord%" (
for /f "usebackq tokens=1,2,* delims==" %%i in ("%trackingRecord%") do (
set exists=0
for /l %%a in (1,1,%index%) do (
if !exists! EQU 0 if "!file[%%a]!" EQU "%%k" (
set exists=1
set "process[%%a]=1"
set /a newIndex+=1
set "newTime[!newIndex!]=!time[%%a]!"
set "newFile[!newIndex!]=%%k"
if "%%j" EQU "!time[%%a]!" (
if "%%i" NEQ "!newIndex!" rename "%trackingClasses%\%%i" !newIndex!
) else (
echo Compiling: %%k
set "changes=1"
rmdir /S /Q "%trackingClasses%\%%i" >nul 2>nul
mkdir "%trackingClasses%\!newIndex!"
"%JDKBin%\javac.exe" !compileArgs! -implicit:none -d "%trackingClasses%\!newIndex!" -cp "%src%;%globalLib%\*;%lib%\*;%localLib%\*" "%%k"
if !ERRORLEVEL! NEQ 0 (
rmdir /S /Q "%trackingClasses%\!newIndex!" >nul 2>nul
set /a newIndex-=1
set err=1
)
)
)
)
if !exists! EQU 0 (
set "changes=1"
echo Removing: %%k
rmdir /S /Q "%trackingClasses%\%%i" >nul 2>nul
)
)
) else (
rmdir /S /Q "%trackingClasses%" >nul 2>nul
mkdir "%trackingClasses%"
)
for /l %%i in (1,1,%index%) do (
if "!process[%%i]!" EQU "0" (
echo Compiling: !file[%%i]!
set "changes=1"
set /a newIndex+=1
set "newTime[!newIndex!]=!time[%%i]!"
set "newFile[!newIndex!]=!file[%%i]!"
if exist "%trackingClasses%\!newIndex!" rmdir /S /Q "%trackingClasses%\!newIndex!" >nul 2>nul
mkdir "%trackingClasses%\!newIndex!"
"%JDKBin%\javac.exe" !compileArgs! -implicit:none -d "%trackingClasses%\!newIndex!" -cp "%src%;%globalLib%\*;%lib%\*;%localLib%\*" "!file[%%i]!"
if !ERRORLEVEL! NEQ 0 (
rmdir /S /Q "%trackingClasses%\!newIndex!" >nul 2>nul
set /a newIndex-=1
set err=1
)
)
)
(
for /L %%i in (1,1,!newIndex!) do echo %%i=!newTime[%%i]!=!newFile[%%i]!
) > "%trackingRecord%"
if %err% EQU 1 (
echo Compilation unsuccessful.
) else if "!changes!" EQU "0" (
echo Compilation skipped.
) else (
echo Compilation successful.
)
endlocal & exit /b %err%
:: Obfuscates text
:: First parameter - name of variable to store result
:: Second parameter - name of variable which has text to obfuscate
:obfuscate
if not exist "%obfuscate%" (
echo var x = WScript.Arguments^(0^)
echo var y = ^"^"
echo for ^(var i=x.length-1;i^>=0;--i^){
echo y+=String.fromCharCode^(x.charCodeAt^(i^)^^4^)
echo }
echo WScript.Echo^(y^)
) > "%obfuscate%"
for /f "tokens=* delims=" %%i in ('cscript //nologo //E:jscript "%obfuscate%" "!%~2!"') do (
setlocal DisableDelayedExpansion
set "tmpVar=%%i"
)
(
endlocal
set "%~1=%tmpVar%"
)
del /F "%obfuscate%" >nul 2>nul
exit /b
:: Loads the configuration file
:loadConfig
setlocal DisableDelayedExpansion
for /f "usebackq tokens=* delims=" %%i in ("%config%") do (
set "%%i"
)
(
endlocal
set "JDKBin=%JDKBin%"
set "WebCTRL=%WebCTRL%"
set "Password=%Password%"
)
exit /b
:: Saves global configuration properties
:saveConfig
(
echo JDKBin=!JDKBin!
echo WebCTRL=!WebCTRL!
echo Password=!Password!
) > "%config%"
exit /b
:: Resolves relative paths to fully qualified path names.
:normalizePath
set "%~1=%~f2"
exit /b
:initWorkspace
echo.
if not exist "%workspace%" mkdir "%workspace%"
cd "%workspace%"
set "root=%workspace%\root"
if not exist "%root%" mkdir "%root%"
:: Folder containing custom batch file command extensions for this utility.
set "ext=%workspace%\ext"
:: Create local launcher within workspace
setlocal
set "batch=%workspace%\Utility.bat"
set "create=1"
if exist "%batch%" (
for /f "tokens=* delims=" %%i in ('call "%batch%" --version') do (
if "%%i" EQU "%version%" (
set "create=0"
)
)
)
if "%create%" EQU "1" (
echo @echo off
echo if "%%1" EQU "--version" ^(
echo echo %version%
echo exit /b
echo ^)
echo %0 %%~dp0
) > "%batch%"
endlocal
:: Source code
set "src=%workspace%\src"
if not exist "%src%" mkdir "%src%"
:: Compiled classes
set "trackingClasses=%workspace%\classes"
set "trackingRecord=%trackingClasses%\index.txt"
set "classes=%root%\webapp\WEB-INF\classes"
if not exist "%classes%" mkdir "%classes%"
:: External dependencies (packaged into the addon)
set "lib=%root%\webapp\WEB-INF\lib"
if not exist "%lib%" mkdir "%lib%"
:: Local runtime dependencies (not packaged into the addon)
set "localLib=%workspace%\lib"
if not exist "%localLib%" mkdir "%localLib%"
:: Dependency record
set "depRecord=%workspace%\DEPENDENCIES"
:: Visual Studio Code Settings
set "vscode=%workspace%\.vscode"
if not exist "%vscode%" mkdir "%vscode%"
set "vscodeSettings=%vscode%\settings.json"
if not exist "%vscodeSettings%" (
echo {
echo "java.project.referencedLibraries": [
echo "%globalLib:\=\\%\\**\\*.jar",
echo "root\\webapp\\WEB-INF\\lib\\**\\*.jar",
echo "lib\\**\\*.jar"
echo ]
echo }
) > "%vscodeSettings%"
:: Retrieve basic add-on information
set "infoXML=%root%\info.xml"
set "name="
if exist "%infoXML%" (
for /f "tokens=* delims=" %%i in ('type "%infoXML%" ^| findstr /C:"<name>"') do (
for /f "tokens=* delims=" %%j in ('echo echo^([Regex]::Match^("%%i"^, " *<name>(.*)</name> *"^).groups[1].Value^) ^| PowerShell -Command -') do (
set "name=%%j"
)
)
)
if "%name%" EQU "" (
echo Enter basic information about your add-on.
set /p "name=Name: "
setlocal
set /p "description=Description: "
set /p "version=Version: "
set /p "vendor=Vendor: "
(
echo ^<extension version="1"^>
echo ^<name^>!name!^</name^>
echo ^<description^>!description!^</description^>
echo ^<version^>!version!^</version^>
echo ^<vendor^>!vendor!^</vendor^>
echo ^</extension^>
) > "%infoXML%"
endlocal
)
:: The resulting .addon file
set "addonFile=%workspace%\!name!.addon"
:: Deployment descriptor
set "webXML=%root%\webapp\WEB-INF\web.xml"
if not exist "%webXML%" (
echo ^<?xml version="1.0" encoding="UTF-8"?^>
echo.
echo ^<web-app^>
echo.
echo ^<servlet^>
echo ^<servlet-name^>^</servlet-name^>
echo ^<servlet-class^>^</servlet-class^>
echo ^</servlet^>
echo ^<servlet-mapping^>
echo ^<servlet-name^>^</servlet-name^>
echo ^<url-pattern^>^</url-pattern^>
echo ^</servlet-mapping^>
echo.
echo ^<security-constraint^>
echo ^<web-resource-collection^>
echo ^<web-resource-name^>WEB^</web-resource-name^>
echo ^<url-pattern^>/*^</url-pattern^>
echo ^</web-resource-collection^>
echo ^</security-constraint^>
echo.
echo ^<filter^>
echo ^<filter-name^>RoleFilterAJAX^</filter-name^>
echo ^<filter-class^>com.controlj.green.addonsupport.web.RoleFilter^</filter-class^>
echo ^<init-param^>
echo ^<param-name^>roles^</param-name^>
echo ^<param-value^>view_administrator_only^</param-value^>
echo ^</init-param^>
echo ^</filter^>
echo ^<filter-mapping^>
echo ^<filter-name^>RoleFilterAJAX^</filter-name^>
echo ^<url-pattern^>/*^</url-pattern^>
echo ^</filter-mapping^>
echo.
echo ^</web-app^>
) > "%webXML%"
:: Git ignore
if not exist "%workspace%\.gitignore" (
echo .gitignore
echo .vscode
echo Utility.bat
echo classes
echo config.txt
echo root/LICENSE
echo root/webapp/WEB-INF/classes
echo root/webapp/WEB-INF/lib
echo lib
echo **/*.addon
) > "%workspace%\.gitignore"
:: License
if not exist "%workspace%\LICENSE" (
copy /Y "%license%" "%workspace%\LICENSE" >nul
)
:: README
if not exist "%workspace%\README.md" (
echo # !name!
) > "%workspace%\README.md"
:: Workspace configuration properties
set "workspaceConfig=%workspace%\config.txt"
if exist "%workspaceConfig%" (
for /f "usebackq tokens=* delims=" %%i in ("%workspaceConfig%") do set "compileArgs=%%i"
)
(
echo !compileArgs!
) > "%workspaceConfig%"
:: Main workspace command processing loop
:main
cls
echo.
echo Add-On Development Utility for WebCTRL
echo Project: !name!
echo.
echo Type 'help' for a list of commands.
echo.
:loop
set "cmd="
set /p "cmd=>"
for /f "tokens=1,* delims= " %%a in ("!cmd!") do (
if /i "%%a" EQU "git" (
call !cmd!
) else if /i "%%a" EQU "cls" (
if "%%b" EQU "" (
goto :main
) else if /i "%%b" EQU "--help" (
call :clsHelp
) else (
echo Unexpected parameter.
)
) else if /i "%%a" EQU "init" (
if "%%b" EQU "" (
goto :initWorkspace
) else if /i "%%b" EQU "--new" (
goto :globalMenu
) else if /i "%%b" EQU "--help" (
call :initHelp
) else (
echo Unexpected parameter.
)
) else if exist "%ext%\%%a.bat" (
call "%ext%\%%a.bat" %%b
) else (
set "exists=0"
for /l %%i in (1,1,%commands%) do (
if /i "!commands[%%i]!" EQU "%%a" set "exists=1"
)
if "!exists!" EQU "1" (
call :!cmd!
) else (
echo Unknown command.
)
)
)
goto loop