मैंने अप्रयुक्त छवियों की पहचान करने के लिए एक पायथन स्क्रिप्ट बनाई है: 'unused_assets.py' @ gist । इसका उपयोग इस तरह किया जा सकता है:
python3 unused_assets.py '/Users/DevK/MyProject' '/Users/DevK/MyProject/MyProject/Assets/Assets.xcassets'
स्क्रिप्ट का उपयोग करने के लिए यहां कुछ नियम दिए गए हैं:
- पहले तर्क के रूप में प्रोजेक्ट फ़ोल्डर पथ को पास करना महत्वपूर्ण है, दूसरे तर्क के रूप में संपत्ति फ़ोल्डर पथ
- यह माना जाता है कि सभी चित्र Assets.xcassets फ़ोल्डर के भीतर बनाए हुए हैं और इनका उपयोग स्विफ्ट फ़ाइलों के भीतर या स्टोरीबोर्ड के भीतर किया जाता है
पहले संस्करण में सीमाएं:
- उद्देश्य c फ़ाइलों के लिए काम नहीं करता है
मैं प्रतिक्रिया के आधार पर समय के साथ इसे बेहतर बनाने की कोशिश करूंगा, हालांकि पहला संस्करण अधिकांश के लिए अच्छा होना चाहिए।
कृपया कोड के नीचे खोजें। कोड स्व व्याख्यात्मक होना चाहिए क्योंकि मैंने प्रत्येक महत्वपूर्ण कदम के लिए उपयुक्त टिप्पणियां जोड़ी हैं ।
# Usage e.g.: python3 unused_assets.py '/Users/DevK/MyProject' '/Users/DevK/MyProject/MyProject/Assets/Assets.xcassets'
# It is important to pass project folder path as first argument, assets folder path as second argument
# It is assumed that all the images are maintained within Assets.xcassets folder and are used either within swift files or within storyboards
"""
@author = "Devarshi Kulshreshtha"
@copyright = "Copyright 2020, Devarshi Kulshreshtha"
@license = "GPL"
@version = "1.0.1"
@contact = "kulshreshtha.devarshi@gmail.com"
"""
import sys
import glob
from pathlib import Path
import mmap
import os
import time
# obtain start time
start = time.time()
arguments = sys.argv
# pass project folder path as argument 1
projectFolderPath = arguments[1].replace("\\", "") # replacing backslash with space
# pass assets folder path as argument 2
assetsPath = arguments[2].replace("\\", "") # replacing backslash with space
print(f"assetsPath: {assetsPath}")
print(f"projectFolderPath: {projectFolderPath}")
# obtain all assets / images
# obtain paths for all assets
assetsSearchablePath = assetsPath + '/**/*.imageset' #alternate way to append: fr"{assetsPath}/**/*.imageset"
print(f"assetsSearchablePath: {assetsSearchablePath}")
imagesNameCountDict = {} # empty dict to store image name as key and occurrence count
for imagesetPath in glob.glob(assetsSearchablePath, recursive=True):
# storing the image name as encoded so that we save some time later during string search in file
encodedImageName = str.encode(Path(imagesetPath).stem)
# initializing occurrence count as 0
imagesNameCountDict[encodedImageName] = 0
print("Names of all assets obtained")
# search images in swift files
# obtain paths for all swift files
swiftFilesSearchablePath = projectFolderPath + '/**/*.swift' #alternate way to append: fr"{projectFolderPath}/**/*.swift"
print(f"swiftFilesSearchablePath: {swiftFilesSearchablePath}")
for swiftFilePath in glob.glob(swiftFilesSearchablePath, recursive=True):
with open(swiftFilePath, 'rb', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
# search all the assests within the swift file
for encodedImageName in imagesNameCountDict:
# file search
if s.find(encodedImageName) != -1:
# updating occurrence count, if found
imagesNameCountDict[encodedImageName] += 1
print("Images searched in all swift files!")
# search images in storyboards
# obtain path for all storyboards
storyboardsSearchablePath = projectFolderPath + '/**/*.storyboard' #alternate way to append: fr"{projectFolderPath}/**/*.storyboard"
print(f"storyboardsSearchablePath: {storyboardsSearchablePath}")
for storyboardPath in glob.glob(storyboardsSearchablePath, recursive=True):
with open(storyboardPath, 'rb', 0) as file, \
mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ) as s:
# search all the assests within the storyboard file
for encodedImageName in imagesNameCountDict:
# file search
if s.find(encodedImageName) != -1:
# updating occurrence count, if found
imagesNameCountDict[encodedImageName] += 1
print("Images searched in all storyboard files!")
print("Here is the list of unused assets:")
# printing all image names, for which occurrence count is 0
print('\n'.join({encodedImageName.decode("utf-8", "strict") for encodedImageName, occurrenceCount in imagesNameCountDict.items() if occurrenceCount == 0}))
print(f"Done in {time.time() - start} seconds!")