Skip to content

Commit

Permalink
基于 Swift 编写严肃脚本工具
Browse files Browse the repository at this point in the history
  • Loading branch information
100mango committed Jul 24, 2021
1 parent 97460e8 commit b53535e
Show file tree
Hide file tree
Showing 18 changed files with 774 additions and 0 deletions.
Binary file added 基于 Swift 编写严肃脚本工具/ppt.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions 基于 Swift 编写严肃脚本工具/script/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "script"
BuildableName = "script"
BlueprintName = "script"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "NO"
buildForArchiving = "NO"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "scriptTests"
BuildableName = "scriptTests"
BlueprintName = "scriptTests"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "core"
BuildableName = "core"
BlueprintName = "core"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "scriptTests"
BuildableName = "scriptTests"
BlueprintName = "scriptTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "script"
BuildableName = "script"
BlueprintName = "script"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "--help"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "script"
BuildableName = "script"
BlueprintName = "script"
ReferencedContainer = "container:">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
16 changes: 16 additions & 0 deletions 基于 Swift 编写严肃脚本工具/script/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"object": {
"pins": [
{
"package": "swift-argument-parser",
"repositoryURL": "https://github.com/apple/swift-argument-parser",
"state": {
"branch": null,
"revision": "986d191f94cec88f6350056da59c2e59e83d1229",
"version": "0.4.3"
}
}
]
},
"version": 1
}
26 changes: 26 additions & 0 deletions 基于 Swift 编写严肃脚本工具/script/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "script",
platforms: [
.macOS(.v11)
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "0.4.0")
],
targets: [
.executableTarget(
name: "script",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser")]),
.target(
name: "core",
dependencies: []),
.testTarget(
name: "scriptTests",
dependencies: ["script"]),
]
)
3 changes: 3 additions & 0 deletions 基于 Swift 编写严肃脚本工具/script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# script

A description of this package.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// File.swift
//
//
// Created by 100mango on 2021/6/26.
//

import Foundation
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@


//
//import Darwin
//
////let arguments: [String] = Array(CommandLine.arguments.dropFirst())
////
////guard let numberString = arguments.first else {
//// print("no argument")
//// exit(1)
////}
////
////guard let number = UInt(numberString) else {
//// print("no unsigned number")
//// exit(1)
////}
////
////print(UInt.random(in: 0...number))
////exit(0)
//
//import ArgumentParser
//
//struct Random: ParsableCommand {
// @Argument(help: "unsigned number")
// var highValue: UInt
//
// func run() {
// print(UInt.random(in: 0...highValue))
// }
//}
//
//Random.main()

//import AppKit
//
//NSApplication.shared.setActivationPolicy(.accessory)
//
//func selectFile() -> URL? {
// let dialog = NSOpenPanel()
// dialog.allowedFileTypes = ["jpg", "png"]
// guard dialog.runModal() == .OK else { return nil }
// return dialog.url
//}
//
//print (selectFile()?.absoluteString ?? "")

import SwiftUI

struct App: SwiftUI.App {
@State var filename = "Filename"
@State var showFileChooser = false

var body: some Scene {
WindowGroup {
HStack {
Text(filename)
Button ("select File")
{
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = false
if panel.runModal() == .OK {
self.filename = panel.url?.lastPathComponent ?? "<none>"
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.windowStyle(HiddenTitleBarWindowStyle())
}
}
App.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import XCTest
import class Foundation.Bundle

final class scriptTests: XCTestCase {
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.

// Some of the APIs that we use below are available in macOS 10.13 and above.
guard #available(macOS 10.13, *) else {
return
}

// Mac Catalyst won't have `Process`, but it is supported for executables.
#if !targetEnvironment(macCatalyst)

let fooBinary = productsDirectory.appendingPathComponent("script")

let process = Process()
process.executableURL = fooBinary

let pipe = Pipe()
process.standardOutput = pipe

try process.run()
process.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8)

XCTAssertEqual(output, "Hello, world!\n")
#endif
}

/// Returns path to the built products directory.
var productsDirectory: URL {
#if os(macOS)
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
return bundle.bundleURL.deletingLastPathComponent()
}
fatalError("couldn't find the products directory")
#else
return Bundle.main.bundleURL
#endif
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit b53535e

Please sign in to comment.