Building Command-Line Tools in Swift: Part 2 โ€” Publishing a CLI Application

captionless image

In the first part of this series, we covered the basics of building a CLI application in Swift using the Swift Package Manager. We learned about creating commands, handling arguments, and outputting results to the terminal. Now, in this part, weโ€™ll explore how to take a more complex CLI application, such as AssetOrganizer, and prepare it for distribution and publishing, including adding GitHub Actions support for CI/CD workflows.

Key Topics in This Part:

  1. Refining the CLI App for Real-World Use

  2. Publishing the Application

Bonus. Integrating GitHub Actions for CI/CD

Asset Organizer

A command-line tool for analyzing and managing unused assets in iOS/macOS projects. This tool helps you identify and clean up unused assets in your Xcode projects, reducing app size and maintaining a cleaner codebase.

Features

  • ๐Ÿ” Analyze asset usage in your project
  • ๐Ÿ“Š Generate detailed reports in multiple formats (Markdown, JSON, Console)
  • ๐ŸŽจ Support for various asset types:
  • Images (.png, .jpg, .jpeg, .gif, .pdf, .svg)
  • Colors (.colorset)
  • Data Sets (.dataset)
  • ๐Ÿ—‘ Clean up unused assets
  • ๐Ÿ’ก Smart asset detection and usage analysis
  • ๐Ÿ›ก Safe deletion with confirmation prompts
  • ๐Ÿ“ Detailed usage reporting showing where assets are used
  • ๐Ÿ”„ Sort assets by name, size, or usage count
  • โšก๏ธ Fast and efficient file scanning
  • ๐ŸŽฏ Filter assets by type or minimum size

captionless image

You should access repository on: AssetOrganizer Github

Refining the CLI App for Real-World Use

Adding Flexible Configuration

Allow users to customize their analysis by adding support for flags:

$ asset-organizer analyze \
 project-path MyXcodeProject \
 report-format json \
 sort-by usage \
 min-size 500

Using Swift Argument Parser can simplify the implementation.

Publishing the Application

GitHub Releases Tagging a release in your repository allows users to download and install your tool:

  1. Create a new tag for the release:
git tag -a v1.0.0 -m "Release 1.0.0"
git push origin v1.0.0
  1. Draft a release in the GitHub โ€œReleasesโ€ section.

Attach prebuilt binaries for macOS/Linux to simplify user installation.

Bonus: Integrating AssetOrganizer to your GitHub Actions Workflow

You can use AssetOrganizer as part of your CI workflows:

- name: AssetOrganizer
 uses: mkemalgokce/AssetOrganizer-action@1.0.0
 with:
 project-path: RookBook # Default is '.'

This generates results that appear directly in the GitHub Actions summary.

Github actions summary

Wrapping Up

In Part 1, we explored the fundamentals of building a CLI application in Swift. We learned how to create commands, handle arguments, and produce output, laying a solid foundation for any command-line tool. By mastering these basics, we established a workflow for quickly developing functional and user-friendly CLI tools.

In Part 2, we delved into publishing a CLI application and used AssetOrganizer as an example. We discussed refining the application for real-world usage, publishing it via GitHub Releases, and as a bonus, demonstrated how to integrate AssetOrganizer into a GitHub Actions workflow to automate its use within CI/CD pipelines.

By combining these techniques, you now have the knowledge to build, refine, and distribute CLI tools effectively. AssetOrganizer serves as an example of how powerful and accessible Swift-based CLI applications can be when designed with care.

Happy coding!