Skip to content

Commit

Permalink
Merged PR 2561: Update unused asset script
Browse files Browse the repository at this point in the history
* More docs
* Test cases
* Argment parser so it's clear what each parameter is required

```
USAGE: run [--library-name <library-name>] [--asset-catalog-name <asset-catalog-name>] --path-to-source-code <path-to-source-code> --path-to-fluent-icon-source <path-to-fluent-icon-source> [--path-to-list-of-icons-to-keep <path-to-list-of-icons-to-keep>]

OPTIONS:
  --library-name <library-name>
                          Name of the icon library. (default: FluentIcons)
  --asset-catalog-name <asset-catalog-name>
                          Name of the asset catalog. (default: IconAssets)
  --path-to-source-code <path-to-source-code>
                          Path to your app's source code.
  --path-to-fluent-icon-source <path-to-fluent-icon-source>
                          Path to the fluent icon library's source code
  --path-to-list-of-icons-to-keep <path-to-list-of-icons-to-keep>
                          Path to a custom list of icons in use (for React
                          Native, Optional).
  -h, --help              Show help information.
```
  • Loading branch information
Nick Romano committed Apr 29, 2020
1 parent b69ea1d commit 2013b68
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ tmp
dev_tests/automatic/screenshots
importer/dist/*
!importer/dist/.keep
tests
*.DS_Store

.project
Expand All @@ -18,3 +17,4 @@ tests

android/library/src/main/res/drawable/
xcuserdata
/ios/FluentIcons/Tests
1 change: 1 addition & 0 deletions ios/.swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--indent 2
2 changes: 1 addition & 1 deletion ios/FluentIcons.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ FluentIcons
# so we need to stick to `resources` here instead.
s.resources = [
'ios/FluentIcons/Assets/IconAssets.xcassets',
'ios/FluentIcons/remove_unused_fluent_icons.swift'
'ios/remove-unused-fluent-icons/remove-unused-fluent-icons'
]

# s.public_header_files = 'Pod/Classes/**/*.h'
Expand Down
167 changes: 0 additions & 167 deletions ios/FluentIcons/remove_unused_fluent_icons.swift

This file was deleted.

24 changes: 16 additions & 8 deletions ios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Cocoapods

```ruby
use_frameworks!
pod "FluentIcons", git: "https://[email protected]/microsoftdesign/Design%20System/_git/fluent-mobile-icons", tag: "1.0.215"
```

Expand Down Expand Up @@ -42,25 +43,32 @@ No more risky stringly typed `UIImage(named: "")!`
At build/release time you can run the following script to ensure all unused assets are stripped from the app:

Cocoapods

```
ICON_SOURCE_PATH="Pods/FluentIcons"
ICON_SOURCE_PATH="./Pods/FluentIcons"
swift $ICON_SOURCE_PATH/ios/FluentIcons/remove_unused_fluent_icons.swift \
MyProjectCode \
$ICON_SOURCE_PATH
$ICON_SOURCE_PATH/ios/remove-unused-fluent-icons/run \
--path-to-source-code "." \
--path-to-fluent-icon-source $ICON_SOURCE_PATH
```

Carthage

```
ICON_SOURCE_PATH="Carthage/Checkouts/fluent-mobile-icons"
ICON_SOURCE_PATH="./Carthage/Checkouts/fluent-mobile-icons"
swift $ICON_SOURCE_PATH/ios/FluentIcons/remove_unused_fluent_icons.swift \
MyProjectCode \
$ICON_SOURCE_PATH
$ICON_SOURCE_PATH/ios/remove-unused-fluent-icons/run \
--path-to-source-code "." \
--path-to-fluent-icon-source $ICON_SOURCE_PATH
carthage build --platform iOS fluent-mobile-icons
```

Optionally if you are using React Native or are referencing icons outside of your codebase, you can pass an icon list to prevent these icons from being removed.
```
--path-to-list-of-icons-to-keep OutlookReactNativeKit/ReactResources/FluentIcons.txt
```

#### 3. Consistent asset rendering

All non-color icons are rendered as template images so you can easily apply a `tintColor` to your `UIImageView` or `UIButton`. You no longer need to specify `.withRenderingMode(.alwaysTemplate)` in case you're unsure the asset was misconfigured.
Expand Down
5 changes: 5 additions & 0 deletions ios/remove-unused-fluent-icons/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions ios/remove-unused-fluent-icons/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "9f04d1ff1afbccd02279338a2c91e5f27c45e93a",
"version": "0.0.5"
}
}
]
},
"version": 1
}
34 changes: 34 additions & 0 deletions ios/remove-unused-fluent-icons/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "remove-unused-fluent-icons",
platforms: [
.macOS(.v10_13),
],
products: [
.executable(name: "remove-unused-fluent-icons", targets: ["remove-unused-fluent-icons"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.0.5"),
],
targets: [
.target(
name: "RemoveUnusedIcons",
dependencies: []
),
.testTarget(
name: "RemoveUnusedIconsTests",
dependencies: ["RemoveUnusedIcons"]
),
.target(
name: "remove-unused-fluent-icons",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
"RemoveUnusedIcons",
]
),
]
)
20 changes: 20 additions & 0 deletions ios/remove-unused-fluent-icons/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Remove Unused Fluent Icons

Source code for the script that removes any unused assets in a project.

## Contributing

### Generate a project

```
swift package generate-xcodeproj
```

### Releasing a new version

Build the binary and commit it to the repo

```
swift build -c release
yes | cp .build/release/remove-unused-fluent-icons run
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//

import Foundation

private func shell(_ command: String) -> String {
let task = Process()
task.launchPath = "/bin/bash"
task.arguments = ["-c", command]

let pipe = Pipe()
task.standardOutput = pipe
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
return String(data: data, encoding: .utf8)!
}

extension String {
func mapToLines() -> [String] {
trimmingCharacters(in: .whitespacesAndNewlines)
.split(separator: "\n")
.map { String($0) }
}
}

enum Language {
case swift
case objc
}

func searchForCodeReferences(in path: String, language: Language, weights: Set<String>, excludingFileName: String) -> [String] {
let include: String
let regex: String
switch language {
case .swift:
include = "--include=\"*.swift\""
regex = "\\.[a-zA-Z0-9]+[0-9]{2}(\(weights.map { $0.capitalized }.joined(separator: "|")))"
case .objc:
include = "--include=\"*.m\" --include=\"*.h\""
regex = "\(excludingFileName)[a-zA-Z0-9]+[0-9]{2}(\(weights.map { $0.uppercased() }.joined(separator: "|")))"
}
let command = """
grep --recursive \
--ignore-case \
--no-filename \
--exclude=\"\(excludingFileName).swift\" \
--extended-regexp \
\(include) \
\"\(regex)\" \(path)
"""
print(command)

return shell(command).mapToLines()
}
Loading

0 comments on commit 2013b68

Please sign in to comment.