Skip to content

Commit

Permalink
Bundle Lua language server into the editor (#8914)
Browse files Browse the repository at this point in the history
This changeset includes the Lua language server into the editor distribution, so it's enabled in all projects. Yay!

Fixes #7704
  • Loading branch information
vlaaad committed May 14, 2024
1 parent dcc44a5 commit e590e93
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 18 deletions.
3 changes: 2 additions & 1 deletion editor/project.clj
Expand Up @@ -139,7 +139,8 @@
"prerelease" ["do" "clean," "protobuf," "sass" "once," "javac," "with-profile" "dev,sidecar,release" "run" "-m" "aot"]}

;; used by `pack` task
:packing {:pack-path "resources/_unpack"}
:packing {:pack-path "resources/_unpack"
:lua-language-server-version "v0.0.6"}

:codox {:sources ["src/clj"]
:output-dir "target/doc/api"
Expand Down
9 changes: 8 additions & 1 deletion editor/src/clj/editor/editor_extensions.clj
Expand Up @@ -791,14 +791,21 @@
:when dynamic-handler]
dynamic-handler))))

(defn- built-in-language-servers []
(let [lua-lsp-root (str (system/defold-unpack-path) "/" (.getPair (Platform/getHostPlatform)) "/bin/lsp/lua")]
#{{:languages #{"lua"}
:watched-files [{:pattern "**/.luacheckrc"}]
:launcher {:command [(str lua-lsp-root "/bin/lua-language-server" (when (util/is-win32?) ".exe"))
(str "--configpath=" lua-lsp-root "/config.json")]}}}))

