-
Notifications
You must be signed in to change notification settings - Fork 0
/
useExtractor
executable file
·103 lines (88 loc) · 3.18 KB
/
useExtractor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bash
# usage: addDep <libname> <build.gradle path>
# example: addDep streamtape-extractor src/en/zoro/build.gradle
addDep(){
local implStr=" implementation(project(':lib-$1'))"
local file=$2
local depsStr="dependencies {"
# Tries to add the implementation line in the dependencies block
# if it isn't there. It will create a dependencies {} block if necessary.
if ! grep -q "$implStr" "$file"; then
if ! grep -q "dependencies" "$file"; then
sed -i "s/apply from:/$depsStr\n$implStr\n}\n\napply from:/" "$file"
else
sed -i "s/$depsStr/$depsStr\n$implStr/" "$file"
fi
fi
}
# Replaces the old "import ...." line with a new one that
# uses the extractor class.
changeImport(){
local fileName=$1
local libName=$2
local className=$3
local basePath="../lib/$libName"
local manifestPath="$basePath/src/main/AndroidManifest.xml"
local gradlePath="$basePath/build.gradle.kts"
local package
if [ -e "$manifestPath" ]; then
package=$(grep package "$manifestPath" | cut -d '"' -f 2)
else
package=$(grep namespace "$gradlePath" | cut -d '"' -f 2)
fi
# package of the extractor lib, like "eu.kanade.tachiyomi.lib.<NAME>"
local importStr="import $package.$className"
if ! grep -qi "$importStr" "$fileName"; then
sed -i "s/import .*$className/$importStr/i" "$fileName"
fi
}
getExtractorClass() {
local libName=$1
local filesPath="../lib/$libName/src/main/java/eu/kanade/tachiyomi/lib/*"
local lastDir=$(echo $filesPath | cut -d "/" -f 11)
ls $filesPath | grep -i "$lastDir.kt" | sed "s/.kt//"
}
extractorLib=$1
if [[ ! $extractorLib =~ "-extractor" ]]; then
extractorLib="$extractorLib-extractor"
fi
if [[ ! -d ../lib/$extractorLib ]]; then
echo "Library $extractorLib does not exist!"
exit
fi
extractorClass=$(getExtractorClass "$extractorLib")
# base path of extensions that have the extractor class
extensions=$(fd -i "$extractorClass" | cut -d "/" -f 1-2)
changedClasses=""
completed=0
all=$(echo "$extensions" | tr " " "\n" | wc -l)
for ext in $extensions; do
buildFile="$ext/build.gradle"
className=$(grep extClass "$buildFile" | cut -d "'" -f 2)
pkgPath=$(grep pkgNameSuffix "$buildFile" | cut -d "'" -f 2 | sed "s@\.@/@")
addDep "$extractorLib" "$buildFile"
fullPath="$ext/src/eu/kanade/tachiyomi/animeextension/$pkgPath"
extractor=$(fd -i "$extractorClass" "$fullPath")
$EDITOR "$extractor"
rm "$extractor"
mainFiles="$fullPath/${className/./}.kt"
if [[ "$className" =~ "Factory" ]]; then
mainFiles+=" ${mainFiles/Factory/*}"
fi
for mainFile in $mainFiles; do
changeImport "$mainFile" "$extractorLib" "$extractorClass"
matchLine=$(grep -ni "$extractorClass(" "$mainFile" | cut -d: -f1)
if [[ -z "$matchLine" ]]; then
$EDITOR "$mainFile"
else
$EDITOR "$mainFile" "+$matchLine"
fi
changedClasses+="$mainFile "
done
completed=$((completed+1))
echo -e -n "\r$ext done, $completed/$all. Press any key to continue "
read -e -r -n 1 _
done
if [[ -n "$changedClasses" ]]; then
ktlint -F $changedClasses
fi