Swiftpack.co - Package - nicholascross/Props

Props

A framework for finding, reading and updating properties for use in scripts and simple command line tools

Why

Continually retyping parameters for your scripts that do not change between executions is causing unnecessary wear and tear on your keyboard and finger joints.

Features

Properties defined in a simple format

aProperty = important
another_property = /path/to/stuff
  • Each property is seperated into property name and value by the =
  • Any white space is trimmed from the property name and value

Properties accessible in a simple format

do {
  guard let properties = try properties(named: ".myprops") else {
    //no properties found
    return [:]
  }
  
  let propertyValue = properties["aProperty"]
  print(propertyValue) //prints: important
}
catch {
  fatalError("properies file was found but was corrupt or otherwise unreadable")
}

Property files located automatically

  • If the property file is not in the current directory it will be located by traversing up the folder heirarchy
  • If the property file is still not found it can be located by looking in the users home directory

Property files updated through code

Save time by avoiding manually finding and editing property files. Add code to your script to update related properties in both local and home directory based property files

var properties = try localProperties(named: ".myprops") ?? [:]
properties["another_property"] = "/path/to/something"
let propertyFileThatWasUpdated = try updateLocal(properties: properties, named: ".myprops")

Installation

To include in a swift app via the swift package manager

Add the dependency to your Package.swift file

// swift-tools-version:3.1

import PackageDescription

let package = Package(
    name: "yourpackage",
    dependencies: [
         .Package(url: "https://github.com/nicholascross/Props.git", majorVersion: 1)
    ],
    swiftLanguageVersions: [3]
)

Run swift build

To include in a swift marathon script

Add the following import

import Props // marathon:https://github.com/nicholascross/Props.git

Read properties

//Load properties by searching up the folder heirarchy until the file is found; if no file is 
//found it will look in the users home directory; will throw an error if the format is invalid 
//& return nil if nothing is found
func properties(named name: String, inFolder folder: Folder = Folder.current, maximumSearchDepth depth: Int = 50) throws -> [String: String]?

//Load properties by searching up the folder heirarchy until the file is found; will throw an error 
//if the format is invalid & return nil if not found
func localProperties(named name: String) throws -> [String: String]?

//Load properties from the users home directory; will throw an error if the format is invalid & return 
//nil if not found
func userProperties(named name: String) throws -> [String: String]?

//Load properties from the specified file; will throw an error if the format is invalid
func properties(fromFile file: File) throws -> [String: String] 

Create & Update properties

//Write properties to the local properties files located by searching up the folder hierarchy from 
//the present working directory.  If not a properties file will be created in the present working directory
func updateLocal(properties:[String: String], named name: String) throws -> File

//Write properties to a file in user home directory
func updateUser(properties:[String: String], named name: String) throws -> File

//Write properties to file
func update(properties:[String: String], inFile file: File) throws

Github

link
Stars: 0
Help us keep the lights on

Dependencies

Used By

Total: 1