Skip to content

πŸ”§ A powerful Go library for parsing Gradle build files. Extract dependencies, plugins, repositories with source location mapping. Perfect for build automation and dependency analysis tools.

License

Notifications You must be signed in to change notification settings

scagogogo/gradle-parser

Repository files navigation

Gradle Parser

CI Quality Go Report Card GoDoc codecov License Release Go Version

A powerful Go library for parsing and manipulating Gradle build files. Extract dependencies, plugins, repositories, and other configuration data with ease. Features structured editing capabilities for programmatic modifications while preserving original formatting.

πŸ“š Documentation

🌟 Complete Documentation: https://scagogogo.github.io/gradle-parser/

πŸ“– δΈ­ζ–‡ζ–‡ζ‘£ | πŸš€ Getting Started | πŸ“‹ API Reference | πŸ’‘ Examples

✨ Features

πŸ” Comprehensive Parsing

  • Parse Gradle build files (supports both build.gradle and build.gradle.kts)
  • Extract project metadata (group, name, version, description)
  • Parse and categorize dependencies with scope classification
  • Analyze plugin configurations and detect project types (Android/Kotlin/Spring Boot)
  • Process repository configurations including custom repositories and authentication

✏️ Structured Editing

  • Precise modifications: Update dependency versions, plugin versions, project properties
  • Minimal diff: Preserve original formatting, modify only necessary parts
  • Source location tracking: Record exact positions of elements in source files
  • Batch operations: Apply multiple modifications in a single operation

πŸ› οΈ Advanced Capabilities

  • Support for both Groovy DSL and Kotlin DSL syntax
  • Customizable parser configuration for different requirements
  • Multi-module Gradle project support
  • Comprehensive error handling and validation

πŸš€ Quick Start

Installation

go get github.com/scagogogo/gradle-parser/pkg/api

Basic Usage

package main

import (
    "fmt"
    "github.com/scagogogo/gradle-parser/pkg/api"
)

func main() {
    // Parse a Gradle file
    result, err := api.ParseFile("path/to/build.gradle")
    if err != nil {
        panic(err)
    }

    // Access project information
    project := result.Project
    fmt.Printf("Project: %s\n", project.Name)
    fmt.Printf("Group: %s\n", project.Group)
    fmt.Printf("Version: %s\n", project.Version)

    // List dependencies
    for _, dep := range project.Dependencies {
        fmt.Printf("Dependency: %s:%s:%s (%s)\n",
            dep.Group, dep.Name, dep.Version, dep.Scope)
    }

    // List plugins
    for _, plugin := range project.Plugins {
        fmt.Printf("Plugin: %s", plugin.ID)
        if plugin.Version != "" {
            fmt.Printf(" v%s", plugin.Version)
        }
        fmt.Println()
    }

    // List repositories
    for _, repo := range project.Repositories {
        fmt.Printf("Repository: %s (%s)\n", repo.Name, repo.URL)
    }
}

πŸ“– Key Features

πŸ” Dependency Analysis

// Extract dependencies directly
dependencies, err := api.GetDependencies("build.gradle")
if err != nil {
    log.Fatal(err)
}

// Group dependencies by scope
dependencySets := api.DependenciesByScope(dependencies)
for _, set := range dependencySets {
    fmt.Printf("Scope: %s\n", set.Scope)
    for _, dep := range set.Dependencies {
        fmt.Printf("  %s:%s:%s\n", dep.Group, dep.Name, dep.Version)
    }
}

πŸ”Œ Plugin Detection

// Extract plugin information
plugins, err := api.GetPlugins("build.gradle")
if err != nil {
    log.Fatal(err)
}

// Detect project types
if api.IsAndroidProject(plugins) {
    fmt.Println("Android project detected")
}
if api.IsKotlinProject(plugins) {
    fmt.Println("Kotlin project detected")
}
if api.IsSpringBootProject(plugins) {
    fmt.Println("Spring Boot project detected")
}

πŸ“ Repository Configuration

// Extract repository configurations
repos, err := api.GetRepositories("build.gradle")
if err != nil {
    log.Fatal(err)
}

for _, repo := range repos {
    fmt.Printf("Repository: %s (%s)\n", repo.Name, repo.URL)
}

✏️ Structured Editing

// Simple version updates
newText, err := api.UpdateDependencyVersion("build.gradle", "mysql", "mysql-connector-java", "8.0.31")
if err != nil {
    log.Fatal(err)
}

newText, err = api.UpdatePluginVersion("build.gradle", "org.springframework.boot", "2.7.2")
if err != nil {
    log.Fatal(err)
}

πŸ› οΈ Advanced Editing

// Create an editor for batch modifications
editor, err := api.CreateGradleEditor("build.gradle")
if err != nil {
    log.Fatal(err)
}

