Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R.swift support and project refreshment #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions AbandonedStrings.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 0720;
LastUpgradeCheck = 1210;
ORGANIZATIONNAME = iJoshSmith;
TargetAttributes = {
0361F13B1C605FE0009D519A = {
Expand All @@ -100,10 +100,11 @@
};
buildConfigurationList = 0361F1371C605FE0009D519A /* Build configuration list for PBXProject "AbandonedStrings" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = 0361F1331C605FE0009D519A;
productRefGroup = 0361F13D1C605FE0009D519A /* Products */;
Expand Down Expand Up @@ -131,17 +132,29 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand Down Expand Up @@ -175,17 +188,29 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
CODE_SIGN_IDENTITY = "-";
Expand All @@ -204,22 +229,25 @@
MACOSX_DEPLOYMENT_TARGET = 10.11;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_COMPILATION_MODE = wholemodule;
};
name = Release;
};
0361F1441C605FE0009D519A /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
0361F1451C605FE0009D519A /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
38 changes: 29 additions & 9 deletions AbandonedStrings/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func contentsOfFile(_ filePath: String) -> String {
return try String(contentsOfFile: filePath)
}
catch {
print("cannot read file!!!")
print(error.localizedDescription)
exit(1)
}
}
Expand All @@ -52,7 +52,10 @@ func concatenateAllSourceCodeIn(_ directories: [String], withStoryboard: Bool) -
if withStoryboard {
extensions.append("storyboard")
}
let sourceFiles = findFilesIn(directories, withExtensions: extensions)

var sourceFiles = findFilesIn(directories, withExtensions: extensions)
sourceFiles.removeAll { $0.contains("R.generated.swift") }

return sourceFiles.reduce("") { (accumulator, sourceFile) -> String in
return accumulator + contentsOfFile(sourceFile)
}
Expand All @@ -73,20 +76,37 @@ func extractStringIdentifiersFrom(_ stringsFile: String) -> [String] {
func extractStringIdentifierFromTrimmedLine(_ line: String) -> String {
let indexAfterFirstQuote = line.index(after: line.startIndex)
let lineWithoutFirstQuote = line[indexAfterFirstQuote...]
let endIndex = lineWithoutFirstQuote.index(of:"\"")!
let endIndex = lineWithoutFirstQuote.firstIndex(of:"\"")!
let identifier = lineWithoutFirstQuote[..<endIndex]
return String(identifier)
}

// MARK: - Abandoned identifier detection

func makeRSwiftIdentifier(from identified: String) -> String {
let components = identified.components(separatedBy: ".")
let reducedComponents = components.reduce("") {
if $0.isEmpty {
return $1
} else {
return $0 + $1.prefix(1).uppercased() + $1.dropFirst()
}
}

return "R.string.localizable." + reducedComponents
}

func findStringIdentifiersIn(_ stringsFile: String, abandonedBySourceCode sourceCode: String) -> [String] {
return extractStringIdentifiersFrom(stringsFile).filter { identifier in
let quotedIdentifier = "\"\(identifier)\""
let quotedIdentifierForStoryboard = "\"@\(identifier)\""
let signalQuotedIdentifierForJs = "'\(identifier)'"
let isAbandoned = (sourceCode.contains(quotedIdentifier) == false && sourceCode.contains(quotedIdentifierForStoryboard) == false &&
sourceCode.contains(signalQuotedIdentifierForJs) == false)
let rSwiftIdentifier = makeRSwiftIdentifier(from: identifier)

let isAbandoned = sourceCode.contains(quotedIdentifier) == false && sourceCode.contains(quotedIdentifierForStoryboard) == false &&
sourceCode.contains(signalQuotedIdentifierForJs) == false &&
sourceCode.contains(rSwiftIdentifier) == false

return isAbandoned
}
}
Expand Down Expand Up @@ -138,8 +158,8 @@ func getRootDirectories() -> [String]? {
if isOptionalParameterForStoryboardAvailable() {
c.removeLast()
}
if isOptionaParameterForWritingAvailable() {
c.remove(at: c.index(of: "write")!)
if isOptionalParameterForWritingAvailable() {
c.remove(at: c.firstIndex(of: "write")!)
}
return c
}
Expand All @@ -148,7 +168,7 @@ func isOptionalParameterForStoryboardAvailable() -> Bool {
return CommandLine.arguments.last == "storyboard"
}

func isOptionaParameterForWritingAvailable() -> Bool {
func isOptionalParameterForWritingAvailable() -> Bool {
return CommandLine.arguments.contains("write")
}

Expand All @@ -173,7 +193,7 @@ if let rootDirectories = getRootDirectories() {
print("Abandoned resource strings were detected:")
displayAbandonedIdentifiersInMap(map)

if isOptionaParameterForWritingAvailable() {
if isOptionalParameterForWritingAvailable() {
map.keys.forEach { (stringsFilePath) in
print("\n\nNow modifying \(stringsFilePath) ...")
let updatedStringsFileContent = stringsFile(stringsFilePath, without: map[stringsFilePath]!)
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Abandoned Resource String Detection
This command line program detects unused resource strings in an iOS or OS X application.

Updated to Swift 3, thanks to @astaeck on Oct-17-2016

## Usage
Open a Terminal to the directory which contains the *AbandonedStrings* executable, and run the following command:

Expand Down