rejuvenation_23.0.0_507c1f00/src/rejuvenation-environment_variables.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
with Ada.Directories;           use Ada.Directories;
with Ada.Environment_Variables; use Ada.Environment_Variables;
with Ada.Text_IO;               use Ada.Text_IO;
with GNAT.Regpat;               use GNAT.Regpat;
with Interfaces.C;              use Interfaces.C;
with Rejuvenation.File_Utils;   use Rejuvenation.File_Utils;

package body Rejuvenation.Environment_Variables is

   procedure Set (Map : String_Maps.Map) is

      procedure Set_Position (Position : Cursor);
      --  Set individual key value pair at position
      procedure Set_Position (Position : Cursor) is
      begin
         Set (Name => Key (Position), Value => Element (Position));
      end Set_Position;

   begin
      Map.Iterate (Set_Position'Access);
   end Set;

   procedure Set_Alire_Project (Project_Directory : String) is
      function Sys (Arg : char_array) return Integer;
      pragma Import (C, Sys, "system");

      Output_File : constant String :=
        Compose (Project_Directory, "temp.alr.printenv", "txt");
      Alr_Command : constant String :=
        "cd """ & Project_Directory & """ && alr printenv > """ & Output_File &
        """";
      Alr_RetVal : constant Integer := Sys (To_C (Alr_Command));
   begin
      if Alr_RetVal /= 0 then
         Put_Line
           (Standard_Error,
            "System call to alr returned " & Alr_RetVal'Image & ".");
         Put_Line (Standard_Error, "Is alr installed and on the path?");
         Put_Line
           (Standard_Error,
            "Does '" & Project_Directory & "' contain an alire.toml file?");
      else
         declare
            Content : constant String := Get_String_From_File (Output_File);
            Current : Natural         := Content'First;

            Export_Matcher : constant Pattern_Matcher :=
              Compile ("export ([A-Z_]+)=""([^""]*)""");
            Matches : Match_Array (0 .. 2);
            Found   : Boolean;
         begin
            loop
               Match (Export_Matcher, Content, Matches, Current);
               Found := Matches (0) /= No_Match;
               exit when not Found;

               Current := Matches (0).Last;
               --  Set environment variable
               declare
                  Name : constant String :=
                    Content (Matches (1).First .. Matches (1).Last);
                  Value : constant String :=
                    Content (Matches (2).First .. Matches (2).Last);
               begin
                  Set (Name => Name, Value => Value);
               end;
            end loop;
         end;
      end if;

      declare
         Del_Command : constant String  := "del """ & Output_File & """";
         Del_RetVal  : constant Integer := Sys (To_C (Del_Command));
      begin
         if Del_RetVal /= 0 then
            Put_Line
              (Standard_Error,
               "Removal of  " & Output_File & " failed with " &
               Del_RetVal'Image & ".");
         end if;
      end;
   end Set_Alire_Project;

end Rejuvenation.Environment_Variables;