An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[![Swift](https://github.com/MarcoEidinger/conditional-files/actions/workflows/swift.yml/badge.svg)](https://github.com/MarcoEidinger/conditional-files/actions/workflows/swift.yml) [![codecov](https://codecov.io/github/MarcoEidinger/conditional-files/branch/main/graph/badge.svg?token=OW4DFFVD4R)](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
```