Swiftpack.co - sharplet/SwiftIO as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by sharplet.
sharplet/SwiftIO 0.2.0
Simple Tools for File I/O in Swift
⭐️ 11
🕓 3 years ago
.package(url: "https://github.com/sharplet/SwiftIO.git", from: "0.2.0")

SwiftIO – Simple Tools for File I/O

SwiftIO aims to provide a small set of useful file input & output capabilities. Rather than invent significant new paradigms, the goal is to provide the smallest abstraction necessary to make a nice Swift API, implemented using the C standard library (or Foundation where appropriate).

Another goal is portability: SwiftIO is distributed via source and Swift Package Manager, has no external dependencies, and supports Swift Package Manager's default lowest deployment target.

This is a work in progress, and contributions are welcome!

Installation

Drop this package declaration into your Package.swift file:

let package = Package(
  name: "MyPackage",
  dependencies: [
    .package(url: "https://github.com/sharplet/SwiftIO.git", from: "0.1.1"),
  ],
  targets: [
    .target(name: "MyTarget", dependencies: ["SwiftIO"]),
  ]
)

Usage

Add the following import statement to the top of your file to use SwiftIO:

import SwiftIO

FileOutputStream

The global variables standardOutput and standardError are provided for use with the print(_:to:) function:

print("Oh no!", to: &standardError)

Or use the errorPrint(_:) functionality, which fits in alongside the standard library's debugPrint(_:) function:

errorPrint("Oh no!")

Launch processes and shell out

Use the system function to run a command. If the exit status is non-zero, an error is raised.

do {
  try system("false")
} catch {
  print("Here we are again.")
}

Use the exec function to replace the currently running process:

let arguments = Array(CommandLine.arguments.dropFirst())
try exec("xcrun", arguments: ["swift", "run", "mycommand"] + arguments)

Both functions accept options. For example, by default the user's PATH will be searched for matching executables. Disable this behaviour like so:

// exec-absolute.swift
let command = CommandLine.arguments[1]
try exec(command, options: .requireAbsolutePath)

// exec-absolute foo
// => foo: No such file or directory

Exiting the process

The exit function is powered up with a nice enum of exit status codes, as defined in sysexits.h:

exit(.EX_USAGE) // user error
exit(.EX_SOFTWARE) // programmer error
exit(.failure) // something just went wrong

// these two are the same
exit(.success)
exit()

Working with file paths

SwiftIO avoids depending on Foundation's URL type to represent file paths, instead providing a lightweight Path struct that wraps a raw String.

// String literal support
var path: Path = "/tmp/foo.txt"

path.basename // "foo.txt"
path.dirname // "/tmp"

path.extension // "txt"
path.extension = "db" // "/tmp/foo.db"
path.components // ["/", "tmp", "foo.db"]

// Path concatenation
path.deleteExtension()
path += "bar" // "/tmp/foo/bar"

Prior Art

SwiftIO is fairly similar in role to swift-tools-support-core. It's more mature, and used throughout Swift tooling, so if it works for you then please use it! This library aims to be community-driven and provide unopinionated and minimal abstractions atop C.

License

SwiftIO is Copyright © 2020 Adam Sharp, and is distributed under the MIT License.

GitHub

link
Stars: 11
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Please Don't File a Complaint
3 years ago

Added:

  • New FileHandle struct that provides a unified interface to interacting with the file system.
  • New Directory namespace collects file operations on directories, including creating and removing directories, and inspecting and changing the current directory.
  • New FileInfo struct provides a Swifty interface to the stat(2) system call.

Changed:

  • ExitError now automatically generates a human-readable description based on the code.
  • Increased compatibility with Linux by removing any dependency on the Objective-C implementation of Foundation.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics