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

https://github.com/algodesigner/eclipse-format

eclipse-format: A command-line tool for formatting Java code using Eclipse's formatter in headless mode with complete symmetry to Eclipse IDE.
https://github.com/algodesigner/eclipse-format

automation build-tools ci-cd cli code-formatting code-quality code-style developer-tools eclipse eclipse-formatter eclipse-ide formatter formatting-tool gradle java java-development java-formatter programming-tools software-quality

Last synced: 6 days ago
JSON representation

eclipse-format: A command-line tool for formatting Java code using Eclipse's formatter in headless mode with complete symmetry to Eclipse IDE.

Awesome Lists containing this project

README

          

# Eclipse Formatter CLI

A command-line tool for formatting Java code using Eclipse's code formatter in headless mode. Provides a simple, user-friendly interface for consistent code formatting across projects.

## Features

- ๐Ÿš€ **Simple CLI interface** - Easy to use with intuitive commands
- โšก **Fast formatting** - Leverages Eclipse's proven formatter engine
- ๐Ÿ”ง **Customisable** - Use your own Eclipse formatter configuration
- ๐Ÿ“ **Batch processing** - Format single files or entire directories
- ๐Ÿ” **Dry run mode** - Preview changes before applying
- ๐Ÿ”„ **Recursive formatting** - Process nested directories
- ๐Ÿงช **Tested** - Comprehensive unit and integration tests
- ๐Ÿ—๏ธ **IDE friendly** - Works with Eclipse and IntelliJ IDEA

## ๐Ÿš€ Quick Installation

### Option 1: One-Command Setup (Easiest & Recommended)
```bash
# Clone and setup in one go
git clone https://github.com/algodesigner/eclipse-format.git
cd eclipse-format
./setup.sh

# Now use it!
./eclipse-format --help
```

### Option 2: Interactive Installer (Full Features)
```bash
git clone https://github.com/algodesigner/eclipse-format.git
cd eclipse-format
./install.sh

# Choose from:
# 1. System-wide installation (requires sudo)
# 2. Local user installation (~/.local/bin)
# 3. Standalone script
# 4. Just build
```

### Option 3: Universal Wrapper (Smart Auto-Detection)
```bash
git clone https://github.com/algodesigner/eclipse-format.git
cd eclipse-format
./gradlew shadowJar
./eclipse-format.sh --help # Auto-finds the JAR!
```

### Option 4: Direct Usage (Simple)
```bash
git clone https://github.com/algodesigner/eclipse-format.git
cd eclipse-format
./gradlew shadowJar
java -jar build/libs/eclipse-format.jar --help
```

### Option 5: Windows Users
```batch
git clone https://github.com/algodesigner/eclipse-format.git
cd eclipse-format
gradlew.bat shadowJar
eclipse-format.bat --help
```

## Usage

### Basic Usage

```bash
# Format a single Java file
eclipse-format MyClass.java

# Format all Java files in a directory
eclipse-format src/

# Format recursively
eclipse-format -r src/

# Dry run (show what would change)
eclipse-format -d src/

# Verbose output
eclipse-format -v MyClass.java

# Auto-discover eclipse-format.xml (walks up from target)
eclipse-format MyClass.java

# Use custom configuration file
eclipse-format -c my-formatter.xml MyClass.java
```

### Command Line Options

```
Usage: eclipse-format [-cdrv] [-c=]
File or directory to format
-c, --config=
Path to Eclipse formatter configuration file.
If not specified, searches for eclipse-format.xml
starting from the target's parent directory and
walking up the directory tree.
-d, --dry-run Show what would be formatted without making changes
-r, --recursive Format files recursively
-v, --verbose Verbose output
--help Show this help message and exit
--version Print version information and exit
```

### Configuration File

The tool uses Eclipse's XML formatter configuration files. You can:

1. **Export from Eclipse IDE**: Window โ†’ Preferences โ†’ Java โ†’ Code Style โ†’ Formatter โ†’ Export...
2. **Use the default** included in this project (`eclipse-format.xml`)
3. **Create your own** XML configuration

**Config file resolution:** When `-c` is not specified, the tool searches
for `eclipse-format.xml` by walking up the directory tree. The search
starts from the target's parent directory (for a file) or the target
directory itself (for a directory), ascending towards the filesystem
root. The first `eclipse-format.xml` found is used. Use `-c` to specify
an exact path and bypass auto-discovery entirely.

