https://github.com/marcoeidinger/conditional-files
CLI tool to mass insert/delete conditional compilation statements
https://github.com/marcoeidinger/conditional-files
cli compiler-directives conditional-statements macos swift utility
Last synced: 4 months ago
JSON representation
CLI tool to mass insert/delete conditional compilation statements
- Host: GitHub
- URL: https://github.com/marcoeidinger/conditional-files
- Owner: MarcoEidinger
- License: mit
- Created: 2022-12-15T00:34:34.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-07-20T01:31:13.000Z (over 2 years ago)
- Last Synced: 2025-05-21T19:53:40.269Z (9 months ago)
- Topics: cli, compiler-directives, conditional-statements, macos, swift, utility
- Language: Swift
- Homepage:
- Size: 25.4 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/MarcoEidinger/conditional-files/actions/workflows/swift.yml) [](https://codecov.io/github/MarcoEidinger/conditional-files)
# ConditionalFiles
A command-line tool intended to insert a conditional compilation statement in multiple Swift files at once.
And generic enough being able to process multiple files and **insert *any* text at the top and the bottom of a file** :)
## Find files with compiler directive (`#if ... #endif`) hugging the file content
```bash
swift run conditional-files find .
```
## Find and remove hugging compilerdirective
```bash
swift run conditional-files find . | xargs swift run conditional-files remove
```
## Add `#if os() ... #endif` to all files in (sub) directory
Set one or more respective flags, e.g. for iOS use `--ios`.
Pass a single dot (`.`) as argument to process all files in the current directory and subdirectories.
Example: `swift run conditional-files --ios .`
File (before) File (after)
```swift
import CarKey
// code
```
```swift
#if os(iOS)
import CarKey
// code
#endif
```
You can add multiple statements by adding multiple flags.
```bash
swift run conditional-files --ios --watchos .
```
File (before) File (after)
```swift
import CarKey
// code
```
```swift
#if os(iOS) || os(watchOS)
import CarKey
// code
#endif
```
## Remove `#if os() ... #endif`
You can remove an existing compiler directive with flag `remove`.
```bash
swift run conditional-files --ios --undo test.swift
```
File (before) File (after)
```swift
#if os(iOS)
import CarKey
// code
#endif
```
```swift
import CarKey
// code
```
## Add `#if os() ... #endif` to specific file(s)
Pass one or more file paths as arguments.
```bash
swift run conditional-files --ios test.swift
```
File (before) File (after)
```swift
import CarKey
// code
```
```swift
#if os(iOS)
import CarKey
// code
#endif
```
## Add `#if !os() ... #endif`
You can negate the #if(os) directive with command `not-os`.
```bash
swift run conditional-files not-os --ios --watchos test.swift
```
File (before) File (after)
```swift
import CarKey
// code
```
```swift
#if !os(iOS) && !os(watchOS)
import CarKey
// code
#endif
```
## Add `#if DEBUG ... #endif`
```text
swift run conditional-files generic --first-line '#if DEBUG' --last-line \#endif test.swift
```
File (before) File (after)
```swift
import CarKey
// code
```
```swift
#if DEBUG
import CarKey
// code
#endif
```
## Add any (generic) top & bottom line
You can also add any top & bottom lines.
```text
swift run conditional-files generic --first-line BEGIN --last-line END test.swift
```
File (before) File (after)
```text
// text
```
```text
BEGIN
// text
END
```