Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up environment before post-fetch actions #456

Merged
merged 10 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions src/alire/alire-environment-formatting.adb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

with Alire.OS_Lib;
with Alire.Directories;
with Alire.Platform;
with Alire.Paths;

package body Alire.Environment.Formatting is

Expand Down Expand Up @@ -54,9 +51,8 @@ package body Alire.Environment.Formatting is
-- Format --
------------

function Format (Rel : Releases.Release;
Value : String;
Is_Root_Release : Boolean)
function Format (Release_Dir : Any_Path;
Value : String)
return String
is
-------------
Expand All @@ -66,12 +62,7 @@ package body Alire.Environment.Formatting is
procedure Replace (Str : in out Unbounded_String;
From, To : Positive)
is
use Alire.OS_Lib;

Id : constant String := Slice (Str, From + 2, To - 1);

Working_Folder : constant Alire.Absolute_Path :=
Alire.Directories.Current;
begin

if Id = "DISTRIB_ROOT" then
Expand All @@ -80,12 +71,7 @@ package body Alire.Environment.Formatting is
elsif Id = "CRATE_ROOT" then
Replace_Slice
(Str, From, To,
Working_Folder /
(if Is_Root_Release
then ".."
else Alire.Paths.Working_Folder_Inside_Root
/ Alire.Paths.Dependency_Dir_Inside_Working_Folder
/ Rel.Unique_Folder));
Release_Dir);

elsif Id = "_ALIRE_TEST_" then
-- This is used to test the env var formatting feature
Expand Down
7 changes: 3 additions & 4 deletions src/alire/alire-environment-formatting.ads
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ with Alire.Releases;

package Alire.Environment.Formatting is

function Format (Rel : Releases.Release;
Value : String;
Is_Root_Release : Boolean)
function Format (Release_Dir : Any_Path;
Value : String)
return String;
-- Format the environment variable falue with ${} replacement patterns
-- Format the environment variable value with ${} replacement patterns

Unknown_Formatting_Key : exception;

Expand Down
76 changes: 68 additions & 8 deletions src/alire/alire-environment.adb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ with GNAT.OS_Lib;

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;

with Alire_Early_Elaboration;
with Alire.Properties.Environment; use Alire.Properties.Environment;
with Alire.Properties.Scenarios;
with Alire.OS_Lib;
with Alire.GPR;
with Alire.Roots;
with Alire.Solutions;
with Alire.Utils;
with Alire.Environment.Formatting;
with Alire.Utils.TTY;

with GNAT.IO;

package body Alire.Environment is

package TTY renames Utils.TTY;

---------
-- Add --
---------
Expand Down Expand Up @@ -62,20 +70,71 @@ package body Alire.Environment is
-- Load --
----------

procedure Load (This : in out Context;
Root : Alire.Roots.Root)
is
Solution : constant Solutions.Solution := Root.Solution;
begin

-- Warnings when setting up an incomplete environment

if not Solution.Is_Complete then
Trace.Debug ("Generating incomplete environment"
& " because of missing dependencies");

-- Normally we would generate a warning, but since that will pollute
-- the output making it unusable, for once we write directly to
-- stderr (unless quiet is in effect):

if not Alire_Early_Elaboration.Switch_Q then
GNAT.IO.Put_Line
(GNAT.IO.Standard_Error,
TTY.Warn ("warn:") & " Generating incomplete environment"
& " because of missing dependencies");
end if;
end if;

-- Project paths for all releases in the solution, implicitly defined by
-- supplied project files.

declare
Sorted_Paths : constant Alire.Utils.String_Set := Root.Project_Paths;
begin
if not Sorted_Paths.Is_Empty then
for Path of Sorted_Paths loop
This.Append ("GPR_PROJECT_PATH", Path, "crates");
end loop;
end if;
end;

-- Custom definitions provided by each release

for Rel of Solution.Releases.Including (Root.Release) loop
This.Load (Root => Root,
Crate => Rel.Name);
end loop;

This.Set ("ALIRE", "True", "Alire");
end Load;

----------
-- Load --
----------

