@@ -20,6 +20,7 @@ import cfig.bootimg.cpio.AndroidCpio
20
20
import rom.fdt.DTC
21
21
import cfig.helper.Helper
22
22
import cfig.helper.ZipHelper
23
+ import cfig.packable.BootImgParser
23
24
import cfig.utils.KernelExtractor
24
25
import com.github.freva.asciitable.HorizontalAlign
25
26
import org.apache.commons.exec.CommandLine
@@ -32,6 +33,8 @@ import java.io.File
32
33
import java.nio.file.Files
33
34
import java.nio.file.Paths
34
35
import java.io.FileInputStream
36
+ import java.io.FileOutputStream
37
+ import java.io.IOException
35
38
import java.lang.NumberFormatException
36
39
import java.nio.ByteBuffer
37
40
import java.nio.ByteOrder
@@ -52,6 +55,12 @@ class Common {
52
55
private val log = LoggerFactory .getLogger(Common ::class .java)
53
56
private const val MAX_ANDROID_VER = 11
54
57
58
+ val loadProperties: (String ) -> Properties = { fileName ->
59
+ Properties ().apply {
60
+ File (fileName).inputStream().use { load(it) }
61
+ }
62
+ }
63
+
55
64
@Throws(IllegalArgumentException ::class )
56
65
fun packOsVersion (x : String? ): Int {
57
66
if (x.isNullOrBlank()) return 0
@@ -423,19 +432,24 @@ class Common {
423
432
)
424
433
}
425
434
426
- fun printPackSummary (imageName : String ) {
435
+ fun printPackSummary (imageName : String , outFile : String? = null ) {
427
436
val prints: MutableList <Pair <String , String >> = mutableListOf ()
428
437
val tableHeader = de.vandermeer.asciitable.AsciiTable ().apply {
429
438
addRule(); addRow(" What" , " Where" ); addRule()
430
439
}
431
440
val tab = de.vandermeer.asciitable.AsciiTable ().let {
432
441
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 ))
436
445
} 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
+ }
439
453
}
440
454
it.addRule()
441
455
it
@@ -457,6 +471,31 @@ class Common {
457
471
}
458
472
}
459
473
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
+
460
499
fun printPackSummaryInternal (imageName : String ) {
461
500
val prints: MutableList <Pair <String , String >> = mutableListOf ()
462
501
val tableHeader = de.vandermeer.asciitable.AsciiTable ().apply {
@@ -485,5 +524,47 @@ class Common {
485
524
log.info(" \n\t\t\t Pack Summary of ${imageName} \n {}\n {}" , tableHeader.render(), tab.render())
486
525
}
487
526
}
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
+ }
488
569
}
489
570
}
0 commit comments