Skip to content

Commit

Permalink
merge file io by groovy grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
kezong committed Dec 13, 2020
1 parent b0589d1 commit 49b29bf
Showing 1 changed file with 8 additions and 29 deletions.
37 changes: 8 additions & 29 deletions source/src/main/groovy/com/kezong/fataar/FatUtils.groovy
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
package com.kezong.fataar

import com.android.utils.FileUtils
import org.gradle.api.Project

import java.lang.ref.WeakReference
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.StandardOpenOption

class FatUtils {

private static WeakReference<Project> mProjectRef
private static Project sProject

def static attach(Project p) {
mProjectRef = new WeakReference<>(p)
sProject = p
}

def static logError(def msg) {
Project p = mProjectRef.get()
if (p != null) {
p.logger.error("[fat-aar]${msg}")
}
sProject.logger.error("[fat-aar]${msg}")
}

def static logInfo(def msg) {
Project p = mProjectRef.get()
if (p != null) {
p.logger.info("[fat-aar]${msg}")
}
sProject.logger.info("[fat-aar]${msg}")
}

def static logAnytime(def msg) {
Project p = mProjectRef.get()
if (p != null) {
p.println("[fat-aar]${msg}")
}
sProject.println("[fat-aar]${msg}")
}

def static showDir(int indent, File file) throws IOException {
Expand Down Expand Up @@ -123,24 +108,18 @@ class FatUtils {
it.exists()
}

if (existingFiles.size() == 1) {
FileUtils.copyFile(existingFiles[0], output)
return
}

// no input? done.
if (existingFiles.isEmpty()) {
return
}

if (!output.exists()) {
output.write("")
output.createNewFile()
}

// otherwise put all the files together append to output file
for (file in existingFiles) {
def content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8)
Files.write(output.toPath(), "\n$content\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND)
for (File file in existingFiles) {
output.append("\n${file.getText("UTF-8")}\n")
}
}
}

0 comments on commit 49b29bf

Please sign in to comment.