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

feat: structured output with alr --format show #1851

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@
[submodule "deps/lml"]
path = deps/lml
url = https://github.com/mosteo/lml_ada
[submodule "deps/yeison"]
path = deps/yeison
url = https://github.com/mosteo/yeison
17 changes: 13 additions & 4 deletions alire.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ diskflags = "~0.1"
gnatcoll = "^21"
lml = "~0.1"
minirest = "~0.3"
optional = "~0.1"
optional = "~0.1.1"
semantic_versioning = "^3.0"
simple_logging = "^2.0"
si_units = "~0.2.2"
Expand All @@ -53,7 +53,7 @@ windows = { ALIRE_OS = "windows" }
[[pins]]
[pins.aaa]
url = "https://github.com/mosteo/aaa"
commit = "ddfeffe2d6c8f9d19161df7b31d16d37bef4ba71"
commit = "04450085232edda086d7376a6dcaf17ab6b45aa9"

[pins.ada_toml]
url = "https://github.com/pmderodat/ada-toml"
Expand All @@ -72,7 +72,7 @@ url = "https://github.com/alire-project/clic"
commit = "56bbdc008e16996b6f76e443fd0165a240de1b13"
[pins.den]
url = "https://github.com/mosteo/den"
commit = "653a4c9ba4469d7e1a8896088789b6514ecdf834"
commit = "377d1d906f83262b81b45c7989de8cd5b4aba67f"

[pins.dirty_booleans]
url = "https://github.com/mosteo/dirty_booleans"
Expand All @@ -87,12 +87,16 @@ url = "https://github.com/alire-project/gnatcoll-core.git"
commit = "4e663b87a028252e7e074f054f8f453661397166"
[pins.lml]
url = "https://github.com/mosteo/lml_ada.git"
commit = "ae156ef82a2fedb7e28bb4dcaeb3d5c0a2e046ec"
commit = "3d79edaff98bc4985014c641327ceb22d655df6b"

[pins.minirest]
url = "https://github.com/mosteo/minirest.git"
commit = "9a9c660f9c6f27f5ef75417e7fac7061dff14d78"

[pins.optional]
url = "https://github.com/mosteo/optional.git"
commit = "7b8132a09a6c3c467409ab15d34fac605b1e5711"

[pins.semantic_versioning]
url = "https://github.com/alire-project/semantic_versioning"
commit = "4861e32bd8a2f0df038d3ecc9a72b6381e7a34cc"
Expand All @@ -113,6 +117,11 @@ commit = "f607a63b714f09bbf6126de9851cbc21cf8666c9"
url = "https://github.com/mosteo/toml_slicer"
commit = "3e5cbdb5673b85a1da6344a41764ef1cbafe3289"

[pins.yeison_12]
url = "https://github.com/mosteo/yeison"
subdir = "yeison_12"
commit = "88b66242f8826fd4847e0ea06c7901b753e03c74"

# To disable version updating, export ALR_VERSION_DONT_PATCH with any value

# Before building, we add the commit to the version, for unique identification:
Expand Down
3 changes: 2 additions & 1 deletion alr_env.gpr
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ aggregate project Alr_Env is
"deps/toml_slicer",
"deps/umwi",
"deps/uri-ada",
"deps/xmlezout"
"deps/xmlezout",
"deps/yeison/yeison_12"
);

for Project_Files use ("alr.gpr");
Expand Down
2 changes: 1 addition & 1 deletion deps/aaa
2 changes: 1 addition & 1 deletion deps/ada-toml
Submodule ada-toml updated 1 files
+1 −1 src/toml.adb
2 changes: 1 addition & 1 deletion deps/den
Submodule den updated 1 files
+0 −9 alire.toml
2 changes: 1 addition & 1 deletion deps/optional
1 change: 1 addition & 0 deletions deps/yeison
Submodule yeison added at 88b662
2 changes: 2 additions & 0 deletions dev/reset-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Restore version in metadata to unknown
git checkout -- src/alire/alire-meta.ads
20 changes: 20 additions & 0 deletions doc/user-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@ stay on top of `alr` new features.

## Release `2.1`

### New `--format` global switch to produce structured output

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

The global switch `--format` can be used to produce JSON format with some
commands, e.g., `index`, `search`, `show`.

