Skip to content

Commit

Permalink
initial implementation of dirac.main
Browse files Browse the repository at this point in the history
  • Loading branch information
darwin committed Jan 6, 2020
1 parent eb6a925 commit 431d54f
Show file tree
Hide file tree
Showing 47 changed files with 1,641 additions and 13 deletions.
4 changes: 4 additions & 0 deletions dev-resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
log4j.rootLogger=DEBUG, console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %c: %m%n
20 changes: 16 additions & 4 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
['version-clj "0.1.2"]
['clansi "1.0.0"]
['funcool/cuerdas "2.2.1"]
['com.rpl/specter "1.1.3"]])
['com.rpl/specter "1.1.3"]
['progrock "0.1.2"]
['me.raynes/conch "0.8.0"]
['clj-sub-command "0.5.1"]])

(def test-deps
[; we cannot use :dependencies under individual profiles because Cursive recognizes only root level
Expand All @@ -38,6 +41,7 @@
['org.clojure/tools.namespace "0.3.1" :scope "test"]
['org.clojure/tools.reader "1.3.2" :scope "test"]
['fipp "0.6.22" :scope "test"]
['nubank/matcher-combinators "1.2.5" :scope "test"]

['clj-logging-config clj-logging-config-version :scope "test"]
['org.slf4j/slf4j-log4j12 slf4j-log4j12-version :scope "test"]
Expand Down Expand Up @@ -90,6 +94,8 @@
"src/settings"
"src/shared"
"src/logging"
"src/main"
"src/home"

"test/src/test_lib"
"test/src/webdriver"
Expand Down Expand Up @@ -131,8 +137,8 @@
:logging-support
^{:pom-scope :provided} ; ! to overcome default jar/pom behaviour, our :dependencies replacement would be ignored for some reason
[{:dependencies [[clj-logging-config ~clj-logging-config-version :scope nil]
[org.slf4j/slf4j-log4j12 ~slf4j-log4j12-version :scope nil]]
:source-paths ["src/logging"]}]
[org.slf4j/slf4j-log4j12 ~slf4j-log4j12-version :scope nil]]
:source-paths ["src/logging"]}]

:lib-with-logging
[:lib :logging-support]
Expand Down Expand Up @@ -182,6 +188,8 @@
"src/nrepl"
"src/shared"
"src/logging"
"src/home"
"src/main"
"test/src/test_lib"
"test/src/webdriver"
"test/browser/src/browser_tests"
Expand All @@ -205,7 +213,7 @@
:compiler {:output-to "test/browser/fixtures/resources/.compiled/tasks/main.js"
:output-dir "test/browser/fixtures/resources/.compiled/tasks"
:optimizations :none ; we rely on optimizations :none in test runner
:external-config {:devtools/config {:dont-detect-custom-formatters true}}
:external-config {:devtools/config {:dont-detect-custom-formatters true}}
:source-map true
:source-map-timestamp true}}
:scenarios01
Expand Down Expand Up @@ -499,6 +507,10 @@
"run-backend-tests-19" ["with-profile" "+test-runner,+clojure19" "run" "-m" "dirac.tests.backend.runner"]
"run-backend-tests-110" ["with-profile" "+test-runner,+clojure110" "run" "-m" "dirac.tests.backend.runner"]

"run-cli-tests-18" ["with-profile" "+test-runner,+clojure18" "run" "-m" "dirac.test-runner"]
"run-cli-tests-19" ["with-profile" "+test-runner,+clojure19" "run" "-m" "dirac.test-runner"]
"run-cli-tests-110" ["with-profile" "+test-runner,+clojure110" "run" "-m" "dirac.test-runner"]

"run-browser-tests" ["shell" "scripts/run-browser-tests.sh" "dirac.tests.browser.runner"]
"run-browser-tests-dev" ["shell" "scripts/run-browser-tests.sh" "dirac.tests.browser.runner/-dev-main"]
"run-browser-tests-agent" ["with-profile" "+test-runner,+debugger-5005" "run" "-m" "dirac.tests.browser.runner/run-agent"]
Expand Down
20 changes: 20 additions & 0 deletions scripts/main-launcher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

# this script tries to launch dirac.main from command line
#
# usage ./main-launcher.sh [args]
#

set -e

VERSION=${DIRAC_MAIN_VERSION:-LATEST} # e.g. 0.1.0 or LATEST
GROUP=${DIRAC_MAIN_GROUP:-binaryage}
ARTIFACT=${DIRAC_MAIN_ARTIFACT:-dirac}

DIRAC_DEP=${DIRAC_MAIN_DIRAC_DEP:-"{$GROUP/$ARTIFACT {:mvn/version \"$VERSION\"}}"}
DEPS=${DIRAC_MAIN_DEPS:-"{:deps $DIRAC_DEP}"}
CLI_NS=${DIRAC_MAIN_CLI_NS:-"dirac.main"}

# $DEPS should be something like '{:deps {binaryage/dirac {:mvn/version "LATEST"}}}'
set -x
exec clojure -Sdeps "$DEPS" -m "$CLI_NS" "$@"
18 changes: 18 additions & 0 deletions scripts/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

# this script tries to launch dirac.main via lein
#
# usage ./main.sh [args]
#

set -e

ARGS=()
if [[ -t 1 ]] ; then
# please note that lein run does some internal piping and prevents TTY detection, we have to force colors by hand
# https://stackoverflow.com/a/29425613/84283
ARGS+=("--force-color")
fi

# set -x
exec lein run -m dirac.main -- "${ARGS[@]}" "$@"
16 changes: 16 additions & 0 deletions scripts/run-cli-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -e -o pipefail
# shellcheck source=_config.sh
source "$(dirname "${BASH_SOURCE[0]}")/_config.sh"

cd "$ROOT"

export DIRAC_TEST_PLAYGROUND="$ROOT/test/playground"

echo "Running CLI tests..."
lein run-cli-tests-110
lein run-cli-tests-19

# because matcher-combinators.matchers uses some 1.9 apis
#lein run-cli-tests-18
1 change: 1 addition & 0 deletions scripts/test-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ redirect_to_test_stage_if_needed

cd "$ROOT"

./scripts/test-cli.sh
./scripts/test-backend.sh
./scripts/test-browser.sh "$@"
13 changes: 13 additions & 0 deletions scripts/test-cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

# this will run browser tests against fully optimized dirac extension (release build)

set -e -o pipefail
# shellcheck source=_config.sh
source "$(dirname "${BASH_SOURCE[0]}")/_config.sh"

redirect_to_test_stage_if_needed

cd "$ROOT"

./scripts/run-cli-tests.sh
10 changes: 10 additions & 0 deletions src/home/dirac/home.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(ns dirac.home
(:require [dirac.home.locations :as locations]))

(def get-home-dir-path locations/get-home-dir-path)

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(comment
(get-home-dir-path)
)
30 changes: 30 additions & 0 deletions src/home/dirac/home/chromium.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
(ns dirac.home.chromium
(:require [dirac.home.chromium.scout :as scout]
[dirac.home.chromium.link :as link]
[dirac.home.chromium.profiles :as profiles]
[dirac.home.defaults :as defaults]
[dirac.home.chromium.extra-args :as extra-args]
[dirac.home.chromium.mapping :as mapping]
[dirac.home.locations :as locations]))

(def find-chrome-executable scout/find-chrome-executable)
(def determine-chrome-version scout/determine-chrome-version)

(def chromium-link-exists? link/chromium-link-exists?)
(def resolve-chromium-link link/resolve-chromium-link)

(def resolve-dirac-release! mapping/resolve-dirac-release!)
(def try-download-releases-file-if-needed! mapping/try-download-releases-file-if-needed!)

(def get-chromium-profile-dir-path profiles/get-chromium-profile-dir-path)

(def read-chromium-extra-args extra-args/read-chromium-extra-args)

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(comment
(find-chrome-executable)
(determine-chrome-version (find-chrome-executable))
(resolve-dirac-release! "81.1.4014.0" defaults/releases-file-url (locations/get-releases-file-path))
(read-chromium-extra-args)
)
23 changes: 23 additions & 0 deletions src/home/dirac/home/chromium/extra_args.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(ns dirac.home.chromium.extra-args
(:require [dirac.home.helpers :as helpers]
[dirac.home.locations :as locations]
[clojure.java.io :as io])
(:import (java.io File)))

(def get-chromium-extra-args-file-path locations/get-chromium-extra-args-file-path)

(defn read-chromium-extra-args
([] (read-chromium-extra-args (get-chromium-extra-args-file-path)))
([^File file]
(let [file (io/file file)]
(if (.exists file)
(-> file
(helpers/read-trimmed-lines)
(helpers/filter-commented-lines))))))

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(comment
(get-chromium-extra-args-file-path)
(read-chromium-extra-args)
)
68 changes: 68 additions & 0 deletions src/home/dirac/home/chromium/link.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
(ns dirac.home.chromium.link
(:require [clojure.java.io :as io]
[clojure.string :as string]
[dirac.home.helpers :as helpers]
[dirac.home.locations :as locations]))

(def get-chromium-link-path locations/get-chromium-link-path)

(defn prepare-followed-paths-chain [paths]
(string/join " -> " paths))

(defn explain-followed-paths [paths]
(let [chain (prepare-followed-paths-chain paths)]
(if-not (string/blank? chain)
(str " Followed paths: '" chain "'."))))

; for users without filesystem links we support text files simulating links
(defn follow-text-file-link [file]
(let [target-path (helpers/read-and-trim-first-line file)
target-file (io/file target-path)]
(if (.isAbsolute target-file)
(.getPath target-file)
(let [parent-dir (.getParent file)
target-file (io/file parent-dir target-path)]
(.getPath target-file)))))

(defn resolve-link
"Try to resolve default chromium link file (or provided file) by following symlinks or textual links recursively.
Stops when it finds an executable and returns {:executable [absolute-path]}.
In case of a failure returns textual description {:failure [text]}.
"
([file-path] (resolve-link file-path []))
([file-path followed-paths]
(try
(let [file (io/file file-path)]
(if-not (.exists file)
(let [paths (dedupe followed-paths)]
{:failure (str "File path '" (.getPath file) "' does not exist." (explain-followed-paths paths))
:paths (dedupe (conj paths (.getPath file)))})
(let [resolved-file (.getCanonicalFile file)]
(if-not (.exists resolved-file)
(let [paths (dedupe (conj followed-paths (.getPath file)))]
{:failure (str "File path '" (.getPath resolved-file) "' does not exist." (explain-followed-paths paths))
:paths (dedupe (conj paths (.getPath resolved-file)))})
(let [paths (concat followed-paths [(.getPath file) (.getPath resolved-file)])]
(if (.canExecute resolved-file)
{:executable (.getAbsolutePath resolved-file)
:paths (dedupe paths)}
(resolve-link (follow-text-file-link resolved-file) paths)))))))
(catch Exception e
{:failure (str "Exception: " (.getMessage e))}))))

(defn resolve-chromium-link
([] (resolve-chromium-link (get-chromium-link-path)))
([chromium-file]
(resolve-link chromium-file)))

(defn chromium-link-exists? []
(.exists (io/file (get-chromium-link-path))))

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(comment
(resolve-chromium-link)
(chromium-link-exists?)
(read-and-trim-first-line (io/file *file*))
)

83 changes: 83 additions & 0 deletions src/home/dirac/home/chromium/mapping.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
(ns dirac.home.chromium.mapping
(:require [clojure.java.io :as io]
[dirac.home.helpers :as helpers]
[dirac.home.locations :as locations]
[dirac.home.chromium.version :as version]
[clojure.tools.logging :as log]
[dirac.home.defaults :as defaults])
(:import (java.io FileNotFoundException)))

(def ^:dynamic *mock-releases*)

(def get-releases-file-path locations/get-releases-file-path)

(defn read-releases-file [file]
(if (bound? #'*mock-releases*)
*mock-releases*
(helpers/read-edn file)))

(defn lookup-dirac-release [chromium-mapping chromium-version]
(let [version-keys (filter string? (keys chromium-mapping))
sorted-version-keys (sort version/compare-versions version-keys)
match? (fn [v] (not= (version/compare-versions chromium-version v) -1))
resolved-version (last (filter match? sorted-version-keys))]
(if (some? resolved-version)
(get chromium-mapping resolved-version)
(get chromium-mapping :unsupported))))

; -- public API -------------------------------------------------------------------------------------------------------------

(defn try-download-releases-file-if-needed! [releases-file-url releases-file-path]
(when-not (bound? #'*mock-releases*)
(try
; TODO: implement Etag or caching
(helpers/download! releases-file-url releases-file-path)
(catch FileNotFoundException e
(log/info (str "Unable to download '" releases-file-url "':\n" e))
(if (.exists (io/file releases-file-path))
(log/info (str "Using on-disk version from '" releases-file-path "' which might be outdated.")))))))

(defn resolve-dirac-release!
([chromium-version url releases-file-path]
(try-download-releases-file-if-needed! url releases-file-path)
(resolve-dirac-release! chromium-version releases-file-path))
([chromium-version releases-file]
(let [releases (read-releases-file releases-file)
chromium-mapping (:chromium releases)
_ (assert chromium-mapping)
release-descriptor (lookup-dirac-release chromium-mapping chromium-version)]
(if (string? release-descriptor)
release-descriptor
(throw (ex-info (str "Unable to retrieve Dirac release for Chromium " chromium-version ".\n"
"Reason: " (:message release-descriptor))
{:chromium-version chromium-version
:release-descriptor release-descriptor}))))))

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(comment
(get-releases-file-path)
(try-download-releases-file-if-needed! defaults/releases-file-url (get-releases-file-path))
(read-releases-file (get-releases-file-path))
(let [mapping {"81.1.4014.0" "1.5.0"
"81.0.4014.3" "1.4.7"
"81.0.4014.1" "1.4.6"
"81.0.4013.0" "1.4.5"
:unsupported {:message "This chromium version is not supported."}}]
[(lookup-dirac-release mapping "82.1.4010") ; => "1.5.0"
(lookup-dirac-release mapping "81.1.4014") ; => "1.5.0"
(lookup-dirac-release mapping "81.0.4016") ; => "1.4.7"
(lookup-dirac-release mapping "81.0.4014.6") ; => "1.4.7"
(lookup-dirac-release mapping "81.0.4014.3") ; => "1.4.7"
(lookup-dirac-release mapping "81.0.4014.2") ; => "1.4.6"
(lookup-dirac-release mapping "81.0.4014.1") ; => "1.4.6"
(lookup-dirac-release mapping "81.0.4013.1") ; => "1.4.5"
(lookup-dirac-release mapping "81.0.4012.0")] ; => :message
)
(binding [*mock-releases* {:chromium {"81.0.4014.0" "1.2.3"}}]
(resolve-dirac-release! "81.0.4014.1"))
(binding [*mock-releases* {:chromium {"81.0.4014.0" "1.2.3"
:unsupported {:message "NOT SUPPORTED"}}}]
(resolve-dirac-release! "81.0.4010.0"))

)
14 changes: 14 additions & 0 deletions src/home/dirac/home/chromium/profiles.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(ns dirac.home.chromium.profiles
(:require [dirac.home.locations :as locations]))

(def get-chromium-profiles-dir-path locations/get-chromium-profiles-dir-path)

(defn get-chromium-profile-dir-path [name]
(locations/canonical-path (get-chromium-profiles-dir-path) name))

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(comment
(get-chromium-profiles-dir-path)
(get-chromium-profile-dir-path "test")
)
Loading

0 comments on commit 431d54f

Please sign in to comment.