procedure Load (This : in out Context;
Rel : Alire.Releases.Release;
Prop : Alire.Properties.Vector;
Is_Root_Release : Boolean)
Root : Roots.Root;
Crate : Crate_Name)
is
Rel : constant Releases.Release := Root.Release (Crate);
Origin : constant String := Rel.Name_Str;
begin

-- Enviromemnt variables defined in the crate manifest
for Act of Rel.Environment (Prop) loop
-- Environment variables defined in the crate manifest
for Act of Rel.Environment (Root.Environment) loop
begin
declare
Value : constant String :=
Formatting.Format (Rel, Act.Value, Is_Root_Release);
Formatting.Format (Root.Release_Base (Rel.Name), Act.Value);
begin
case Act.Action is

Expand All @@ -102,7 +161,7 @@ package body Alire.Environment is
end loop;

-- Environment variables for GPR external scenario variables
for Property of Rel.On_Platform_Properties (Prop) loop
for Property of Rel.On_Platform_Properties (Root.Environment) loop
if Property in Alire.Properties.Scenarios.Property'Class then
declare
use all type Alire.GPR.Variable_Kinds;
Expand Down Expand Up @@ -216,7 +275,8 @@ package body Alire.Environment is

when Properties.Environment.Set =>
Raise_Checked_Error
("Trying to set an alredy defined environment variable");
("Trying to set an alredy defined environment variable: "
& (+Key) & " is already defined as " & (+Value));

when Properties.Environment.Append =>
Value := Value & Separator & Act.Value;
Expand Down
23 changes: 14 additions & 9 deletions src/alire/alire-environment.ads
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ with Ada.Strings.Unbounded;
with Alire.Releases;
with Alire.Properties;
with Alire.Platforms;
limited with Alire.Roots;

private with Ada.Strings.Unbounded.Hash;
private with Ada.Containers.Vectors;
Expand All @@ -29,19 +30,17 @@ package Alire.Environment is
procedure Prepend (This : in out Context; Name, Value, Origin : String);
-- Prepend a value to a variable in the context

procedure Load (This : in out Context;
Rel : Alire.Releases.Release;
Prop : Alire.Properties.Vector;
Is_Root_Release : Boolean);
-- Load the enviroment variables of a release (GPR_PROJECT_PATH and custom
-- variables) in the context.
procedure Load (This : in out Context;
Root : Alire.Roots.Root);
-- Load the environment variables of a releases found in the workspace
-- Solution (GPR_PROJECT_PATH and custom variables) in the context.

procedure Export (This : Context);
-- Export the enviroment variables built from the variables previously
-- Export the environment variables built from the variables previously
-- loaded and defined in the context.

procedure Print_Shell (This : Context; Kind : Platforms.Shells);
-- Print the shell commands that can be used to export the enviroment
-- Print the shell commands that can be used to export the environment
-- variables.

procedure Print_Details (This : Context);
Expand All @@ -58,7 +57,7 @@ private
type Var_Array is array (Natural range <>) of Var;

function Compile (This : Context) return Var_Array;
-- Return an array of enviroment variable key/value built from the
-- Return an array of environment variable key/value built from the
-- variables previously loaded and defined in the context.

type Env_Action is record
Expand All @@ -84,4 +83,10 @@ private

procedure Add (This : in out Context; Name : String; Action : Env_Action);

procedure Load (This : in out Context;
Root : Roots.Root;
Crate : Crate_Name);
-- Load the environment variables of a release (GPR_PROJECT_PATH and custom
-- variables) in the context.

end Alire.Environment;
5 changes: 4 additions & 1 deletion src/alire/alire-releases.adb
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,10 @@ package body Alire.Releases is
for File of Files loop
if Contains (File, "/") then
Paths.Include
(File (File'First .. Fixed.Index (File, "/", Backward)));
(File (File'First .. Fixed.Index (File, "/", Backward) - 1));
Fabien-Chouteau marked this conversation as resolved.
Show resolved Hide resolved
-- To match the output of root crate paths and Ada.Directories
-- full path normalization, a path separator in the last
-- position is not included.
end if;
end loop;
end return;
Expand Down
Loading