Skip to content
This repository has been archived by the owner on Feb 4, 2023. It is now read-only.

Commit

Permalink
AuxiliaryExecute Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakr233 committed Jan 10, 2022
1 parent 5d1c4ea commit 92981bf
Show file tree
Hide file tree
Showing 9 changed files with 411 additions and 140 deletions.
8 changes: 6 additions & 2 deletions Application/iridium.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,14 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = GXZ23M5TP2;
ENABLE_BITCODE = NO;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = iridium/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Iridium;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.developer-tools";
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2022 Lakr Aream. All rights reserved.";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
Expand Down Expand Up @@ -447,12 +449,14 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = GXZ23M5TP2;
ENABLE_BITCODE = NO;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = iridium/Info.plist;
INFOPLIST_KEY_CFBundleDisplayName = Iridium;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.developer-tools";
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2022 Lakr Aream. All rights reserved.";
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen;
INFOPLIST_KEY_UIMainStoryboardFile = Main;
Expand Down
119 changes: 119 additions & 0 deletions Foundation/AuxiliaryExecute/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
!default.mode1v3
!default.mode2v3
!default.pbxuser
!default.perspectivev3
!default.xcworkspace
*.dSYM
*.dSYM.zip
*.hmap
*.ipa
*.lcov
*.lock
*.log
*.mode1v3
*.mode2v3
*.moved-aside
*.pbxuser
*.perspectivev3
*.pid
*.pid.lock
*.seed
*.swp
*.tgz
*.tsbuildinfo
*.xccheckout
*.xcscmblueprint
*.xcuserstate
*~.nib
.AppleDB
.AppleDesktop
.AppleDouble
.DS_Store
.DocumentRevisions-V100
.LSOverride
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
._*
.apdisk
.build
.bundle
.cache
.cache/
.com.apple.timemachine.donotpresent
.dynamodb/
.env
.env.test
.eslintcache
.fseventsd
.fusebox/
.grunt
.idea
.lock-wscript
.next
.node_repl_history
.npm
.nuxt
.nyc_output
.parcel-cache
.pnp.*
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
.serverless/
.swiftpm
.tern-port
.vscode-test
.vuepress/dist
.yarn-integrity
.yarn/build-state.yml
.yarn/cache
.yarn/unplugged
/*.gcno
Artifacts/
CI
CI-Pods.tar
Carthage/Build
Carthage/Build/
DerivedData
DerivedData/
Icon
Network Trash Folder
Pipeline/Dockers/Buildtime/
Podfile.lock
Pods/
Temporary Items
artifacts/
bower_components
build/
build/Release
coverage
default.profraw
dist
dockerbuild
dockermnt
fastlane/Preview.html
fastlane/report.xml
fastlane/screenshots/**/*.png
fastlane/test_output
iOSInjectionProject/
jspm_packages/
lerna-debug.log*
lib-cov
logs
node_modules/
npm-debug.log*
pids
profile
project.xcworkspace
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
temp/
temps/
web_modules/
xcuserdata
xcuserdata/
yarn-debug.log*
yarn-error.log*

52 changes: 52 additions & 0 deletions Foundation/AuxiliaryExecute/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# AuxiliaryExecute

A Swift wrapper for system shell over posix_spawn with search path and env support.

## Usage

```
import AuxiliaryExecute
AuxiliaryExecute.local.bash(command: "echo nya")
```

## Customization & Defaults

The source for this package is well explained in details along with comments. Feel free looking for them.

```
// automatically search for binary within env PATH
let result = AuxiliaryExecute.local.shell(
command: "bash",
args: ["-c", "echo $mua"],
environment: ["mua": "nya"],
timeout: 0
) { stdout in
print(stdout)
} stderrBlock: { stderr in
print(stderr)
}
// or call with binary's full path
func spawn(
command: String,
args: [String] = [],
environment: [String: String] = [:],
timeout: Double = 0,
stdoutBlock: ((String) -> Void)? = nil,
stderrBlock: ((String) -> Void)? = nil
)
// for customize option for shell
func appendSearchPath(with value: String)
func updateExtraSearchPath(with block: (inout [String]) -> Void)
func updateOverwriteTable(with block: (inout [String: String?]) -> Void)
```

## License

AuxiliaryExecute is licensed under [MIT](./LICENSE).

---

Copyright © 2021 Lakr Aream. All Rights Reserved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// AuxiliaryExecute+Spawn.swift
// AuxiliaryExecute
//
// Created by Cyandev on 2022/1/10.
//

#if swift(>=5.5)

import Foundation

