Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-tan committed Mar 31, 2024
1 parent 6d7beec commit dad31de
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 59 deletions.
22 changes: 6 additions & 16 deletions source/shaft/command_line_tool.d
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,7 @@ int execute(CommandLineTool clt, TypedParameters params, Runtime runtime, Evalua

// 7. Execute the process.
auto pid = spawnProcess(args, stdin, stdout, stderr, env, Config.newEnv, runtime.outdir)
.ifThrown!ProcessException((e) {
import std.process : Pid;
enforce!SystemException(false, e.msg);
return Pid.init;
});
.ifThrown!ProcessException(e => throw new SystemException(e.msg));
stdThreadLocalLog.info(() {
import std : JSONValue, escapeShellCommand;
return JSONValue([
Expand Down Expand Up @@ -337,10 +333,7 @@ auto processParameters(Param[] params, Node inputs, Runtime runtime, Evaluator e
return params
.map!(p =>
applyRules(p.tupleof[1..$], inputs, runtime, evaluator, useShell)
.ifThrown!InvalidDocument((e) {
enforce(false, new InvalidDocument(format!"%s in `%s`"(e.msg, p.key[1]), e.mark));
return CmdElemType.init;
})
.ifThrown!InvalidDocument(e => throw new InvalidDocument(format!"%s in `%s`"(e.msg, p.key[1]), e.mark))
.match!(
(Str s) => [s],
ss => ss,
Expand Down Expand Up @@ -439,13 +432,10 @@ auto toCmdElems(CmdElemType val, CommandLineBinding clb, bool useShell)
(Str _) {
clb.itemSeparator_
.tryMatch!((None _) => true)
.ifThrown!MatchException((e) {
enforce(false, new InvalidDocument(
"`itemSeparator` is supported only for array types",
clb.mark,
));
return true;
});
.ifThrown!MatchException(e => throw new InvalidDocument(
"`itemSeparator` is supported only for array types",
clb.mark,
));
return val;
},
(Str[] vs) => clb.itemSeparator_.match!(
Expand Down
5 changes: 1 addition & 4 deletions source/shaft/evaluator/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,7 @@ Node evalJSExpression(
auto evaled = engine
.evaluate(exp, inputs, runtime, self, libs)
.ifThrown!ExpressionFailed((_) {
enforce!ExpressionFailed(false, format!"Evaluation failed: `%s`"(exp));
return "";
});
.ifThrown!ExpressionFailed(_ => throw new ExpressionFailed(format!"Evaluation failed: `%s`"(exp)));
auto retNode = Loader.fromString(evaled).load;
Expand Down
2 changes: 0 additions & 2 deletions source/shaft/logger.d
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ immutable(TimeZone) getTimeZone() @safe
auto toJSONLogEntry(LogEntry)(in LogEntry payload, immutable TimeZone tz) @safe
{
import std.conv : to;
import std.exception : ifThrown;
import std.json : JSONException, JSONValue, JSONType, parseJSON;
import std.datetime.timezone : LocalTime;

JSONValue log;
log["time"] = payload.timestamp.toOtherTZ(tz).toISOExtString;
Expand Down
38 changes: 10 additions & 28 deletions source/shaft/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ int shaftMain(string[] args)
"show-supported-versions", "show supported CWL specs", &showSupportedVersions,
"license", "show license information", &showLicense,
"version", "show version information", &showVersion,
).ifThrown!GetOptException((e) {
enforce!SystemException(false, e.msg);
return GetoptResult.init;
});
).ifThrown!GetOptException(e => throw new SystemException(e.msg));

if (!compatOptions.empty)
{
Expand Down Expand Up @@ -212,19 +209,12 @@ EOS".outdent[0 .. $ - 1])(args[0].baseName);
// See_Also: https://www.commonwl.org/v1.2/CommandLineTool.html#Generic_execution_process
// 1. Load input object.
auto loader = args.length == 3 ? Loader.fromFile(args[2].absolutePath)
.ifThrown!YAMLException((e) {
enforce!SystemException(
false,
"Input parameter file not found: "~args[2].absolutePath,
);
return Loader.init;
})
.ifThrown!YAMLException(e => throw new SystemException(
"Input parameter file not found: "~args[2].absolutePath
))
: Loader.fromString("{}");
auto inp = loader.load
.ifThrown!MarkedYAMLException((e) {
enforce(false, new InputCannotBeLoaded(e.msg.chomp, e.mark));
return Node.init;
});
.ifThrown!MarkedYAMLException(e => throw new InputCannotBeLoaded(e.msg.chomp, e.mark));
enforce(inp.type == NodeType.mapping,
new InputCannotBeLoaded("Input should be a mapping but it is not", inp.startMark));

Expand Down Expand Up @@ -299,27 +289,19 @@ EOS".outdent[0 .. $ - 1])(args[0].baseName);
// - no

// 2. Load, process and validate a CWL document, yielding one or more process objects. The $namespaces present in the CWL document are also used when validating and processing the input object.
import std.string : chomp;

auto path = args[1];
auto cwlfile = discoverDocumentURI(path);
auto process = importFromURI(cwlfile, "main").tryMatch!(
(DocumentRootType doc) => doc.tryMatch!(
(CommandLineTool _) => doc,
(ExpressionTool _) => doc,
(other) {
enforce!FeatureUnsupported(false, format!"Document class `%s` is not supported"(other.class_));
return doc;
}
other => throw new FeatureUnsupported(format!"Document class `%s` is not supported"(other.class_)),
),
)
.ifThrown!DocumentException((e) {
enforce(false, new InvalidDocument(e.msg, e.mark));
return DocumentRootType.init;
})
.ifThrown!MarkedYAMLException((e) {
import std.string : chomp;
enforce(false, new InputCannotBeLoaded(e.msg.chomp, e.mark));
return DocumentRootType.init;
});
.ifThrown!DocumentException(e => throw new InvalidDocument(e.msg, e.mark))
.ifThrown!MarkedYAMLException(e => throw new InputCannotBeLoaded(e.msg.chomp, e.mark));

stdThreadLocalLog.info((){
import std.json;
Expand Down
5 changes: 1 addition & 4 deletions source/shaft/runtime.d
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ auto availableRamImpl() @trusted
rlimit lim; // in bytes
errnoEnforce(
getrlimit(RLIMIT_AS, &lim) == 0
).ifThrown!ErrnoException((e) {
enforce!SystemException(false, e.msg);
return false;
});
).ifThrown!ErrnoException(e => throw new SystemException(e.msg));

version(linux)
{
Expand Down
8 changes: 3 additions & 5 deletions source/shaft/type/input.d
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,9 @@ in(params.type == NodeType.mapping)

TypedValue v = n
.bindType(type, defMap, context)
.ifThrown!TypeConflicts((e) {
auto msg = new TypeConflicts(e.expected_, e.actual_, id).msg;
enforce(false, new InputCannotBeLoaded(msg, n.startMark));
return TypedValue.init;
});
.ifThrown!TypeConflicts(e => throw new InputCannotBeLoaded(
new TypeConflicts(e.expected_, e.actual_, id).msg, n.startMark)
);
v.type.match!(
(ref PrimitiveType t) {
// set StagingOption
Expand Down

0 comments on commit dad31de

Please sign in to comment.