Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add terminating newlines to output files #59

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/rules_clojure/fs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,8 @@

(defn new-temp-file [dir prefix suffix]
(Files/createTempFile (->path dir) prefix suffix (into-array FileAttribute [])))

(defn spit-file [f content & opts]
(let [has-newline? (= \newline (last content))
content (if has-newline? content (str content \newline))]
(apply spit f (str content) opts)))
6 changes: 3 additions & 3 deletions src/rules_clojure/gen_build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@
(-> dir
(fs/->path "BUILD.bazel")
fs/path->file
(spit content :encoding "UTF-8"))))
(fs/spit-file content :encoding "UTF-8"))))

(s/fdef gen-source-paths- :args (s/cat :a (s/keys :req-un [::deps-edn-dir ::src-ns->label ::dep-ns->label ::jar->lib ::deps-repo-tag ::deps-bazel]) :paths (s/coll-of fs/path?)))
(defn gen-source-paths-
Expand Down Expand Up @@ -808,7 +808,7 @@
"generates the BUILD file for @deps//: with a single target containing all deps.edn-resolved dependencies"
[{:keys [repository-dir deps-build-dir dep-ns->label jar->lib lib->jar lib->deps deps-repo-tag deps-bazel] :as args}]
(println "writing to" (-> (fs/->path deps-build-dir "BUILD.bazel") fs/path->file))
(spit (-> (fs/->path deps-build-dir "BUILD.bazel") fs/path->file)
(fs/spit-file (-> (fs/->path deps-build-dir "BUILD.bazel") fs/path->file)
(str/join "\n\n" (concat
[(emit-bazel (list 'package (kwargs {:default_visibility ["//visibility:public"]})))
(emit-bazel (list 'load "@rules_clojure//:rules.bzl" "clojure_library"))]
Expand Down Expand Up @@ -987,7 +987,7 @@
(apply disj $ output-ns exclude-nses)
(sort $))
conditional-require? (re-matches #".*\.cljc$" output-filename)]
(spit (fs/path->file (fs/->path workspace-root output-filename))
(fs/spit-file (fs/path->file (fs/->path workspace-root output-filename))
(str ";;; Generated by bazel, do not edit\n\n"
(str "(ns " output-ns "\n")
(str " "
Expand Down
4 changes: 4 additions & 0 deletions test/rules_clojure/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ clojure_test(name="persistent-classloader-test",
clojure_test(name="compile-test",
deps=[":test-deps"],
test_ns = "rules-clojure.compile-test")

clojure_test(name="fs-test",
deps=[":test-deps"],
test_ns = "rules-clojure.fs-test")
20 changes: 20 additions & 0 deletions test/rules_clojure/fs_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns rules-clojure.fs-test
(:require [clojure.java.shell :as shell]
[clojure.string :as str]
[clojure.test :refer :all]
[rules-clojure.fs :as fs]))

(defn count-lines [f]
(-> (shell/sh "wc" "-l" f)
:out
str/trim
first
str
Long/parseLong))

(deftest spit-file
(let [tmp-file "/tmp/spit-test.txt"]
(spit tmp-file "test with spit")
(is (= 0 (count-lines tmp-file)))
(fs/spit-file tmp-file "test with fs/spit-file")
(is (= 1 (count-lines tmp-file)))))