fix: fixed sonya not working properly with stdin #1

shell now calls standard system from stdlib.h from c
works on macOS, needed to test on linux
regex strings in VarRegex were refactored into RegexBuilder
all tests passed.
This commit is contained in:
2026-04-22 09:19:53 +09:00
parent 96ba9e3a56
commit 25666f1659
6 changed files with 49 additions and 23 deletions
+8
View File
@@ -1,3 +1,4 @@
import Foundation
import sonyalib import sonyalib
@main @main
@@ -12,18 +13,25 @@ struct sonya {
break break
case .noSonyafile: case .noSonyafile:
print("sonya: no sonyafile found in current directory") print("sonya: no sonyafile found in current directory")
exit(1)
case .isEmpty: case .isEmpty:
print("sonya: sonyafile is empty") print("sonya: sonyafile is empty")
exit(2)
case .syntaxError: case .syntaxError:
print("sonya: syntax error in sonyafile") print("sonya: syntax error in sonyafile")
exit(3)
case .noSuchRecipe: case .noSuchRecipe:
print("sonya: no recipe named '\(recipe)'") print("sonya: no recipe named '\(recipe)'")
exit(4)
case .noSuchDependency: case .noSuchDependency:
print("sonya: unknown dependency") print("sonya: unknown dependency")
exit(5)
case .shellReturnedError: case .shellReturnedError:
print("sonya: command failed") print("sonya: command failed")
exit(6)
case .noSuchVariable: case .noSuchVariable:
print("sonya: undefined variable referenced in recipe '\(recipe)'") print("sonya: undefined variable referenced in recipe '\(recipe)'")
exit(7)
} }
} }
} }
+7 -6
View File
@@ -1,6 +1,4 @@
// swift-tools-version: 6.2 // swift-tools-version: 6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription import PackageDescription
let package = Package( let package = Package(
@@ -9,17 +7,20 @@ let package = Package(
.macOS(.v13) .macOS(.v13)
], ],
products: [ products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library( .library(
name: "sonyalib", name: "sonyalib",
targets: ["sonyalib"] targets: ["sonyalib"]
), ),
], ],
targets: [ targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target( .target(
name: "sonyalib" name: "ShellHelper",
path: "Sources/shellHelper",
publicHeadersPath: "."
),
.target(
name: "sonyalib",
dependencies: ["ShellHelper"]
), ),
.testTarget( .testTarget(
name: "sonyalibTests", name: "sonyalibTests",
@@ -0,0 +1,13 @@
//
// shell_helper.c
// sonyalib
//
// Created by hwacha on 4/22/26.
//
#include <stdlib.h>
#include "shell_helper.h"
int run_system(const char *cmd) {
return system(cmd);
}
@@ -0,0 +1,8 @@
//
// shell_helper.h
// sonyalib
//
// Created by hwacha on 4/22/26.
//
int run_system(const char *cmd);
+2 -15
View File
@@ -6,6 +6,7 @@
// //
import Foundation import Foundation
import ShellHelper
func openSonyafile() -> String? { func openSonyafile() -> String? {
let fileManager = FileManager.default let fileManager = FileManager.default
@@ -21,19 +22,5 @@ func openSonyafile() -> String? {
@discardableResult @discardableResult
func shell(_ command: String) -> Int32 { func shell(_ command: String) -> Int32 {
let process = Process() return run_system(command)
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
process.executableURL = URL(fileURLWithPath: "/bin/bash")
process.arguments = ["-c", command]
try? process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
FileHandle.standardOutput.write(data)
return process.terminationStatus
} }
@@ -7,9 +7,18 @@
// TODO: args // TODO: args
nonisolated(unsafe) let varRegex = #/\$\(([^\s\n]*)\)/# import RegexBuilder
nonisolated(unsafe) let argRegex = #/\$(\d+)/# nonisolated(unsafe) let varRegex = Regex {
"$("
Capture(ZeroOrMore(CharacterClass(.whitespace, .newlineSequence).inverted))
")"
}
nonisolated(unsafe) let argRegex = Regex {
"$"
OneOrMore(.digit)
}
func resolve(_ command: String, vars: [String: String]) -> String? { func resolve(_ command: String, vars: [String: String]) -> String? {
return resolveVars(command, vars: vars) return resolveVars(command, vars: vars)