This new switch accepts an optional parameter that can be JSON, TOML or YAML, to
select the desired output language:

```
$ alr --format=TOML search --crates hello
[[data]]
description = "'Hello, world!' demonstration project"
name = "hello"
[[data]]
description = "Basic library demonstration project"
name = "libhello"
```

### Allow pinning a crate in a subdirectory of a git repository

PR [#1857](https://github.com/alire-project/alire/pull/1857)
Expand Down
11 changes: 10 additions & 1 deletion src/alire/alire-dependencies-graphs.adb
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,22 @@ package body Alire.Dependencies.Graphs is

Filtered : constant Graph := This.Filtering_Unused (Solution.Crates);
begin
if Utils.Tables.Structured_Output then
Table.Header ("Dependent").Header ("Dependency").New_Row;
end if;

for Dep of Filtered loop
Table.Append
(Prefix & Label_Dependent (+Dep.Dependent, Solution, TTY => True));
Table.Append ("-->");

if not Utils.Tables.Structured_Output then
Table.Append ("-->");
end if;

Table.Append
(Label_Dependee
(+Dep.Dependent, +Dep.Dependee, Solution, For_Plot => False));

Table.New_Row;
end loop;

Expand Down
16 changes: 16 additions & 0 deletions src/alire/alire-formatting.adb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ with Alire.OS_Lib;
with Alire.Platforms.Current;
with Alire.Version;

with LML.Input.TOML;
with LML.Output.Factory;

package body Alire.Formatting is

--------------
Expand Down Expand Up @@ -216,4 +219,17 @@ package body Alire.Formatting is
end if;
end Format;

-----------
-- Print --
-----------

procedure Print (This : TOML.TOML_Value;
Format : Formats := Utils.Tables.Structured_Output_Format)
is
Builder : LML.Output.Builder'Class := LML.Output.Factory.Get (Format);
begin
LML.Input.TOML.From_TOML (This, Builder);
Trace.Always (LML.Encode (Builder.To_Text));
end Print;

end Alire.Formatting;
18 changes: 18 additions & 0 deletions src/alire/alire-formatting.ads
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
with Alire.Roots;
with Alire.Utils.Tables;

with TOML;

private with Ada.Containers.Indefinite_Ordered_Maps;

Expand Down Expand Up @@ -49,6 +52,21 @@ package Alire.Formatting is

Unknown_Formatting_Key : exception;

------------------------------
-- Structured Data Output --
------------------------------

-- Features to dump lightweight markup when --format is in effect

subtype Formats is Utils.Tables.Formats;

Structured_Output : Boolean renames Utils.Tables.Structured_Output;

procedure Print (This : TOML.TOML_Value;
Format : Formats := Utils.Tables.Structured_Output_Format);
-- We require TOML input because that is what's currently being generated
-- everywhere. The actual output is according to Utils.Tables.Format

private

package Pattern_String_Maps is new
Expand Down
51 changes: 47 additions & 4 deletions src/alire/alire-releases.adb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ with Alire.Directories;
with Alire.Defaults;
with Alire.Errors;
with Alire.Flags;
with Alire.Formatting;
with Alire.Origins.Deployers.System;
with Alire.Paths;
with Alire.Properties.Bool;
with Alire.Properties.Scenarios;
with Alire.TOML_Load;
with Alire.Utils.Tables;
with Alire.Utils.YAML;
with Alire.Warnings;

Expand Down Expand Up @@ -484,7 +486,8 @@ package body Alire.Releases is
Pins => Base.Pins,
Forbidden => Base.Forbidden,
Properties => Base.Properties,
Available => Base.Available)
Available => Base.Available,
Imported => Base.Imported)
do
null;
end return;
Expand Down Expand Up @@ -539,7 +542,8 @@ package body Alire.Releases is
Pins => <>,
Forbidden => Conditional.For_Dependencies.Empty,
Properties => Properties,
Available => Available);
Available => Available,
Imported => No_TOML_Value);

-----------------------
-- New_Empty_Release --
Expand Down Expand Up @@ -572,7 +576,8 @@ package body Alire.Releases is
Pins => <>,
Forbidden => Conditional.For_Dependencies.Empty,
Properties => Properties,
Available => Conditional.Empty
Available => Conditional.Empty,
Imported => No_TOML_Value
);

