Giter Site home page Giter Site logo

nyabkun / qq-compact-lib Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 281 KB

Kotlin library that can extract code elements from your codebase and make a compact library.

License: MIT License

Kotlin 100.00%
kotlin-library meta-analysis single-file-library kotlin-compiler

qq-compact-lib's Introduction

๐Ÿ• qq-compact-lib

qq-compact-lib allows you to extract specific parts from your codebase and publish them as a reusable library.

This library itself is also extracted portions of my codebase.

How to use

  • Just copy and paste split-src directory and gradle dependencies into your project
  • There is no release in jar format yet
  • You have to edit QMyDep.kt, if you want to automatically add external library dependencies

Note

  • This library is still under development and is in the alpha stage
  • This library was originally designed for my personal use
  • Users of this library may need to make various adjustments on their end to adapt it for their sources

Mechanics

  • This program will decompose the source code into three levels of nodes.
    • Top Level
      • top level class / function / property
    • Second Level
      • Inner functions, properties, and companion objects within a top-level class
    • Third Level
      • function / property within a second level companion object
  • When you specify a public api node from your project, the program will automatically retrieve the dependent nodes.
  • This retrieval process continues recursively, forming a chain of call or reference dependencies.
  • This program uses kotlin-compiler-embeddable and PSI to resolve dependencies for function calls and references to classes.

Code Example

When you run the gralde qRunExample command in this repository, it analyzes the sample source code under the rsc-test/src directory and creates a compact library that contains only required portions to say Hello World.

fun main(): Unit = runBlocking {
  // build includes
  // - analyse src dirs
  // - create compact src code
  //   - single-file version
  //   - split-file version
  // - create test src code ( if exists )
  //   - single-file version
  //   - split-file version
  // - create example src code
  // - create jars
  // - create build.gradle.kts
  // - create README.md, LICENSE, .gitignore, etc
  // - run test ( if exists )
  // - run example
  val result = build()

//    if( result.buildSuccess ) {
//        // - create GitHub repository
//        result.createGitHubRepo(
//            result.lib.author,
//            result.lib.libName,
//            result.lib.readmeDescription,
//            listOf("kotlin-library", "hello-world", "compact-library")
//        )
//
//        // - add / commit / push
//        // - release jars
//        result.release("[auto] Reflect the changes of main repository.")
//    }
}

private suspend fun build(): QCompactLibResult {
  val result = qCompactLib(
    libName = "hello-world-compact",
    author = "nyabkun",
  ) {
    baseFileName("CompactHelloWorld")
    exampleSrcFile = Paths.get("rsc-test/src-example/CompactHelloWorldExample.kt")

    versionUpdateStrategy = QVersionUpdateStrategy.UpdateOnlyIfSplitSrcHashChanged

    readmeDescription = """Kotlin library that can say Hello."""

    versionSuffix = "-alpha"

    references = ""

    srcDirs {
      main("rsc-test/src")
      test("rsc-test/src-test")
    }

    createSingleFileSrc = true

    createLibJarFromSplitFileSrc = true

    ifFileExists {
      ifExistsReadMeFile = QIfExistsWrite.BackupAndOverwriteAtomic
//                        ifExistsReadeMeFile = QIfExistsWrite.DoNothing
    }

    readme {
      default()
    }

    splitSrcFileImportFilter filter@{
      default()
    }

    singleFileAnnotation {
      """@file:Suppress("UNCHECKED_CAST")"""
    }

    topLevelElementsSorter = Comparator<QTopLevelCompactElement> { a, b ->
      a.analysisCtx.isTest.compareTo(b.analysisCtx.isTest)
    }.thenComparator { a, b ->
      a.filePath.compareTo(b.filePath)
    }.thenComparator { a, b ->
      a.lineNumber.compareTo(b.lineNumber)
    }

    srcCode {
      visibilityChange {

        if (compactEle.isChainRoot()) {
          QKtVisibilityChange.NoChange
        } else if (!srcSetType.isSingleFileSelfContained) {
          QKtVisibilityChange.ToInternal
        } else {
          QKtVisibilityChange.ToPrivate
        }
      }
    }
  }

  return result
}

full source: QCompactLibExample.kt

Dependency

Refer to build.gradle.kts to directly check project settings.

dependencies {
  implementation("org.apache.httpcomponents:httpclient:4.5.14")
  implementation("org.jetbrains.kotlin:kotlin-compiler-embeddable:1.8.20")
  implementation("org.jetbrains.kotlin:kotlin-reflect:1.8.20")
  implementation("org.jetbrains.kotlin:kotlin-stdlib:1.8.20")
  implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
  implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}

My Compact Libraries

The following libraries are all extracted from my codebase using this library.

qq-compact-lib's People

Contributors

nyabkun avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.