awa_unit_2.4.0_59135a52/dynamo/src/dynamo.adb

  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
-----------------------------------------------------------------------
--  dynamo -- Ada Code Generator
--  Copyright (C) 2009, 2010, 2011, 2012, 2015, 2017, 2020 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with GNAT.Command_Line;  use GNAT.Command_Line;
with GNAT.Traceback.Symbolic;

with Sax.Readers;
with Ada.Text_IO;

with Ada.Strings.Unbounded;
with Ada.Exceptions;
with Ada.Directories;
with Ada.Command_Line;
with Ada.Environment_Variables;

with Util.Files;
with Util.Log.Loggers;
with Util.Systems.Os;
with Util.Commands;
with Gen.Utils.GNAT;
with Gen.Generator;
with Gen.Commands;
with Gen.Configs;
procedure Dynamo is
   use Ada;
   use Ada.Strings.Unbounded;
   use Ada.Directories;
   use Ada.Command_Line;
   use Gen.Commands;

   procedure Set_Config_Directory (Path   : in String;
                                   Silent : in Boolean := False);

   procedure Print_Configuration (Generator : in Gen.Generator.Handler);

   --  Print environment variable setup
   procedure Print_Environment (Generator : in Gen.Generator.Handler;
                                C_Env     : in Boolean := False);

   function Get_Installation_Directory return String;

   Out_Dir      : Unbounded_String;
   Config_Dir   : Unbounded_String;
   Template_Dir : Unbounded_String;
   Status       : Exit_Status := Success;
   Debug        : Boolean := False;
   Print_Config : Boolean := False;
   Print_Env    : Boolean := False;
   Print_CEnv   : Boolean := False;
   First        : Natural := 0;

   --  ------------------------------
   --  Print information about dynamo configuration
   --  ------------------------------
   procedure Print_Configuration (Generator : in Gen.Generator.Handler) is
   begin
      Ada.Text_IO.Put_Line ("Dynamo version          : " & Gen.Configs.VERSION);
      Ada.Text_IO.Put_Line ("Config directory        : " & Generator.Get_Config_Directory);
      Ada.Text_IO.Put_Line ("UML directory           : "
                            & Generator.Get_Parameter (Gen.Configs.GEN_UML_DIR));
      Ada.Text_IO.Put_Line ("Templates directory     : "
                            & Generator.Get_Parameter (Gen.Configs.GEN_TEMPLATES_DIRS));
      Ada.Text_IO.Put_Line ("GNAT project directory  : "
                            & Generator.Get_Parameter (Gen.Configs.GEN_GNAT_PROJECT_DIRS));
   end Print_Configuration;

   --  ------------------------------
   --  Print environment variable setup
   --  ------------------------------
   procedure Print_Environment (Generator : in Gen.Generator.Handler;
                                C_Env     : in Boolean := False) is
   begin
      if C_Env then
         Ada.Text_IO.Put ("setenv " & Gen.Utils.GNAT.ADA_PROJECT_PATH_NAME & " """);
      else
         Ada.Text_IO.Put ("export " & Gen.Utils.GNAT.ADA_PROJECT_PATH_NAME & "=""");
      end if;
      if Ada.Environment_Variables.Exists (Gen.Utils.GNAT.ADA_PROJECT_PATH_NAME) then
         Ada.Text_IO.Put (Ada.Environment_Variables.Value (Gen.Utils.GNAT.ADA_PROJECT_PATH_NAME));
         Ada.Text_IO.Put (Util.Systems.Os.Path_Separator);
      end if;
      Ada.Text_IO.Put_Line (Generator.Get_Parameter (Gen.Configs.GEN_GNAT_PROJECT_DIRS) & """");
   end Print_Environment;

   --  ------------------------------
   --  Verify and set the configuration path
   --  ------------------------------
   procedure Set_Config_Directory (Path   : in String;
                                   Silent : in Boolean := False) is
      Log_Path : constant String := Ada.Directories.Compose (Path, "log4j.properties");
   begin
      --  Ignore if the config directory was already set.
      if Length (Config_Dir) > 0 then
         return;
      end if;

      --  Check that we can read some configuration file.
      if not Ada.Directories.Exists (Log_Path) then
         if not Silent then
            Ada.Text_IO.Put_Line ("Invalid config directory: " & Path);
            Status := Failure;
         end if;
         return;
      end if;

      --  Configure the logs
      Util.Log.Loggers.Initialize (Log_Path);
      Config_Dir := To_Unbounded_String (Path);
   end Set_Config_Directory;

   function Get_Installation_Directory return String is
      Name : constant String := Ada.Command_Line.Command_Name;
      Path : constant String := Ada.Directories.Containing_Directory (Name);
   begin
      if Path = "." then
         return ".";
      else
         return Ada.Directories.Containing_Directory (Path);
      end if;
   end Get_Installation_Directory;

begin
   Initialize_Option_Scan (Stop_At_First_Non_Switch => True, Section_Delimiters => "targs");
   --  Parse the command line
   loop
      case Getopt ("* v d e E o: t: c:") is
         when ASCII.NUL => exit;

         when 'o' =>
            Out_Dir := To_Unbounded_String (Parameter & "/");
            First := First + 1;

         when 't' =>
            Template_Dir := To_Unbounded_String (Parameter & "/");
            First := First + 1;

         when 'c' =>
            Set_Config_Directory (Parameter);
            First := First + 1;

         when 'e' =>
            Print_Env := True;

         when 'E' =>
            Print_CEnv := True;

         when 'd' =>
            Debug := True;

         when 'v' =>
            Print_Config := True;

         when '*' =>
            exit;

         when others =>
            null;
      end case;
      First := First + 1;
   end loop;

   if Length (Config_Dir) = 0 then
      declare
         Dir  : constant String := Get_Installation_Directory;
      begin
         Set_Config_Directory (Compose (Dir, "config"), True);
         Set_Config_Directory (Util.Files.Compose (Dir, "share/dynamo/base"), True);
         Set_Config_Directory (Gen.Configs.CONFIG_DIR, True);
      end;
   end if;
   if Status /= Success then
      Gen.Commands.Short_Help_Usage;
      Ada.Command_Line.Set_Exit_Status (Status);
      return;
   end if;

   if Ada.Command_Line.Argument_Count = 0 then
      Gen.Commands.Short_Help_Usage;
      Set_Exit_Status (Failure);
      return;
   end if;

   declare
      use type Gen.Commands.Command_Access;

      Args      : Util.Commands.Default_Argument_List (First + 1);
      Cmd_Name  : constant String := Full_Switch;
      Cmd       : Gen.Commands.Command_Access;
      Generator : Gen.Generator.Handler;
   begin
      if Length (Out_Dir) > 0 then
         Gen.Generator.Set_Result_Directory (Generator, To_String (Out_Dir));
      end if;
      if Length (Template_Dir) > 0 then
         Gen.Generator.Set_Template_Directory (Generator, Template_Dir);
      end if;

      Gen.Generator.Initialize (Generator, Config_Dir, Debug);
      if Print_Config then
         Print_Configuration (Generator);
         return;
      elsif Print_Env then
         Print_Environment (Generator, False);
         return;
      elsif Print_CEnv then
         Print_Environment (Generator, True);
         return;
      end if;

      Cmd := Gen.Commands.Driver.Find_Command (Cmd_Name);

      --  Check that the command exists.
      if Cmd = null then
         if Cmd_Name'Length > 0 then
            Ada.Text_IO.Put_Line ("Invalid command: '" & Cmd_Name & "'");
         end if;
         Gen.Commands.Short_Help_Usage;
         Set_Exit_Status (Failure);
         return;
      end if;

      Cmd.Execute (Cmd_Name, Args, Generator);

      Ada.Command_Line.Set_Exit_Status (Gen.Generator.Get_Status (Generator));
   end;

exception
   when E : Invalid_Switch =>
      Ada.Text_IO.Put_Line ("Invalid option: " & Ada.Exceptions.Exception_Message (E));
      Gen.Commands.Short_Help_Usage;
      Ada.Command_Line.Set_Exit_Status (2);

   when E : Gen.Generator.Fatal_Error =>
      Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Message (E));
      Ada.Command_Line.Set_Exit_Status (1);

   when E : Sax.Readers.XML_Fatal_Error =>
      Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Message (E));
      Ada.Command_Line.Set_Exit_Status (1);

   when E : others =>
      Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Message (E));
      Ada.Text_IO.Put_Line (GNAT.Traceback.Symbolic.Symbolic_Traceback (E));
      Ada.Command_Line.Set_Exit_Status (1);

end Dynamo;