Skip to content

Commit

Permalink
Merge pull request #6 from Ether-CLI/develop
Browse files Browse the repository at this point in the history
Created Manifest.data and .path Properties
  • Loading branch information
calebkleveter authored Apr 7, 2018
2 parents 53ab424 + bcdfde9 commit a47655d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## [0.3.0] 2018-04-07

### Added
- `Manifest.data` which acts as the manifest file instead of data fetched from a real file.
- `Manifest.path` for setting a custom path to the manifest file to interact with.

## [0.2.0] 2018-04-06

### Added
- `Manifest.reset` for setting the manifest file to it's initial state when the project was created.

## [0.1.0] 2018-03-28

### Added
- Pretty much everything. Well, not that much, but this release covers all the basic interactions of the manifest file.
42 changes: 36 additions & 6 deletions Sources/Manifest/Manifest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,51 @@ public class Manifest {
public static var environment: Environment = .commandline

/// Gets the current project's manifest.
public static let current = Manifest()
public static var current: Manifest {
if Manifest.environment == .commandline {
return Manifest(path: "file:\(FileManager.default.currentDirectoryPath)/Package.swift")
} else {
return Manifest(data: Data(testManifest.utf8))
}
}

/// A custom path to the manifest.
/// This property gets set to `./Package.swift`
/// when the static` .current` property is used.
public let path: String?

/// Data that acts as the manifest file.
public private(set) var data: Data?

internal let fileManager = FileManager.default

private init() {}
///
public init(path: String) {
self.path = path
}

///
public init(data: Data) {
self.data = data
self.path = nil
}

/// Gets the contents of the project's manifest as `Data`.
///
/// - Returns: The manifest's contents.
/// - Throws: Errors that occur if there is a badly formed URL or the manifest is not found.
public func contents()throws -> Data {
if Manifest.environment == .testing { return testManifest.data(using: .utf8)! }
if let data = self.data {
return data
}

guard let resolvedURL = URL(string: "file:\(fileManager.currentDirectoryPath)/Package.swift") else {
guard let resolvedURL = URL(string: self.path!) else {
throw ManifestError(identifier: "badURL", reason: "Unable to create URL for package manifest file.")
}
if !fileManager.fileExists(atPath: "\(fileManager.currentDirectoryPath)/Package.swift") {
if !fileManager.fileExists(atPath: self.path!) {
throw ManifestError(identifier: "badManifestPath", reason: "Bad path to package manifest. Make sure you are in the project root.")
}

return try Data(contentsOf: resolvedURL)
}

Expand Down Expand Up @@ -56,11 +80,17 @@ public class Manifest {
/// - Parameter string: The data to rewrite the manifest with.
/// - Throws: `ManifestError.badURL` if the URL to the manifest cannot be created.
public func write(with string: String)throws {
if self.data != nil {
self.data = Data(string.utf8)
return
}

if Manifest.environment == .testing { testManifest = string; return }

guard let manifestURL = URL(string: "file:\(fileManager.currentDirectoryPath)/Package.swift") else {
guard let manifestURL = URL(string: self.path!) else {
throw ManifestError(identifier: "badURL", reason: "Unable to create URL for package manifest file.")
}

try string.data(using: .utf8)?.write(to: manifestURL)
}

Expand Down

0 comments on commit a47655d

Please sign in to comment.