-------------------------
Expand Down Expand Up @@ -845,6 +850,33 @@ package body Alire.Releases is
procedure Print (R : Release) is
use GNAT.IO;
begin
if Alire.Utils.Tables.Structured_Output then
if R.Imported.Is_Present then
-- This field may be missing if R.Whenever has been used, in which
-- case we properly want to print the re-exported information
-- without dynamic expressions (else branch below). It may be also
-- missing for releases being created from scratch during `alr
-- init`, but there's no way for a user to get us here until
-- after the release has been reloaded from its manifest.
Formatting.Print (R.Imported.all);
else
if R.Properties.Is_Unconditional then
Formatting.Print
(R.To_TOML
(if R.Origin.Kind in Origins.Filesystem
then Manifest.Local
else Manifest.Index));
else
-- Shouldn't happen as conditional releases should have the
-- Imported field populated (they always come from a loaded
-- manifest).
raise Program_Error with
"Cannot export release with dynamic information";
end if;
end if;
return;
end if;

-- MILESTONE
Put_Line (R.Milestone.TTY_Image & ": " & R.TTY_Description);

Expand Down Expand Up @@ -1017,6 +1049,12 @@ package body Alire.Releases is
return This : Release := New_Empty_Release
(Name => +From.Unwrap.Get (TOML_Keys.Name).As_String)
do
-- Keep the original TOML to be able to export with conditional
-- expressions unresolved. Keep a copy since TOML is using reference
-- semantics.

This.Imported.all := From.Unwrap.Clone;

-- Extract the version ASAP to show it properly during logging

if From.Contains (TOML_Keys.Version) then
Expand Down Expand Up @@ -1271,7 +1309,12 @@ package body Alire.Releases is
Pins => R.Pins,
Forbidden => R.Forbidden.Evaluate (P),
Properties => R.Properties.Evaluate (P),
Available => R.Available.Evaluate (P));
Available => R.Available.Evaluate (P),

Imported => No_TOML_Value
-- We are discarding information above, so the imported information
-- would no longer match.
);

----------------------
-- Long_Description --
Expand Down
19 changes: 18 additions & 1 deletion src/alire/alire-releases.ads
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ with Semantic_Versioning;
with TOML;

private with Alire.OS_Lib;
private with CLIC.TTY;
private with Alire.Utils.TTY;
private with CLIC.TTY;

package Alire.Releases is

Expand Down Expand Up @@ -398,6 +398,11 @@ private
return Alire.Properties.Vector;
-- Properties that R has under platform properties P

type TOML_Value_Ptr is access TOML.TOML_Value;

function No_TOML_Value return TOML_Value_Ptr
is (new TOML.TOML_Value'(TOML.No_TOML_Value));

type Release (Prj_Len,
Notes_Len : Natural)
is new Interfaces.Yamlable
Expand All @@ -412,6 +417,18 @@ private
Forbidden : Conditional.Dependencies;
Properties : Conditional.Properties;
Available : Conditional.Availability;

Imported : TOML_Value_Ptr := No_TOML_Value;
-- For releases loaded from a manifest, this is the original structured
-- data that generated it, in which case Imported.Is_Present.
--
-- GNATs<14 have trouble with this value, raising during main Ada lib
-- finalization. Given that GNAT 14 is happy, and that TOML_Value is
-- a by-reference type internally, it seems pretty likely this is some
-- obscure bug in older GNATs. The only workaround I've found at this
-- time is to avoid finalization by explicitly allocating the value.
-- This is leaky, so we may want to revisit this issue in the future.
-- TODO: find better workaround.
end record;

function From_TOML (This : in out Release;
Expand Down
2 changes: 2 additions & 0 deletions src/alr/alr-commands-action.adb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ package body Alr.Commands.Action is
procedure Execute (Cmd : in out Command;
Args : AAA.Strings.Vector) is
begin
Cmd.Forbids_Structured_Output;

if Args.Is_Empty then
Cmd.List;
else
Expand Down
2 changes: 2 additions & 0 deletions src/alr/alr-commands-build.adb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ package body Alr.Commands.Build is
is
use type Alire.Builds.Stop_Points;
begin
Cmd.Forbids_Structured_Output;

-- Prevent premature update of dependencies, as the exact folders
-- will depend on the build hashes, which are yet unknown until
-- build profiles are applied.
Expand Down
Loading
Loading