Debian Codegraph @

tools/internal/x3d-nodes-to-pascal/x3d_nodes_to_pascal.lpr

raw
1 // -*- compile-command: "castle-engine compile && castle-engine run" -*-
2 {
3 Copyright 2015-2023 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Generate Pascal code from X3D nodes specification.
18 Writes Pascal include files with auto-generated pieces of Pascal code
19 that define X3D nodes.
20 This allows to define all X3D fiels in a consistent way,
21 and easier change it in the future.
22 }
23
24 uses SysUtils, CastleParameters, CastleClassUtils, CastleStringUtils,
25 CastleTimeUtils, CastleLog, CastleColors, CastleUtils,
26 CastleApplicationProperties, CastleFindFiles, CastleFilesUtils,
27 GenerateProcessors;
28
29 var
30 { When are input files (*.txt) located.
31 May but doesn't have to end with PathDelim. }
32 InputPath: String = './nodes-specification/';
33
34 const
35 Options: array [0..4] of TOption =
36 (
37 (Short: 'h'; Long: 'help'; Argument: oaNone),
38 (Short: 'v'; Long: 'version'; Argument: oaNone),
39 (Short: #0 ; Long: 'verbose'; Argument: oaNone),
40 (Short: #0 ; Long: 'input-path'; Argument: oaRequired),
41 (Short: #0 ; Long: 'output-path'; Argument: oaRequired)
42 );
43
44 procedure OptionProc(OptionNum: Integer; HasArgument: boolean;
45 const Argument: string; const SeparateArgs: TSeparateArgs; Data: Pointer);
46 begin
47 case OptionNum of
48 0: begin
49 Writeln(
50 'x3d-nodes-to-pascal: Generate Pascal code' +NL+
51 'from X3D nodes specification, for Castle Game Engine.' +NL+
52 NL+
53 'Usage:' +NL+
54 ' x3d-nodes-to-pascal [options...]' +NL+
55 NL+
56 'Available options:' +NL+
57 ' -h / --help Print this help message and exit' +NL+
58 ' -v / --version Print the version and exit' +NL+
59 ' --verbose Be verbose' +NL+
60 ' --input-path PATH Directory with txt files that describe nodes.' +NL+
61 ' --output-path PATH Directory with generated (Pascal include) files.' +NL+
62 NL+
63 ApplicationProperties.Description);
64 Halt;
65 end;
66 1: begin
67 // include ApplicationName in version, good for help2man
68 Writeln(ApplicationName + ' ' + CastleEngineVersion);
69 Halt;
70 end;
71 2: Verbose := true;
72 3: InputPath := Argument;
73 4: OutputPath := Argument;
74 else raise EInternalError.Create('OptionProc');
75 end;
76 end;
77
78 procedure RemoveFilesMatching(const Path, Mask: String);
79 var
80 FilesToRemove: TFileInfoList;
81 FileToRemove: TFileInfo;
82 begin
83 Writeln('Removing files matching "' + Mask + '" in "' + Path + '"');
84 FilesToRemove := FindFilesList(Path, Mask, false, []);
85 try
86 for FileToRemove in FilesToRemove do
87 CheckDeleteFile(FileToRemove.AbsoluteName, true);
88 finally FreeAndNil(FilesToRemove) end;
89 end;
90
91 var
92 Processor: TProcessor;
93 InputFiles: TFileInfoList;
94 InputFile: TFileInfo;
95 begin
96 ApplicationProperties.ApplicationName := 'x3d-nodes-to-pascal';
97 ApplicationProperties.Version := '1.0';
98 ApplicationProperties.OnWarning.Add(@ApplicationProperties.WriteWarningOnConsole);
99
100 if not DirectoryExists(OutputPath) then
101 raise Exception.CreateFmt('Output path "%s" does not exist', [OutputPath]);
102 if not DirectoryExists(InputPath) then
103 raise Exception.CreateFmt('Output path "%s" does not exist', [InputPath]);
104
105 RemoveFilesMatching(OutputPath, 'x3dnodes_*.inc');
106
107 Parameters.Parse(Options, @OptionProc, nil);
108 Parameters.CheckHigh(0);
109
110 try
111 Processor := THelperProcessor.Create;
112
113 InputFiles := FindFilesList(InputPath, '*.txt', false, []);
114
115 if InputFiles.Count = 0 then
116 raise Exception.CreateFmt('No input files found in "%s"', [InputPath]);
117
118 for InputFile in InputFiles do
119 begin
120 Writeln('Processing "' + InputFile.Name + '"');
121 Processor.ProcessFile(InputFile.AbsoluteName);
122 end;
123 finally
124 { Knowing that these instances are initially nil, we can write simple
125 finally clause that deals with all of them,
126 instead of nesting multiple try..finally clauses. }
127 FreeAndNil(InputFiles);
128 FreeAndNil(Processor);
129 end;
130 end.