(defn- reload-language-servers! [project]
(let [lsp (lsp/get-node-lsp project)
state (ext-state project)
ui (:ui state)]
(lsp/set-servers!
lsp
(into
#{}
(built-in-language-servers)
(comp
(mapcat
(fn [[path language-servers]]
Expand Down
2 changes: 0 additions & 2 deletions editor/src/java/com/defold/libs/ResourceUnpacker.java
Expand Up @@ -166,7 +166,6 @@ private static void unpackResourceFile(String resourceFileName, Path target, boo
FileUtils.deleteQuietly(outputFile);
}

logger.debug("unpacking file '{}' to '{}'", resourceFileName, outputPath);
File outputDirectory = outputFile.getParentFile();

if (outputDirectory != null) {
Expand Down Expand Up @@ -210,7 +209,6 @@ private static void unpackResourceDir(String resourceDir, Path target) throws IO
if (destFile.exists() && destFile.isDirectory()) {
FileUtils.deleteQuietly(destFile);
}
logger.debug("unpacking '{}' to '{}'", source, dest);
try {
Files.copy(source, dest, StandardCopyOption.REPLACE_EXISTING);
}
Expand Down
74 changes: 61 additions & 13 deletions editor/tasks/leiningen/pack.clj
Expand Up @@ -13,13 +13,16 @@
;; specific language governing permissions and limitations under the License.

(ns leiningen.pack
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[leiningen.util.http-cache :as http-cache])
(:import
(java.util.zip ZipFile)
(org.apache.commons.io FileUtils)))
(:require [clojure.java.io :as io]
[clojure.string :as str]
[leiningen.util.http-cache :as http-cache])
(:import [java.io File]
[java.nio.file CopyOption Files FileSystems FileVisitor FileVisitResult LinkOption Path Paths]
[java.nio.file.attribute FileAttribute]
[java.util.zip ZipEntry ZipFile]
[org.apache.commons.io FileUtils]))

(set! *warn-on-reflection* true)

(defn dynamo-home [] (get (System/getenv) "DYNAMO_HOME"))

Expand Down Expand Up @@ -88,7 +91,7 @@

(defn artifact-files
[]
(let [subst (fn [s] (.replace s "${DYNAMO-HOME}" (dynamo-home)))]
(let [subst (fn [s] (str/replace s "${DYNAMO-HOME}" (dynamo-home)))]
(into {} (for [[src dest] artifacts]
[(io/file (subst src)) (io/file (subst dest))]))))

Expand All @@ -102,7 +105,7 @@
"windows-x64" ["x86_64-win32"]})

(defn jar-file
[[artifact version & {:keys [classifier]} :as dependency]]
^File [[artifact version & {:keys [classifier]} :as dependency]]
(io/file (str (System/getProperty "user.home")
"/.m2/repository/"
(str/replace (namespace artifact) "." "/")
Expand All @@ -122,7 +125,7 @@
(let [java-platform (str/replace-first classifier "natives-" "")
natives-path (str "natives/" java-platform)]
(with-open [zip-file (ZipFile. (jar-file dependency))]
(doseq [entry (enumeration-seq (.entries zip-file))]
(doseq [^ZipEntry entry (enumeration-seq (.entries zip-file))]
(when (.startsWith (.getName entry) natives-path)
(let [libname (.getName (io/file (.getName entry)))]
(doseq [target-platform (jogl-classifier->platforms java-platform)]
Expand All @@ -136,11 +139,55 @@
(doseq [jogl-native-dep (filter jogl-native-dep? dependencies)]
(extract-jogl-native-dep jogl-native-dep pack-path)))

(defn pack-lua-language-server [pack-path lua-language-server-version]
(let [release-path (-> (format "https://github.com/defold/lua-language-server/releases/download/%s/release.zip"
lua-language-server-version)
http-cache/download
.toPath)
file-attributes (into-array FileAttribute [])
^"[Ljava.nio.file.CopyOption;" copy-options (into-array CopyOption [])]
(with-open [fs (FileSystems/newFileSystem release-path)]
(doseq [platform (keys engine-artifacts)
:let [zip-file-name (str platform ".zip")
src-zip-path (.getPath fs "lsp-lua-language-server" (into-array String ["plugins" zip-file-name]))
dst-root-path (Paths/get pack-path (into-array String [platform "bin" "lsp" "lua"]))]]
;; Copy config.json to the pack path
(let [source-path (.getPath fs "lsp-lua-language-server" (into-array String ["plugins" "share" "config.json"]))
target-path (.resolve dst-root-path "config.json")]
(Files/createDirectories (.getParent target-path) file-attributes)
(Files/copy source-path target-path copy-options))
;; Copy contents of bin zips to the pack path
(with-open [fs (FileSystems/newFileSystem src-zip-path)]
(doseq [^Path root-path (.getRootDirectories fs)
:let [entry-path->dst-path (fn [^Path p]
(let [name-count (.getNameCount p)]
(when (< 2 name-count)
(.resolve dst-root-path
(-> root-path
(.relativize p)
;; remove leading "bin/${platform}"
(.subpath 2 name-count)
str)))))]]
(Files/walkFileTree
root-path
(reify FileVisitor
(preVisitDirectory [_ path _]
(when-let [^Path target-path (entry-path->dst-path path)]
(when-not (Files/exists target-path (into-array LinkOption []))
(Files/createDirectories target-path file-attributes)))
FileVisitResult/CONTINUE)
(visitFile [_ path _]
(when-let [^Path target-path (entry-path->dst-path path)]
(Files/deleteIfExists target-path)
(Files/copy ^Path path target-path copy-options))
FileVisitResult/CONTINUE)
(postVisitDirectory [_ _ _] FileVisitResult/CONTINUE)))))))))

(defn copy-artifacts
[pack-path archive-domain git-sha]
(let [files (merge (engine-artifact-files archive-domain git-sha)
(artifact-files))]
(doseq [[src dest] files]
(doseq [[^File src dest] files]
(let [dest (io/file pack-path dest)]
(println (format "copying '%s' to '%s'" (str src) (str dest)))
(if-not (.exists src)
Expand All @@ -154,7 +201,8 @@
[{:keys [dependencies packing] :as project} & [git-sha]]
(let [sha (or git-sha (:engine project))
archive-domain (get project :archive-domain)
{:keys [pack-path]} packing]
{:keys [pack-path lua-language-server-version]} packing]
(FileUtils/deleteQuietly (io/file pack-path))
(copy-artifacts pack-path archive-domain sha)
(pack-jogl-natives pack-path dependencies)))
(pack-jogl-natives pack-path dependencies)
(pack-lua-language-server pack-path lua-language-server-version)))
3 changes: 2 additions & 1 deletion editor/tasks/leiningen/util/http_cache.clj
Expand Up @@ -109,7 +109,8 @@
(catch FileNotFoundException e
{:code 404}))))

(defn download [url]
(defn download
^File [url]
(let [c (->cache "~/.dcache" (* 4 1000000000))
hit (cache-get c url)
headers (if hit
Expand Down

0 comments on commit e590e93

Please sign in to comment.