Swift Script that transforms the .xcassets folder into a type safe enum
Swift Ruby Shell
Latest commit 4fb6bbc Sep 23, 2016 @kaandedeoglu Simplify code.
Permalink
Failed to load latest commit information.
.gitignore Initial commit Jul 13, 2015
LICENSE Initial commit Jul 13, 2015
README.md Update README Sep 19, 2016
Shark.podspec update Podspec for 0.2.0 version (Swift 3.0) Sep 19, 2016
Shark.swift Simplify code. Sep 22, 2016
setup.sh Update setup.sh Aug 24, 2015

README.md

Shark

Swift Script that transforms the .xcassets folder into a type safe Enum.

The master branch of Shark is Swift 3.0 compatible. For Swift 2 support, please check out the Swift2 branch.

Blog Post & Tutorial

http://kaandedeoglu.com/2015/07/28/Shark/

Quick Setup:

Cocoapods:

  • Add pod 'Shark' to your Podfile
  • Go to your targets build phases and add a Run Script Phase and place it before the Compile Sources phase.
  • Shark takes two parameters - path to your image assets (folder with the .xcassets extension), and the path to the desired output folder. Fill the run script area by typing these out. Here's an example:

"$PODS_ROOT/Shark/shark" "${PROJECT_DIR}/${PROJECT_NAME}/Assets.xcassets" "${PROJECT_DIR}/${PROJECT_NAME}"

Manually

  • Place Shark.swift anywhere in your project directory - you don't have to add it to your project or your targets.
  • Go to your targets build phases and add a Run Script Phase and place it before the Compile Sources phase.
  • Shark takes two parameters - path to your image assets (folder with the .xcassets extension), and the path to the desired output folder. Fill the run script area by typing these out. Here's an example:

"${PROJECT_DIR}/path_to_Shark.swift} "${PROJECT_DIR}/${PROJECT_NAME}/Assets.xcassets" "${PROJECT_DIR}/${PROJECT_NAME}"

Building the executable yourself:

  • Clone the repo
  • Run the setup script from the terminal - sh setup.sh. This will compile the source, create an executable and move it to /usr/local/bin. After this, Shark is available from the command line as shark
  • Go to your targets build phases and add a Run Script Phase and place it before the Compile Sources phase
  • Shark takes two parameters - path to your image assets (folder with the .xcassets extension), and the path to the desired output folder. Fill the run script area by typing these out. Here's an example:

shark "${PROJECT_DIR}/${PROJECT_NAME}/Assets.xcassets" "${PROJECT_DIR}/${PROJECT_NAME}"

Finally

Whichever method you chose, the final steps are the same, and they are outlined below:

  • Build once - a file named SharkImages.swift should magically appear at your output destination.
  • Add the file to your project.
  • You're done! the file SharkImage.swift will be updated every time you build the project.

After the setup, we can use Shark to load images in two ways:

myImageView.image = UIImage(shark: Shark.EmptyIcons.programs_empty_icon)
myImageView.image = Shark.EmptyIcons.programs_empty_icon.image

Notes

  • Using nested folders makes working with Shark easier. This way you can do Shark.Buttons.Active.Login.fb_button.image rather than Shark.fb_button.image (which is harder to find when there are 100s of images in your assets)
  • Name your images with valid enum case names ( login_button is valid whereas login-button or login button are not), Swift 2 automatically sets case names to raw values in String backed Enums. If you name your images with valid names - Shark will generate something like case login_button, otherwise you'll see something like case "loginbutton = "login button"

Sample SharkImages.swift file:

//SharkImageNames.swift
//Generated by Shark

import UIKit

public protocol SharkImageConvertible {}

public extension SharkImageConvertible where Self: RawRepresentable, Self.RawValue == String {
    public var image: UIImage? {
        return UIImage(named: self.rawValue)
    }
}

public extension UIImage {
    convenience init?<T: RawRepresentable>(shark: T) where T.RawValue == String {
        self.init(named: shark.rawValue)
    }
}


public enum Shark {

    public enum SocialIcons: String, SharkImageConvertible {
            case facebook_share_checked = "facebook-share-checked"
            case facebook_share_unchecked = "facebook-share-unchecked"
            case facebook_icon
            case tumblr_share_checked = "tumblr-share-checked"
            case tumblr_share_unchecked = "tumblr-share-unchecked"
            case TumblrIcon
            case twitter_icon = "twitter-icon"
            case twitter_share_checked = "twitter-share-checked"
            case twitter_share_unchecked = "twitter-share-unchecked"
    }


    public enum SideMenuIcons: String, SharkImageConvertible {
            case side_menu_discover_icon = "side-menu-discover-icon"
            case side_menu_light = "side-menu-light"
            case side_menu_line = "side-menu-line"
            case side_menu_news_icon = "side-menu-news-icon"
            case side_menu_notifications_icon = "side-menu-notifications-icon"
            case side_menu_settings_icon = "side-menu-settings-icon"
            case side_menu_tvguide_icon = "side-menu-tvguide-icon"
            case side_menu_watchlist_icon = "side-menu-watchlist-icon"
    }


    public enum Other: String, SharkImageConvertible {
            case badge_locked
            case news_filter_confirm = "news-filter-confirm"
            case noluyo_logo
            case NoluyoTitleText
            case profile_placeholder
            case program_watching_eye
            case rating_star_checked = "rating-star-checked"
            case rating_star_unchecked = "rating-star-unchecked"
            case register_add_image
            case tv_guide_now_button = "tv-guide-now-button"
            case tv_guide_back_arrow
            case tv_guide_filter_delete_button
            case tv_guide_filter_down_arrow
            case tv_guide_forward_arrow
            case watchlist_down_arrow = "watchlist-down-arrow"
    }


    public enum EmptyIcons: String, SharkImageConvertible {
            case feed_empty_icon
            case news_empty_icon
            case programs_empty_icon
            case reminders_empty_icon
            case watch_list_empty_icon
    }


    public enum ApplicationIcons: String, SharkImageConvertible {
            case checkbox_icon = "checkbox-icon"
            case comment_count_icon = "comment-count-icon"
            case delete_account_icon
            case end_of_feed_icon = "end-of-feed-icon"
            case feed_view_like_icon_checked = "feed-view-like-icon-checked"
            case feed_view_like_icon_unchecked = "feed-view-like-icon-unchecked"
            case feed_view_post_follow_icon = "feed-view-post-follow-icon"
            case feed_view_post_link_icon = "feed-view-post-link-icon"
            case feed_view_post_watch_icon = "feed-view-post-watch-icon"
            case feed_view_share_icon = "feed-view-share-icon"
            case follow_icon_unchecked = "follow-icon-unchecked"
            case hamburger_menu_icon = "hamburger-menu-icon"
            case logout_icon
            case media_icon_checked = "media-icon-checked"
            case media_icon_unchecked = "media-icon-unchecked"
            case nav_back_icon = "nav-back-icon"
            case nav_forward_icon = "nav-forward-icon"
            case new_post_icon = "new-post-icon"
            case post_media_delete_icon = "post-media-delete-icon"
            case post_delete_icon
            case profile_icon_programs = "profile-icon-programs"
            case profile_reminders_delete_icon = "profile-reminders-delete-icon"
            case program_badge_icon
            case program_feed_icon
            case program_game_icon
            case program_schedule_icon
    }

}

To-Do

  • Add example project
  • Cocoapods Support

License

The MIT License (MIT)

Copyright (c) 2016 Kaan Dedeoglu

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.