@@ -20,6 +20,7 @@ import cfig.bootimg.cpio.AndroidCpio
2020import rom.fdt.DTC
2121import cfig.helper.Helper
2222import cfig.helper.ZipHelper
23+ import cfig.packable.BootImgParser
2324import cfig.utils.KernelExtractor
2425import com.github.freva.asciitable.HorizontalAlign
2526import org.apache.commons.exec.CommandLine
@@ -32,6 +33,8 @@ import java.io.File
3233import java.nio.file.Files
3334import java.nio.file.Paths
3435import java.io.FileInputStream
36+ import java.io.FileOutputStream
37+ import java.io.IOException
3538import java.lang.NumberFormatException
3639import java.nio.ByteBuffer
3740import java.nio.ByteOrder
@@ -52,6 +55,12 @@ class Common {
5255 private val log = LoggerFactory .getLogger(Common ::class .java)
5356 private const val MAX_ANDROID_VER = 11
5457
58+ val loadProperties: (String ) -> Properties = { fileName ->
59+ Properties ().apply {
60+ File (fileName).inputStream().use { load(it) }
61+ }
62+ }
63+
5564 @Throws(IllegalArgumentException ::class )
5665 fun packOsVersion (x : String? ): Int {
5766 if (x.isNullOrBlank()) return 0
@@ -423,19 +432,24 @@ class Common {
423432 )
424433 }
425434
426- fun printPackSummary (imageName : String ) {
435+ fun printPackSummary (imageName : String , outFile : String? = null ) {
427436 val prints: MutableList <Pair <String , String >> = mutableListOf ()
428437 val tableHeader = de.vandermeer.asciitable.AsciiTable ().apply {
429438 addRule(); addRow(" What" , " Where" ); addRule()
430439 }
431440 val tab = de.vandermeer.asciitable.AsciiTable ().let {
432441 it.addRule()
433- if (File ( " $imageName .signed " ).exists() ) {
434- it.addRow(" re-packed $imageName " , " $imageName .signed " )
435- prints.add(Pair (" re-packed $imageName " , " $imageName .signed " ))
442+ if (outFile != null ) {
443+ it.addRow(" re-packed $imageName " , outFile )
444+ prints.add(Pair (" re-packed $imageName " , outFile ))
436445 } else {
437- it.addRow(" re-packed $imageName " , " $imageName .clear" )
438- prints.add(Pair (" re-packed $imageName " , " $imageName .clear" ))
446+ if (File (" $imageName .signed" ).exists()) {
447+ it.addRow(" re-packed $imageName " , " $imageName .signed" )
448+ prints.add(Pair (" re-packed $imageName " , " $imageName .signed" ))
449+ } else {
450+ it.addRow(" re-packed $imageName " , " $imageName .clear" )
451+ prints.add(Pair (" re-packed $imageName " , " $imageName .clear" ))
452+ }
439453 }
440454 it.addRule()
441455 it
@@ -457,6 +471,31 @@ class Common {
457471 }
458472 }
459473
474+ /*
475+ be_caller_dir: set in "be" script, to support out of tree invocation
476+ */
477+ fun shortenPath (fullPath : String , inCurrentPath : String = System .getProperty("user.dir")): String {
478+ val currentPath = System .getenv(" be_caller_dir" ) ? : inCurrentPath
479+ val full = Paths .get(fullPath).normalize().toAbsolutePath()
480+ val base = Paths .get(currentPath).normalize().toAbsolutePath()
481+ return try {
482+ base.relativize(full).toString()
483+ } catch (e: IllegalArgumentException ) {
484+ full.toString()
485+ }
486+ }
487+
488+ fun String.toShortenPath (inCurrentPath : String = System .getProperty("user.dir")): String {
489+ val currentPath = System .getenv(" be_caller_dir" ) ? : inCurrentPath
490+ val full = Paths .get(this ).normalize().toAbsolutePath()
491+ val base = Paths .get(currentPath).normalize().toAbsolutePath()
492+ return try {
493+ base.relativize(full).toString()
494+ } catch (e: IllegalArgumentException ) {
495+ full.toString()
496+ }
497+ }
498+
460499 fun printPackSummaryInternal (imageName : String ) {
461500 val prints: MutableList <Pair <String , String >> = mutableListOf ()
462501 val tableHeader = de.vandermeer.asciitable.AsciiTable ().apply {
@@ -485,5 +524,47 @@ class Common {
485524 log.info(" \n\t\t\t Pack Summary of ${imageName} \n {}\n {}" , tableHeader.render(), tab.render())
486525 }
487526 }
527+
528+ fun createWorkspaceIni (fileName : String , iniFileName : String = "workspace.ini", prefix : String? = null) {
529+ log.trace(" create workspace file" )
530+ val workDir = Helper .prop(" workDir" )
531+ val workspaceFile = File (workDir, iniFileName)
532+
533+ try {
534+ if (prefix.isNullOrBlank()) {
535+ // override existing file entirely when prefix is null or empty
536+ val props = Properties ().apply {
537+ setProperty(" file" , fileName)
538+ setProperty(" workDir" , workDir)
539+ setProperty(" role" , File (fileName).name)
540+ }
541+ FileOutputStream (workspaceFile).use { out ->
542+ props.store(out , " unpackInternal configuration (overridden)" )
543+ }
544+ log.info(" workspace file overridden: ${workspaceFile.canonicalPath} " )
545+ } else {
546+ // merge into existing (or create new) with prefixed keys
547+ val props = Properties ().apply {
548+ if (workspaceFile.exists()) {
549+ FileInputStream (workspaceFile).use { load(it) }
550+ }
551+ }
552+
553+ fun key (name : String ) = " ${prefix.trim()} .$name "
554+ props.setProperty(key(" file" ), fileName)
555+ props.setProperty(key(" workDir" ), workDir)
556+ props.setProperty(key(" role" ), File (fileName).name)
557+
558+ FileOutputStream (workspaceFile).use { out ->
559+ props.store(out , " unpackInternal configuration (with prefix='$prefix ')" )
560+ }
561+ log.info(" workspace file created/updated with prefix '$prefix ': ${workspaceFile.canonicalPath} " )
562+ }
563+ } catch (e: IOException ) {
564+ log.error(" error writing workspace file: ${e.message} " )
565+ }
566+
567+ log.trace(" create workspace file done" )
568+ }
488569 }
489570}
0 commit comments