Skip to content

Commit

Permalink
Report defignored files as non-existent
Browse files Browse the repository at this point in the history
Additionally, this changeset makes the editor sync resources after save if `.defignore` file was modified.

Fixes #6400
  • Loading branch information
vlaaad committed Jul 6, 2022
1 parent 42287cd commit 1dc3edd
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 63 deletions.
3 changes: 3 additions & 0 deletions editor/src/clj/editor/code/text_file.clj
Expand Up @@ -60,6 +60,9 @@
:icon "icons/32/Icons_11-Script-general.png"}
{:ext "appmanifest"
:label "App Manifest"
:icon "icons/32/Icons_11-Script-general.png"}
{:ext "defignore"
:label "Defignore"
:icon "icons/32/Icons_11-Script-general.png"}])

(g/defnode TextNode
Expand Down
31 changes: 18 additions & 13 deletions editor/src/clj/editor/disk.clj
Expand Up @@ -22,6 +22,7 @@
[editor.error-reporting :as error-reporting]
[editor.pipeline.bob :as bob]
[editor.progress :as progress]
[editor.resource :as resource]
[editor.resource-watch :as resource-watch]
[editor.ui :as ui]
[editor.workspace :as workspace]))
Expand Down Expand Up @@ -135,19 +136,23 @@
(let [evaluation-context (g/make-evaluation-context)
save-data (project/dirty-save-data-with-progress project evaluation-context render-save-progress!)]
(project/write-save-data-to-disk! save-data {:render-progress! render-save-progress!})
(render-save-progress! (progress/make-indeterminate "Reading timestamps..."))
(let [updated-file-resource-status-map-entries (mapv save-data-status-map-entry save-data)]
(render-save-progress! progress/done)
(ui/run-later
(try
(project/update-system-cache-save-data! evaluation-context)
(g/update-property! workspace :resource-snapshot resource-watch/update-snapshot-status updated-file-resource-status-map-entries)
(project/invalidate-save-data-source-values! save-data)
(when (some? changes-view)
(changes-view/refresh! changes-view render-reload-progress!))
(complete! true)
(catch Throwable error
(fail! error)))))))
(if (and (some #(= "/.defignore" (resource/proj-path (:resource %))) save-data)
(not (blocking-reload! render-reload-progress! workspace [] nil)))
(complete! false)
(do
(render-save-progress! (progress/make-indeterminate "Reading timestamps..."))
(let [updated-file-resource-status-map-entries (mapv save-data-status-map-entry save-data)]
(render-save-progress! progress/done)
(ui/run-later
(try
(project/update-system-cache-save-data! evaluation-context)
(g/update-property! workspace :resource-snapshot resource-watch/update-snapshot-status updated-file-resource-status-map-entries)
(project/invalidate-save-data-source-values! save-data)
(when (some? changes-view)
(changes-view/refresh! changes-view render-reload-progress!))
(complete! true)
(catch Throwable error
(fail! error)))))))))
(catch Throwable error
(fail! error))))
success-promise))
Expand Down
8 changes: 4 additions & 4 deletions editor/src/clj/editor/game_project.clj
Expand Up @@ -96,10 +96,10 @@
(openable? [this] (resource/openable? resource))

io/IOFactory
(io/make-input-stream [this opts] (io/input-stream resource))
(io/make-reader [this opts] (io/reader resource))
(io/make-output-stream [this opts] (io/output-stream resource))
(io/make-writer [this opts] (io/writer resource)))
(make-input-stream [this opts] (io/input-stream resource))
(make-reader [this opts] (io/reader resource))
(make-output-stream [this opts] (io/output-stream resource))
(make-writer [this opts] (io/writer resource)))

