compilertools support python dependencies in virtualenv. close #37

This commit is contained in:
2021-07-31 19:22:45 -06:00
parent a1c9b56e68
commit 88f418a62a
4 changed files with 65 additions and 3 deletions

View File

@@ -24,7 +24,7 @@ typedef CompilationArgs = {
?importHxFile:String,
// path to a file with hxml args in it (SHOULD NOT specify target or main class)
?hxmlFile:String,
// path to a package.json or requirements.txt file
// path to a package.json, requirements.txt, or setup.py file
?langProjectFile:String,
// path to a haxe file defining the Main class
?mainHxFile:String,
@@ -56,6 +56,8 @@ class CompilerTools {
}
// if folder exists, delete it
// TODO this assumes the user hasn't put folders in the folder, which
// would cause a failure because we're not deleting recursively
if (FileSystem.exists(args.outputFolder)) {
for (file in FileSystem.readDirectory(args.outputFolder)) {
FileSystem.deleteFile(Path.join([args.outputFolder, file]));
@@ -123,20 +125,40 @@ class CompilerTools {
Prelude.assertProcess("haxelib", ["install", "--always", args.hxmlFile]);
}
// TODO install language-specific dependencies from langProjectFile (which might be tricky because we can't set the working directory)
// Compile the script
Prelude.assertProcess("haxe", ["--cwd", args.outputFolder].concat(hxmlFiles.map(Path.withoutDirectory)));
// TODO install language-specific dependencies from langProjectFile (which might be tricky because we can't set the working directory)
var command = "";
var scriptExt = "";
switch (lang) {
case JavaScript:
command = "node";
scriptExt = "js";
// npm install outputfolder
case Python:
command = "python";
scriptExt = "py";
if (args.langProjectFile != null) {
trace(args.langProjectFile);
// Make a virtual environment
// NOTE this is placed outside the output folder, so it will get reused.
// In some cases this might be bad if the virtual environment gets bad
// versions of dependencies stuck in it
var envFolder = '${args.outputFolder}-env';
Prelude.assertProcess("python", ["-m", "venv", envFolder]);
var envPython = Path.join([envFolder, "Scripts", "python"]);
command = envPython;
switch (args.langProjectFile.extension()) {
case "txt":
// the requirements file's original path is fine for this case
Prelude.assertProcess(envPython, ["-m", "pip", "install", "-r", args.langProjectFile]);
case "py":
// python setup.py install
Prelude.assertProcess(envPython, [Path.join([args.outputFolder, args.langProjectFile.withoutDirectory()]), "install"]);
}
}
}
// return an expression for a lambda that calls new Process() that runs the target-specific file

View File

@@ -15,6 +15,7 @@ class CompilerToolsTestCase extends Test {
#if ((sys || hxnodejs) && !cs)
function testCompileHelloWorldJs() {
Assert.equals("Hello world!", _testCompileHelloWorldJs()());
Assert.equals("Hello world!", _testCompileHelloWorldJsWithPackageJson()());
}
static macro function _testCompileHelloWorldJs() {
@@ -24,8 +25,18 @@ class CompilerToolsTestCase extends Test {
});
}
static macro function _testCompileHelloWorldJsWithPackageJson() {
return CompilerTools.compileFileToScript(
"kiss/template/src/template/Main.kiss", JavaScript, {
outputFolder: "bin/helloWorldJsTestWithPackageJson",
// langProjectFile: "kiss/src/test/files/package.json"
});
}
function testCompileHelloWorldPy() {
Assert.equals("Hello world!", _testCompileHelloWorldPy()());
Assert.equals("Hello world!", _testCompileHelloWorldPyWithRequirementsTxt()());
Assert.equals("Hello world!", _testCompileHelloWorldPyWithSetupPy()());
}
static macro function _testCompileHelloWorldPy() {
@@ -35,6 +46,22 @@ class CompilerToolsTestCase extends Test {
});
}
static macro function _testCompileHelloWorldPyWithRequirementsTxt() {
return CompilerTools.compileFileToScript(
"kiss/template/src/template/Main.kiss", Python, {
outputFolder: "bin/helloWorldPyTestWithRequirementsTxt",
langProjectFile: "kiss/src/test/files/requirements.txt"
});
}
static macro function _testCompileHelloWorldPyWithSetupPy() {
return CompilerTools.compileFileToScript(
"kiss/template/src/template/Main.kiss", Python, {
outputFolder: "bin/helloWorldPyTestWithSetupPy",
langProjectFile: "kiss/src/test/files/setup.py"
});
}
// TODO test what happens when passing more arguments/files
#end
}

View File

@@ -0,0 +1 @@
requests

View File

@@ -0,0 +1,12 @@
from distutils.core import setup
setup(name='HelloWorld',
version='1.0',
description='HelloWorld python extern program',
author='Nat',
author_email='natquaylenelson@gmail.com',
url='https://github.com/NQNStudios/kisslang',
install_requires=[
'requests',
],
)