Skip to content

Commit

Permalink
Modernize project (#66)
Browse files Browse the repository at this point in the history
* Add Mintfile

* Update Makefile

* Use old swiftlint and swiftformat versions

* Lint

* Update SwiftLint and SwiftFormat Versions + Lint

* Re-add documented make rules

* Update sourcery to 2.2.5

* Update SwiftLint and Lint
  • Loading branch information
ctreffs authored Oct 9, 2024
1 parent a5c98c4 commit 2c1c588
Show file tree
Hide file tree
Showing 27 changed files with 177 additions and 176 deletions.
11 changes: 11 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# file options
--exclude Sources/**/*.generated.swift
--exclude Sources/FirebladeECS/Entity+Component.swift # problems with self.get { }
--exclude Tests/**/*.swift

# format options
--extensionacl on-declarations
--stripunusedargs closure-only
--commas inline
--self remove
--selfrequired get
23 changes: 4 additions & 19 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,11 @@ identifier_name:
line_length: 220
number_separator:
minimum_length: 5
analyzer_rules:
- explicit_self
- unused_declaration
- unused_import
opt_in_rules:
#- explicit_acl
#- explicit_enum_raw_value
#- explicit_type_interface
#- extension_access_modifier
#- file_name
#- file_types_order
#- indentation_width
#- missing_docs
#- multiline_arguments_brackets
#- multiline_literal_brackets
#- multiline_parameters_brackets
#- no_grouping_extension
#- required_deinit
#- type_contents_order
#- unowned_variable_capture
- anyobject_protocol
- array_init
- attributes
- closure_body_length
Expand All @@ -48,7 +36,6 @@ opt_in_rules:
- enum_case_associated_values_count
- expiring_todo
- explicit_init
- explicit_self
- explicit_top_level_acl
- fallthrough
- fatal_error_message
Expand Down Expand Up @@ -109,8 +96,6 @@ opt_in_rules:
- unavailable_function
- unneeded_parentheses_in_closure_argument
- untyped_error_in_catch
- unused_declaration
- unused_import
- vertical_parameter_alignment_on_call
- vertical_whitespace_between_cases
- vertical_whitespace_closing_braces
Expand Down
126 changes: 54 additions & 72 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,79 +1,61 @@
# Version 1.0.0
UNAME_S := $(shell uname -s)

# Lint
lint:
swiftlint autocorrect --format
swiftlint lint --quiet

lintErrorOnly:
@swiftlint autocorrect --format --quiet
@swiftlint lint --quiet | grep error

# Git
precommit: generateCode generateTestsCode lint genLinuxTests

submodule:
git submodule init
git submodule update --recursive

# Tests
genLinuxTests:
swift test --generate-linuxmain
swiftlint autocorrect --format --path Tests/

test: genLinuxTests
swift test

# Package
latest:
swift package update

resolve:
swift package resolve

# Xcode
genXcode:
swift package generate-xcodeproj --enable-code-coverage --skip-extra-files

genXcodeOpen: genXcode
open *.xcodeproj

# Clean
clean:
swift package reset
-rm -rdf .swiftpm/xcode
-rm -rdf .build/
-rm Package.resolved
-rm .DS_Store

cleanArtifacts:
swift package clean
SWIFT_PACKAGE_VERSION := $(shell swift package tools-version)

# Lint fix and format code.
.PHONY: lint-fix
swiftlint:
mint run swiftlint lint --fix --config .swiftlint.yml --format --quiet
swiftformat:
mint run swiftformat . --swiftversion ${SWIFT_PACKAGE_VERSION}
lint-fix: swiftlint swiftformat

# Generate code
.PHONY: generate-code
generate-code:
mint run sourcery --quiet --config ./.sourcery.yml
mint run sourcery --quiet --config ./.sourceryTests.yml

# Run pre-push tasks
.PHONY: pre-push
pre-push: generate-code lint-fix

.PHONY: precommit
precommit: pre-push

.PHONY: setup-brew
setup-brew:
@which -s brew || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
@brew update

.PHONY: install-dependencies-macOS
install-dependencies-macOS: setup-brew
brew install mint
mint bootstrap

.PHONY: setupEnvironment
setupEnvironment: install-dependencies-macOS

# Build debug version
.PHONY: build-debug
build-debug:
swift build -c debug

# Build release version
.PHONY: build-release
build-release:
swift build -c release --skip-update

# Test links in README
# requires <https://github.com/tcort/markdown-link-check>
.PHONY: testReadme
testReadme:
markdown-link-check -p -v ./README.md

generateCode:
sourcery --config ./.sourcery.yml --verbose
generateTestsCode:
sourcery --config ./.sourceryTests.yml --verbose

brewInstallDeps: brewUpdate
brew install swiftenv
brew install swiftlint
brew install sourcery

brewSetup:
which -s brew || /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

brewUpdate: brewSetup
brew update

setupEnvironment: brewInstallDeps
open Package.swift
# Delete package build artifacts.
.PHONY: clean
clean: clean-sourcery
swift package clean

# lines of code
loc: clean
find . -name "*.swift" -print0 | xargs -0 wc -l
# Clean sourcery cache
.PHONY: clean-sourcery
clean-sourcery:
rm -rdf ${HOME}/Library/Caches/Sourcery
3 changes: 3 additions & 0 deletions Mintfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
realm/[email protected]
nicklockwood/[email protected]
krzysztofzablocki/[email protected]
2 changes: 1 addition & 1 deletion Sources/FirebladeECS/CodingStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public struct DynamicCodingKey: CodingKey {
public var intValue: Int?
public var stringValue: String

public init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
public init?(intValue: Int) { self.intValue = intValue; stringValue = "\(intValue)" }
public init?(stringValue: String) { self.stringValue = stringValue }
}
8 changes: 4 additions & 4 deletions Sources/FirebladeECS/ComponentIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public struct ComponentIdentifier {
extension ComponentIdentifier {
@usableFromInline
init<C>(_ componentType: C.Type) where C: Component {
self.id = Self.makeRuntimeHash(componentType)
id = Self.makeRuntimeHash(componentType)
}

/// object identifier hash (only stable during runtime) - arbitrary hash is ok.
internal static func makeRuntimeHash<C>(_ componentType: C.Type) -> Identifier where C: Component {
static func makeRuntimeHash<C>(_ componentType: C.Type) -> Identifier where C: Component {
ObjectIdentifier(componentType).hashValue
}
}

extension ComponentIdentifier: Equatable { }
extension ComponentIdentifier: Hashable { }
extension ComponentIdentifier: Equatable {}
extension ComponentIdentifier: Hashable {}
11 changes: 6 additions & 5 deletions Sources/FirebladeECS/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public struct Entity {
/// The unique entity identifier.
public private(set) var identifier: EntityIdentifier

internal init(nexus: Nexus, id: EntityIdentifier) {
init(nexus: Nexus, id: EntityIdentifier) {
self.nexus = nexus
self.identifier = id
identifier = id
}

/// Returns the number of components for this entity.
Expand Down Expand Up @@ -130,7 +130,7 @@ public struct Entity {

extension Entity {
public struct ComponentsIterator: IteratorProtocol {
private var iterator: IndexingIterator<([Component])>?
private var iterator: IndexingIterator<[Component]>?

@usableFromInline
init(nexus: Nexus, entityIdentifier: EntityIdentifier) {
Expand All @@ -144,8 +144,9 @@ extension Entity {
}
}
}
extension Entity.ComponentsIterator: LazySequenceProtocol { }
extension Entity.ComponentsIterator: Sequence { }

extension Entity.ComponentsIterator: LazySequenceProtocol {}
extension Entity.ComponentsIterator: Sequence {}

extension Entity: Equatable {
public static func == (lhs: Entity, rhs: Entity) -> Bool {
Expand Down
6 changes: 3 additions & 3 deletions Sources/FirebladeECS/EntityIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public struct EntityIdentifier {
}
}

extension EntityIdentifier: Equatable { }
extension EntityIdentifier: Hashable { }
extension EntityIdentifier: Equatable {}
extension EntityIdentifier: Hashable {}

extension EntityIdentifier: RawRepresentable {
/// The entity identifier represented as a raw value.
Expand All @@ -33,7 +33,7 @@ extension EntityIdentifier: RawRepresentable {

@inlinable
public init(rawValue: Identifier) {
self.id = rawValue
id = rawValue
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/FirebladeECS/EntityIdentifierGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct LinearIncrementingEntityIdGenerator: EntityIdentifierGenerator {
let initialInUse: [EntityIdentifier.Identifier] = initialEntityIds.map { $0.id }
let maxInUseValue = initialInUse.max() ?? 0
let inUseSet = Set(initialInUse) // a set of all eIds in use
let allSet = Set(0...maxInUseValue) // all eIds from 0 to including maxInUseValue
let allSet = Set(0 ... maxInUseValue) // all eIds from 0 to including maxInUseValue
let freeSet = allSet.subtracting(inUseSet) // all "holes" / unused / free eIds
let initialFree = Array(freeSet).sorted().reversed() // order them to provide them linear increasing after all initially used are provided.
stack = initialFree + initialInUse
Expand Down Expand Up @@ -83,12 +83,12 @@ public struct LinearIncrementingEntityIdGenerator: EntityIdentifierGenerator {

@inlinable
public init<EntityIds>(startProviding initialEntityIds: EntityIds) where EntityIds: BidirectionalCollection, EntityIds.Element == EntityIdentifier {
self.storage = Storage(startProviding: initialEntityIds)
storage = Storage(startProviding: initialEntityIds)
}

@inlinable
public init() {
self.storage = Storage()
storage = Storage()
}

@inline(__always)
Expand Down
11 changes: 5 additions & 6 deletions Sources/FirebladeECS/FSM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ extension ComponentTypeProvider: ComponentProvider {
/// This component provider always returns the same instance of the component. The instance
/// is created when first required and is of the type passed in to the initializer.
public final class ComponentSingletonProvider {
private lazy var instance: Component = {
componentType.init()
}()
private lazy var instance: Component = componentType.init()

private var componentType: ComponentInitializable.Type

Expand Down Expand Up @@ -171,7 +169,7 @@ extension DynamicComponentProvider: ComponentProvider {
/// Represents a state for an EntityStateMachine. The state contains any number of ComponentProviders which
/// are used to add components to the entity when this state is entered.
public class EntityState {
internal var providers = [ComponentIdentifier: ComponentProvider]()
var providers = [ComponentIdentifier: ComponentProvider]()

public init() {}

Expand Down Expand Up @@ -272,7 +270,7 @@ public class StateComponentMapping {
/// by more specific mappings if other methods are called.
/// - Parameter creatingState: The EntityState that the mapping will belong to
/// - Parameter type: The component type for the mapping
internal init(creatingState: EntityState, type: ComponentInitializable.Type) {
init(creatingState: EntityState, type: ComponentInitializable.Type) {
self.creatingState = creatingState
componentType = type
provider = ComponentTypeProvider(type: type)
Expand Down Expand Up @@ -411,7 +409,8 @@ public class EntityStateMachine<StateIdentifier: Hashable> {

for (identifier, _) in currentState.providers {
if let other = toAdd[identifier], let current = currentState.providers[identifier],
current.identifier == other.identifier {
current.identifier == other.identifier
{
toAdd[identifier] = nil
} else {
entity.remove(identifier)
Expand Down
4 changes: 3 additions & 1 deletion Sources/FirebladeECS/Family+Coding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ extension CodingUserInfoKey {
}

// MARK: - encoding

extension FamilyMemberContainer: Encodable where R: FamilyEncoding {
func encode(to encoder: Encoder) throws {
let strategy = encoder.userInfo[.nexusCodingStrategy] as? CodingStrategy ?? DefaultCodingStrategy()
Expand Down Expand Up @@ -50,11 +51,12 @@ extension Family where R: FamilyEncoding {
}

// MARK: - decoding

extension FamilyMemberContainer: Decodable where R: FamilyDecoding {
init(from decoder: Decoder) throws {
var familyContainer = try decoder.unkeyedContainer()
let strategy = decoder.userInfo[.nexusCodingStrategy] as? CodingStrategy ?? DefaultCodingStrategy()
self.components = try R.decode(componentsIn: &familyContainer, using: strategy)
components = try R.decode(componentsIn: &familyContainer, using: strategy)
}
}

Expand Down
Loading

0 comments on commit 2c1c588

Please sign in to comment.