Skip to content

Commit 1fe8b05

Browse files
authored
feature: Make changes to package.json for upgrade (RDMR-720, RDMR-721) (#13)
1 parent 04bf232 commit 1fe8b05

File tree

8 files changed

+201
-16
lines changed

8 files changed

+201
-16
lines changed

Package.resolved

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let package = Package(
99
dependencies: [
1010
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "1.5.1"),
1111
.package(url: "https://github.com/apple/swift-syntax.git", from: "601.0.1"),
12+
.package(url: "https://github.com/SwiftyJSON/SwiftyJSON.git", from: "5.0.0"),
1213
],
1314
targets: [
1415
.executableTarget(
@@ -36,14 +37,18 @@ let package = Package(
3637
.target(name: "JavascriptPackageTools")
3738
]
3839
),
39-
.target(name: "JavascriptPackageTools"),
40+
.target(name: "JavascriptPackageTools",
41+
dependencies: [
42+
.product(name: "SwiftyJSON", package: "SwiftyJSON")
43+
]
44+
),
4045

4146
// Test Targets
4247
.testTarget(name: "JavascriptPackageToolsTests",
4348
dependencies: [
4449
.target(name: "JavascriptPackageTools"),
4550
],
46-
resources: [.copy("package-test.json")]
51+
resources: [.copy("../Resources/package-new.json")]
4752
),
4853
.testTarget(name: "CapacitorPluginSyntaxToolsTests",
4954
dependencies: [

Sources/CapacitorPluginTools/CapacitorPluginPackage.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import Foundation
22
import CapacitorPluginSyntaxTools
33
import JavascriptPackageTools
44

5-
enum CapacitorPluginError: Error {
5+
public enum CapacitorPluginError: Error {
66
case objcFileCount(Int)
77
case objcHeaderCount(Int)
88
case oldPluginMissing
99

10-
var message: String {
10+
public var message: String {
1111
switch self {
1212
case .objcFileCount(let numberOfFiles):
1313
return "Found \(numberOfFiles) Objective-C *.m files, expected \(numberOfFiles)"
@@ -73,8 +73,8 @@ public class CapacitorPluginPackage {
7373
return url
7474
}
7575

76-
public func findSwiftPluginFile() throws -> URL {
77-
guard let oldPlugin else { throw CapacitorPluginError.oldPluginMissing }
76+
public func findSwiftPluginFile() throws(CapacitorPluginError) -> URL {
77+
guard let oldPlugin else { throw .oldPluginMissing }
7878

7979
let fileName = "\(oldPlugin.capacitorPlugin.identifier).swift"
8080

@@ -86,4 +86,24 @@ public class CapacitorPluginPackage {
8686

8787
return URL(filePath: fileName, directoryHint: .notDirectory, relativeTo: basePathURL)
8888
}
89+
90+
public func updatePackageJSON(for podName: String) throws {
91+
try? packageJSONParser.changeScript(named: "verify:ios",
92+
to: "xcodebuild -scheme \(podName) -destination generic/platform=iOS")
93+
94+
var newFiles = packageJSONParser.files
95+
96+
newFiles.removeAll(where: { $0 == "ios/Plugin" || $0 == "ios/Plugin/" })
97+
98+
if newFiles.contains(where: { $0 != "ios/"}) {
99+
newFiles.append("ios/Sources")
100+
newFiles.append("ios/Tests")
101+
}
102+
103+
newFiles.append("Package.swift")
104+
105+
packageJSONParser.files = newFiles
106+
107+
try packageJSONParser.writePackageJSON()
108+
}
89109
}

Sources/CommandLineTool/cap2spm.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ struct Cap2SPM: ParsableCommand {
5858
try deleteFiles(at: unneededFiles, shouldBackup: shouldBackup)
5959

6060
try modifyGitignores(for: capacitorPluginPackage)
61+
6162
try moveSourceDirectories(for: capacitorPluginPackage)
63+
64+
try capacitorPluginPackage.updatePackageJSON(for: podspec.podName)
6265
}
6366
}

Sources/JavascriptPackageTools/PackageJSONParser.swift

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,34 @@
11
import Foundation
2+
import SwiftyJSON
23

3-
enum PackageJSONError: Error {
4+
public enum PackageJSONError: Error {
45
case noPodspec
6+
case scriptEntryNotFound
7+
case fileEntryNotFound
8+
case jsonStringGenerationFailed
59
}
610

711
public struct PackageJSONParser: CustomDebugStringConvertible {
812
private let package: PackageJSON
9-
13+
private var json: JSON
14+
private let jsonURL: URL
15+
1016
public var npmName: String {
1117
package.name
1218
}
1319

1420
public var version: String {
1521
package.version
1622
}
23+
24+
public var files: [String] {
25+
get {
26+
package.files
27+
}
28+
set {
29+
json["files"] = JSON(newValue)
30+
}
31+
}
1732

1833
public var podspec: String = ""
1934

@@ -33,16 +48,36 @@ public struct PackageJSONParser: CustomDebugStringConvertible {
3348
return plugins
3449
}
3550

36-
public var scripts: [String: String] {
37-
package.scripts
51+
public var jsonString: String? {
52+
json.rawString(.utf8, options: [.withoutEscapingSlashes, .prettyPrinted])
3853
}
3954

4055
public init(with url: URL) throws {
56+
jsonURL = url
57+
4158
let data = try Data(contentsOf: url)
59+
json = try JSON(data: data)
60+
4261
package = try JSONDecoder().decode(PackageJSON.self, from: data)
4362
podspec = try findPodspec()
4463
}
4564

65+
public mutating func changeScript(named: String, to runString: String) throws(PackageJSONError) {
66+
if json["scripts"][named] != JSON.null {
67+
json["scripts"][named] = JSON(runString)
68+
} else {
69+
throw .scriptEntryNotFound
70+
}
71+
}
72+
73+
public func writePackageJSON() throws {
74+
guard let data = jsonString?.data(using: .utf8) else {
75+
throw PackageJSONError.jsonStringGenerationFailed
76+
}
77+
78+
try data.write(to: jsonURL)
79+
}
80+
4681
public var debugDescription: String {
4782
"""
4883
NPM Name: \(npmName)
@@ -53,12 +88,12 @@ public struct PackageJSONParser: CustomDebugStringConvertible {
5388
"""
5489
}
5590

56-
private func findPodspec() throws -> String {
91+
private func findPodspec() throws(PackageJSONError) -> String {
5792
for file in package.files {
5893
if file.hasSuffix("podspec") {
5994
return file
6095
}
6196
}
62-
throw PackageJSONError.noPodspec
97+
throw .noPodspec
6398
}
6499
}

Tests/JavascriptPackageToolsTests/PackageJSONParserTests.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,33 @@ import Foundation
33
import JavascriptPackageTools
44

55
struct PackageJSONParserTests {
6-
var packageJSONParser: PackageJSONParser
6+
let packageJSONParser: PackageJSONParser
77

88
init() throws {
9-
let testJSON = try #require(Bundle.module.url(forResource: "package-test", withExtension: "json"))
9+
let testJSON = try #require(Bundle.module.url(forResource: "package-new", withExtension: "json"))
1010
packageJSONParser = try PackageJSONParser(with: testJSON)
1111
}
1212

1313
@Test("Correctly finds the podspec")
1414
func findsPodSpec() async throws {
1515
#expect(packageJSONParser.podspec == "Typical.podspec")
1616
}
17-
17+
18+
@Test("Can change scripts")
19+
func canChangeScript() async throws {
20+
var parser = packageJSONParser
21+
let oldString = try #require(parser.jsonString)
22+
try parser.changeScript(named: "verify:ios", to: "new-test")
23+
let calculatedSting = try #require(parser.jsonString)
24+
#expect(calculatedSting != oldString)
25+
}
26+
27+
@Test("Can change files")
28+
func canSetFiles() async throws {
29+
var parser = packageJSONParser
30+
let oldString = try #require(parser.jsonString)
31+
parser.files = ["new", "list", "files"]
32+
let calculatedSting = try #require(parser.jsonString)
33+
#expect(calculatedSting != oldString)
34+
}
1835
}
File renamed without changes.

Tests/Resources/package-old.json

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"name": "@capacitor/google-maps",
3+
"version": "7.0.2",
4+
"description": "Google maps on Capacitor",
5+
"main": "dist/plugin.cjs.js",
6+
"module": "dist/esm/index.js",
7+
"typings": "dist/typings/index.d.ts",
8+
"typesVersions": {
9+
"<4.1": {
10+
"dist/typings/index.d.ts": [
11+
"dist/typings/ts_old/index.d.ts"
12+
]
13+
}
14+
},
15+
"unpkg": "dist/plugin.js",
16+
"files": [
17+
"android/src/main/",
18+
"android/build.gradle",
19+
"dist/",
20+
"ios/Plugin/",
21+
"CapacitorGoogleMaps.podspec"
22+
],
23+
"author": "Ionic <[email protected]>",
24+
"license": "MIT",
25+
"repository": {
26+
"type": "git",
27+
"url": "git+https://github.com/ionic-team/capacitor-google-maps.git"
28+
},
29+
"bugs": {
30+
"url": "https://github.com/ionic-team/capacitor-google-maps/issues"
31+
},
32+
"keywords": [
33+
"capacitor",
34+
"plugin",
35+
"native",
36+
"google-maps"
37+
],
38+
"scripts": {
39+
"verify": "pnpm run verify:ios && pnpm run verify:android && pnpm run verify:web",
40+
"verify:ios": "cd ios && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -sdk iphonesimulator && cd ..",
41+
"verify:android": "cd android && ./gradlew clean build test && cd ..",
42+
"verify:web": "pnpm run build",
43+
"lint": "pnpm eslint . --ext ts && pnpm prettier \"./**/*.{css,html,ts,js,java}\" --check && pnpm node-swiftlint lint",
44+
"fmt": "pnpm eslint . --ext ts --fix && pnpm prettier \"./**/*.{css,html,ts,js,java}\" --write && pnpm node-swiftlint --fix --format",
45+
"docgen": "docgen --api GoogleMapInterface --output-readme README.md --output-json dist/docs.json",
46+
"build": "pnpm run clean && pnpm run docgen && tsc && rollup -c rollup.config.js && pnpm run downleveldts",
47+
"clean": "rimraf ./dist",
48+
"watch": "tsc --watch",
49+
"prepublishOnly": "pnpm run build",
50+
"publish:cocoapod": "pod trunk push ./CapacitorGoogleMaps.podspec --allow-warnings",
51+
"downleveldts": "pnpm downlevel-dts dist/typings dist/typings/ts_old --to=3.5",
52+
"pack-local": "pnpm run build && pnpm pack && find . -name 'capacitor-google-maps-*tgz' -exec bash -c 'mv $0 capacitor-google-maps.tgz' {} \\; ",
53+
"unittest:ios": "xcodebuild test -project ./unit-tests/ios/GoogleMapsPlugin/GoogleMapsPlugin.xcodeproj -scheme TestApp -destination 'platform=iOS Simulator,name=iPhone 12,OS=15.2' | xcpretty && exit ${PIPESTATUS[0]}",
54+
"unittest:android": "cd ./unit-tests/android && ./gradlew testDebugUnitTest"
55+
},
56+
"devDependencies": {
57+
"@capacitor/android": "next",
58+
"@capacitor/core": "next",
59+
"@capacitor/docgen": "0.3.0",
60+
"@capacitor/ios": "next",
61+
"@ionic/prettier-config": "^1.0.1",
62+
"@types/resize-observer-browser": "^0.1.7",
63+
"@types/supercluster": "^7.1.0",
64+
"@typescript-eslint/eslint-plugin": "^5.59.2",
65+
"@typescript-eslint/parser": "^5.59.2",
66+
"downlevel-dts": "^0.7.0",
67+
"eslint": "^8.57.0",
68+
"eslint-config-prettier": "^8.8.0",
69+
"eslint-plugin-import": "^2.25.4",
70+
"prettier": "^2.8.8",
71+
"prettier-plugin-java": "~2.1.0",
72+
"rimraf": "^3.0.2",
73+
"rollup": "^2.78.1",
74+
"swiftlint": "^1.0.2",
75+
"typescript": "^5.4.2"
76+
},
77+
"peerDependencies": {
78+
"@capacitor/core": ">=7.0.0"
79+
},
80+
"capacitor": {
81+
"ios": {
82+
"src": "ios"
83+
},
84+
"android": {
85+
"src": "android"
86+
}
87+
},
88+
"publishConfig": {
89+
"access": "public"
90+
},
91+
"dependencies": {
92+
"@googlemaps/js-api-loader": "~1.16.8",
93+
"@googlemaps/markerclusterer": "~2.5.3",
94+
"@types/google.maps": "~3.58.1"
95+
}
96+
}

0 commit comments

Comments
 (0)