Skip to content
This repository was archived by the owner on Mar 21, 2023. It is now read-only.
Open
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
112 changes: 73 additions & 39 deletions src/fetchMetals.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,90 @@
import * as semver from "semver";
import path from "path";
import fs from "fs";
import { ChildProcessPromise, spawn } from "promisify-child-process";
import { JavaConfig } from "./getJavaConfig";
import { OutputChannel } from "./interfaces/OutputChannel";

interface FetchMetalsOptions {
serverVersion: string;
serverProperties: string[];
javaConfig: JavaConfig;
}

export function fetchMetals({
serverVersion,
serverProperties,
javaConfig: { javaPath, javaOptions, extraEnv, coursierPath },
}: FetchMetalsOptions): ChildProcessPromise {
export function fetchMetals(
{
serverVersion,
serverProperties,
javaConfig: { javaPath, javaOptions, extraEnv, coursierPath },
}: FetchMetalsOptions,
output: OutputChannel
): ChildProcessPromise {
const fetchProperties = serverProperties.filter(
(p) => !p.startsWith("-agentlib")
);

const serverDependency = calcServerDependency(serverVersion);
return spawn(
javaPath,
[
...javaOptions,
...fetchProperties,
"-Dfile.encoding=UTF-8",
"-jar",
coursierPath,
"fetch",
"-p",
"--ttl",
// Use infinite ttl to avoid redunant "Checking..." logs when using SNAPSHOT
// versions. Metals SNAPSHOT releases are effectively immutable since we
// never publish the same version twice.
"Inf",
serverDependency,
"-r",
"bintray:scalacenter/releases",
"-r",
"sonatype:public",
"-r",
"sonatype:snapshots",
"-p",
],
{
env: {
COURSIER_NO_TERM: "true",
...extraEnv,
...process.env,
},
stdio: ["ignore"], // Due to Issue: #219
}
);

const coursierArgs = [
"fetch",
"-p",
"--ttl",
// Use infinite ttl to avoid redunant "Checking..." logs when using SNAPSHOT
// versions. Metals SNAPSHOT releases are effectively immutable since we
// never publish the same version twice.
"Inf",
serverDependency,
"-r",
"bintray:scalacenter/releases",
"-r",
"sonatype:public",
"-r",
"sonatype:snapshots",
"-p",
];

let possibleCoursier = process.env["PATH"]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let -> const

?.split(path.delimiter)
.flatMap((p) => {
if (fs.statSync(p).isDirectory()) {
return fs.readdirSync(p).map((sub) => path.resolve(p, sub));
} else return [p];
})
.find(
(p) => p.endsWith(path.sep + "cs") || p.endsWith(path.sep + "coursier")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably not a huge deal, but I had a lot of problems with this in nvim-metals when trying to find cs on windows. I think this will just pass over and default to coursier which is fine, but if you did also want to check for windows and a cs.bat I do that here. Again, I don't think we need to, but looking for cs on windows won't work afaik.

);

function spawnDefault(): ChildProcessPromise {
return spawn(
javaPath,
[
...javaOptions,
...fetchProperties,
"-Dfile.encoding=UTF-8",
"-jar",
coursierPath,
].concat(coursierArgs),
{
env: {
COURSIER_NO_TERM: "true",
...extraEnv,
...process.env,
},
}
);
}
if (possibleCoursier) {
let coursier: string = possibleCoursier;
console.debug(`Using coursier located at ${coursier}`);
output.appendLine(`Using coursier located at ${coursier}`);
spawn(coursier, ["version"])
.then((_out) => spawn(coursier, coursierArgs))
.catch((err: Error) => {
output.appendLine(err.message);
console.debug(err);
return spawnDefault();
});
}
return spawnDefault();
Comment on lines +75 to +87

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now this won't work at all because if branch doesn't have return statement so spawn(coursier, ["version"]) will be discarded and spawnDefault promise will be returned.

}

export function calcServerDependency(serverVersion: string): string {
Expand Down