Skip to content
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
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@ The SpdxTool can be driven using workflow yaml files of the following format:
```yaml
steps:
- command: <command-name>
<arguments mapping>
inputs:
<arguments mapping>

- command: <command-name>
<arguments mapping>
inputs:
<arguments mapping>
```

## YAML Commands
Expand All @@ -67,24 +69,30 @@ steps:

# Run a separate workflow file
- command: run-workflow
file: other-workflow-file.yaml
inputs:
file: other-workflow-file.yaml
parameters:
<optional parameters>

# Create a summary markdown from the specified SPDX document
- command: to-markdown
spdx: input.spdx.json
markdown: output.md
inputs:
spdx: input.spdx.json
markdown: output.md

# Rename the SPDX-ID of an element in an SPDX document
- command: rename-id
spdx: <spdx.json>
old: <old-id>
new: <new-id>
inputs:
spdx: <spdx.json>
old: <old-id>
new: <new-id>

# Copy a package from one SPDX document to another SPDX document
- command: copy-package
from: <from.spdx.json>
to: <to.spdx.json>
package: <package>
relationship: <relationship>
element: <element>
inputs:
from: <from.spdx.json>
to: <to.spdx.json>
package: <package>
relationship: <relationship>
element: <element>
```
87 changes: 85 additions & 2 deletions src/DemaConsulting.SpdxTool/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,88 @@ public abstract class Command
/// Run the command
/// </summary>
/// <param name="step">Command step</param>
public abstract void Run(YamlMappingNode step);
}
/// <param name="variables">Workflow variables</param>
public abstract void Run(YamlMappingNode step, Dictionary<string, string> variables);

/// <summary>
/// Expand variables in text
/// </summary>
/// <param name="text">Text to expand</param>
/// <param name="variables">Variables</param>
/// <returns>Expanded text</returns>
/// <exception cref="InvalidOperationException">on error</exception>
public static string Expand(string text, Dictionary<string, string> variables)
{
while (true)
{
// Find the last macro to expand
var start = text.LastIndexOf("${{", StringComparison.Ordinal);
if (start < 0)
return text;

// Find the end of the macro
var end = text.IndexOf("}}", start, StringComparison.Ordinal);
if (end < 0)
throw new InvalidOperationException("Unmatched '${{' in variable expansion");

// Get the variable name
var name = text[(start + 3)..end].Trim();

// Replace the variable
if (!variables.TryGetValue(name, out var value))
throw new InvalidOperationException($"Undefined variable {name}");

// Apply the replacement
text = text[..start] + value + text[(end + 2)..];
}
}

/// <summary>
/// Get a map from a map
/// </summary>
/// <param name="map">Parent map node</param>
/// <param name="name">Entry name</param>
/// <returns>Child map node or null</returns>
public static YamlMappingNode? GetMapMap(YamlMappingNode? map, string name)
{
// Handle null map
if (map == null)
return null;

// Get the entry
return map.Children.TryGetValue(name, out var value) ? value as YamlMappingNode : null;
}

/// <summary>
/// Get a sequence from a map
/// </summary>
/// <param name="map">Parent map node</param>
/// <param name="name">Entry name</param>
/// <returns>Child sequence node or null</returns>
public static YamlSequenceNode? GetMapSequence(YamlMappingNode? map, string name)
{
// Handle null map
if (map == null)
return null;

// Get the entry
return map.Children.TryGetValue(name, out var value) ? value as YamlSequenceNode : null;
}

/// <summary>
/// Get a map value from a map
/// </summary>
/// <param name="map">Map node</param>
/// <param name="key">Map key</param>
/// <param name="variables">Variables for expansion</param>
/// <returns>Map value or null</returns>
public static string? GetMapString(YamlMappingNode? map, string key, Dictionary<string, string> variables)
{
// Handle null map
if (map == null)
return null;

// Get the parameter
return map.Children.TryGetValue(key, out var value) ? Expand(value.ToString(), variables) : null;
}
}
49 changes: 26 additions & 23 deletions src/DemaConsulting.SpdxTool/Commands/CopyPackageCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ public class CopyPackageCommand : Command
"",
"From a YAML file this can be used as:",
" - command: copy-package",
" from: <from.spdx.json>",
" to: <to.spdx.json>",
" package: <package>",
" relationship: <relationship>",
" element: <element>",
" inputs:",
" from: <from.spdx.json>",
" to: <to.spdx.json>",
" package: <package>",
" relationship: <relationship>",
" element: <element>",
"",
"The <package> argument is the name of a package in <from.spdx.json> to copy.",
"The <relationship> argument describes the <element> relationship to <package>.",
Expand Down Expand Up @@ -65,31 +66,33 @@ public override void Run(string[] args)
}

/// <inheritdoc />
public override void Run(YamlMappingNode step)
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the from-filename
if (!step.Children.TryGetValue("from", out var fromFile))
throw new YamlException(step.Start, step.End, "'copy-package' command missing 'from' parameter");
// Get the step inputs
var inputs = GetMapMap(step, "inputs");

// Get the to-filename
if (!step.Children.TryGetValue("to", out var toFile))
throw new YamlException(step.Start, step.End, "'copy-package' command missing 'to' parameter");
// Get the 'from' input
var fromFile = GetMapString(inputs, "from", variables) ??
throw new YamlException(step.Start, step.End, "'copy-package' missing 'from' input");

// Get the package name
if (!step.Children.TryGetValue("package", out var package))
throw new YamlException(step.Start, step.End, "'copy-package' command missing 'package' parameter");
// Get the 'to' input
var toFile = GetMapString(inputs, "to", variables) ??
throw new YamlException(step.Start, step.End, "'copy-package' missing 'to' input");

// Get the relationship type
if (!step.Children.TryGetValue("relationship", out var relationship))
throw new YamlException(step.Start, step.End, "'copy-package' command missing 'relationship' parameter");
// Get the 'package' input
var package = GetMapString(inputs, "package", variables) ??
throw new YamlException(step.Start, step.End, "'copy-package' missing 'package' input");

// Get the element name
if (!step.Children.TryGetValue("element", out var element))
throw new YamlException(step.Start, step.End, "'copy-package' command missing 'element' parameter");
// Get the 'relationship' input
var relationship = GetMapString(inputs, "relationship", variables) ??
throw new YamlException(step.Start, step.End, "'copy-package' missing 'relationship' input");

// Get the 'element' input
var element = GetMapString(inputs, "element", variables) ??
throw new YamlException(step.Start, step.End, "'copy-package' missing 'element' input");

// Copy the package
CopyPackage(fromFile.ToString(), toFile.ToString(), package.ToString(), relationship.ToString(),
element.ToString());
CopyPackage(fromFile, toFile, package, relationship, element);
}

/// <summary>
Expand Down
16 changes: 10 additions & 6 deletions src/DemaConsulting.SpdxTool/Commands/HelpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class HelpCommand : Command
"",
"From a YAML file this can be used as:",
" - command: help",
" about: <command>"
" inputs:",
" about: <command>"
},
Instance);

Expand All @@ -52,14 +53,17 @@ public override void Run(string[] args)
}

/// <inheritdoc />
public override void Run(YamlMappingNode step)
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the about command
if (!step.Children.TryGetValue("about", out var about))
throw new YamlException(step.Start, step.End, "'help' command missing 'about' parameter");
// Get the step inputs
var inputs = GetMapMap(step, "inputs");

// Get the 'about' input
var about = GetMapString(inputs, "about", variables) ??
throw new YamlException(step.Start, step.End, "'help' command missing 'about' input");

// Generate the markdown
ShowUsage(about.ToString());
ShowUsage(about);
}

/// <summary>
Expand Down
32 changes: 18 additions & 14 deletions src/DemaConsulting.SpdxTool/Commands/RenameIdCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public class RenameIdCommand : Command
"",
"From a YAML file this can be used as:",
" - command: rename-id",
" spdx: <spdx.json>",
" old: <old-id>",
" new: <new-id>"
" inputs:",
" spdx: <spdx.json>",
" old: <old-id>",
" new: <new-id>"
},
Instance);

Expand All @@ -55,22 +56,25 @@ public override void Run(string[] args)
}

/// <inheritdoc />
public override void Run(YamlMappingNode step)
public override void Run(YamlMappingNode step, Dictionary<string, string> variables)
{
// Get the spdx filename
if (!step.Children.TryGetValue("spdx", out var spdxFile))
throw new YamlException(step.Start, step.End, "'rename-id' command missing 'spdx' parameter");
// Get the step inputs
var inputs = GetMapMap(step, "inputs");

// Get the old ID
if (!step.Children.TryGetValue("old", out var oldId))
throw new YamlException(step.Start, step.End, "'rename-id' command missing 'old' parameter");
// Get the 'spdx' input
var spdxFile = GetMapString(inputs, "spdx", variables) ??
throw new YamlException(step.Start, step.End, "'rename-id' command missing 'spdx' input");

// Get the new ID
if (!step.Children.TryGetValue("new", out var newId))
throw new YamlException(step.Start, step.End, "'rename-id' command missing 'new' parameter");
// Get the 'new' input
var newId = GetMapString(inputs, "new", variables) ??
throw new YamlException(step.Start, step.End, "'rename-id' command missing 'new' input");

// Get the 'old' input
var oldId = GetMapString(inputs, "old", variables) ??
throw new YamlException(step.Start, step.End, "'rename-id' command missing 'spdx' input");

// Rename the ID
RenameId(spdxFile.ToString(), oldId.ToString(), newId.ToString());
RenameId(spdxFile, oldId, newId);
}

/// <summary>
Expand Down
Loading