Skip to content

Commit

Permalink
Merge pull request #1692 from davidpcaldwell/davidpcaldwell/issue/#1670
Browse files Browse the repository at this point in the history
#1670: jsh.shell.jsh.home JSAPI -> Fifty
  • Loading branch information
davidpcaldwell authored Oct 18, 2024
2 parents 40ec9e5 + 390ef8b commit bf48bfd
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 136 deletions.
4 changes: 2 additions & 2 deletions contributor/jrunscript.jsh.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@
return p;
};

suite.add("jsh/jsh.shell/suite", new jsh.unit.Suite.Fork(withShell({
suite.add("jsh/jsh.shell/jsh", new jsh.unit.Suite.Fork(withShell({
run: jsh.shell.jsh,
script: SRC.getFile("rhino/shell/test/jsh.shell.jsh.suite.jsh.js"),
script: SRC.getFile("jrunscript/jsh/shell/test/jsh.shell.jsh.suite.jsh.js"),
arguments: ["-view","stdio"]
})));

Expand Down
4 changes: 4 additions & 0 deletions jrunscript/jsh/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace slime.jsh.test {
PATH: slime.jrunscript.file.Searchpath
settings: remote.Settings

// TODO should support local scripts, too, a real use case for remote shells
/**
* A path, relative within the SLIME project, of a remote script to run.
*/
Expand All @@ -54,6 +55,9 @@ namespace slime.jsh.test {
& shells.Invocable
>

/**
* Maps an operating system pathname to a packaged shell.
*/
packaged: slime.$api.fp.Mapping<
string,
(
Expand Down
2 changes: 0 additions & 2 deletions jrunscript/jsh/script/plugin.jsh.fifty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ namespace slime.jsh.script {
}

export interface Exports {
// TODO Additional, possibly excessive, tests are in rhino/shell/test/jsh.shell.jsh.suite.jsh.js

/**
* @deprecated
*
Expand Down
167 changes: 163 additions & 4 deletions jrunscript/jsh/shell/jsh.fifty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,30 @@ namespace slime.jsh.shell {
export namespace oo {
export interface EngineResult {
status: number

/**
* An additional argument which describes the `jsh` invocation.
*/
jsh: {
/**
* The script that was launched.
*/
script: Invocation["script"]

/**
* The arguments passed to the script.
*/
arguments: Invocation["arguments"]
}
environment: Invocation["environment"]
directory: Invocation["directory"]
workingDirectory: Invocation["workingDirectory"]

// TODO is this real? Documentation brought from JSAPI.
// /**
// * The classpath supplied to the given script, as specified by the caller.
// */
// classpath: slime.jrunscript.file.Searchpath
}

// export interface ForkResult extends EngineResult {
Expand All @@ -564,12 +581,34 @@ namespace slime.jsh.shell {
// // etc. and make sure everything was strictly encapsulated.
// stdio: Invocation["stdio"]
// }
export type ForkResult = slime.jrunscript.shell.run.old.Result
export type ForkResult = slime.jrunscript.shell.run.old.Result & {
/**
* The operating system command invoked.
*/
command: string

/**
* The arguments sent to the operating system command.
*/
arguments: string[]
}

/**
* An object with the same properties as those of the evaluate property of the argument to
* {@link slime.jsh.shell.Exports | Exports.shell()}, with differences specified by the {@link ForkResult} and
* {@link EngineResult} types.
*/
export type Result = ForkResult | EngineResult

/**
* @template R An arbitrary type to return from a `jsh` invocation that is computed using the results of the invocation.
*/
export type evaluate<I,R> = (p: I) => R

/**
* An argument with the same semantics as the argument to `shell`, except that it includes some properties in
* addition specific to running `jsh` scripts.
*/
export interface Invocation<R = Result> {
/**
* The pathname of the script to run.
Expand Down Expand Up @@ -607,15 +646,18 @@ namespace slime.jsh.shell {

export interface EngineInvocation<R = EngineResult> extends Invocation<R> {
fork?: false
evaluate?: (p: EngineResult) => R
evaluate?: evaluate<EngineResult,R>
}

export interface ForkInvocation<R = ForkResult> extends Invocation<R> {
/**
* A directory representing the location of a built shell, or a directory representing the location of an unbuilt shell.
*/
shell?: slime.jrunscript.file.Directory
fork?: true
vmarguments?: any

evaluate?: (p: ForkResult) => R
evaluate?: evaluate<ForkResult,R>
}
}

Expand All @@ -624,11 +666,128 @@ namespace slime.jsh.shell {
<R>(p: oo.EngineInvocation<R>): R
}

export interface JshShellJsh {
/**
* (contingent; only present for built shells) The home directory of the installed shell.
*/
home?: slime.jrunscript.file.Directory
}

(
function(
fifty: slime.fifty.test.Kit
) {
const { verify } = fifty;
const { $api, jsh } = fifty.global;

fifty.tests.exports.jsh.home = function() {
var unbuilt = test.shells.unbuilt();
var built = test.shells.built();
var packaged = test.shells.packaged(
fifty.jsh.file.relative("../test/jsh-data.jsh.js").pathname
);
var remote = test.shells.remote();

var environment = $api.Object.compose(
jsh.shell.environment,
{
PATH: (function() {
var pathnames = jsh.shell.PATH.pathnames;
pathnames.unshift(
jsh.file.Pathname(
jsh.shell.java.Jdk.from.javaHome().base
).directory.getRelativePath("bin")
);
return jsh.file.Searchpath(pathnames).toString();
})()
}
);

var values: { unbuilt: any, built: string, packaged: any, remote: any } = {
unbuilt: $api.fp.now(
$api.fp.world.Sensor.now({
sensor: jsh.shell.subprocess.question,
subject: unbuilt.invoke({
script: fifty.jsh.file.relative("../test/jsh-data.jsh.js").pathname,
environment: $api.fp.thunk.value(environment),
stdio: {
output: "string"
}
})
}),
function(exit) {
if (exit.status != 0) throw new Error("Exit status: " + exit.status);
return JSON.parse(exit.stdio.output)["jsh.shell.jsh.home"];
}
),
built: $api.fp.now(
$api.fp.world.Sensor.now({
sensor: jsh.shell.subprocess.question,
subject: built.invoke({
script: fifty.jsh.file.relative("../test/jsh-data.jsh.js").pathname,
environment: $api.fp.thunk.value(environment),
stdio: {
output: "string"
}
})
}),
function(exit) {
if (exit.status != 0) throw new Error("Exit status: " + exit.status);
var json = JSON.parse(exit.stdio.output);
var string = json["jsh.shell.jsh.home"].string;
// strip trailing slash from the emitted JSON
return string.substring(0,string.length-1);
}
),
packaged: $api.fp.now(
$api.fp.world.Sensor.now({
sensor: jsh.shell.subprocess.question,
subject: packaged.invoke({
environment: $api.fp.thunk.value(environment),
stdio: {
output: "string"
}
})
}),
function(exit) {
if (exit.status != 0) throw new Error("Exit status: " + exit.status);
return JSON.parse(exit.stdio.output)["jsh.shell.jsh.home"];
}
),
remote: $api.fp.now(
remote,
function(remote) {
var intention = remote.getShellIntention({
PATH: jsh.shell.PATH,
settings: {
branch: "local"
},
script: "jrunscript/jsh/test/jsh-data.jsh.js"
});
return $api.fp.world.Sensor.now({
sensor: jsh.shell.subprocess.question,
subject: intention
});
},
function(exit) {
if (exit.status != 0) throw new Error("Exit status: " + exit.status);
return JSON.parse(exit.stdio.output)["jsh.shell.jsh.home"];
}
)
};
verify(values).unbuilt.is(void(0));
verify(values).built.is(built.home);
verify(values).packaged.is(void(0));
verify(values).remote.is(void(0));
}
}
//@ts-ignore
)(fifty);

export interface JshShellJsh extends JshInvoke {
src?: slime.jrunscript.file.Directory

lib?: slime.jrunscript.file.Directory
home?: slime.jrunscript.file.Directory

relaunch: (p?: {
environment?: (initial: slime.jrunscript.shell.Exports["environment"]) => slime.jrunscript.shell.Exports["environment"]
Expand Down
119 changes: 0 additions & 119 deletions jrunscript/jsh/shell/plugin.jsh.api.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,137 +114,18 @@ <h1>Exports</h1>
</script>
<div class="type" jsapi:replace="getApi('../../../rhino/shell/api.html').getElement('types.stdio')">
</div>
<!--
TODO the below does not work for unknown reason
<div jsapi:reference="getApi('api.html').getElement('types.stdio')">
See detailed documentation.
</div>
-->
<ul>
<li jsapi:replace="window.slime.definition.inherited()">

</li>
<li class="function" jsapi:id="jsh">
<div class="name"><a id="exports.jsh">jsh</a></div>
<span>
Runs a jsh script. <a href="test/jsh.shell.jsh.suite.jsh.js">Tests</a> are in a separate file.
</span>
<div class="arguments">
<div class="label">Arguments</div>
<ol>
<li class="value">
<span class="type">object</span>
<span>
An argument with the same semantics as the argument to <code>shell</code>, except that
it includes the following properties in addition:
</span>
<div class="label">has properties:</div>
<ul>
<!--
TODO implement this
<li class="value">
<div class="name">shell</div>
<span class="type">__TYPE__</span>
<span>
A directory representing the location of a built shell, or a directory representing
the location of an unbuilt shell.
</span>
</li>
-->
<!-- TODO update the below using jsapi:reference -->
<li class="function">
<div class="name">evaluate</div>
<div class="arguments">
<div class="label">Arguments</div>
<ol>
<li class="object">
<span>
An object with the same properties as those of
the evaluate property of the argument to
<a href="#shell">shell</a>,
with the following differences:
</span>
<div class="label">has properties:</div>
<ul>
<li class="object">
<div class="name">jsh</div>
<span>An additional argument which describes the jsh invocation.</span>
<div class="label">has properties:</div>
<ul>
<li class="value">
<div class="name">script</div>
<span class="type"><a href="../../rhino/file/api.html#types.file">file</a></span>
<span>The script that was launched.</span>
</li>
<li class="value">
<div class="name">arguments</div>
<span class="type">Array of string</span>
<span>The arguments passed to the script.</span>
</li>
</ul>
</li>
<li class="value">
<div class="name">classpath</div>
<span class="type"><a href="../../rhino/file/api.html#types.Searchpath">Searchpath</a></span>
<span>The classpath supplied to the given script, as specified by the caller.</span>
</li>
<li class="value">
<div class="name">command</div>
<!--
<span class="type"><a href="../../rhino/file/api.html#types.file">file</a></span>
-->
<span class="type">string</span>
<span>
(if an operating system shell was created) The operating system command
invoked. Not present if the subshell was executed in-process.
</span>
</li>
<li class="value">
<div class="name">arguments</div>
<span class="type">Array of string</span>
<span>
(if an operating system shell was created) The arguments sent to the
operating system command. Not present if the subshell was
executed in-process.
</span>
</li>
</ul>
</li>
</ol>
</div>
<div class="returns">
<div class="label">Returns</div>
<span class="type">(any)</span>
<span>An arbitrary value to return from the <code>jsh</code> method.</span>
</div>
</li>
</ul>
</li>
</ol>
</div>
<script type="application/x.jsapi#initialize">
// TODO add packaged shell
// TODO add remote shell
</script>
<div class="label">has properties:</div>
<ul>
<li class="value">
<div class="name">home</div>
<span class="type"><a href="../../rhino/file/api.html#types.directory">directory</a></span>
<span>
(contingent; only present for built shells)
The home directory of the installed shell.
</span>
<script type="application/x.jsapi#tests">
var shell = $jsapi.environment.jsh;
verify(shell).unbuilt.data.evaluate.property("jsh.shell.jsh.home").is(void(0));
verify(shell).built.data.evaluate.property("jsh.shell.jsh.home").string.is(shell.built.home.toString());
verify(shell).packaged.data.evaluate.property("jsh.shell.jsh.home").is(void(0));
if (shell.remote) {
verify(shell).remote.data.evaluate.property("jsh.shell.jsh.home").is(void(0));
}
</script>
</li>
<li class="value">
<div class="name">src</div>
<span class="type"><a href="../../rhino/file/api.html#types.directory">directory</a></span>
Expand Down
File renamed without changes.
Loading

0 comments on commit bf48bfd

Please sign in to comment.