Skip to content

Instantly share code, notes, and snippets.

View MahdiBM's full-sized avatar

Mahdi Bahrami MahdiBM

View GitHub Profile
@MahdiBM
MahdiBM / DecodeNilable.swift
Created April 20, 2021 13:11
PropertyWrapper for Codable to not fail when finding `nil`
import Foundation
//MARK: - EmptyInitializable
protocol EmptyInitializable {
init()
}
extension String: EmptyInitializable {}
extension Bool: EmptyInitializable {}
extension Int: EmptyInitializable {}
@MahdiBM
MahdiBM / dropCaches.swift
Last active April 21, 2022 17:08
Drop Caches Shell (Ubuntu 20.04)
#!/usr/bin/env swift
import Foundation
// Drops caches if (freeRam / totalRam) < 0.2
print("Program started")
let pipe = Pipe()
let free = Process()
free.executableURL = URL(fileURLWithPath: "/usr/bin/env")
free.arguments = ["free"]
@MahdiBM
MahdiBM / Simple Circular Progress View
Last active June 16, 2021 20:32
Simple Circular Progress View
import SwiftUI
struct ContentView: View {
@State var progress: CGFloat = 0 /* 0 <= progress <= 1 */
let radius: CGFloat = 150
let lineWidth: CGFloat = 10
let startAngle: Angle = .degrees(35)
let endAngle: Angle = .degrees(145)
var deltaArc: CGFloat {
@MahdiBM
MahdiBM / DocC+Nginx
Last active July 22, 2021 11:00
Setting up routing for a DocC Archive (`.doccarchive`) in Nginx
# Read the whole file once, if you're going to use it. There are a few things you need to change.
# Disclaimer:
# I'm using a very similar code myself, but i'm not a Nginx expert, so use this at your own risk.
server {
server_name YOURDOMAIN.COM WWW.YOURDOMAIN.COM;
# Example: `server_name oauthbm.mahdibm.com www.oauthbm.mahdibm.com;`
root /path/to/NAME_OF_ARCHIVE.doccarchive;
# Example: `home/ubuntu/OAuthBM/OAuthBM.doccarchive;`
@MahdiBM
MahdiBM / asyncMaps.swift
Created November 5, 2021 02:34
Swift async/await: Async and Concurrent maps
extension Sequence {
/// Maps through the elements one by one,
/// executing the closure for each of them one at a time.
func asyncMap<T>(
_ closure: @Sendable (Element) async throws -> T
) async rethrows -> [T] {
var array: [T] = []
array.reserveCapacity(self.underestimatedCount)
@MahdiBM
MahdiBM / ComputedField.swift
Created November 28, 2021 16:14
Fluent ComputedField
/*
Use Case:
@ComputedField(key: "someKey", onSet: { transfromValue($0) })
var someVar: Foo
*/
import FluentKit
extension Fields {
@MahdiBM
MahdiBM / 59-build-failures-penny.txt
Created September 20, 2023 08:44
Translated 5.9 release linux linker failures for Penny
[2711/2755] Compiling GHHooksLambda Requester.swift
[2712/2755] Emitting module Penny
undefined reference to '[QueuedProposal]'
var queuedProposals: [QueuedProposal] = []
^
undefined reference to 'rotoco'
struct QueuedProposal: Codable {
^
, Sendable
[2713/2773] Compiling Penny MainService.swift
@MahdiBM
MahdiBM / vapor+openapi+tasklocals.swift
Last active February 27, 2024 20:14
swift-openapi-vapor + `TaskLocal`s to pass the `Request` to the OpenAPI handler
/// PREREQUISITES:
/// Add https://github.com/pointfreeco/swift-dependencies as a dependency of your target, currently it looks like so:
/// `.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.0.0"),`
// MARK: '+DependencyValues.swift'
import Vapor
import Dependencies
extension DependencyValues {