Last active
June 14, 2021 04:31
-
-
Save ampersanda/aac70cc0644df12199ea32988f3c4d73 to your computer and use it in GitHub Desktop.
Switch Flutter SDK project version in IntelliJ IDEA/Android Studio and fvm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{:paths ["."] | |
:deps {org.clojure/tools.cli {:mvn/version "1.0.194"}}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;#!/usr/bin/env bb | |
;; Helper to switch SDK versions in IntelliJ IDEA and fvm | |
(ns runner | |
(:require [clojure.java.shell :as shell] | |
[clojure.string :refer [replace]] | |
[clojure.java.io :refer [file]] | |
[clojure.tools.cli :refer [parse-opts]])) | |
(def cli-options | |
[["-h" "--help" "Show help"]]) | |
;; TODO(lucky): need "version" validation | |
(defn change-idea-if-exists [version] | |
(let [path ".idea/libraries/Dart_SDK.xml" | |
exists? (.exists (file path))] | |
(if exists? | |
;; file exists, find current version | |
(let [content (slurp path) | |
current-version (second | |
(first | |
(re-seq #"\<root url\=.*\/fvm\/versions\/(.*)\/bin.*\/>" content))) | |
same? (= current-version version)] | |
(if same? | |
(do | |
(println (str "Current IDEA Flutter project is already on " version)) | |
;; return success status | |
false) | |
(let [pattern (re-pattern (replace current-version #"\." "\\\\.")) | |
new-content (replace content pattern version)] | |
(spit path new-content) | |
(println (str "Current IDEA Flutter project SDK is set to " version)) | |
;; return success status | |
true))) | |
;; file is not exists, print error | |
(do | |
(println ".idea SDK folder configuration is not found") | |
;; return success status | |
false)))) | |
;; TODO(lucky): need "version" validation | |
(defn change-fvm [version] | |
(println (:out (shell/sh "fvm" "use" version))) | |
(println (:out (shell/sh "fvm" "flutter" "pub" "get")))) | |
(defn change-sdk [{:keys [options arguments summary errors] :as args}] | |
(when (change-idea-if-exists (first arguments)) | |
(change-fvm (first arguments)))) | |
(let [{:keys [options arguments summary errors] :as args} (parse-opts *command-line-args* cli-options) | |
is-args-empty? (empty? | |
;; to avoid NPE in babashka, bash will pass empty string to arguments | |
(filter #(not= % "") arguments))] | |
(cond | |
is-args-empty? (println | |
(str "ERROR: Error changing sdk.\n\n" summary)) | |
errors (println errors "\n" summary) | |
(:help options) (println summary) | |
arguments (change-sdk args))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment