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

New command exec and support of aliases #853

Merged
merged 3 commits into from
Sep 30, 2021
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
2 changes: 1 addition & 1 deletion alire.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ aaa = { url = "https://github.com/mosteo/aaa.git", commit = "22fcef5c465ece2f9cf
ajunitgen = { url = "https://github.com/mosteo/ajunitgen.git", commit = "e5d01db5e7834d15c4066f0a8e33d780deae3cc9" }
ada_toml = { url = "https://github.com/pmderodat/ada-toml.git", commit = "2a671ffb1039a036f2bb68bdc88afc8d3dc68c10" }
ansiada = { url = "https://github.com/mosteo/ansi-ada.git", commit = "acf9afca3afe1f8b8843c061f3cef860d7567307" }
clic = { url = "https://github.com/alire-project/clic.git", commit = "50fa7aed80e60972b96a873223256f7f88890d7d" }
clic = { url = "https://github.com/alire-project/clic.git", commit = "5a1f32326013784113a336a1aded509270dacc76" }
gnatcoll = { url = "https://github.com/alire-project/gnatcoll-core.git", commit = "f3bd1c51d12962879f52733e790b394f5bbfe05f" }
minirest = { url = "https://github.com/mosteo/minirest.git", commit = "4550aa356d55b9cd55f26acd34701f646021c5ff" }
optional = { url = "https://github.com/mosteo/optional.git", commit = "0c7d20c0c8b48ccb6b25fb648d48382e598c25c3" }
Expand Down
44 changes: 41 additions & 3 deletions doc/user-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,54 @@ stay on top of `alr` new features.

## Release 1.2-dev

### User defined command aliases

PR [#853](https://github.com/alire-project/alire/pull/853)

It is now possible to define in configuration (local or global) aliases for the
`alr` commands.

```console
$ alr config --set --global alias.graph 'show --graph'
$ alr graph
```
Will run the `alr show` command with the `--graph` switch.

### New command `alr exec -- <command line>`

PR [#853](https://github.com/alire-project/alire/pull/853)

This new command takes an executable and arguments and run them in the Alire
environment/context of the current crate.

```console
$ alr exec -- sh -c 'echo ${ALIRE}'
True
```

### Pass alr clean switches to gprclean

PR [#853](https://github.com/alire-project/alire/pull/853)

Using the `--` delimiter the switches and arguments for `alr clean` can now be
passed to the underlying `gprclean` execution.

For instance:
```console
$ alr clean -- -XTEST=42
```


### Pass alr build switches to gprbuild

PR [#850](https://github.com/alire-project/alire/pull/850)

All the switches and arguments for `alr build` are now passed to the underlying
`gprbuild` execution.
Using the `--` delimiter, the switches and arguments for `alr build` are now
passed to the underlying `gprbuild` execution.

For instance:
```console
$ alr build -f
$ alr build -- -f
```
will force recompilation.

Expand Down
9 changes: 5 additions & 4 deletions src/alr/alr-commands-build.ads
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ package Alr.Commands.Build is
is ("build");

overriding
function Switches_As_Args (This : Command) return Boolean
is (True);
-- For the build command we want all the args to pass them to gprbuild
function Switch_Parsing (This : Command)
return CLIC.Subcommand.Switch_Parsing_Kind
is (CLIC.Subcommand.Before_Double_Dash);
-- For the build command we want the args after -- to pass them to gprbuild

overriding
procedure Execute (Cmd : in out Command;
Expand Down Expand Up @@ -40,6 +41,6 @@ package Alr.Commands.Build is

overriding
function Usage_Custom_Parameters (Cmd : Command) return String
is ("[gprbuild switches and arguments]");
is ("[--] [gprbuild switches and arguments]");

end Alr.Commands.Build;
13 changes: 5 additions & 8 deletions src/alr/alr-commands-clean.adb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ with Ada.Directories;

with Alire.Config.Edit;
with Alire.Directories;
with Alire.GPR;
with Alire.Paths;
with Alire.Spawn;

Expand Down Expand Up @@ -122,10 +121,6 @@ package body Alr.Commands.Clean is
use AAA.Strings;
begin

if Args.Count /= 0 then
Reportaise_Wrong_Arguments (Cmd.Name & " doesn't take arguments");
end if;

if not (Cmd.Cache or else Cmd.Temp) then
Cmd.Requires_Valid_Session;
Cmd.Root.Export_Build_Environment;
Expand All @@ -141,13 +136,17 @@ package body Alr.Commands.Clean is
Empty_Vector &
"-r" &
"-P" & Gpr_File &
Scenario.As_Command_Line,
Args,
Understands_Verbose => True);
end loop;

return;
end if;

if Args.Count /= 0 then
Reportaise_Wrong_Arguments (Cmd.Name & " doesn't take arguments");
end if;

if Cmd.Cache then

-- We do not want to use Cmd.Root here, as it will check for a valid
Expand Down Expand Up @@ -216,8 +215,6 @@ package body Alr.Commands.Clean is
Cmd.Temp'Access,
Long_Switch => "--temp",
Help => "Delete dangling temporary files");

Add_GPR_Scenario_Switch (Config);
end Setup_Switches;

end Alr.Commands.Clean;
8 changes: 7 additions & 1 deletion src/alr/alr-commands-clean.ads
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ package Alr.Commands.Clean is
function Name (Cmd : Command) return CLIC.Subcommand.Identifier
is ("clean");

overriding
function Switch_Parsing (This : Command)
return CLIC.Subcommand.Switch_Parsing_Kind
is (CLIC.Subcommand.Before_Double_Dash);
-- For the clean command we want the args after -- to pass them to gprclean

overriding
procedure Execute (Cmd : in out Command;
Args : AAA.Strings.Vector);
Expand All @@ -27,7 +33,7 @@ package Alr.Commands.Clean is

overriding
function Usage_Custom_Parameters (Cmd : Command) return String
is ("[--cache] [--temp]");
is ("[--cache] [--temp] [--] [gprclean switches and arguments]");

private

Expand Down
2 changes: 1 addition & 1 deletion src/alr/alr-commands-config.adb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ package body Alr.Commands.Config is
Trace.Always
(CLIC.Config.Info.List
(Alire.Config.DB,
Filter => "*",
Filter => ".*",
Show_Origin => Cmd.Show_Origin).Flatten (ASCII.LF));
when 1 =>
Trace.Always
Expand Down
2 changes: 1 addition & 1 deletion src/alr/alr-commands-config.ads
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ package Alr.Commands.Config is

overriding
function Usage_Custom_Parameters (Cmd : Command) return String is
("[--list] [--show-origin] [key_glob] |" &
("[--list] [--show-origin] [key_regex] |" &
" --get <key> |" &
" --set <key> <value> |" &
" --unset <key>");
Expand Down
36 changes: 36 additions & 0 deletions src/alr/alr-commands-exec.adb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
with Alire.Spawn;

package body Alr.Commands.Exec is

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

overriding
procedure Execute (Cmd : in out Command;
Args : AAA.Strings.Vector)
is
use AAA.Strings;
begin
if Args.Is_Empty then
Reportaise_Wrong_Arguments
(Cmd.Name & " takes at least one argument");
end if;

declare
Cmd_Args : AAA.Strings.Vector := Args;
Cmd_Name : constant String := Cmd_Args.First_Element;
begin
Cmd.Requires_Valid_Session;
Cmd.Root.Export_Build_Environment;

-- Remove command name from the arguments
Cmd_Args.Delete_First;

Alire.Spawn.Command (Cmd => Cmd_Name,
Args => Cmd_Args,
Understands_Verbose => False);
end;
end Execute;

end Alr.Commands.Exec;
48 changes: 48 additions & 0 deletions src/alr/alr-commands-exec.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
with AAA.Strings;

package Alr.Commands.Exec is

type Command is new Commands.Command with private;

overriding
function Name (Cmd : Command) return CLIC.Subcommand.Identifier
is ("exec");

overriding
function Switch_Parsing (This : Command)
return CLIC.Subcommand.Switch_Parsing_Kind
is (CLIC.Subcommand.Before_Double_Dash);

overriding
procedure Execute (Cmd : in out Command;
Args : AAA.Strings.Vector);

overriding
function Long_Description (Cmd : Command)
return AAA.Strings.Vector
is (AAA.Strings.Empty_Vector
.Append ("Alr sets up the environment variables (GPR_PROJECT_PATH, ")
.Append ("PATH, etc.) and then spawns the given command.")
.New_Line
.Append ("This can be used to run tools or scripts on Alire projects.")
);

overriding
procedure Setup_Switches
(Cmd : in out Command;
Config : in out CLIC.Subcommand.Switches_Configuration)
is null;

overriding
function Short_Description (Cmd : Command) return String
is ("Run the given command in the alire project context");

overriding
function Usage_Custom_Parameters (Cmd : Command) return String
is ("[--] <executable/script> [<switches and arguments>]");

private

type Command is new Commands.Command with null record;

end Alr.Commands.Exec;
30 changes: 30 additions & 0 deletions src/alr/alr-commands-topics-aliases.ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
with AAA.Strings;

with CLIC.Subcommand;

package Alr.Commands.Topics.Aliases is

type Topic is new CLIC.Subcommand.Help_Topic with null record;

overriding
function Name (This : Topic) return CLIC.Subcommand.Identifier
is ("aliases");

overriding
function Title (This : Topic) return String
is ("User defined command aliases");

overriding
function Content (This : Topic) return AAA.Strings.Vector
is (AAA.Strings.Empty_Vector
.Append ("Command aliases can be defined in local or global ")
.Append ("configuration.")
.New_Line
.Append ("For example the following command:")
.Append ("""$ alr config --set --global alias.graph 'show --graph'""")
.Append ("Defines a global alias for the 'show' command with a ")
.Append ("'--graph' switch.")
.New_Line
.Append ("""$ alr graph"" is equivalent to ""alr show --graph"""));

end Alr.Commands.Topics.Aliases;
48 changes: 7 additions & 41 deletions src/alr/alr-commands.adb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ with Alr.Commands.Clean;
with Alr.Commands.Config;
with Alr.Commands.Dev;
with Alr.Commands.Edit;
with Alr.Commands.Exec;
with Alr.Commands.Get;
with Alr.Commands.Index;
with Alr.Commands.Init;
Expand All @@ -38,9 +39,9 @@ with Alr.Commands.Withing;

with Alr.Commands.Topics.Naming_Convention;
with Alr.Commands.Topics.Toolchains;
with Alr.Commands.Topics.Aliases;

with GNAT.OS_Lib;
with GNAT.Source_Info;

with GNATCOLL.VFS;
with Alr.Platform;
Expand Down Expand Up @@ -407,6 +408,8 @@ package body Alr.Commands is
Create_Alire_Folders;

begin
Sub_Cmd.Load_Aliases (Alire.Config.DB);

Sub_Cmd.Execute;
Log ("alr " & Sub_Cmd.What_Command & " done", Detail);
exception
Expand Down Expand Up @@ -478,46 +481,6 @@ package body Alr.Commands is
Cmd.Optional_Root := Alire.Roots.Optional.Outcome_Success (Root);
end Set;

---------------------
-- Handle_X_Switch --
---------------------

procedure Handle_X_Switch (Switch, Value : String) is
use AAA.Strings;
begin
if Switch /= "-X" then
Reportaise_Command_Failed ("Unexpected switch '" & Switch & "' in " &
GNAT.Source_Info.Enclosing_Entity);
end if;

declare
S : constant Vector := Split (Value, '=');
begin
if S.Count /= 2 then
Reportaise_Wrong_Arguments ("Invalid switch '" &
Switch & Value & "'");
end if;
Alire.GPR.Add_Argument (Scenario, S (1), S (2));
end;
end Handle_X_Switch;

-----------------------------
-- Add_GPR_Scenario_Switch --
-----------------------------

procedure Add_GPR_Scenario_Switch
(Config : in out CLIC.Subcommand.Switches_Configuration)
is

begin
CLIC.Subcommand.Define_Switch
(Config,
Callback => Handle_X_Switch'Access,
Switch => "-X!",
Help => "Scenario variable for gprbuild/gprclean",
Argument => "Var=Arg");
end Add_GPR_Scenario_Switch;

begin

-- Commands --
Expand All @@ -533,6 +496,7 @@ begin
Sub_Cmd.Register ("Build", new Edit.Command);
Sub_Cmd.Register ("Build", new Run.Command);
Sub_Cmd.Register ("Build", new Test.Command);
Sub_Cmd.Register ("Build", new Exec.Command);

Sub_Cmd.Register ("Index", new Get.Command);
Sub_Cmd.Register ("Index", new Index.Command);
Expand All @@ -548,4 +512,6 @@ begin
-- Help topics --
Sub_Cmd.Register (new Topics.Naming_Convention.Topic);
Sub_Cmd.Register (new Topics.Toolchains.Topic);
Sub_Cmd.Register (new Topics.Aliases.Topic);

end Alr.Commands;
Loading