From 88f418a62abd1cce4ca0b0e534d0543fb84f29b5 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sat, 31 Jul 2021 19:22:45 -0600 Subject: [PATCH] compilertools support python dependencies in virtualenv. close #37 --- kiss/src/kiss/CompilerTools.hx | 28 +++++++++++++++++--- kiss/src/test/cases/CompilerToolsTestCase.hx | 27 +++++++++++++++++++ kiss/src/test/files/requirements.txt | 1 + kiss/src/test/files/setup.py | 12 +++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 kiss/src/test/files/requirements.txt create mode 100644 kiss/src/test/files/setup.py diff --git a/kiss/src/kiss/CompilerTools.hx b/kiss/src/kiss/CompilerTools.hx index 2f029ab3..f4d4f072 100644 --- a/kiss/src/kiss/CompilerTools.hx +++ b/kiss/src/kiss/CompilerTools.hx @@ -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 diff --git a/kiss/src/test/cases/CompilerToolsTestCase.hx b/kiss/src/test/cases/CompilerToolsTestCase.hx index 72b6cd6d..64a652e2 100644 --- a/kiss/src/test/cases/CompilerToolsTestCase.hx +++ b/kiss/src/test/cases/CompilerToolsTestCase.hx @@ -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 } diff --git a/kiss/src/test/files/requirements.txt b/kiss/src/test/files/requirements.txt new file mode 100644 index 00000000..663bd1f6 --- /dev/null +++ b/kiss/src/test/files/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/kiss/src/test/files/setup.py b/kiss/src/test/files/setup.py new file mode 100644 index 00000000..ab18e687 --- /dev/null +++ b/kiss/src/test/files/setup.py @@ -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', + ], + ) \ No newline at end of file