@available(iOS 15.0, macOS 12.0.0, *)
public extension AuxiliaryExecute {
/// async/await function for spawn using withCheckedContinuation
/// - Parameters:
/// - command: full path of the binary file. eg: "/bin/cat"
/// - args: arg to pass to the binary, exclude argv[0] which is the path itself. eg: ["nya"]
/// - environment: any environment to be appended/overwrite when calling posix spawn. eg: ["mua" : "nya"]
/// - timeout: any wall timeout if lager than 0, in seconds. eg: 6
/// - stdout: a block call from pipeControlQueue in background when buffer from stdout available for read
/// - stderr: a block call from pipeControlQueue in background when buffer from stderr available for read
/// - Returns: execution recipe, see it's definition for details
@discardableResult
static func spawnAsync(
command: String,
args: [String] = [],
environment: [String: String] = [:],
timeout: Double = 0,
stdoutBlock: ((String) -> Void)? = nil,
stderrBlock: ((String) -> Void)? = nil
) async -> ExecuteRecipe {
await withCheckedContinuation { cont in
self.spawn(
command: command,
args: args,
environment: environment,
timeout: timeout,
stdoutBlock: stdoutBlock,
stderrBlock: stderrBlock
) { recipe in
cont.resume(returning: recipe)
}
}
}

/// async/await function for spawn using withCheckedContinuation
/// - Parameters:
/// - command: full path of the binary file. eg: "/bin/cat"
/// - args: arg to pass to the binary, exclude argv[0] which is the path itself. eg: ["nya"]
/// - environment: any environment to be appended/overwrite when calling posix spawn. eg: ["mua" : "nya"]
/// - timeout: any wall timeout if lager than 0, in seconds. eg: 6
/// - output: a block call from pipeControlQueue in background when buffer from stdout or stderr available for read
/// - Returns: execution recipe, see it's definition for details
@discardableResult
static func spawnAsync(
command: String,
args: [String] = [],
environment: [String: String] = [:],
timeout: Double = 0,
output: ((String) -> Void)? = nil
) async -> ExecuteRecipe {
await spawnAsync(
command: command,
args: args,
environment: environment,
timeout: timeout,
stdoutBlock: { str in
output?(str)
}, stderrBlock: { str in
output?(str)
}
)
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ public extension AuxiliaryExecute {
/// - args: arg to pass to the binary, exclude argv[0] which is the path itself. eg: ["nya"]
/// - environment: any environment to be appended/overwrite when calling posix spawn. eg: ["mua" : "nya"]
/// - timeout: any wall timeout if lager than 0, in seconds. eg: 6
/// - stdout: a block call from pipeReadQueue in background when buffer from stdout available for read
/// - stderr: a block call from pipeReadQueue in background when buffer from stderr available for read
/// - stdout: a block call from pipeControlQueue in background when buffer from stdout available for read
/// - stderr: a block call from pipeControlQueue in background when buffer from stderr available for read
/// - Returns: execution recipe, see it's definition for details
@discardableResult
func shell(
Expand Down Expand Up @@ -151,21 +151,11 @@ public extension AuxiliaryExecute {
}
// make sure we find the command
guard let commandLocation = commandLocation else {
return .init(
exitCode: -1,
error: .commandNotFound,
stdout: "",
stderr: ""
)
return ExecuteRecipe.failure(error: .commandNotFound)
}
// now, let's validate the command
guard isBinaryValid(at: URL(fileURLWithPath: commandLocation)) else {
return .init(
exitCode: -1,
error: .commandInvalid,
stdout: "",
stderr: ""
)
return ExecuteRecipe.failure(error: .commandInvalid)
}
// finally let‘s call the spawn
let recipe = AuxiliaryExecute.spawn(
Expand All @@ -184,8 +174,8 @@ public extension AuxiliaryExecute {
/// - command: script to be passed to bash. eg: "echo nya"
/// - environment: any environment to be appended/overwrite when calling posix spawn. eg: ["mua" : "nya"]
/// - timeout: any wall timeout if lager than 0, in seconds. eg: 6
/// - stdout: a block call from pipeReadQueue in background when buffer from stdout available for read
/// - stderr: a block call from pipeReadQueue in background when buffer from stderr available for read
/// - stdout: a block call from pipeControlQueue in background when buffer from stdout available for read
/// - stderr: a block call from pipeControlQueue in background when buffer from stderr available for read
/// - Returns: execution recipe, see it's definition for details
@discardableResult
func bash(
Expand Down
Loading

0 comments on commit 92981bf

Please sign in to comment.