@@ -77,12 +77,11 @@ package body LSP.Ada_Handlers.Alire is
7777 -- Run_Alire --
7878 -- -------------
7979
80- procedure Run_Alire
80+ procedure Determine_Alire_Project
8181 (Root : String;
8282 Has_Alire : out Boolean;
8383 Error : out VSS.Strings.Virtual_String;
84- Project : out VSS.Strings.Virtual_String;
85- Environment : in out GPR2.Environment.Object)
84+ Project : out VSS.Strings.Virtual_String)
8685 is
8786 use type GNAT.OS_Lib.String_Access;
8887
@@ -96,10 +95,6 @@ package body LSP.Ada_Handlers.Alire is
9695 VSS.Regular_Expressions.To_Regular_Expression
9796 (" +Project_File: ([^\n]+)" );
9897
99- Export_Pattern : constant VSS.Regular_Expressions.Regular_Expression :=
100- VSS.Regular_Expressions.To_Regular_Expression
101- (" export ([^=]+)="" ([^\n]+)"" " );
102-
10398 Lines : VSS.String_Vectors.Virtual_String_Vector;
10499 begin
105100 Project.Clear;
@@ -117,66 +112,64 @@ package body LSP.Ada_Handlers.Alire is
117112 return ;
118113 end if ;
119114
120- -- Find project file in `alr show` output
121-
122- declare
123- First : constant VSS.Strings.Virtual_String := Lines (1 );
124- -- We should keep copy of regexp subject string while we have a match
125- Match : constant VSS.Regular_Expressions.Regular_Expression_Match :=
126- Crate_Pattern.Match (First);
127- begin
128- if Match.Has_Match then
129- Project := Match.Captured (1 );
130- Project.Append (" .gpr" );
131- end if ;
132- end ;
133-
115+ -- Find project file in `alr show` output. There are several cases to cover.
116+ --
117+ -- If alire.toml contains a "project-files" entry providing a list of
118+ -- projects, `alr show` prints one line per project of the pattern:
119+ -- Project_File: ...
120+ --
121+ -- Otherwise `alr show` doesn't print a Project_File line and we can expect
122+ -- there to be a project named identically to the crate name.
123+ --
124+ -- So the strategy below is to first use the crate name as a project
125+ -- name, and then override it if a Project_File line is found (the first
126+ -- one is taken).
127+
128+ -- When `alr show` is called in a directory where /alire/ and /config/
129+ -- don't exist, the command auto-generates those directories and prints a
130+ -- few lines of output before the actual crate information. So we can't
131+ -- assume that the crate name will be on the first line.
134132 for Line of Lines loop
135133 declare
136- Match : constant VSS.Regular_Expressions.Regular_Expression_Match
137- := Project_Pattern.Match (Line, Anchored);
134+ -- We should keep copy of regexp subject string while we have a match
135+ Match : constant VSS.Regular_Expressions.Regular_Expression_Match :=
136+ Crate_Pattern.Match (Line);
138137 begin
139138 if Match.Has_Match then
140139 Project := Match.Captured (1 );
140+ Project.Append (" .gpr" );
141141 exit ;
142142 end if ;
143143 end ;
144144 end loop ;
145145
146- if Project.Is_Empty then
147- Error.Append (" No project file is found by alire" );
148- end if ;
149-
150- -- Find variables in `alr printenv` output
151-
152- Start_Alire
153- (ALR.all , " --non-interactive" , " printenv" , Root, Error, Lines);
154-
155- GNAT.OS_Lib.Free (ALR);
156-
157- -- Find variables in `alr printenv` output
158-
146+ -- Next check if there is a Project_File line, take the first one.
159147 for Line of Lines loop
160148 declare
161149 Match : constant VSS.Regular_Expressions.Regular_Expression_Match
162- := Export_Pattern .Match (Line, Anchored);
150+ := Project_Pattern .Match (Line, Anchored);
163151 begin
164152 if Match.Has_Match then
165- Environment.Insert
166- (Key => VSS.Strings.Conversions.To_UTF_8_String
167- (Match.Captured (1 )),
168- Value => VSS.Strings.Conversions.To_UTF_8_String
169- (Match.Captured (2 )));
153+ Project := Match.Captured (1 );
154+ exit ;
170155 end if ;
171156 end ;
172157 end loop ;
173- end Run_Alire ;
158+
159+ if Project.Is_Empty then
160+ Error.Append (" No project file could be determined from the output of `alr show`:" );
161+ for Line of Lines loop
162+ Error.Append (Line);
163+ end loop ;
164+ end if ;
165+
166+ end Determine_Alire_Project ;
174167
175168 -- -------------
176169 -- Run_Alire --
177170 -- -------------
178171
179- procedure Run_Alire
172+ procedure Setup_Alire_Env
180173 (Root : String;
181174 Has_Alire : out Boolean;
182175 Error : out VSS.Strings.Virtual_String;
@@ -221,7 +214,7 @@ package body LSP.Ada_Handlers.Alire is
221214 end if ;
222215 end ;
223216 end loop ;
224- end Run_Alire ;
217+ end Setup_Alire_Env ;
225218
226219 -- ---------------
227220 -- Start_Alire --
0 commit comments