Skip to content

Commit

Permalink
First working version
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiikhliustin committed Sep 4, 2022
1 parent b441f46 commit 3f0f121
Show file tree
Hide file tree
Showing 53 changed files with 6,038 additions and 169 deletions.
19 changes: 11 additions & 8 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

build \
--announce_rc \
--spawn_strategy=local \
--strategy=SwiftCompile=worker \
--verbose_failures \
--apple_platform_type=macos \
--compilation_mode=dbg \
--features=swift.use_global_module_cache
build --announce_rc
build --spawn_strategy=local
build --strategy=SwiftCompile=worker
build --verbose_failures
build --apple_platform_type=macos
build --compilation_mode=dbg
build --features=swift.use_global_module_cache
build --objc_enable_binary_stripping=true
build --features=dead_strip
build --experimental_multi_threaded_digest
build --disk_cache=./bazel-cache
46 changes: 9 additions & 37 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@build_bazel_rules_swift//swift:swift.bzl", "swift_library")
load("@build_bazel_rules_apple//apple:macos.bzl", "macos_command_line_application", "macos_unit_test")
load("@build_bazel_rules_apple//apple:macos.bzl", "macos_command_line_application")
load("@rules_cc//cc:defs.bzl", "objc_library")

objc_library(
Expand All @@ -12,65 +12,37 @@ objc_library(
# PodToBUILD is a core library enabling Starlark code generation
swift_library(
name = "PodToBUILD",
srcs = glob(["Sources/PodToBUILD/*.swift"]),
srcs = glob(["Sources/PodToBUILD/**/*.swift"]),
deps = [":ObjcSupport"],
copts = ["-swift-version", "5"],
visibility = ["//Tests:__pkg__"]
)

# Compiler
macos_command_line_application(
name = "Compiler",
minimum_os_version = "10.13",
minimum_os_version = "10.11",
deps = [":CompilerLib"],
)

swift_library(
name = "CompilerLib",
srcs = glob(["Sources/Compiler/*.swift"]),
deps = [":PodToBUILD", "@swift-argument-parser//:ArgumentParser"],
srcs = glob(["Sources/Compiler/**/*.swift"]),
deps = [":PodToBUILD", "@bazelpods-swift-argument-parser//:ArgumentParser"],
copts = ["-swift-version", "5"],
)

# Generator

macos_command_line_application(
name = "Generator",
minimum_os_version = "10.13",
minimum_os_version = "10.11",
deps = [":GeneratorLib"],
)

swift_library(
name = "GeneratorLib",
srcs = glob(["Sources/Generator/*.swift"]),
deps = [":PodToBUILD", "@swift-argument-parser//:ArgumentParser"],
srcs = glob(["Sources/Generator/**/*.swift"]),
deps = [":PodToBUILD", "@bazelpods-swift-argument-parser//:ArgumentParser"],
copts = ["-swift-version", "5"],
)

# This tests RepoToolsCore and Starlark logic
swift_library(
name = "PodToBUILDTestsLib",
srcs = glob(["Tests/PodToBUILDTests/*.swift"]),
deps = ["@podtobuild-SwiftCheck//:SwiftCheck"],
data = glob(["Examples/**/*.podspec.json"])
)

macos_unit_test(
name = "PodToBUILDTests",
deps = [":PodToBUILDTestsLib"],
minimum_os_version = "10.13",
)

swift_library(
name = "BuildTestsLib",
srcs = glob(["Tests/BuildTests/*.swift"]),
deps = ["@podtobuild-SwiftCheck//:SwiftCheck"],
data = glob(["Examples/**/*.podspec.json"])
)

# This tests RepoToolsCore and Starlark logic
macos_unit_test(
name = "BuildTests",
deps = [":BuildTestsLib"],
minimum_os_version = "10.13",
)

57 changes: 57 additions & 0 deletions Sources/Compiler/RootCommand.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// MainCommand.swift
// PodToBUILD
//
// Created by Sergey Khliustin on 26.08.2022.
//

import Foundation
import ArgumentParser
import PodToBUILD
import ObjcSupport

extension String: LocalizedError {
public var errorDescription: String? { return self }
}

struct RootCommand: ParsableCommand {
static var configuration = CommandConfiguration(commandName: "Compiler", abstract: "Compiles podspec.json to BUILD file")
@Argument(help: "podspec.json", completion: .file(extensions: ["json"]))
var podspecJson: String

@Option(name: .long, help: "Sources root")
var src: String?

@Option(name: .long, parsing: .upToNextOption, help: "Subspecs list")
var subspecs: [String] = []

@Option(name: .long, help: "Minimum iOS version if not listed in podspec")
var minIos: String = "13.0"

func run() throws {
_ = CrashReporter()
let jsonData = try NSData(contentsOfFile: podspecJson, options: []) as Data
let jsonFile = try JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as AnyObject

guard let jsonPodspec = jsonFile as? JSONDict else {
throw "Error parsing podspec at path \(podspecJson)"
}

let podSpec = try PodSpec(JSONPodspec: jsonPodspec)

let podSpecURL = NSURL(fileURLWithPath: podspecJson)
let assumedPodName = podSpecURL.lastPathComponent!.components(separatedBy: ".")[0]
let options = BasicBuildOptions(podName: assumedPodName,
subspecs: subspecs,
iosPlatform: minIos)

let result = PodBuildFile.with(podSpec: podSpec, buildOptions: options).compile()
print(result)
}

func absolutePath(_ path: String) -> String {
guard let src = src else { return path }
guard !path.starts(with: "/") else { return path }
return (src as NSString).appendingPathComponent(path)
}
}
2 changes: 2 additions & 0 deletions Sources/Compiler/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

RootCommand.main()
14 changes: 14 additions & 0 deletions Sources/Generator/PodConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// PodConfig.swift
// PodToBUILD
//
// Created by Sergey Khliustin on 25.08.2022.
//

import Foundation

struct PodConfig: Decodable {
let name: String
let podspec: String
let development: Bool
}
47 changes: 47 additions & 0 deletions Sources/Generator/PodSpecification.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// PodSpecification.swift
// PodToBUILD
//
// Created by Sergey Khliustin on 25.08.2022.
//

import Foundation
import PodToBUILD

struct PodSpecification {
let name: String
let subspecs: [String]
let podspec: String
let development: Bool

static func resolve(with podConfigsMap: [String: PodConfig]) -> [PodSpecification] {
let podConfigs = Array(podConfigsMap.values)
let (podspecPaths, subspecsByPodName) =
podConfigs.reduce(([String: String](), [String: [String]]())) { partialResult, podConfig in
var podspecPaths = partialResult.0
var subspecsByPodName = partialResult.1

let components = podConfig.name.components(separatedBy: "/")
guard let podName = components.first else {
return partialResult
}

podspecPaths[podName] = podConfig.podspec

if components.count == 2, let subspecName = components.last {
var subspecs = subspecsByPodName[podName] ?? []
subspecs.append(subspecName)
subspecsByPodName[podName] = subspecs
}
return (podspecPaths, subspecsByPodName)
}
return podspecPaths.map({
PodSpecification(name: $0.key, subspecs: subspecsByPodName[$0.key] ?? [], podspec: $0.value, development: podConfigsMap[$0.key]?.development ?? false)
})
}

func toBuildOptions(src: String, ios: String) -> BuildOptions {
let path = (src as NSString).appendingPathComponent("Pods/\(name)")
return BasicBuildOptions(podName: name, subspecs: subspecs, podspecPath: podspec, sourcePath: path, iosPlatform: ios)
}
}
Loading

0 comments on commit 3f0f121

Please sign in to comment.