add resource renamer script

master
Florian Schrofner 2022-10-16 14:41:01 +02:00
parent b141bdadc3
commit bdd59cc199
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,21 @@
# Resource Renamer
A tool that enables you to quickly rename a single Android resource that is already split up into multiple folders, e.g. `drawable-xhdpi`, `drawable-xxhdpi`, ...
It will reuse the file extension of the files that are already present inside the directory.
## Example Usage
```bash
./rr.clj -d "/home/schrofi/Downloads/random_asset" -n "ic_photo"
```
##
### Fish
To more conveniently use the tool from the commandline, you can define a fish function like this:
```
function rr
PATH_TO_SCRIPT/rr.clj -d $(pwd) -n $argv[1]
end
funcsave rr
```
And then just use the tool by opening a terminal in the resource folder you want to rename and execute `rr TARGET_NAME`.

23
resource-renamer/rr.clj Executable file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env bb
(require '[clojure.tools.cli :refer [parse-opts]])
(def cli-options
[["-d" "--directory DIRECTORY" "Directory"]
["-n" "--name NAME" "Target resource name"]])
(def options (:options (parse-opts *command-line-args* cli-options)))
(defn- rename-child
[folder target-name]
(let [files (.listFiles folder)]
(if (= (count files) 1)
(let [resource (first files)
extension (last (str/split (.getName resource) #"\."))]
(when (nil? extension) (throw (.Exception "file does not have a file extension")))
(.renameTo resource (io/file (str (.getParent resource) (java.io.File/separator) target-name "." extension))))
(throw (.Exception "directories need to contain exactly one file")))))
(let [directory (io/file (:directory options))]
(doseq [subdirectory (.listFiles directory)]
(rename-child subdirectory (:name options))))