Place `eclipse-format.xml` in your project root and run the tool from
any subdirectory โ€” it will find it automatically. This matches the
behaviour of tools like clang-format.

Example configuration file structure:
```xml



```

## Development

### Prerequisites

- Java 15 or higher
- Gradle 8.5 or higher

### Setup Development Environment

```bash
# Clone and setup
git clone https://github.com/algodesigner/eclipse-format.git
cd eclipse-format

# Setup for Eclipse
./gradlew setupEclipse

# Setup for IntelliJ IDEA
./gradlew setupIntelliJ

# Clean IDE files
./gradlew cleanIDE
```

### Build Commands

```bash
# Build the project
./gradlew build

# Run tests
./gradlew test

# Run integration tests
./gradlew integrationTest

# Create executable JAR
./gradlew shadowJar

# Run the application
./gradlew run --args="-v MyClass.java"
```

### Project Structure

```
eclipse-format/
โ”œโ”€โ”€ src/
โ”‚ โ”œโ”€โ”€ main/java/com/algodesigner/eclipseformat/cli/
โ”‚ โ”‚ โ”œโ”€โ”€ EclipseFormatCli.java # Main CLI entry point
โ”‚ โ”‚ โ””โ”€โ”€ FormatterService.java # Core formatting logic
โ”‚ โ””โ”€โ”€ test/java/com/algodesigner/eclipseformat/cli/
โ”‚ โ”œโ”€โ”€ EclipseFormatCliTest.java # Unit tests
โ”‚ โ””โ”€โ”€ IntegrationTest.java # Integration tests
โ”œโ”€โ”€ eclipse-format.xml # Default formatter config
โ”œโ”€โ”€ build.gradle # Gradle build configuration
โ”œโ”€โ”€ gradlew # Gradle wrapper
โ””โ”€โ”€ README.md # This file
```

## Examples

### Example 1: Format a Project

```bash
# Format all Java files (auto-discovers eclipse-format.xml in project root)
eclipse-format -r src/main/java/

# With custom configuration
eclipse-format -c company-style.xml -r src/
```

### Example 2: Preview Changes

```bash
# See what files would be formatted
eclipse-format -d -r src/

# Output:
# [DRY RUN] Would format: src/main/java/com/example/Main.java
# [DRY RUN] Would format: src/main/java/com/example/Util.java
```

### Example 3: CI/CD Integration

```bash
# Check if code is properly formatted (exit code 0 if formatted, 1 if not)
eclipse-format -d -r src/ && echo "Code is properly formatted"
```

## Testing

The project includes comprehensive test coverage:

```bash
# Run all tests
./gradlew test

# Run specific test class
./gradlew test --tests "EclipseFormatterCliTest"

# Run integration tests
./gradlew test --tests "IntegrationTest"

# Generate test coverage report
./gradlew jacocoTestReport
```

## Contributing

1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests for new functionality
5. Ensure all tests pass
6. Submit a pull request

### Code Style

Please ensure your code follows the project's formatting rules:
```bash
# Format your code before submitting
./gradlew run --args="src/main/java/"
```

## License

This project is licensed under the BSD 3-Clause License - see the [LICENSE](LICENSE) file for details.

## Uninstalling

Easy removal with the uninstall script:

```bash
# Interactive uninstall (recommended)
./uninstall.sh

# Remove everything
./uninstall.sh --all

# Clean up PATH entries
./uninstall.sh --clean-path
```

See [USAGE.md](USAGE.md) for complete uninstall options.

## Support

- ๐Ÿ“– **Documentation**: [README.md](README.md) and [USAGE.md](USAGE.md)
- ๐Ÿ› **Issues**: Report installation or usage issues
- ๐Ÿ”ง **Uninstall**: Use `./uninstall.sh` for clean removal

## Acknowledgments

- Built with [Picocli](https://picocli.info/) for CLI parsing
- Uses Eclipse's Java Development Tools for formatting
- Inspired by the need for consistent code formatting in CI/CD pipelines

---

Made with โค๏ธ by [Vlad Shurupov](https://github.com/algodesigner) for clean code everywhere