Skip to content

Commit

Permalink
Merge pull request #5 from nfdi4plants/ro-crate
Browse files Browse the repository at this point in the history
Include ro-crate and rework output format selection and handling
  • Loading branch information
HLWeil authored Aug 1, 2024
2 parents fe3bba5 + d1c7044 commit b0f7a95
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 274 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# arc-export
Automatic building of a Docker container for exporting ARCs to Arc.json

# Setup

## local build
```shell
docker build . -t arc-export
Expand All @@ -14,3 +16,38 @@ docker pull ghcr.io/nfdi4plants/arc-export:main

docker run -v C:\Repos\ArcRepo:/arc ghcr.io/nfdi4plants/arc-export:main /arc-export -p arc
```

# Basic usage

## Output format Selection

Output format can be specified with the `-f` flag, followed by one of the following:

- `isa-json` will produce a `arc-isa.json` file
- `rocrate-metadata` will produce a `arc-ro-crate-metadata.json` file
- `summary-markdown` will produce a `arc-summary.md` file

E.g.

```shell
docker run -v C:\Repos\ArcRepo:/arc arc-export:latest /arc-export -p arc -f rocrate-metadata -f isa-json -f summary-markdown
```

will produce all three output files.

## Help

```cli
USAGE: arc-export [--help] --arc-directory <path> [--out-directory <path>]
[--output-format <isa-json|rocrate-metadata|summary-markdown>]
OPTIONS:
--arc-directory, -p <path>
Specify a directory that contains the arc to convert.
--out-directory, -o <path>
Optional. Specify a output directory for the invenio metadata record.
--output-format, -f <isa-json|rocrate-metadata|summary-markdown>
Optional. Specify the output format. Default is ISA-JSON.
--help display this list of options.
```
111 changes: 0 additions & 111 deletions src/arc-export/ARCExtension.fs

This file was deleted.

9 changes: 8 additions & 1 deletion src/arc-export/CLIArgs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@
open Argu
open System.IO

type OutputFormat =
| ISA_Json
| ROCrate_Metadata
| Summary_Markdown

type CliArguments =
| [<Mandatory>][<AltCommandLine("-p")>] ARC_Directory of path:string
| [<AltCommandLine("-o")>] Out_Directory of path:string
| [<Unique>][<AltCommandLine("-o")>] Out_Directory of path:string
| [<AltCommandLine("-f")>] Output_Format of OutputFormat

// to-do once we have a common schema, implement as optional flag to validate all generated json via schema
//| [<AltCommandLine("-val")>] Validate
Expand All @@ -15,6 +21,7 @@ type CliArguments =
match s with
| ARC_Directory _ -> "Specify a directory that contains the arc to convert."
| Out_Directory _ -> "Optional. Specify a output directory for the invenio metadata record."
| Output_Format _ -> "Optional. Specify the output format. Default is ISA-JSON."
// to-do once we have a common schema, implement as optional flag to validate all generated json via schema
//| Validate -> "Optional. Validate the output against the metadata record schema"

Expand Down
90 changes: 22 additions & 68 deletions src/arc-export/Program.fs
Original file line number Diff line number Diff line change
@@ -1,90 +1,44 @@
open System.IO
open ARCtrl
open ARCtrl.FileSystem
open ARCtrl.ISA
open ARCtrl.NET
open Argu
open FileSystemTreeExtension
open ARCExtension
open ArcSummaryMarkdown
open ARCtrl.NET.Contract

let getAllFilePaths (directoryPath : string) =
let directoryPath = System.IO.Path.GetFullPath(directoryPath)
let rec allFiles dirs =
if Seq.isEmpty dirs then Seq.empty else
seq { yield! dirs |> Seq.collect Directory.EnumerateFiles
yield! dirs |> Seq.collect Directory.EnumerateDirectories |> allFiles }

allFiles [directoryPath]
|> Seq.toArray
|> Array.map System.IO.Path.GetFullPath
|> Array.map (fun p -> p.Replace(directoryPath, "").Replace("\\","/"))

let loadARCCustom (arcPath : string) =

//// EINFACH DIESE ZEIELE AUSTAUSCHEN

let paths = getAllFilePaths arcPath

let arc = ARC.fromFilePaths paths

let contracts = arc.GetReadContracts()

let fulFilledContracts =
contracts
|> Array.map (fulfillReadContract arcPath)

arc.SetISAFromContracts(fulFilledContracts,true)
arc

try
let args = CLIArgs.cliArgParser.ParseCommandLine()

let arcPath = args.GetResult(CLIArgs.ARC_Directory)

let outPath =
let outDir =
args.TryGetResult(CLIArgs.Out_Directory)
|>Option.defaultValue arcPath

let jsonFile = Path.Combine(outPath,"arc.json")

let mdfile = Path.Combine(outPath,"arc-summary.md")


let inv, mdContent =
try
let arc = loadARCCustom arcPath
let registeredPayload =
try
arc.GetRegisteredPayload(IgnoreHidden = true)
with err ->
printfn "Could not get payload content, defaulting to all filesystem entries."
printfn "error: %s" err.Message
arc.FileSystem.Tree

