-
Notifications
You must be signed in to change notification settings - Fork 18
feature: Detect coursier if it's available locally #524
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"] | ||
| ?.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") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| ); | ||
|
|
||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| } | ||
|
|
||
| export function calcServerDependency(serverVersion: string): string { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let->const