- Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathPackageCollectionDiff.swift
79 lines (64 loc) · 3.08 KB
/
PackageCollectionDiff.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Package Collection Generator open source project
//
// Copyright (c) 2020-2023 Apple Inc. and the Swift Package Collection Generator project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift Package Collection Generator project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import ArgumentParser
import Foundation
import Backtrace
import Basics
import PackageCollectionsModel
import Utilities
@main
publicstructPackageCollectionDiff:ParsableCommand{
publicstaticletconfiguration=CommandConfiguration(
abstract:"Compare two package collections to determine if they are the same or different."
)
@Argument(help:"The path to the JSON document containing package collection #1")
privatevarcollectionOnePath:String
@Argument(help:"The path to the JSON document containing package collection #2")
privatevarcollectionTwoPath:String
@Flag(name:.shortAndLong, help:"Show extra logging for debugging purposes.")
privatevarverbose:Bool=false
typealiasModel=PackageCollectionModel.V1
publicinit(){}
publicfunc run()throws{
Backtrace.install()
print("Comparing collections located at \(self.collectionOnePath) and \(self.collectionTwoPath)", inColor:.cyan, verbose:self.verbose)
letjsonDecoder=JSONDecoder.makeWithDefaults()
letcollectionOne=tryself.parsePackageCollection(at:self.collectionOnePath, using: jsonDecoder)
letcollectionTwo=tryself.parsePackageCollection(at:self.collectionTwoPath, using: jsonDecoder)
ifself.collectionsAreEqual(collectionOne, collectionTwo){
returnprint("The package collections are the same.", inColor:.green, verbose:true)
}else{
returnprint("The package collections are different.", inColor:.red, verbose:true)
}
}
privatefunc parsePackageCollection(at path:String, using jsonDecoder:JSONDecoder)throws->Model.Collection{
do{
returntry jsonDecoder.decode(Model.Collection.self, from:Data(contentsOf:URL(fileURLWithPath: path)))
}catch{
printError("Failed to parse package collection: \(error)")
throw error
}
}
privatefunc collectionsAreEqual(_ lhs:Model.Collection, _ rhs:Model.Collection)->Bool{
guard lhs.name == rhs.name else{returnfalse}
guard lhs.overview == rhs.overview else{returnfalse}
guard lhs.keywords == rhs.keywords else{returnfalse}
guard lhs.packages == rhs.packages else{returnfalse}
guard lhs.formatVersion == rhs.formatVersion else{returnfalse}
guard lhs.revision == rhs.revision else{returnfalse}
// Don't compare generatedAt
guard lhs.generatedBy == rhs.generatedBy else{returnfalse}
returntrue
}
}