let inv = arc.ISA |> Option.get

getAllFilePaths arcPath |> Seq.iter (printfn "%s")

inv,
MARKDOWN_TEMPLATE
.Replace("[[ARC_TITLE]]", inv.Title |> Option.defaultValue "Untitled ARC")
.Replace("[[FILE_TREE]]", FileSystemTree.toMarkdownTOC registeredPayload)
with
| err ->
let arc =

try ARC.load arcPath with
| err ->
printfn "Could not read investigation, writing empty arc json."
let comment1 = Comment.fromString "Status" "Could not parse ARC"
let comment2 = Comment.fromString "ErrorMessage" $"Could not parse ARC:\n{err.Message}"
ArcInvestigation(Identifier.createMissingIdentifier() , comments = [|comment1;comment2|]),
"Could not read investigation, unable to display registered file content."
let comment1 = Comment("Status","Could not parse ARC")
let comment2 = Comment("ErrorMessage",$"Could not parse ARC:\n{err.Message}")
let fs = ARCtrl.NET.Path.getAllFilePaths arcPath |> FileSystemTree.fromFilePaths |> FileSystem.create
let inv =
ArcInvestigation(Helper.Identifier.createMissingIdentifier() , comments = ResizeArray [|comment1;comment2|])
let arc = ARC(inv,fs = fs)
arc

let outputFormats = args.GetResults(CLIArgs.Output_Format)

//args.Contains(CLIArgs.Output_Format)

if outputFormats |> List.contains CLIArgs.OutputFormat.ISA_Json || List.isEmpty outputFormats then
Writers.write_isa_json outDir arc

File.WriteAllText(mdfile, mdContent)
if outputFormats |> List.contains CLIArgs.OutputFormat.ROCrate_Metadata then
Writers.write_ro_crate_metadata outDir arc

inv
|> ARCtrl.ISA.Json.ArcInvestigation.toString
|> fun json -> File.WriteAllText(jsonFile, json)
if outputFormats |> List.contains CLIArgs.OutputFormat.Summary_Markdown then
Writers.write_arc_summary_markdown outDir arc

with
| :? ArguParseException as ex ->
Expand Down
37 changes: 37 additions & 0 deletions src/arc-export/Writers.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Writers

open ARCtrl
open ARCtrl.Json
open System.IO
open ARCtrl.FileSystem
open ArcSummaryMarkdown

[<Literal>]
let ro_crate_metadata_filename = "arc-ro-crate-metadata.json"

[<Literal>]
let isa_json_filename = "arc-isa.json"

[<Literal>]
let arc_summary_markdown_filename = "arc-summary.md"

let write_ro_crate_metadata (outDir: string) (arc: ARC) =
let ro_crate_metadata = ARC.toROCrateJsonString(2) arc
let ro_crate_metadata_path = Path.Combine(outDir, ro_crate_metadata_filename)
File.WriteAllText(ro_crate_metadata_path, ro_crate_metadata)

let write_isa_json (outDir: string) (arc: ARC) =
let inv = arc.ISA |> Option.get
let isa_json = inv.ToROCrateJsonString(2)
let isa_json_path = Path.Combine(outDir, isa_json_filename)
File.WriteAllText(isa_json_path, isa_json)

let write_arc_summary_markdown (outDir: string) (arc: ARC) =
let inv = arc.ISA |> Option.get
let registeredPayload = arc.GetRegisteredPayload(IgnoreHidden = true)
let markdownContent =
MARKDOWN_TEMPLATE
.Replace("[[ARC_TITLE]]", inv.Title |> Option.defaultValue "Untitled ARC")
.Replace("[[FILE_TREE]]", FileSystemTree.toMarkdownTOC registeredPayload)
let arc_summary_markdown_path = Path.Combine(outDir, arc_summary_markdown_filename)
File.WriteAllText(arc_summary_markdown_path, markdownContent)
7 changes: 3 additions & 4 deletions src/arc-export/arc-export.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
<Compile Include="Defaults.fs" />
<Compile Include="CLIArgs.fs" />
<Compile Include="FileSystemTreeExtension.fs" />
<Compile Include="ARCExtension.fs" />
<Compile Include="ArcSummaryMarkdown.fs" />
<Compile Include="Writers.fs" />
<Compile Include="Program.fs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ARCtrl" Version="[1.0.0-alpha3]" />
<PackageReference Include="ARCtrl.NET" Version="[1.0.0-alpha1]" />
<PackageReference Include="Argu" Version="[6.1.1]" />
<PackageReference Include="ARCtrl.NET" Version="2.0.0-beta.1" />
<PackageReference Include="Argu" Version="6.2.4" />
<PackageReference Include="JsonDSL" Version="[0.1.0]" />
<PackageReference Update="FSharp.Core" Version="7.*" />
</ItemGroup>
Expand Down
Loading

0 comments on commit b0f7a95

Please sign in to comment.