libgpr2_25.0.0_70fe0fcf/testsuite/tests/build_db/dynamic/src/scenario.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
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
with Ada.Text_IO;
with Ada.Containers.Vectors;

with GNATCOLL.Strings; use GNATCOLL.Strings;

with GPR2.Path_Name;
with GPR2.Build.Tree_Db;
with GPR2.Build.View_Db;
with GPR2.Build.Source.Sets;
with GPR2.Options;
with GPR2.Path_Name;
with GPR2.Project.Tree;
with GPR2.Project.View;

with Test;
with Objects; use Objects;

package body Scenario is

   use GNATCOLL.VFS;
   use GPR2;
   use GPR2.Build;

   Tok_Load    : constant XString := To_XString ("load");
   --  Load the tree

   Tok_Unload  : constant XString := To_XString ("unload");
   --  Unload the tree

   Tok_Refresh : constant XString := To_XString ("refresh");
   --  Refresh the build db

   Tok_Dump    : constant XString := To_XString ("dump");
   --  Refresh the build db

   Tok_Mkdir       : constant XString := To_XString ("mkdir");
   --  Create a directory

   Tok_Copy        : constant XString := To_XString ("copy");
   --  Copy a file

   Tok_Create_Spec      : constant XString := To_XString ("create_spec");
   Tok_Create_Body      : constant XString := To_XString ("create_body");
   Tok_Create_Separate  : constant XString := To_XString ("create_separate");
   Tok_Create_Proc_Body : constant XString := To_XString ("create_proc_body");

   Tok_Remove  : constant XString := To_XString ("remove");
   --  Remove a file

   package VFS_Vectors is new Ada.Containers.Vectors (Positive, Virtual_File);

   -------------
   -- Execute --
   -------------

   procedure Execute (Path : String)
   is
      function To_File (Token : XString) return Virtual_File;

      -------------
      -- To_File --
      -------------

      function To_File (Token : XString) return Virtual_File
      is
      begin
         return Path_Name.Create_File
           (Filename_Type (Token.To_String)).Virtual_File;
      end To_File;

      Scenario_Path : constant Virtual_File := To_File (To_XString (Path));
      Scenario      : constant XString := Scenario_Path.Read_File;
      Lines         : constant XString_Array := Split (Scenario, ASCII.LF);
      Success       : Boolean;
      Old_Dir       : constant Virtual_File := Get_Current_Dir;
      Artifacts     : VFS_Vectors.Vector;
      Loaded        : Boolean := False;

   begin
      Scenario_Path.Get_Parent.Change_Dir;
      Ada.Text_IO.Put_Line ("=======================================");
      Ada.Text_IO.Put_Line ("Executing " & Path);
      Ada.Text_IO.Put_Line ("=======================================");
      Ada.Text_IO.New_Line;

      for L of Lines loop
         declare
            Tokens : constant XString_Array := L.Trim.Split (' ');
            Cmd    : XString;

         begin
            if Tokens'Length > 0 and then not Tokens (1).Is_Empty and then Tokens (1).To_String /= "#" then
               Cmd := Tokens (1).To_Lower;

               if Cmd = Tok_Load then
                  Ada.Text_IO.Put_Line
                    ("--- Loading project " & Tokens (2).To_String);

                  if Loaded then
                     Tree.Unload;
                     Loaded := False;
                  end if;

                  declare
                     Opt : GPR2.Options.Object;
                  begin
                     Opt.Add_Switch (Options.P, Tokens (2).To_String);
                     if Tree.Load (Opt, Absent_Dir_Error => No_Error) then
                        Tree.Update_Sources;
                        Loaded := True;
                     end if;
                  end;

               elsif Cmd = Tok_Unload then
                  Ada.Text_IO.Put_Line
                    ("--- Unloading project");
                  Tree.Unload;
                  Loaded := False;

               elsif Cmd = Tok_Refresh then
                  Ada.Text_IO.Put_Line
                    ("--- Refresh list of sources");
                  Tree.Update_Sources;

               elsif Cmd = Tok_Dump then
                  Ada.Text_IO.Put_Line ("-----------------------------------");
                  Test.Dump;
                  Ada.Text_IO.Put_Line ("-----------------------------------");

               elsif Cmd = Tok_Mkdir then
                  Ada.Text_IO.Put_Line
                    ("--- mkdir " & Tokens (2).To_String);
                  declare
                     D : constant Virtual_File := To_File (Tokens (2));
                  begin
                     D.Ensure_Directory;
                     D.Make_Dir;
                     Artifacts.Prepend (D);
                  end;

               elsif Cmd = Tok_Copy then
                  Ada.Text_IO.Put_Line
                    ("--- cp " & Tokens (2).To_String & " " &
                       Tokens (3).To_String);

                  declare
                     Src : constant Virtual_File := To_File (Tokens (2));
                     Dest : Virtual_File := To_File (Tokens (3));
                  begin
                     if Dest.Is_Directory then
                        Dest := Dest.Create_From_Dir (Src.Base_Name);
                     end if;

                     Src.Copy (Dest.Full_Name, Success);
                     exit when not Success;
                     Artifacts.Prepend (Dest);
                  end;

               elsif Cmd = Tok_Remove then
                  Ada.Text_IO.Put_Line ("--- rm " & Tokens (2).To_String);

                  declare
                     Path : constant Virtual_File :=
                              To_File (Tokens (2));
                  begin
                     Path.Delete (Success);

                     if Success and then not Artifacts.Is_Empty then
                        for Idx in 1 .. Positive (Artifacts.Last_Index) loop
                           if Artifacts (Idx) = Path then
                              Artifacts.Delete (Idx);
                              exit;
                           end if;
                        end loop;
                     end if;
                  end;

               elsif Cmd = Tok_Create_Spec then
                  Ada.Text_IO.Put_Line ("--- create spec " & Tokens (3).To_String);
                  declare
                     Unit_Name : constant String := Tokens (2).To_String;
                     File      : constant Virtual_File := To_File (Tokens (3));
                     Path      : constant Filesystem_String := File.Full_Name;
                     Fd        : Ada.Text_IO.File_Type;
                     use Ada.Text_IO;
                  begin
                     Ada.Text_IO.Create
                       (Fd,
                        Ada.Text_IO.Out_File,
                        String (Path));
                     Put_Line (Fd, "package " & Unit_Name & " is");
                     Put_Line (Fd, "end " & Unit_Name & ";");
                     Close (Fd);
                     Artifacts.Prepend (File);
                  end;

               elsif Cmd = Tok_Create_Body then
                  Ada.Text_IO.Put_Line ("--- create body " & Tokens (3).To_String);
                  declare
                     Unit_Name : constant String := Tokens (2).To_String;
                     File      : constant Virtual_File := To_File (Tokens (3));
                     Path      : constant Filesystem_String := File.Full_Name;
                     Fd        : Ada.Text_IO.File_Type;
                     use Ada.Text_IO;
                  begin
                     Ada.Text_IO.Create
                       (Fd,
                        Ada.Text_IO.Out_File,
                        String (Path));
                     Put_Line (Fd, "package body " & Unit_Name & " is");
                     Put_Line (Fd, "end " & Unit_Name & ";");
                     Close (Fd);
                     Artifacts.Prepend (File);
                  end;

               elsif Cmd = Tok_Create_Separate then
                  Ada.Text_IO.Put_Line ("--- create separate " & Tokens (4).To_String);
                  declare
                     Unit_Name : constant String := Tokens (2).To_String;
                     Sep_Name  : constant String := Tokens (3).To_String;
                     File      : constant Virtual_File := To_File (Tokens (4));
                     Path      : constant Filesystem_String := File.Full_Name;
                     Fd        : Ada.Text_IO.File_Type;
                     use Ada.Text_IO;
                  begin
                     Ada.Text_IO.Create
                       (Fd,
                        Ada.Text_IO.Out_File,
                        String (Path));
                     Put_Line (Fd, "separate (" & Unit_Name & ")");
                     Put_Line (Fd, "package body " & Sep_Name & " is");
                     Put_Line (Fd, "end " & Sep_Name & ";");
                     Close (Fd);
                     Artifacts.Prepend (File);
                  end;

               elsif Cmd = Tok_Create_Proc_Body then
                  Ada.Text_IO.Put_Line ("--- create procedure body " &
                                          Tokens (3).To_String);
                  declare
                     Unit_Name : constant String := Tokens (2).To_String;
                     File      : constant Virtual_File := To_File (Tokens (3));
                     Path      : constant Filesystem_String := File.Full_Name;
                     Fd        : Ada.Text_IO.File_Type;
                     use Ada.Text_IO;
                  begin
                     Ada.Text_IO.Create
                       (Fd,
                        Ada.Text_IO.Out_File,
                        String (Path));
                     Put_Line (Fd, "procedure " & Unit_Name & " is");
                     Put_Line (Fd, "begin");
                     Put_Line (Fd, "end " & Unit_Name & ";");
                     Close (Fd);
                     Artifacts.Prepend (File);
                  end;

               else
                  Ada.Text_IO.Put_Line
                    ("Unexpected command " & Tokens (1).To_String);
                  exit;
               end if;
            end if;
         end;
      end loop;

      Ada.Text_IO.New_Line;

      while not Artifacts.Is_Empty loop
         declare
            Path : constant Virtual_File := Artifacts.First_Element;
         begin
            Artifacts.Delete_First;
            if Path.Is_Directory then
               Path.Remove_Dir (Recursive => True, Success => Success);
            else
               Path.Delete (Success);
            end if;
         end;
      end loop;

      if Loaded then
         Tree.Unload;
      end if;

      Old_Dir.Change_Dir;
   end Execute;

end Scenario;