(defn- make-custom-build-target [node-id resource]
(bt/with-content-hash
Expand Down
63 changes: 45 additions & 18 deletions editor/src/clj/editor/resource.clj
Expand Up @@ -95,10 +95,36 @@
(when-let [last-slash (string/last-index-of proj-path "/")]
(subs proj-path 0 last-slash)))

(def ^:private defignore-cache
;; path->{:mtime ... :pred ...}
(atom {}))

;; root -> pred if project path (string starting with /) is ignored
(defn- defignore-pred [^File root]
(let [defignore-file (io/file root ".defignore")
defignore-path (.getCanonicalPath defignore-file)
latest-mtime (.lastModified defignore-file)
{:keys [mtime pred]} (get @defignore-cache defignore-path)]
(if (= mtime latest-mtime)
pred
(let [pred (if (.isFile defignore-file)
(let [prefixes (into
#{}
(filter #(string/starts-with? % "/"))
(string/split-lines (slurp defignore-file)))]
(fn ignored-path? [path]
(boolean (some #(string/starts-with? path %) prefixes))))
(constantly false))]
(swap! defignore-cache assoc defignore-path {:mtime latest-mtime :pred pred})
pred))))

(defn ignored-project-path? [^File root path]
((defignore-pred root) path))

;; Note! Used to keep a file here instead of path parts, but on
;; Windows (File. "test") equals (File. "Test") which broke
;; FileResource equality tests.
(defrecord FileResource [workspace ^String abs-path ^String project-path ^String name ^String ext source-type children]
(defrecord FileResource [workspace ^String root ^String abs-path ^String project-path ^String name ^String ext source-type children]
Resource
(children [this] children)
(ext [this] ext)
Expand All @@ -115,6 +141,7 @@
;; fix it manually and hopefully remember to update the scripts too.
(let [file (io/file this)]
(and (.exists file)
(not (ignored-project-path? (io/file root) project-path))
(string/ends-with? (->unix-seps (.getCanonicalPath file)) project-path)))
(catch IOException _
false)
Expand All @@ -134,13 +161,13 @@
(openable? [this] (= :file source-type))

io/IOFactory
(io/make-input-stream [this opts] (io/make-input-stream (io/file this) opts))
(io/make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(io/make-output-stream [this opts] (io/make-output-stream (io/file this) opts))
(io/make-writer [this opts] (io/make-writer (io/make-output-stream this opts) opts))
(make-input-stream [this opts] (io/make-input-stream (io/file this) opts))
(make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(make-output-stream [this opts] (io/make-output-stream (io/file this) opts))
(make-writer [this opts] (io/make-writer (io/make-output-stream this opts) opts))

io/Coercions
(io/as-file [this] (File. abs-path)))
(as-file [this] (File. abs-path)))

(defn make-file-resource [workspace ^String root ^File file children]
(let [source-type (if (.isDirectory file) :folder :file)
Expand All @@ -149,16 +176,16 @@
name (.getName file)
project-path (if (= "" name) "" (str "/" (relative-path (File. root) (io/file path))))
ext (FilenameUtils/getExtension path)]
(FileResource. workspace abs-path project-path name ext source-type children)))
(FileResource. workspace root abs-path project-path name ext source-type children)))

(defn file-resource? [resource]
(instance? FileResource resource))

(core/register-read-handler!
"file-resource"
(transit/read-handler
(fn [{:keys [workspace ^String abs-path ^String project-path ^String name ^String ext source-type children]}]
(FileResource. workspace abs-path project-path name ext source-type children))))
(fn [{:keys [workspace ^String root ^String abs-path ^String project-path ^String name ^String ext source-type children]}]
(FileResource. workspace root abs-path project-path name ext source-type children))))

(core/register-write-handler!
FileResource
Expand Down Expand Up @@ -196,10 +223,10 @@
(openable? [this] false)

io/IOFactory
(io/make-input-stream [this opts] (io/make-input-stream (IOUtils/toInputStream ^String (:data this)) opts))
(io/make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(io/make-output-stream [this opts] (io/make-output-stream (.toCharArray ^String (:data this)) opts))
(io/make-writer [this opts] (io/make-writer (io/make-output-stream this opts) opts)))
(make-input-stream [this opts] (io/make-input-stream (IOUtils/toInputStream ^String (:data this)) opts))
(make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(make-output-stream [this opts] (io/make-output-stream (.toCharArray ^String (:data this)) opts))
(make-writer [this opts] (io/make-writer (io/make-output-stream this opts) opts)))

(core/register-record-type! MemoryResource)

Expand Down Expand Up @@ -236,13 +263,13 @@
(openable? [this] (= :file (source-type this)))

io/IOFactory
(io/make-input-stream [this opts] (make-zip-resource-input-stream this))
(io/make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(io/make-output-stream [this opts] (throw (Exception. "Zip resources are read-only")))
(io/make-writer [this opts] (throw (Exception. "Zip resources are read-only")))
(make-input-stream [this opts] (make-zip-resource-input-stream this))
(make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(make-output-stream [this opts] (throw (Exception. "Zip resources are read-only")))
(make-writer [this opts] (throw (Exception. "Zip resources are read-only")))

io/Coercions
(io/as-file [this] (io/as-file zip-uri)))
(as-file [this] (io/as-file zip-uri)))

(core/register-record-type! ZipResource)

Expand Down
19 changes: 1 addition & 18 deletions editor/src/clj/editor/resource_watch.clj
Expand Up @@ -96,26 +96,9 @@

(def reserved-proj-paths #{"/builtins" "/build" "/.internal" "/.git"})

(def ^:private ignored-proj-paths-atom (atom nil))

(defn- ignored-proj-paths [^File root]
(assert (and (some? root)
(.isDirectory root)))
(swap! ignored-proj-paths-atom
(fn [ignored-proj-paths]
(if (some? ignored-proj-paths)
ignored-proj-paths
(let [defignore-file (io/file root ".defignore")]
(if (.isFile defignore-file)
(set (str/split-lines (slurp defignore-file)))
#{}))))))

(defn ignored-proj-path? [^File root path]
(contains? (ignored-proj-paths root) path))

(defn reserved-proj-path? [^File root path]
(or (reserved-proj-paths path)
(ignored-proj-path? root path)))
(resource/ignored-project-path? root path)))

(defn- file-resource-filter [^File root ^File f]
(not (or (let [file-name (.getName f)]
Expand Down
10 changes: 5 additions & 5 deletions editor/src/clj/editor/workspace.clj
Expand Up @@ -79,13 +79,13 @@ ordinary paths."
(openable? [this] false)

io/IOFactory
(io/make-input-stream [this opts] (io/make-input-stream (File. (resource/abs-path this)) opts))
(io/make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(io/make-output-stream [this opts] (let [file (File. (resource/abs-path this))] (io/make-output-stream file opts)))
(io/make-writer [this opts] (io/make-writer (io/make-output-stream this opts) opts))
(make-input-stream [this opts] (io/make-input-stream (File. (resource/abs-path this)) opts))
(make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(make-output-stream [this opts] (let [file (File. (resource/abs-path this))] (io/make-output-stream file opts)))
(make-writer [this opts] (io/make-writer (io/make-output-stream this opts) opts))

io/Coercions
(io/as-file [this] (File. (resource/abs-path this))))
(as-file [this] (File. (resource/abs-path this))))

(def build-resource? (partial instance? BuildResource))

Expand Down
15 changes: 14 additions & 1 deletion editor/src/reveal/editor/reveal.clj
Expand Up @@ -7,6 +7,7 @@
[internal.graph.types :as gt]
[internal.system :as is])
(:import [clojure.lang IRef]
[editor.resource FileResource ZipResource]
[internal.graph.types Endpoint]))

(defn- node-value-or-err [ec node-id label]
Expand Down Expand Up @@ -119,4 +120,16 @@
(r/stream (gt/endpoint-node-id endpoint))
r/separator
(r/stream (gt/endpoint-label endpoint))
(r/raw-string "]" {:fill :object})))
(r/raw-string "]" {:fill :object})))

(r/defstream FileResource [resource]
(r/horizontal
(r/raw-string "#resource/file" {:fill :object})
r/separator
(r/stream (resource/proj-path resource))))

(r/defstream ZipResource [resource]
(r/horizontal
(r/raw-string "#resource/zip" {:fill :object})
r/separator
(r/stream (resource/proj-path resource))))
8 changes: 4 additions & 4 deletions editor/test/integration/test_util.clj
Expand Up @@ -177,10 +177,10 @@
(openable? [this] (= :file source-type))

io/IOFactory
(io/make-input-stream [this opts] (io/make-input-stream content opts))
(io/make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(io/make-output-stream [this opts] (assert false "writing to not supported"))
(io/make-writer [this opts] (assert false "writing to not supported")))
(make-input-stream [this opts] (io/make-input-stream content opts))
(make-reader [this opts] (io/make-reader (io/make-input-stream this opts) opts))
(make-output-stream [this opts] (assert false "writing to not supported"))
(make-writer [this opts] (assert false "writing to not supported")))

(defn make-fake-file-resource
([workspace root file content]
Expand Down

0 comments on commit 1dc3edd

Please sign in to comment.