Skip to content

Commit

Permalink
App: Updated script to also run as command line tool if there's speci…
Browse files Browse the repository at this point in the history
…al env variable

This helps in headless environments, thanks for edwellbrook for initial concept implementation
  • Loading branch information
vashpan committed Mar 6, 2024
1 parent d2da415 commit c7260d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions DevCleaner/Base/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ private func commandLineDebugEnabled() -> Bool {
}

private func isRunningFromCommandLine(args: [String]) -> Bool {
let isTTY = isatty(STDIN_FILENO) // with this param true, we can always assune we run from command line
// We have few ways of checking it, first is checking if our STDOUT is bound to some terminal
// and second is checking for presence of env variable set in dev-cleaner.sh script. It helps
// in cases where we want to run the script from headless environments where there's no TTY.
//
// Maybe there're better & more sure ways of handling/checking that, but I could't really found them.
//

// it seems that's enough, but maybe in the future we can also try to check parent PID,
// to make sure. We also check for a special argument passed usually by Xcode & debugger to mark we run from Xcode and usually
// want a full window, it's not a great way though
let isTTY = isatty(STDIN_FILENO)
let haveProperEnvValue = Preferences.shared.envKeyPresent(key: "DEV_CLEANER_FROM_COMMAND_LINE")

let runningFromCommandLine = isTTY == 1 || haveProperEnvValue

#if DEBUG
let isRunningFromXcode = args.contains("-NSDocumentRevisionsDebugMode")
let runningFromXcode = args.contains("-NSDocumentRevisionsDebugMode")
#else
let isRunningFromXcode = false
let runningFromXcode = false
#endif

return commandLineDebugEnabled() || (isTTY == 1 && !isRunningFromXcode)
return commandLineDebugEnabled() || (runningFromCommandLine && !runningFromXcode)
}

private func cleanedCommandLineArguments(args: [String]) -> [String] {
Expand Down
5 changes: 4 additions & 1 deletion DevCleaner/Resources/dev-cleaner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# "dev-cleaner" command line tool wrapper script.
#
# Copyright © 2019 One Minute Games. All rights reserved.
# Copyright © 2019-2024 One Minute Games. All rights reserved.
#
# DevCleaner is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand All @@ -23,6 +23,9 @@
# get app updated app path
DEV_CLEANER_PATH=$(defaults read com.oneminutegames.XcodeCleaner DCAppFolder)

# set a command line run env value
DEV_CLEANER_FROM_COMMAND_LINE=1

if [ -d $DEV_CLEANER_PATH ]; then
"$DEV_CLEANER_PATH/Contents/MacOS/DevCleaner" "$@"
else
Expand Down

0 comments on commit c7260d8

Please sign in to comment.