// Perform multiple modifications
editor.UpdateProperty("version", "1.0.0")
editor.UpdatePluginVersion("org.springframework.boot", "2.7.2")
editor.UpdateDependencyVersion("com.google.guava", "guava", "31.1-jre")
editor.AddDependency("org.apache.commons", "commons-text", "1.9", "implementation")

// Apply all modifications
serializer := editor.NewGradleSerializer(editor.GetSourceMappedProject().OriginalText)
finalText, err := serializer.ApplyModifications(editor.GetModifications())
if err != nil {
    log.Fatal(err)
}

// Generate diff for review
diffLines := serializer.GenerateDiff(editor.GetModifications())
for _, diffLine := range diffLines {
    fmt.Println(diffLine.String())
}

βš™οΈ Custom Parser Configuration

// Create custom parser options
options := api.DefaultOptions()
options.SkipComments = true
options.ParsePlugins = true
options.ParseDependencies = true
options.ParseRepositories = true

// Create custom parser
parser := api.NewParser(options)
result, err := parser.ParseFile("build.gradle")
if err != nil {
    log.Fatal(err)
}

πŸ—οΈ Project Structure

The project uses a modular design with clean separation of concerns:

β”œβ”€β”€ pkg/                  # Core packages
β”‚   β”œβ”€β”€ api/              # Main API interface
β”‚   β”œβ”€β”€ config/           # Configuration parsing
β”‚   β”œβ”€β”€ dependency/       # Dependency parsing
β”‚   β”œβ”€β”€ editor/           # Structured editor
β”‚   β”œβ”€β”€ model/            # Data models
β”‚   β”œβ”€β”€ parser/           # Parser core
β”‚   └── util/             # Utility functions
└── examples/             # Example code
    β”œβ”€β”€ 01_basic/         # Basic usage
    β”œβ”€β”€ 02_dependencies/  # Dependency extraction
    β”œβ”€β”€ 03_plugins/       # Plugin extraction
    β”œβ”€β”€ 04_repositories/  # Repository extraction
    β”œβ”€β”€ 05_complete/      # Complete features
    β”œβ”€β”€ 06_editor/        # Structured editing
    └── sample_files/     # Sample Gradle files

πŸ“š Resources

πŸ“– Examples

Explore the examples directory for comprehensive code samples demonstrating different features:

  • Basic Usage: Simple parsing and data extraction
  • Advanced Features: Complex parsing scenarios and customization
  • Structured Editing: Programmatic file modifications
  • Project Analysis: Complete project inspection and reporting

πŸ§ͺ Testing

Run the comprehensive test suite:

# Run all tests
go test ./...

# Run test suite with coverage
cd test && ./scripts/run-tests.sh

# Run examples
cd examples && ./run-all-examples.sh

Test Coverage: Target >90%

πŸ”„ Continuous Integration

This project uses GitHub Actions for comprehensive quality assurance:

  • πŸ”„ CI: Multi-version Go testing, code validation, example verification
  • πŸ“Š Quality: Code coverage, security scanning, complexity analysis
  • πŸ“š Docs: Documentation building and deployment
  • πŸš€ Release: Automated releases and asset building

Quality Standards:

  • βœ… Unit and integration tests
  • πŸ” Code quality checks (golangci-lint)
  • πŸ›‘οΈ Security vulnerability scanning
  • πŸ“ˆ Performance benchmarking
  • πŸ“ Documentation link validation

πŸ‘₯ Contributors

Thanks to all the amazing people who have contributed to this project! πŸŽ‰

CC11001100
CC11001100

πŸ’» πŸ“– 🎨 🚧
AdamKorcz
Adam Korczynski

πŸ› πŸ”§

πŸ† Contribution Types

  • πŸ’» Code
  • πŸ“– Documentation
  • 🎨 Design
  • 🚧 Maintenance
  • πŸ› Bug fixes
  • πŸ”§ Tools

🀝 Contributing

Contributions are welcome! Please see the Contributing Guide for details.

Development Setup

# Clone the repository
git clone https://github.com/scagogogo/gradle-parser.git
cd gradle-parser

# Install dependencies
go mod download

# Run tests
go test ./...

# Try examples
cd examples/01_basic && go run main.go

Reporting Issues

Found a bug or have a feature request? Please report it in GitHub Issues.

πŸ—ΊοΈ Roadmap

  • Enhanced Kotlin DSL support
  • Performance optimizations
  • More editing capabilities
  • IDE plugin support
  • Additional Gradle DSL syntax support

πŸ“„ License

MIT License - See LICENSE file for details.


⭐ If this project helps you, please give it a star!

Made with ❀️ by scagogogo

About

πŸ”§ A powerful Go library for parsing Gradle build files. Extract dependencies, plugins, repositories with source location mapping. Perfect for build automation and dependency analysis tools.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •