mirror of
https://github.com/enso-org/enso.git
synced 2025-01-01 16:52:37 +03:00
Derive --in-project from --run source location (#8775)
This commit is contained in:
parent
71427c3319
commit
a764618bd9
@ -1020,6 +1020,7 @@
|
|||||||
- [Added opt-in type checks of return type][8502]
|
- [Added opt-in type checks of return type][8502]
|
||||||
- [Introduce Arrow language][8512]
|
- [Introduce Arrow language][8512]
|
||||||
- [DataflowError.withoutTrace doesn't store stacktrace][8608]
|
- [DataflowError.withoutTrace doesn't store stacktrace][8608]
|
||||||
|
- [Derive --in-project from --run source location][8775]
|
||||||
|
|
||||||
[3227]: https://github.com/enso-org/enso/pull/3227
|
[3227]: https://github.com/enso-org/enso/pull/3227
|
||||||
[3248]: https://github.com/enso-org/enso/pull/3248
|
[3248]: https://github.com/enso-org/enso/pull/3248
|
||||||
@ -1173,6 +1174,7 @@
|
|||||||
[8502]: https://github.com/enso-org/enso/pull/8502
|
[8502]: https://github.com/enso-org/enso/pull/8502
|
||||||
[8512]: https://github.com/enso-org/enso/pull/8512
|
[8512]: https://github.com/enso-org/enso/pull/8512
|
||||||
[8608]: https://github.com/enso-org/enso/pull/8608
|
[8608]: https://github.com/enso-org/enso/pull/8608
|
||||||
|
[8775]: https://github.com/enso-org/enso/pull/8775
|
||||||
|
|
||||||
# Enso 2.0.0-alpha.18 (2021-10-12)
|
# Enso 2.0.0-alpha.18 (2021-10-12)
|
||||||
|
|
||||||
|
14
build.sbt
14
build.sbt
@ -2079,12 +2079,14 @@ lazy val `engine-runner` = project
|
|||||||
commands += WithDebugCommand.withDebug,
|
commands += WithDebugCommand.withDebug,
|
||||||
inConfig(Compile)(truffleRunOptionsSettings),
|
inConfig(Compile)(truffleRunOptionsSettings),
|
||||||
libraryDependencies ++= Seq(
|
libraryDependencies ++= Seq(
|
||||||
"org.graalvm.sdk" % "polyglot-tck" % graalMavenPackagesVersion % Provided,
|
"org.graalvm.sdk" % "polyglot-tck" % graalMavenPackagesVersion % Provided,
|
||||||
"org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion % Provided,
|
"org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion % Provided,
|
||||||
"commons-cli" % "commons-cli" % commonsCliVersion,
|
"commons-cli" % "commons-cli" % commonsCliVersion,
|
||||||
"com.monovore" %% "decline" % declineVersion,
|
"com.monovore" %% "decline" % declineVersion,
|
||||||
"org.jline" % "jline" % jlineVersion,
|
"org.jline" % "jline" % jlineVersion,
|
||||||
"org.typelevel" %% "cats-core" % catsVersion
|
"org.typelevel" %% "cats-core" % catsVersion,
|
||||||
|
"junit" % "junit" % junitVersion % Test,
|
||||||
|
"com.github.sbt" % "junit-interface" % junitIfVersion % Test
|
||||||
),
|
),
|
||||||
run / connectInput := true
|
run / connectInput := true
|
||||||
)
|
)
|
||||||
|
69
engine/runner/src/main/java/org/enso/runner/Utils.java
Normal file
69
engine/runner/src/main/java/org/enso/runner/Utils.java
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
package org.enso.runner;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import scala.Tuple3;
|
||||||
|
|
||||||
|
final class Utils {
|
||||||
|
private Utils() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verifies path and project path.
|
||||||
|
*
|
||||||
|
* @param path file or project to execute
|
||||||
|
* @param projectPath project path or {@code null} if it hasn't been specified
|
||||||
|
* @return tuple with boolean, File to execute and path for project to use or {@code null} if
|
||||||
|
* execution shall finish
|
||||||
|
*/
|
||||||
|
static scala.Tuple3<Boolean, File, String> findFileAndProject(String path, String projectPath)
|
||||||
|
throws IOException {
|
||||||
|
var file = new File(path);
|
||||||
|
if (!file.exists()) {
|
||||||
|
System.err.println("File " + file + " does not exist.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
var projectMode = file.isDirectory();
|
||||||
|
var canonicalFile = file.getCanonicalFile();
|
||||||
|
String projectRoot;
|
||||||
|
if (projectMode) {
|
||||||
|
if (projectPath != null) {
|
||||||
|
var canonicalProjectFile = new File(projectPath).getCanonicalFile();
|
||||||
|
if (!canonicalProjectFile.equals(canonicalFile)) {
|
||||||
|
var msg =
|
||||||
|
"It is not possible to run a project ("
|
||||||
|
+ canonicalFile
|
||||||
|
+ ") in context of another "
|
||||||
|
+ "project ("
|
||||||
|
+ canonicalProjectFile
|
||||||
|
+ "), please do not use the `--in-project` option for "
|
||||||
|
+ "running projects.";
|
||||||
|
System.err.println(msg);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
projectRoot = canonicalFile.getPath();
|
||||||
|
} else {
|
||||||
|
if (projectPath != null) {
|
||||||
|
projectRoot = projectPath;
|
||||||
|
} else {
|
||||||
|
var f = canonicalFile;
|
||||||
|
for (; ; ) {
|
||||||
|
if (f == null) {
|
||||||
|
projectRoot = "";
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
var p = f.getParentFile();
|
||||||
|
if ("src".equals(f.getName())) {
|
||||||
|
if (p != null && new File(p, "package.yaml").isFile()) {
|
||||||
|
projectRoot = p.getPath();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f = p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Tuple3.apply(projectMode, canonicalFile, projectRoot);
|
||||||
|
}
|
||||||
|
}
|
@ -602,27 +602,15 @@ object Main {
|
|||||||
executionEnvironment: Option[String],
|
executionEnvironment: Option[String],
|
||||||
warningsLimit: Int
|
warningsLimit: Int
|
||||||
): Unit = {
|
): Unit = {
|
||||||
val file = new File(path)
|
val fileAndProject =
|
||||||
if (!file.exists) {
|
Utils.findFileAndProject(path, projectPath.getOrElse(null))
|
||||||
println(s"File $file does not exist.")
|
if (fileAndProject == null) {
|
||||||
exitFail()
|
exitFail()
|
||||||
}
|
}
|
||||||
val projectMode = file.isDirectory
|
val projectMode = fileAndProject._1
|
||||||
val projectRoot =
|
val file = fileAndProject._2
|
||||||
if (projectMode) {
|
val projectRoot = fileAndProject._3
|
||||||
projectPath match {
|
val options = new HashMap[String, String]()
|
||||||
case Some(inProject) if inProject != path =>
|
|
||||||
println(
|
|
||||||
"It is not possible to run a project in context of another " +
|
|
||||||
"project, please do not use the `--in-project` option for " +
|
|
||||||
"running projects."
|
|
||||||
)
|
|
||||||
exitFail()
|
|
||||||
case _ =>
|
|
||||||
}
|
|
||||||
file.getAbsolutePath
|
|
||||||
} else projectPath.getOrElse("")
|
|
||||||
val options = new HashMap[String, String]()
|
|
||||||
if (dump) {
|
if (dump) {
|
||||||
options.put("engine.TraceCompilation", "true")
|
options.put("engine.TraceCompilation", "true")
|
||||||
options.put("engine.MultiTier", "false")
|
options.put("engine.MultiTier", "false")
|
||||||
|
103
engine/runner/src/test/java/org/enso/runner/UtilsTest.java
Normal file
103
engine/runner/src/test/java/org/enso/runner/UtilsTest.java
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package org.enso.runner;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
|
public class UtilsTest {
|
||||||
|
@Rule public TemporaryFolder folder = new TemporaryFolder();
|
||||||
|
|
||||||
|
public UtilsTest() {}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detectParentProject() throws Exception {
|
||||||
|
var dir = folder.newFolder("dir", "prj", "src", "some", "file");
|
||||||
|
var prj = dir.getParentFile().getParentFile().getParentFile();
|
||||||
|
assertEquals("prj", prj.getName());
|
||||||
|
var yaml = new File(prj, "package.yaml");
|
||||||
|
Files.writeString(yaml.toPath(), "enso pkg");
|
||||||
|
var src = new File(dir, "Main.enso");
|
||||||
|
Files.writeString(src.toPath(), "main = 42");
|
||||||
|
|
||||||
|
var found = Utils.findFileAndProject(src.getPath(), null);
|
||||||
|
assertNotNull("Project found", found);
|
||||||
|
assertFalse("No project mode for a source file", found._1());
|
||||||
|
assertEquals("Source detected", src, found._2());
|
||||||
|
assertEquals("Project folder found", prj.getPath(), found._3());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void specifyProjectDir() throws Exception {
|
||||||
|
var dir = folder.newFolder("dir", "prj", "src", "some", "file");
|
||||||
|
var prj = dir.getParentFile().getParentFile().getParentFile();
|
||||||
|
assertEquals("prj", prj.getName());
|
||||||
|
var yaml = new File(prj, "package.yaml");
|
||||||
|
Files.writeString(yaml.toPath(), "enso pkg");
|
||||||
|
var src = new File(dir, "Main.enso");
|
||||||
|
Files.writeString(src.toPath(), "main = 42");
|
||||||
|
|
||||||
|
var found = Utils.findFileAndProject(prj.getPath(), null);
|
||||||
|
assertNotNull("Project found", found);
|
||||||
|
assertTrue("prj directory means project mode", found._1());
|
||||||
|
assertEquals("Source is the project", prj, found._2());
|
||||||
|
assertEquals("Project folder found", prj.getPath(), found._3());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void specifyProjectAndFile() throws Exception {
|
||||||
|
var dir = folder.newFolder("dir", "prj", "src", "some", "file");
|
||||||
|
var prj = dir.getParentFile().getParentFile().getParentFile();
|
||||||
|
assertEquals("prj", prj.getName());
|
||||||
|
var yaml = new File(prj, "package.yaml");
|
||||||
|
Files.writeString(yaml.toPath(), "enso pkg");
|
||||||
|
var src = folder.newFile("Standalone.enso");
|
||||||
|
Files.writeString(src.toPath(), "main = 42");
|
||||||
|
|
||||||
|
var found = Utils.findFileAndProject(src.getPath(), prj.getPath());
|
||||||
|
assertNotNull("Project found", found);
|
||||||
|
assertFalse("source and project implies non-project mode", found._1());
|
||||||
|
assertEquals("Source is kept", src, found._2());
|
||||||
|
assertEquals("Project folder is kept", prj.getPath(), found._3());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dontDetectParentProjectIfMissingPackageYaml() throws Exception {
|
||||||
|
var dir = folder.newFolder("dir", "prj", "src", "some", "file");
|
||||||
|
var prj = dir.getParentFile().getParentFile().getParentFile();
|
||||||
|
assertEquals("prj", prj.getName());
|
||||||
|
var yamlInWrongDir = new File(dir.getParent(), "package.yaml");
|
||||||
|
Files.writeString(yamlInWrongDir.toPath(), "enso pkg");
|
||||||
|
var src = new File(dir, "Main.enso");
|
||||||
|
Files.writeString(src.toPath(), "main = 42");
|
||||||
|
|
||||||
|
var found = Utils.findFileAndProject(src.getPath(), null);
|
||||||
|
assertNotNull("Project found", found);
|
||||||
|
assertFalse("No project mode for a source file", found._1());
|
||||||
|
assertEquals("Source detected", src, found._2());
|
||||||
|
assertEquals("No project folder found", "", found._3());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dontDetectParentProjectWithoutSrcDir() throws Exception {
|
||||||
|
var dir = folder.newFolder("dir", "prj", "nosrc", "some", "file");
|
||||||
|
var prj = dir.getParentFile().getParentFile().getParentFile();
|
||||||
|
assertEquals("prj", prj.getName());
|
||||||
|
var yaml = new File(prj, "package.yaml");
|
||||||
|
Files.writeString(yaml.toPath(), "enso pkg");
|
||||||
|
var src = new File(dir, "Main.enso");
|
||||||
|
Files.writeString(src.toPath(), "main = 42");
|
||||||
|
|
||||||
|
var found = Utils.findFileAndProject(src.getPath(), null);
|
||||||
|
assertNotNull("Project found", found);
|
||||||
|
assertFalse("No project mode for a source file", found._1());
|
||||||
|
assertEquals("Source detected", src, found._2());
|
||||||
|
assertEquals("No project folder detected without src dir", "", found._3());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user